From 80e3204596f9ab48a350a322e91d78b2a095cf33 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 7 Dec 2023 14:01:50 +0100 Subject: Add support for random scenario selection and scenario groupings. --- public/join.js | 4 ++++ server.js | 27 ++++++++++++++++++++++++--- views/create.pug | 23 ++++++++++++++++++----- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/public/join.js b/public/join.js index 704868f..e51b548 100644 --- a/public/join.js +++ b/public/join.js @@ -148,6 +148,10 @@ function action_link(player, action, color, text) { } function update() { + if (game.scenario !== "Standard") + document.querySelector("h1").textContent = game.title_name + " - " + game.scenario + else + document.querySelector("h1").textContent = game.title_name for (let i = 0; i < roles.length; ++i) { let role = roles[i] let role_id = "role_" + role.replace(/ /g, "_") diff --git a/server.js b/server.js index 2910e36..7f87bf5 100644 --- a/server.js +++ b/server.js @@ -1189,6 +1189,7 @@ const SQL_FINISH_GAME = SQL(` `) const SQL_UPDATE_GAME_ACTIVE = SQL("update games set active=?,mtime=datetime(),moves=moves+1 where game_id=?") +const SQL_UPDATE_GAME_SCENARIO = SQL("update games set scenario=? where game_id=?") const SQL_SELECT_GAME_STATE = SQL("select state from game_state where game_id=?").pluck() const SQL_INSERT_GAME_STATE = SQL("insert or replace into game_state (game_id,state) values (?,?)") @@ -1624,6 +1625,18 @@ function options_json_replacer(key, value) { return value } +function is_random_scenario(title_id, scenario) { + if (RULES[title_id].is_random_scenario) + return RULES[title_id].is_random_scenario(scenario) + return false +} + +function select_random_scenario(title_id, scenario, seed) { + if (RULES[title_id].select_random_scenario) + return RULES[title_id].select_random_scenario(scenario, seed) + return scenario +} + app.post("/create/:title_id", must_be_logged_in, function (req, res) { let title_id = req.params.title_id let priv = req.body.is_private === "true" ? 1 : 0 @@ -1640,8 +1653,9 @@ app.post("/create/:title_id", must_be_logged_in, function (req, res) { if (!(title_id in RULES)) return res.send("Invalid title.") - if (!RULES[title_id].scenarios.includes(scenario)) - return res.send("Invalid scenario.") + + if (is_random_scenario(title_id, scenario)) + rand = 1 let player_count = get_game_roles(title_id, scenario, parse_game_options(options)).length @@ -1936,13 +1950,20 @@ app.post('/start/:game_id', must_be_logged_in, function (req, res) { function start_game(game) { let options = parse_game_options(game.options) let seed = random_seed() - let state = RULES[game.title_id].setup(seed, game.scenario, options) + let state = null SQL_BEGIN.run() try { + if (is_random_scenario(game.title_id, game.scenario)) { + game.scenario = select_random_scenario(game.title_id, game.scenario, seed) + SQL_UPDATE_GAME_SCENARIO.run(game.scenario, game.game_id) + } + if (game.is_random) assign_random_roles(game, options, SQL_SELECT_PLAYERS_JOIN.all(game.game_id)) + state = RULES[game.title_id].setup(seed, game.scenario, options) + SQL_START_GAME.run(state.active, game.game_id) put_replay(game.game_id, null, ".setup", [seed, game.scenario, options]) put_snap(game.game_id, state) diff --git a/views/create.pug b/views/create.pug index 717c4f0..7a8a6b8 100644 --- a/views/create.pug +++ b/views/create.pug @@ -14,14 +14,27 @@ html +gamecover(title.title_id) form(method="post" action="/create/"+title.title_id) - if scenarios.length > 1 + if Array.isArray(scenarios) + if scenarios.length > 1 + p Scenario: + br + select(name="scenario") + each scenario in scenarios + option(value=scenario)= scenario + else + input(type="hidden" name="scenario" value=scenarios[0]) + else p Scenario: br select(name="scenario") - each scenario in scenarios - option(value=scenario)= scenario - else - input(type="hidden" name="scenario" value=scenarios[0]) + each list, name in scenarios + if name === "" + each scenario in list + option(value=scenario)= scenario + else + optgroup(label=name) + each scenario in list + option(value=scenario)= scenario | !{ title.create_html } -- cgit v1.2.3