From 7c1756320916352d100ac8724d032ab937978832 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 22 May 2022 15:22:57 +0200 Subject: Send "game over" mail notifications. --- server.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'server.js') diff --git a/server.js b/server.js index 10d6b63..b206ee2 100644 --- a/server.js +++ b/server.js @@ -1520,6 +1520,19 @@ function mail_new_message(user, msg_id, msg_from) { } } +function mail_game_over_notification(user, game_id, result, victory) { + if (mailer) { + let game = SQL_SELECT_GAME_FULL_VIEW.get(game_id); + let subject = `${game.title_name} #${game_id} (${user.role}) - Finished!`; + let body = mail_game_info(game) + + victory + "\n\n" + + mail_game_link(game_id, user) + + MAIL_FOOTER; + console.log("SENT MAIL:", mail_addr(user), subject); + mailer.sendMail({ from: MAIL_FROM, to: mail_addr(user), subject: subject, text: body }, mail_callback); + } +} + function mail_your_turn_notification(user, game_id, interval) { if (mailer) { let too_soon = SQL_SELECT_NOTIFIED.get(interval, game_id, user.user_id); @@ -1581,6 +1594,13 @@ function mail_your_turn_notification_to_offline_users(game_id, old_active, activ } } +function mail_game_over_notification_to_offline_users(game_id, result, victory) { + let players = SQL_SELECT_PLAYERS.all(game_id); + for (let p of players) + if (p.notify && !is_online(game_id, p.user_id)) + mail_game_over_notification(p, game_id, result, victory); +} + function notify_your_turn_reminder() { for (let item of QUERY_LIST_YOUR_TURN.all()) { mail_your_turn_notification(item, item.game_id, '+25 hours'); @@ -1661,6 +1681,7 @@ function get_game_state(game_id) { function put_game_state(game_id, state, old_active) { if (state.state === 'game_over') { SQL_UPDATE_GAME_RESULT.run(2, state.result, game_id); + mail_game_over_notification_to_offline_users(game_id, state.result, state.victory); } SQL_UPDATE_GAME_STATE.run(game_id, JSON.stringify(state), state.active); for (let other of clients[game_id]) -- cgit v1.2.3