summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-12-21 23:11:02 +0100
committerTor Andersson <tor@ccxvii.net>2023-12-23 23:47:01 +0100
commitae5ce79bb58b46d04d79134ca8fe094085496dd8 (patch)
tree70de2cc5eb314e0a7377bdc72de7288da2f8f480
parent6aa382f07a75872db327f5f1ed1c012da0d3d5e3 (diff)
downloadserver-ae5ce79bb58b46d04d79134ca8fe094085496dd8.tar.gz
Hoist resign into common server code.
-rw-r--r--server.js32
1 files changed, 27 insertions, 5 deletions
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.")