summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2025-02-11 01:01:04 +0100
committerTor Andersson <tor@ccxvii.net>2025-02-12 12:04:43 +0100
commita764abe6565544bea8591b26839ad931f3f42e4d (patch)
treeb085b01746fb339a55e06c2423cdb90bf674581c
parent61e0d18ed045ed89ba9abd711fe7b1b5be2ec095 (diff)
downloadserver-a764abe6565544bea8591b26839ad931f3f42e4d.tar.gz
Use state.active to detect finished games.
Instead of looking for (state.state === "game_over"). Add is_active_role helper function.
-rw-r--r--public/common/client.js10
-rw-r--r--schema.sql4
-rw-r--r--server.js23
3 files changed, 21 insertions, 16 deletions
diff --git a/public/common/client.js b/public/common/client.js
index a11f712..288f479 100644
--- a/public/common/client.js
+++ b/public/common/client.js
@@ -361,9 +361,9 @@ function toggle_notepad() {
show_notepad()
}
-/* REMATCH & REPLAY BUTTONS WHEN GAME OVER */
+/* REMATCH & REPLAY BUTTONS WHEN GAME IS FINISHED */
-function on_game_over() {
+function on_finished() {
remove_resign_menu()
add_icon_button(1, "replay_button", "sherlock-holmes-mirror",
@@ -547,8 +547,10 @@ function connect_play() {
if (typeof on_update === "function")
on_update()
on_update_log(view.log_start, game_log.length)
- if (view.game_over)
- on_game_over()
+ break
+
+ case "finished":
+ on_finished()
break
case "snapsize":
diff --git a/schema.sql b/schema.sql
index 8abfe06..5fd189e 100644
--- a/schema.sql
+++ b/schema.sql
@@ -137,7 +137,7 @@ create view user_dynamic_view as
status = 1
and user_count = player_count
and players.user_id = users.user_id
- and active in ( players.role, 'Both' )
+ and active in ( 'Both', players.role )
) + (
select
count(*)
@@ -875,7 +875,7 @@ begin
update players
set clock = clock - (julianday() - julianday(old.mtime))
where
- players.game_id = old.game_id and players.role in ( 'Both', old.active );
+ players.game_id = old.game_id and old.active in ( 'Both', players.role );
end;
-- Trigger to remove game data when filing a game as archived
diff --git a/server.js b/server.js
index 456acda..8b5c0b6 100644
--- a/server.js
+++ b/server.js
@@ -2537,10 +2537,14 @@ function send_chat_activity_notification(game_id, p) {
send_play_notification(p, game_id, "Chat activity")
}
+function is_active_role(active, role) {
+ return active === "Both" || active === role
+}
+
function send_game_started_notification(game_id, active) {
let players = SQL_SELECT_PLAYERS.all(game_id)
for (let p of players) {
- let p_is_active = active === p.role || active === "Both"
+ let p_is_active = is_active_role(active, p.role)
if (p_is_active)
send_play_notification(p, game_id, "Started - Your turn")
else
@@ -2555,8 +2559,8 @@ function send_your_turn_notification_to_offline_users(game_id, old_active, activ
let players = SQL_SELECT_PLAYERS.all(game_id)
for (let p of players) {
- let p_was_active = old_active === p.role || old_active === "Both"
- let p_is_active = active === p.role || active === "Both"
+ let p_was_active = is_active_role(old_active, p.role)
+ let p_is_active = is_active_role(active, p.role)
if (!p_was_active && p_is_active) {
if (!is_player_online(game_id, p.user_id))
send_play_notification(p, game_id, "Your turn")
@@ -3503,13 +3507,14 @@ function send_state(socket, state) {
view.log_start = view.log.length
socket.seen = view.log.length
view.log = view.log.slice(view.log_start)
- if (state.state === "game_over")
- view.game_over = 1
let this_view = JSON.stringify(view)
if (view.actions || socket.last_view !== this_view) {
socket.send('["state",' + this_view + "," + game_cookies[socket.game_id] + "]")
socket.last_view = this_view
}
+ if (!state.active || state.active === "None") {
+ socket.send('["finished"]')
+ }
} catch (err) {
console.log(err)
return send_message(socket, "error", err.toString())
@@ -3549,9 +3554,7 @@ function put_replay(game_id, role, action, args) {
}
function dont_snap(rules, state, old_active) {
- if (state.active === old_active)
- return true
- if (state.state === "game_over")
+ if (state.active === old_active || !state.active || state.active === "None")
return true
if (rules.dont_snap && rules.dont_snap(state))
return true
@@ -3577,7 +3580,7 @@ function put_game_state(game_id, state, old_active, current_role) {
SQL_UPDATE_PLAYERS_ADD_TIME.run(game_id, current_role)
}
- if (state.state === "game_over") {
+ if (!state.active || state.active === "None") {
SQL_FINISH_GAME.run(state.result, game_id)
if (state.result && state.result !== "None")
update_elo_ratings(game_id)
@@ -3600,7 +3603,7 @@ function put_new_state(title_id, game_id, state, old_active, role, action, args)
for (let other of game_clients[game_id])
send_state(other, state)
- if (state.state === "game_over")
+ if (!state.active || state.active === "None")
send_game_finished_notification_to_offline_users(game_id, state.result)
else
send_your_turn_notification_to_offline_users(game_id, old_active, state.active)