diff options
author | Tor Andersson <tor@ccxvii.net> | 2025-02-10 22:09:13 +0100 |
---|---|---|
committer | Tor Andersson <tor@ccxvii.net> | 2025-02-12 12:05:30 +0100 |
commit | 4f123a7c8c8719491ddc4a497d37e99bb5dce549 (patch) | |
tree | 3a97a6f60689468428d0169e80967bc82783bc06 | |
parent | b04c539731bac278eee8544a01c6413b88a088c7 (diff) | |
download | server-4f123a7c8c8719491ddc4a497d37e99bb5dce549.tar.gz |
Support multi-active player states for 3p+ games.
-rw-r--r-- | schema.sql | 21 | ||||
-rw-r--r-- | server.js | 28 | ||||
-rwxr-xr-x | tools/patchgame.js | 6 |
3 files changed, 33 insertions, 22 deletions
@@ -154,7 +154,9 @@ create view user_dynamic_view as status = 1 and user_count = player_count and players.user_id = users.user_id - and active in ( 'Both', players.role ) + -- and active in ( 'Both', players.role ) + and ( active = 'Both' or instr(active, players.role) ) + ) + ( select count(*) @@ -510,13 +512,17 @@ create view player_view as when 0 then owner_id = user_id when 1 then - active in ( 'Both', role ) + -- active in ( 'Both', role ) + ( active = 'Both' or instr(active, role) ) else 0 end ) as is_active, ( - case when active in ( 'Both', role ) then + case when + -- active in ( 'Both', role ) + ( active = 'Both' or instr(active, role) ) + then clock - (julianday() - julianday(mtime)) else clock @@ -543,7 +549,8 @@ create view time_control_view as join players using(game_id) where status = 1 - and active in ( 'Both', role ) + -- and active in ( 'Both', role ) + and ( active = 'Both' or instr(active, role) ) and clock - (julianday() - julianday(mtime)) < 0 ; @@ -888,7 +895,8 @@ begin update players set clock = clock - (julianday() - julianday(old.mtime)) where players.game_id = old.game_id - and old.active in ( 'Both', players.role ) + -- and old.active in ( 'Both', players.role ) + and ( old.active = 'Both' or instr(old.active, players.role) ) ; end; @@ -902,7 +910,8 @@ begin user_id, old.game_id, (julianday() - julianday(old.mtime)) from players where players.game_id = old.game_id - and old.active in ( 'Both', players.role ) + -- and old.active in ( 'Both', players.role ) + and ( old.active = 'Both' or instr(old.active, players.role) ) ; end; @@ -1539,7 +1539,8 @@ const QUERY_NEXT_GAME_OF_USER = SQL(` join players using(game_id) where status = ${STATUS_ACTIVE} - and active in (role, 'Both') + -- and active in (role, 'Both') + and ( active = 'Both' or instr(active, role) > 0 ) and user_id = ? and is_opposed order by mtime @@ -2197,7 +2198,7 @@ function start_game(game) { state = RULES[game.title_id].setup(seed, game.scenario, options) - SQL_START_GAME.run(state.active, game.game_id) + SQL_START_GAME.run(String(state.active), game.game_id) let replay_id = put_replay(game.game_id, null, ".setup", [ seed, game.scenario, options ]) put_snap(game.game_id, replay_id, state) SQL_INSERT_GAME_STATE.run(game.game_id, JSON.stringify(state)) @@ -2248,7 +2249,7 @@ function rewind_game_to_snap(game_id, snap_id) { SQL_DELETE_GAME_REPLAY.run(game_id, snap.replay_id) SQL_INSERT_GAME_STATE.run(game_id, JSON.stringify(snap_state)) - SQL_REWIND_GAME.run(snap_id - 1, snap_state.active, game_id) + SQL_REWIND_GAME.run(snap_id - 1, String(snap_state.active), game_id) SQL_REWIND_GAME_CLOCK.run(game_id) update_join_clients(game_id) @@ -2539,7 +2540,7 @@ function send_chat_activity_notification(game_id, p) { } function is_active_role(active, role) { - return active === "Both" || active === role + return active === "Both" || active === role || active.includes(role) } function send_game_started_notification(game_id, active) { @@ -3557,7 +3558,7 @@ function put_replay(game_id, role, action, args) { } function dont_snap(rules, state, old_active) { - if (state.active === old_active || !state.active || state.active === "None") + if (String(state.active) === old_active || !state.active || state.active === "None") return true if (rules.dont_snap && rules.dont_snap(state)) return true @@ -3573,11 +3574,12 @@ function put_snap(game_id, replay_id, state) { function put_game_state(game_id, state, old_active, current_role) { // TODO: separate state, undo, and log entries (and reuse "snap" json stringifaction?) + let new_active = String(state.active) SQL_INSERT_GAME_STATE.run(game_id, JSON.stringify(state)) - if (state.active !== old_active) { - SQL_UPDATE_GAME_ACTIVE.run(state.active, game_id) + if (new_active !== old_active) { + SQL_UPDATE_GAME_ACTIVE.run(new_active, game_id) // add time for the player who took the current action SQL_UPDATE_PLAYERS_ADD_TIME.run(game_id, current_role) @@ -3600,7 +3602,7 @@ function put_new_state(title_id, game_id, state, old_active, role, action, args) put_game_state(game_id, state, old_active, role) - if (state.active !== old_active) + if (String(state.active) !== old_active) update_join_clients(game_id) if (game_clients[game_id]) for (let other of game_clients[game_id]) @@ -3632,11 +3634,11 @@ function on_action(socket, action, args, cookie) { try { let state = get_game_state(socket.game_id) - let old_active = state.active + let old_active = String(state.active) // Don't update cookie during simultaneous turns, as it results // in many in-flight collisions. - if (old_active !== "Both") + if (old_active !== "Both" && !old_active.includes(",")) game_cookies[socket.game_id] ++ state = RULES[socket.title_id].action(state, socket.role, action, args) @@ -3670,7 +3672,7 @@ function on_abondon(socket) { function do_abandon(game_id, role) { let game = SQL_SELECT_GAME.get(game_id) let state = get_game_state(game_id) - let old_active = state.active + let old_active = String(state.active) state = finish_game_state(game.title_id, state, "None", role + " abandoned the game.") put_new_state(game.title_id, game_id, state, old_active, role, ".abandon", null) } @@ -3678,7 +3680,7 @@ function do_abandon(game_id, role) { function do_timeout(game_id, role) { let game = SQL_SELECT_GAME.get(game_id) let state = get_game_state(game_id) - let old_active = state.active + let old_active = String(state.active) state = finish_game_state(game.title_id, state, "None", role + " timed out.") put_new_state(game.title_id, game_id, state, old_active, role, ".timeout", null) } @@ -3686,7 +3688,7 @@ function do_timeout(game_id, role) { function do_resign(game_id, role) { let game = SQL_SELECT_GAME.get(game_id) let state = get_game_state(game_id) - let old_active = state.active + let old_active = String(state.active) let result = "None" let roles = get_game_roles(game.title_id, game.scenario, game.options) diff --git a/tools/patchgame.js b/tools/patchgame.js index a28ce1f..bd719ba 100755 --- a/tools/patchgame.js +++ b/tools/patchgame.js @@ -74,7 +74,7 @@ function snapshot(state) { 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 (state.active !== role && state.active !== "Both" && !state.active.includes(role)) return false let view = rules.view(state, role) let va = view.actions[action] @@ -90,7 +90,7 @@ function is_valid_action(rules, state, role, action, arg) { function dont_snap(rules, state, old_active) { if (state.state === "game_over") return true - if (state.active === old_active) + if (String(state.active) === String(old_active)) return true if (rules.dont_snap && rules.dont_snap(state)) return true @@ -219,7 +219,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") |