diff options
Diffstat (limited to 'server.js')
-rw-r--r-- | server.js | 62 |
1 files changed, 27 insertions, 35 deletions
@@ -232,6 +232,7 @@ app.locals.PACE_TEXT = [ app.locals.human_date = human_date app.locals.format_options = format_options +app.locals.format_minutes = format_minutes app.set("x-powered-by", false) app.set("etag", false) @@ -320,6 +321,15 @@ function human_date(date) { return new Date(epoch_from_julianday(date)).toISOString().substring(0,10) } +function format_minutes(mins) { + if (mins > 59) { + var hh = mins / 60 | 0 + var mm = mins % 60 + return `${hh} hours ${mm} minutes` + } + return mins + " minutes" +} + function is_valid_email(email) { return REGEX_MAIL.test(email) } @@ -2298,8 +2308,6 @@ function start_game(game) { put_snap(game.game_id, replay_id, state) SQL_INSERT_GAME_STATE.run(game.game_id, JSON.stringify(state)) - SQL_UPDATE_PLAYERS_INIT_TIME.run(game.game_id) - SQL_COMMIT.run() } finally { if (db.inTransaction) @@ -2604,6 +2612,10 @@ function message_link(msg_id) { return SITE_URL + "/message/read/" + msg_id } +function tour_pool_link(pool_id) { + return SITE_URL + "/tm/pool/" + pool_id +} + function send_notification(user, link, message) { if (WEBHOOKS) { let webhook = SQL_SELECT_WEBHOOK_SEND.get(user.user_id) @@ -2638,6 +2650,10 @@ 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}`) } +function send_tour_notification(user, pool_name, message) { + send_notification(user, tour_pool_link(pool_name), `${pool_name} - ${message}`) +} + function send_chat_activity_notification(game_id, p) { send_play_notification(p, game_id, "Chat activity") } @@ -2784,34 +2800,8 @@ setTimeout(purge_game_ticker, 89 * 1000) * TIME CONTROL */ -const SQL_UPDATE_PLAYERS_INIT_TIME = SQL(` - update players - set clock = ( - case (select pace from games where games.game_id = players.game_id) - when 1 then 1 - when 2 then 3 - when 3 then 3 - else 21 - end - ) - where - players.game_id = ? -`) - -const SQL_UPDATE_PLAYERS_ADD_TIME = SQL(` - update players - set clock = ( - case (select pace from games where games.game_id = players.game_id) - when 1 then min(clock + ${4 / 24}, 3) - when 2 then min(clock + ${12 / 24}, 5) - when 3 then min(clock + ${36 / 24}, 10) - else 21 - end - ) - where - players.game_id = ? and players.role = ? -`) - +// SQL_UPDATE_PLAYERS_INIT_TIME is handled by trigger +// SQL_UPDATE_PLAYERS_ADD_TIME is handled by trigger // SQL_UPDATE_PLAYERS_USE_TIME is handled by trigger const SQL_SELECT_TIME_CONTROL = SQL("select * from time_control_view") @@ -3534,9 +3524,11 @@ function start_tournament_seed(seed_id, level) { function tm_reap_pools() { // reap pools that are finished (and promote winners) + // reap pools that are finished (and notify players) let ended = TM_SELECT_ENDED_POOLS.all() for (let item of ended) { console.log("TM POOL - END", item.pool_name) + SQL_BEGIN.run() try { TM_UPDATE_POOL_FINISHED.run(item.pool_id) @@ -3550,6 +3542,10 @@ function tm_reap_pools() { if (db.inTransaction) SQL_ROLLBACK.run() } + + let players = TM_SELECT_PLAYERS_IN_POOL.all(item.pool_id) + for (let user of players) + send_tour_notification(user, item.pool_name, "Finished") } } @@ -3703,13 +3699,9 @@ function put_game_state(game_id, state, old_active, current_role) { // TODO: separate state, undo, and log entries (and reuse "snap" json stringifaction?) SQL_INSERT_GAME_STATE.run(game_id, JSON.stringify(state)) - if (is_changed_active(old_active, state.active)) { + if (is_changed_active(old_active, state.active)) SQL_UPDATE_GAME_ACTIVE.run(String(state.active), game_id) - // add time for the player who took the current action - SQL_UPDATE_PLAYERS_ADD_TIME.run(game_id, current_role) - } - if (is_nobody_active(state.active)) { SQL_FINISH_GAME.run(state.result, game_id) if (state.result && state.result !== "None") |