From a3cf56846a50185e7e57e4f71ad5ae24f5af70bf Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 28 Jan 2024 13:25:15 +0100 Subject: Track new chat status centrally instead of per client. --- public/common/client.js | 48 +++++++++++++++++++++++++++--------------------- server.js | 28 ++++++++++++---------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/public/common/client.js b/public/common/client.js index f399e86..0b9925c 100644 --- a/public/common/client.js +++ b/public/common/client.js @@ -149,9 +149,7 @@ window.addEventListener("focus", stop_blinker) let chat = null function init_chat() { - // only fetch new messages when we reconnect! if (chat !== null) { - send_message("getchat", chat.log) return } @@ -171,13 +169,10 @@ function init_chat() { chat = { is_visible: false, text_element: document.getElementById("chat_text"), - key: "chat/" + params.game_id, last_day: null, log: 0 } - chat.seen = window.localStorage.getItem(chat.key) | 0 - drag_element_with_mouse("#chat_window", "#chat_header") document.getElementById("chat_form").addEventListener("submit", e => { @@ -207,12 +202,6 @@ function init_chat() { } } }) - - send_message("getchat", 0) -} - -function save_chat() { - window.localStorage.setItem(chat.key, chat.log) } function update_chat(chat_id, raw_date, user, message) { @@ -248,14 +237,24 @@ function update_chat(chat_id, raw_date, user, message) { } add_chat_line(format_time(date), user, message) } - if (chat_id > chat.seen) { - let button = document.getElementById("chat_button") - start_blinker("NEW MESSAGE") - if (!chat.is_visible) - button.classList.add("new") - else - save_chat() - } +} + +function fetch_chat() { + send_message("getchat", chat.log) +} + +function update_chat_new() { + let button = document.getElementById("chat_button") + start_blinker("NEW MESSAGE") + if (chat && chat.is_visible) + fetch_chat() + else + button.classList.add("new") +} + +function update_chat_old() { + let button = document.getElementById("chat_button") + document.getElementById("chat_button").classList.remove("new") } function show_chat() { @@ -264,7 +263,7 @@ function show_chat() { document.getElementById("chat_window").classList.add("show") document.getElementById("chat_input").focus() chat.is_visible = true - save_chat() + fetch_chat() } } @@ -496,6 +495,14 @@ function connect_play() { } break + case "newchat": + init_chat() + if (arg > 0) + update_chat_new() + else + update_chat_old() + break + case "chat": update_chat(arg[0], arg[1], arg[2], arg[3]) break @@ -509,7 +516,6 @@ function connect_play() { init_player_names(arg[1]) document.body.classList.add(player.replace(/\W/g, "_")) if (player !== "Observer") { - init_chat() init_notepad() add_resign_menu() } else { diff --git a/server.js b/server.js index 71972d4..d783c5f 100644 --- a/server.js +++ b/server.js @@ -1213,11 +1213,12 @@ const SQL_SELECT_GAME_STATE = SQL("select state from game_state where game_id=?" const SQL_INSERT_GAME_STATE = SQL("insert or replace into game_state (game_id,state) values (?,?)") const SQL_SELECT_UNREAD_CHAT_GAMES = SQL("select game_id from unread_chats where user_id = ?").pluck() +const SQL_SELECT_UNREAD_CHAT = SQL("select exists (select 1 from unread_chats where user_id = ? and game_id = ?)").pluck() const SQL_INSERT_UNREAD_CHAT = SQL("insert or ignore into unread_chats (user_id,game_id) values (?,?)") const SQL_DELETE_UNREAD_CHAT = SQL("delete from unread_chats where user_id = ? and game_id = ?") const SQL_SELECT_GAME_CHAT = SQL("SELECT chat_id,unixepoch(time),name,message FROM game_chat_view WHERE game_id=? AND chat_id>?").raw() -const SQL_INSERT_GAME_CHAT = SQL("INSERT INTO game_chat (game_id,chat_id,user_id,message) VALUES (?, (select coalesce(max(chat_id), 0) + 1 from game_chat where game_id=?), ?,?) RETURNING chat_id,unixepoch(time),NULL,message").raw() +const SQL_INSERT_GAME_CHAT = SQL("INSERT INTO game_chat (game_id,chat_id,user_id,message) VALUES (?, (select coalesce(max(chat_id), 0) + 1 from game_chat where game_id=?), ?,?)") const SQL_SELECT_GAME_NOTE = SQL("SELECT note FROM game_notes WHERE game_id=? AND role=?").pluck() const SQL_UPDATE_GAME_NOTE = SQL("INSERT OR REPLACE INTO game_notes (game_id,role,note) VALUES (?,?,?)") @@ -2776,25 +2777,16 @@ function on_chat(socket, message) { } function send_chat_message(game_id, from_id, from_name, message) { - let chat = SQL_INSERT_GAME_CHAT.get(game_id, game_id, from_id, message) - chat[2] = from_name + SQL_INSERT_GAME_CHAT.run(game_id, game_id, from_id, message) + + let users = SQL_SELECT_PLAYERS_ID.all(game_id) + for (let user_id of users) + SQL_INSERT_UNREAD_CHAT.run(user_id, game_id) if (game_clients[game_id]) { for (let other of game_clients[game_id]) if (other.role !== "Observer") - send_message(other, "chat", chat) - } - - let users = SQL_SELECT_PLAYERS_ID.all(game_id) - for (let user_id of users) { - let found = false - if (game_clients[game_id]) { - for (let other of game_clients[game_id]) - if (other.user && other.user.user_id === user_id && other.role !== "Observer") - found = true - } - if (!found) - SQL_INSERT_UNREAD_CHAT.run(user_id, game_id) + send_message(other, "newchat", 1) } } @@ -2913,6 +2905,7 @@ wss.on("connection", (socket, req) => { if (socket.role !== "Observer") { if (!socket.user) return socket.close(1000, "You are not logged in!") + if (socket.role && socket.role !== "undefined" && socket.role !== "null") { let me = players.find(p => p.user_id === socket.user.user_id && p.role === socket.role) if (!me) @@ -2921,6 +2914,9 @@ wss.on("connection", (socket, req) => { let me = players.find(p => p.user_id === socket.user.user_id) socket.role = me ? me.role : "Observer" } + + let new_chat = SQL_SELECT_UNREAD_CHAT.get(socket.user.user_id, socket.game_id) + send_message(socket, "newchat", new_chat) } if (socket.seen === 0) { -- cgit v1.2.3