From ae5ce79bb58b46d04d79134ca8fe094085496dd8 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 21 Dec 2023 23:11:02 +0100 Subject: Hoist resign into common server code. --- server.js | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index e1deeed..b30bbfd 100644 --- a/server.js +++ b/server.js @@ -2514,17 +2514,39 @@ function on_action(socket, action, args, cookie) { function on_resign(socket) { SLOG(socket, "RESIGN") try { - // TODO: shared "resign" function - let state = get_game_state(socket.game_id) - let old_active = state.active - state = socket.rules.resign(state, socket.role) - put_new_state(socket.game_id, state, old_active, socket.role, ".resign", null) + do_resign(socket.game_id, socket.role, "resigned") } catch (err) { console.log(err) return send_message(socket, 'error', err.toString()) } } +function do_resign(game_id, role, how) { + let game = SQL_SELECT_GAME_VIEW.get(game_id) + let state = get_game_state(game_id) + let old_active = state.active + + let result = "None" + + if (game.player_count === 2) { + if (game.moves > 6) { + let roles = get_game_roles(game.title_id, game.scenario, parse_game_options(game.options)) + for (let r of roles) + if (r !== role) + result = r + } + } + + state.state = "game_over" + state.active = "None" + state.result = result + state.victory = role + " " + how + "." + state.log.push("") + state.log.push(state.victory) + + put_new_state(game_id, state, old_active, role, ".resign", null) +} + function on_restore(socket, state_text) { if (!DEBUG) send_message(socket, 'error', "Debugging is not enabled on this server.") -- cgit v1.2.3