From 06f757c23d0d9f7a2b86f6c828f886ee8e642e03 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Tue, 14 Jun 2022 20:40:25 +0200 Subject: Clean up game list logic. --- server.js | 86 ++++++++++-------------------------------------- views/games_active.pug | 20 +++++------ views/games_finished.pug | 4 +-- views/games_public.pug | 6 +++- views/head.pug | 32 +++++++++++------- views/info.pug | 5 +++ views/user.pug | 9 ++++- 7 files changed, 67 insertions(+), 95 deletions(-) diff --git a/server.js b/server.js index 0f7470e..c1cdf66 100644 --- a/server.js +++ b/server.js @@ -569,12 +569,7 @@ app.get('/user/:who_name', function (req, res) { who.atime = human_date(who.atime) let games = QUERY_LIST_ACTIVE_GAMES_OF_USER.all({ user_id: who.user_id }) annotate_games(games, 0) - res.render('user.pug', { - user: req.user, - who: who, - active_games: games.filter(game => !is_finished_game(game)), - finished_games: games.filter(is_finished_game), - }) + res.render('user.pug', { user: req.user, who: who, games: games }) } else { return res.status(404).send("Invalid user name.") } @@ -874,26 +869,6 @@ let RULES = {} let HTML_ABOUT = {} let HTML_CREATE = {} -function is_open_game(game) { - return game.status === 0 && !game.is_ready -} - -function is_ready_game(game) { - return game.status === 0 && game.is_ready -} - -function is_replacement_game(game) { - return game.status === 1 && !game.is_ready -} - -function is_active_game(game) { - return game.status === 1 && game.is_ready -} - -function is_finished_game(game) { - return game.status === 2 -} - function load_rules() { const SQL_SELECT_TITLES = SQL("SELECT * FROM titles") for (let title of SQL_SELECT_TITLES.all()) { @@ -1046,25 +1021,26 @@ function annotate_game(game, user_id) { for (let i = 0; i < players.length; ++i) { let p = players[i] - if (p.user_id === user_id) { - your_role = p.role - your_count++ - } + let p_is_owner = false + if (game.status === 0 && (game.owner_id === p.user_id)) + p_is_owner = true let p_is_active = false - if (game.status === 0 && (game.owner_id === p.user_id)) - p_is_active = true if (game.status === 1 && (game.active === p.role || game.active === "Both" || game.active === "All")) p_is_active = true + if (p.user_id === user_id) { + your_role = p.role + your_count++ + if (p_is_active || (p_is_owner && game.is_ready)) + game.your_turn = true + } + let link - if (p_is_active) { + if (p_is_active || p_is_owner) link = `${p.name}` - if (p.user_id === user_id) - game.your_turn = true - } else { + else link = `${p.name}` - } if (game.player_names.length > 0) game.player_names += ", " @@ -1113,24 +1089,13 @@ function sort_your_turn(a, b) { app.get('/games/active', must_be_logged_in, function (req, res) { let games = QUERY_LIST_ACTIVE_GAMES_OF_USER.all({ user_id: req.user.user_id }) annotate_games(games, req.user.user_id) - res.render('games_active.pug', { - user: req.user, - open_games: games.filter(is_open_game), - replacement_games: games.filter(is_replacement_game), - ready_games: games.filter(is_ready_game).sort(sort_your_turn), - active_games: games.filter(is_active_game).sort(sort_your_turn), - finished_games: games.filter(is_finished_game), - }) + res.render('games_active.pug', { user: req.user, who: req.user, games: games }) }) app.get('/games/finished', must_be_logged_in, function (req, res) { let games = QUERY_LIST_FINISHED_GAMES_OF_USER.all({user_id: req.user.user_id}) annotate_games(games, req.user.user_id) - res.render('games_finished.pug', { - user: req.user, - who: req.user, - finished_games: games, - }) + res.render('games_finished.pug', { user: req.user, who: req.user, games: games }) }) app.get('/games/finished/:who_name', function (req, res) { @@ -1138,11 +1103,7 @@ app.get('/games/finished/:who_name', function (req, res) { if (who) { let games = QUERY_LIST_FINISHED_GAMES_OF_USER.all({ user_id: who.user_id }) annotate_games(games, 0) - res.render('games_finished.pug', { - user: req.user, - who: who, - finished_games: games, - }) + res.render('games_finished.pug', { user: req.user, who: who, games: games }) } else { return res.status(404).send("Invalid user name.") } @@ -1154,14 +1115,7 @@ app.get('/games/public', function (req, res) { annotate_games(games, req.user.user_id) else annotate_games(games, 0) - res.render('games_public.pug', { - user: req.user, - open_games: games.filter(is_open_game), - replacement_games: games.filter(is_replacement_game), - ready_games: games.filter(is_ready_game), - active_games: games.filter(is_active_game), - finished_games: games.filter(is_finished_game), - }) + res.render('games_public.pug', { user: req.user, games: games }) }) app.get('/info/:title_id', function (req, res) { @@ -1180,11 +1134,7 @@ function get_title_page(req, res, title_id) { user: req.user, title: title, about_html: HTML_ABOUT[title_id], - open_games: active_games.filter(is_open_game), - replacement_games: active_games.filter(is_replacement_game), - ready_games: active_games.filter(is_ready_game), - active_games: active_games.filter(is_active_game), - finished_games: finished_games, + games: active_games.concat(finished_games) }) } diff --git a/views/games_active.pug b/views/games_active.pug index a72a203..66ee86c 100644 --- a/views/games_active.pug +++ b/views/games_active.pug @@ -1,28 +1,23 @@ //- vim:ts=4:sw=4: +- let open_games = games.filter(game => game.status === 0) +- let active_games = games.filter(game => game.status === 1) +- let finished_games = games.filter(game => game.status === 2) doctype html html head include head title= SITE_NAME if active_games.length > 0 - meta(http-equiv="refresh" content=300) + meta(http-equiv="refresh" content=600) body include header article h1 Your games - if ready_games.length > 0 - h2 Ready to start - +gamelist(ready_games) - if open_games.length > 0 h2 Open +gamelist(open_games) - if replacement_games.length > 0 - h2 Need replacement - +gamelist(replacement_games) - if active_games.length > 0 h2 Active +gamelist(active_games) @@ -31,7 +26,8 @@ html h2 Recently finished +gamelist(finished_games) - p All your finished games - - if open_games.length === 0 && ready_games.length === 0 && active_games.length === 0 && finished_games.length === 0 + if open_games.length === 0 && active_games.length === 0 && finished_games.length === 0 p Nothing here. + + p + a(href="/games/finished") All your finished games diff --git a/views/games_finished.pug b/views/games_finished.pug index df7f038..26d0378 100644 --- a/views/games_finished.pug +++ b/views/games_finished.pug @@ -12,7 +12,7 @@ html else h1 #{who.name}’s finished games - if finished_games.length > 0 - +gamelist(finished_games) + if games.length > 0 + +gamelist(games) else p Nothing here. diff --git a/views/games_public.pug b/views/games_public.pug index 44b250d..a3b1407 100644 --- a/views/games_public.pug +++ b/views/games_public.pug @@ -1,11 +1,15 @@ //- vim:ts=4:sw=4: +- let open_games = games.filter(game => game.status === 0 && !game.is_ready) +- let ready_games = games.filter(game => game.status === 0 && game.is_ready) +- let replacement_games = games.filter(game => game.status === 1 && !game.is_ready) +- let active_games = games.filter(game => game.status === 1 && game.is_ready) doctype html html head include head title= SITE_NAME if user - meta(http-equiv="refresh" content=600) + meta(http-equiv="refresh" content=900) body include header article diff --git a/views/head.pug b/views/head.pug index 748d417..005a4b2 100644 --- a/views/head.pug +++ b/views/head.pug @@ -59,18 +59,28 @@ mixin gamelist(list,hide_title=0) | #{item.game_id} – | #{item.title_name} - if item.status === 0 || !item.is_ready - a(class="command" href=`/join/${item.game_id}`) Join - else - if item.is_yours - - let verb = (item.status > 1) ? "Review" : "Play" - if item.your_role - a(class="command" href=`/${item.title_id}/play:${item.game_id}:${item.your_role}`)= verb + case item.status + when 0 + if item.is_yours && item.is_ready + a(class="command" href=`/join/${item.game_id}`) Start else - a(class="command" href="/join/"+item.game_id)= verb - else - - let verb = (item.status > 1) ? "Review" : "View" - a(class="command" href=`/${item.title_id}/play:${item.game_id}`)= verb + a(class="command" href=`/join/${item.game_id}`) Join + when 1 + if item.is_yours + if item.your_role + a(class="command" href=`/${item.title_id}/play:${item.game_id}:${item.your_role}`) Play + else + a(class="command" href="/join/"+item.game_id) Play + else + a(class="command" href=`/${item.title_id}/play:${item.game_id}`) View + when 2 + if item.is_yours + if item.your_role + a(class="command" href=`/${item.title_id}/play:${item.game_id}:${item.your_role}`) Review + else + a(class="command" href="/join/"+item.game_id) Review + else + a(class="command" href=`/${item.title_id}/play:${item.game_id}`) Review div.game_main div.game_info diff --git a/views/info.pug b/views/info.pug index 24425e5..62698c9 100644 --- a/views/info.pug +++ b/views/info.pug @@ -1,4 +1,9 @@ //- vim:ts=4:sw=4: +- let open_games = games.filter(game => game.status === 0 && !game.is_ready) +- let ready_games = games.filter(game => game.status === 0 && game.is_ready) +- let replacement_games = games.filter(game => game.status === 1 && !game.is_ready) +- let active_games = games.filter(game => game.status === 1 && game.is_ready) +- let finished_games = games.filter(game => game.status === 2) doctype html html head diff --git a/views/user.pug b/views/user.pug index 73c2e2d..fb98da7 100644 --- a/views/user.pug +++ b/views/user.pug @@ -1,4 +1,7 @@ //- vim:ts=4:sw=4: +- let open_games = games.filter(game => game.status === 0) +- let active_games = games.filter(game => game.status === 1) +- let finished_games = games.filter(game => game.status === 2) doctype html html head @@ -22,8 +25,12 @@ html p a(href="/message/send/"+who.name) Send message + if open_games.length > 0 + h2 Open + +gamelist(open_games) + if active_games.length > 0 - h2 Playing + h2 Active +gamelist(active_games) if finished_games.length > 0 -- cgit v1.2.3