diff options
Diffstat (limited to 'tools/patchgame.js')
-rwxr-xr-x | tools/patchgame.js | 70 |
1 files changed, 62 insertions, 8 deletions
diff --git a/tools/patchgame.js b/tools/patchgame.js index 1426a75..de8721e 100755 --- a/tools/patchgame.js +++ b/tools/patchgame.js @@ -71,34 +71,82 @@ function snapshot(state) { return snap } +function is_role_active(active, role) { + return active === role || active === "Both" || active.includes(role) +} + +function is_nobody_active(active) { + return !active || active === "None" +} + +function is_multi_active(active) { + if (!active) + return false + if (Array.isArray(active)) + return true + return active === "Both" || active.includes(",") +} + +function is_changed_active(old_active, new_active) { + return String(old_active) !== String(new_active) +} + function is_valid_action(rules, state, role, action, arg) { if (action === "undo") // for jc, hots, r3, and cr compatibility return true - if (state.active !== role && state.active !== "Both") + if (!is_role_active(state.active, role)) return false let view = rules.view(state, role) let va = view.actions[action] if (va) { - if (Array.isArray(arg)) - arg = arg[0] if (Array.isArray(va) && va.includes(arg)) return true - if (arg === undefined || arg === null || arg === 1) + if (arg === undefined || arg === null || arg === 1 || Array.isArray(arg)) return (va === 1 || va === true || typeof va === "string") } return false } function dont_snap(rules, state, old_active) { - if (state.state === "game_over") + if (is_nobody_active(state.active)) return true - if (state.active === old_active) + if (is_multi_active(old_active) && is_multi_active(state.active)) + return true + if (!is_changed_active(old_active, state.active)) return true if (rules.dont_snap && rules.dont_snap(state)) return true return false } +function get_game_roles(rules, scenario, options) { + let roles = rules.roles + if (typeof roles === "function") { + if (typeof options === "string") + options = JSON.parse(options) + return roles(scenario, options) + } + return roles +} + +function get_resign_result(roles, role) { + return roles.filter(r => r !== role).join(", ") +} + +function finish_game(rules, state, result, message) { + if (typeof rules.finish === "function") { + state = RULES[title_id].finish(state, result, message) + } else { + state.state = "game_over" + state.active = "None" + state.result = result + state.victory = message + state.log.push("") + state.log.push(message) + } + return state +} + function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_undo=false, delete_invalid=false}, verbose) { let game = select_game.get(game_id) if (!game) { @@ -108,6 +156,7 @@ function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_und let title_id = game.title_id let rules = require("../public/" + title_id + "/rules.js") + let roles = get_game_roles(rules, game.scenario, game.options) let replay = select_replay.all(game_id) if (replay.length === 0) @@ -131,8 +180,13 @@ function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_und case ".setup": state = rules.setup(...args) break + case ".timeout": + finish_game(rules, state, "None", item.role + " timed out.") + break + case ".abandon": + finish_game(rules, state, "None", item.role + " abandoned the game.") case ".resign": - state = rules.resign(state, item.role) + finish_game(rules, state, get_resign_result(roles, item.role), item.role + " resigned.") break default: if (validate_actions) { @@ -187,7 +241,7 @@ function patch_game(game_id, {validate_actions=true, save_snaps=true, delete_und insert_snap.run(game_id, ++snap_id, item.replay_id, item.state) } - update_active.run(state.active, game_id) + update_active.run(String(state.active), game_id) update_state.run(JSON.stringify(state), game_id) if (state.state === "game_over") |