From 2c459221704db36c4b3512e4e66432dc1eadd791 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 12 Nov 2023 01:22:05 +0100 Subject: Allow first player choosing rematch to select player role assignment. Pick one of swap, keep, or random. In case of mid-air collision, player who chose first gets to decide. --- server.js | 52 ++++++++++++++++++++++++++++++++++++++++++---------- views/rematch.pug | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 views/rematch.pug diff --git a/server.js b/server.js index 23228ed..16ac288 100644 --- a/server.js +++ b/server.js @@ -1270,7 +1270,7 @@ const SQL_INSERT_REMATCH = SQL(` insert or ignore into games (owner_id, title_id, scenario, options, player_count, pace, is_private, is_random, notice) select - $owner_id, title_id, scenario, options, player_count, pace, is_private, 0, $magic + $owner_id, title_id, scenario, options, player_count, pace, is_private, $random, $magic from games where @@ -1653,7 +1653,7 @@ app.get('/delete/:game_id', must_be_logged_in, function (req, res) { res.redirect('/'+title_id) }) -function insert_replay_players(old_game_id, new_game_id, req_user_id) { +function insert_replay_players(old_game_id, new_game_id, req_user_id, order) { let game = SQL_SELECT_GAME.get(old_game_id) let players = SQL_SELECT_PLAYERS_JOIN.all(old_game_id) let roles = get_game_roles(game.title_id, game.scenario, parse_game_options(game.options)) @@ -1662,28 +1662,60 @@ function insert_replay_players(old_game_id, new_game_id, req_user_id) { if (players.length !== n) throw new Error("missing players") - if (TITLE_TABLE[game.title_id].is_symmetric) - shuffle(players) - else + switch (order) { + default: + case "swap": players.sort((a, b) => roles.indexOf(a.role) - roles.indexOf(b.role)) - - for (let i = 0; i < n; ++i) - players[i].role = roles[(i+1) % n] + for (let i = 0; i < n; ++i) + players[i].role = roles[(i+1) % n] + break + case "keep": + // do nothing + break + case "shuffle": + // unused for now - random but known + shuffle(players) + for (let i = 0; i < n; ++i) + players[i].role = roles[i] + break + case "random": + for (let i = 0; i < n; ++i) + players[i].role = "Random " + (i+1) + break + } for (let p of players) SQL_INSERT_PLAYER_ROLE.run(new_game_id, p.role, p.user_id, p.user_id !== req_user_id ? 1 : 0) } app.get('/rematch/:old_game_id', must_be_logged_in, function (req, res) { + let old_game_id = req.params.old_game_id | 0 + let magic = "\u{1F503} " + old_game_id + let new_game_id = SQL_SELECT_REMATCH.get(magic) + if (new_game_id) + return res.redirect("/join/" + new_game_id) + + let game = SQL_SELECT_GAME.get(old_game_id) + let players = SQL_SELECT_PLAYERS_JOIN.all(old_game_id) + res.render("rematch.pug", { + user: req.user, + title: TITLE_TABLE[game.title_id], + game, + players, + }) +}) + +app.post('/rematch/:old_game_id', must_be_logged_in, function (req, res) { let old_game_id = req.params.old_game_id | 0 let magic = "\u{1F503} " + old_game_id let new_game_id = 0 + let order = req.body.order SQL_BEGIN.run() try { - new_game_id = SQL_INSERT_REMATCH.get({owner_id: req.user.user_id, old_game_id, magic}) + new_game_id = SQL_INSERT_REMATCH.get({owner_id: req.user.user_id, random: order === "random" ? 1 : 0, old_game_id, magic}) if (new_game_id) - insert_replay_players(old_game_id, new_game_id, req.user.user_id) + insert_replay_players(old_game_id, new_game_id, req.user.user_id, order) else new_game_id = SQL_SELECT_REMATCH.get(magic) SQL_COMMIT.run() diff --git a/views/rematch.pug b/views/rematch.pug new file mode 100644 index 0000000..09df6ec --- /dev/null +++ b/views/rematch.pug @@ -0,0 +1,55 @@ +//- vim:ts=4:sw=4: +doctype html +html + head + include head + title= title.title_name + body + include header + article + if game.scenario === "Standard" + h1 #{title.title_name} + else + h1 #{title.title_name} - #{game.scenario} + + div.logo + +gamecover(title.title_id) + + p Suggest rematch with + each p, i in players + if i > 0 && players.length > 2 + | , + if i === players.length - 1 + | + | and + | + | #{p.name} + | ? + + form(method="post" action="/rematch/"+game.game_id) + p Player roles: + if title.is_symmetric + br + label + input(type="radio" name="order" value="keep") + | Keep + br + label + input(type="radio" name="order" value="random" checked) + | Random + else + br + label + input(type="radio" name="order" value="swap" checked) + | Swap + br + label + input(type="radio" name="order" value="keep") + | Keep + br + label + input(type="radio" name="order" value="random") + | Random + + p + button(type="submit") Rematch -- cgit v1.2.3