diff options
-rw-r--r-- | server.js | 38 | ||||
-rw-r--r-- | views/games.ejs | 56 |
2 files changed, 94 insertions, 0 deletions
@@ -447,6 +447,26 @@ const QUERY_LIST_PUBLIC_GAMES = db.prepare(` ORDER BY status ASC, mtime DESC `); +const QUERY_LIST_OPEN_GAMES = db.prepare(` + SELECT + games.game_id, + games.title_id AS title_id, + games.scenario AS scenario, + games.owner AS owner_id, + users.name AS owner_name, + games.ctime, + games.mtime, + games.description, + games.status, + games.result, + games.active, + titles.title_name + FROM games + LEFT JOIN users ON games.owner = users.user_id + LEFT JOIN titles ON games.title_id = titles.title_id + WHERE private = 0 AND status < 2 +`); + const QUERY_LIST_USER_GAMES = db.prepare(` SELECT DISTINCT games.game_id, @@ -592,6 +612,24 @@ app.get('/profile', must_be_logged_in, function (req, res) { }); }); +app.get('/games', must_be_logged_in, function (req, res) { + LOG(req, "GET /join"); + let games = QUERY_LIST_OPEN_GAMES.all(); + humanize(games); + for (let game of games) { + game.players = QUERY_PLAYER_NAMES.all(game.game_id); + game.your_turn = is_your_turn(game, req.user); + } + let open_games = games.filter(game => game.status == 0); + let active_games = games.filter(game => game.status == 1); + res.set("Cache-Control", "no-store"); + res.render('games.ejs', { user: req.user, + open_games: open_games, + active_games: active_games, + message: req.flash('message') + }); +}); + app.get('/info/:title_id', function (req, res) { LOG(req, "GET /info/" + req.params.title_id); let title_id = req.params.title_id; diff --git a/views/games.ejs b/views/games.ejs new file mode 100644 index 0000000..fb58be8 --- /dev/null +++ b/views/games.ejs @@ -0,0 +1,56 @@ +<%- include('header', { title: "All Public Games", refresh: (user ? 300 : 0) }) %> +<style> +td.nowrap a { color: black; text-decoration: none; } +</style> + +<h2>Open</h2> +<table class="wide"> +<tr><th>ID<th>Title<th>Scenario<th>Owner<th>Description<th>Created<th>Players<th> +<% if (open_games.length > 0) { %> +<% open_games.forEach((row) => { %> +<tr> +<td><%= row.game_id %> +<td class="nowrap"><a href="/info/<%= row.title_id %>"><%= row.title_name %></a> +<td><%= row.scenario %> +<td><%= row.owner_name %> +<td><%= row.description %> +<td class="nowrap"><%= row.ctime %> +<td><%= row.players.join(", ") %> +<td><a href="/join/<%= row.game_id %>">Join</a> +<% }); } else { %> +<tr><td colspan="6">No open games. +<% } %> +</table> + +<h2>Active</h2> +<table class="wide"> +<tr><th>ID<th>Title<th>Scenario<th>Description<th>Changed<th>Players<th>Active<th> +<% if (active_games.length > 0) { %> +<% active_games.forEach((row) => { %> +<tr> +<td><%= row.game_id %> +<td class="nowrap"><a href="/info/<%= row.title_id %>"><%= row.title_name %></a> +<td><%= row.scenario %> +<td><%= row.description %> +<td class="nowrap"><%= row.mtime %> +<td><%= row.players.join(", ") %> +<% + if (row.your_turn) { + %><td class="your_turn"><%= row.active %><% + } else { + %><td><%= row.active %><% + } + let me = row.players.reduce((n,p) => n + (p === user.name ? 1 : 0), 0); + if (me == 1) { + %><td><a href="/play/<%= row.game_id %>">Play</a><% + } else if (me > 1) { + %><td><a href="/join/<%= row.game_id %>">Play</a><% + } else { + %><td><a href="/join/<%= row.game_id %>">View</a><% + } +%> +<% }); } else { %> +<tr><td colspan="6">No active games. +<% } %> +</table> + |