From a1c93c992e456764415b5a4c302e1137673c0a5f Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 1 Jan 2024 17:36:56 +0100 Subject: Add time control enforcement. --- server.js | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index 620fd58..71972d4 100644 --- a/server.js +++ b/server.js @@ -1183,7 +1183,8 @@ function is_game_ready(player_count, players) { load_rules() const SQL_INSERT_GAME = SQL("INSERT INTO games (owner_id,title_id,scenario,options,player_count,pace,is_private,is_random,notice,is_match) VALUES (?,?,?,?,?,?,?,?,?,?) returning game_id").pluck() -const SQL_DELETE_GAME = SQL("DELETE FROM games WHERE game_id=? AND owner_id=?") +const SQL_DELETE_GAME_BY_OWNER = SQL("delete from games where game_id=? and owner_id=?") +const SQL_DELETE_GAME = SQL("delete from games where game_id=?") const SQL_START_GAME = SQL(` update games set @@ -1430,6 +1431,7 @@ function annotate_game_info(game, user_id, unread) { let your_count = 0 let your_role = null + let time_left = Infinity let roles = get_game_roles(game.title_id, game.scenario, options) @@ -1449,6 +1451,9 @@ function annotate_game_info(game, user_id, unread) { game.your_turn = true } + if (p.is_active) + time_left = Math.min(time_left, p.time_left) + let link if (p.is_invite) link = `${p.name}?` @@ -1475,6 +1480,9 @@ function annotate_game_info(game, user_id, unread) { }).join(", ") } + if (game.is_ready && game.status === 1) + game.time_left = time_left + if (your_count > 0) { game.is_yours = true if (your_count === 1) @@ -1675,7 +1683,7 @@ app.post("/create/:title_id", must_be_logged_in, function (req, res) { app.get("/delete/:game_id", must_be_logged_in, function (req, res) { let game_id = req.params.game_id let title_id = SQL_SELECT_GAME_TITLE.get(game_id) - let info = SQL_DELETE_GAME.run(game_id, req.user.user_id) + let info = SQL_DELETE_GAME_BY_OWNER.run(game_id, req.user.user_id) if (info.changes === 0) return res.send("Not authorized to delete that game ID.") if (info.changes === 1) @@ -2443,6 +2451,27 @@ function purge_game_ticker() { setInterval(purge_game_ticker, 31 * 60 * 1000) setTimeout(purge_game_ticker, 89 * 1000) +/* + * TIME CONTROL + */ + +const QUERY_LIST_TIME_CONTROL = SQL("select * from time_control_view join users using(user_id) where time_left < 0") + +function time_control_ticker() { + for (let item of QUERY_LIST_TIME_CONTROL.all()) { + if (item.is_opposed) { + console.log("TIMED OUT GAME:", item.game_id, item.name, item.time_left) + do_resign(item.game_id, item.role, "timed out") + } else { + SQL_DELETE_GAME.run(item.game_id) + } + } +} + +// Run time control checks every 13 minutes. +setInterval(time_control_ticker, 13 * 60 * 1000) +setTimeout(time_control_ticker, 13 * 1000) + /* * GAME SERVER */ -- cgit v1.2.3