From cb2a5796df9733145e97b13a8fde6f842fc3476d Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 27 Dec 2023 13:38:05 +0100 Subject: Start games automatically instead of sending reminders to start. Blacklists and invitations handle the cases for which manual starting of games was needed. --- public/join.js | 6 ++---- schema.sql | 20 ++++++-------------- server.js | 41 ++++++++++++++++++++++++++--------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/public/join.js b/public/join.js index e0f6c3c..089a615 100644 --- a/public/join.js +++ b/public/join.js @@ -157,10 +157,8 @@ function update_common() { let message = window.message if (game.status === 0) { - if (ready && (game.owner_id === user_id)) - message.innerHTML = "Ready to start..." - else if (ready) - message.innerHTML = `Waiting for ${game.owner_name} to start the game...` + if (ready) + message.innerHTML = "Waiting to start..." else message.innerHTML = "Waiting for players to join..." } else if (game.status === 1) { diff --git a/schema.sql b/schema.sql index f10ab2c..617c809 100644 --- a/schema.sql +++ b/schema.sql @@ -491,17 +491,6 @@ create view player_view as join users using(user_id) ; -drop view if exists ready_to_start_reminder; -create view ready_to_start_reminder as - select - game_id, owner_id as user_id, name, mail, notify - from - games - join users on user_id = owner_id - where - status = 0 and is_ready - ; - drop view if exists your_turn_reminder; create view your_turn_reminder as select @@ -538,7 +527,8 @@ begin set join_count = ( select count(1) from players where players.game_id = new.game_id ), user_count = ( select count(distinct user_id) from players where players.game_id = new.game_id ), - invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ) + invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ), + mtime = datetime() where games.game_id = new.game_id; end; @@ -551,7 +541,8 @@ begin set join_count = ( select count(1) from players where players.game_id = old.game_id ), user_count = ( select count(distinct user_id) from players where players.game_id = old.game_id ), - invite_count = ( select count(1) from players where players.game_id = old.game_id and players.is_invite ) + invite_count = ( select count(1) from players where players.game_id = old.game_id and players.is_invite ), + mtime = datetime() where games.game_id = old.game_id; end; @@ -562,7 +553,8 @@ begin update games set - invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ) + invite_count = ( select count(1) from players where players.game_id = new.game_id and players.is_invite ), + mtime = datetime() where games.game_id = old.game_id; end; diff --git a/server.js b/server.js index 36cf78f..731fc0d 100644 --- a/server.js +++ b/server.js @@ -1973,6 +1973,8 @@ function start_game(game) { let seed = random_seed() let state = null + console.log("STARTING GAME", game.game_id, game.title_id, game.scenario) + SQL_BEGIN.run() try { if (is_random_scenario(game.title_id, game.scenario)) { @@ -2268,7 +2270,6 @@ function send_play_notification(user, game_id, message) { send_notification(user, game_play_link(game_id, title_id, user), `${title_name} #${game_id} (${user.role}) - ${message}`) } -const QUERY_LIST_READY_TO_START = SQL("select * from ready_to_start_reminder") const QUERY_LIST_YOUR_TURN = SQL("SELECT * FROM your_turn_reminder") const QUERY_LIST_INVITES = SQL("SELECT * FROM invite_reminder") @@ -2330,26 +2331,36 @@ function notify_invited_reminder() { } } -function notify_ready_to_start_reminder() { - for (let item of QUERY_LIST_READY_TO_START.all()) { - if (!is_player_online(item.game_id, item.user_id)) { - if (should_send_reminder(item, item.game_id)) { - insert_last_notified(item, item.game_id) - send_join_notification(item, item.game_id, "Ready to start") - } - } - } -} - // Send "you've been invited" notifications every 5 minutes. setInterval(notify_invited_reminder, 5 * 60 * 1000) -// Check and send ready to start notifications every 7 minutes. -setInterval(notify_ready_to_start_reminder, 7 * 60 * 1000) - // Check and send daily your turn reminders every 17 minutes. setInterval(notify_your_turn_reminder, 17 * 60 * 1000) +const QUERY_READY_TO_START = SQL(` + select + * + from + games + where + status = 0 + and not is_match + and is_ready + and julianday(mtime) < julianday('now', '-30 seconds') +`) + +function ready_game_ticker() { + for (let game of QUERY_READY_TO_START.all()) { + try { + start_game(game) + } catch (err) { + console.log(err) + } + } +} + +setInterval(ready_game_ticker, 47 * 1000) + /* * GAME SERVER */ -- cgit v1.2.3