From f8fa260447a19c12e8427c12c2b9f7590828ef62 Mon Sep 17 00:00:00 2001 From: Wrab Date: Sun, 24 Nov 2024 14:33:53 +0300 Subject: [PATCH 1/3] refresh notifications with heartbeat --- src/components/GameWindow.svelte | 16 ++++++++++++++++ src/components/Header.astro | 11 +++-------- src/pages/api/games/playtime/heartbeat.ts | 12 ++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/components/GameWindow.svelte b/src/components/GameWindow.svelte index 2088d2ff..13e5bb4e 100644 --- a/src/components/GameWindow.svelte +++ b/src/components/GameWindow.svelte @@ -46,6 +46,7 @@ const TWELVE_HOURS = 1000 * 60 * 60 * 12; let antiThrottler: HTMLAudioElement | undefined; + let lastNotificationCount: string; function updateActivity() { lastInteraction = Date.now(); @@ -165,6 +166,8 @@ diffHolder.classList.add("diff"); currentHolder.append(diffHolder); + const notifications = document.querySelectorAll(".notif"); + let diff = 0; let timer = 60; @@ -204,6 +207,19 @@ ); diff++; + if(lastNotificationCount !== xp.notification){ + lastNotificationCount = xp.notification + if (xp.notification !== "0"){ + notifications.forEach(bubble => { + bubble.textContent = xp.notification + bubble.style.removeProperty("display") + }); + } else { + notifications.forEach(bubble => { + bubble.style.display = "none" + }); + } + } } busy = false; timerHolder.innerHTML = `

next xp in

${timer.toLocaleString( diff --git a/src/components/Header.astro b/src/components/Header.astro index 3bf24cab..5756e1b0 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -259,11 +259,9 @@ const xpToNext = getXPToNextLevel(level);
{/* prettier-ignore */} - {mcount !== "0" && ( - + {mcount} - - )}{username}{username}me view profile {/* prettier-ignore */} - mailbox{ - mcount !== "0" && ( - {mcount} - )} + mailbox{mcount}

preferences

diff --git a/src/pages/api/games/playtime/heartbeat.ts b/src/pages/api/games/playtime/heartbeat.ts index ade35f66..248ce0c9 100644 --- a/src/pages/api/games/playtime/heartbeat.ts +++ b/src/pages/api/games/playtime/heartbeat.ts @@ -2,6 +2,7 @@ import { getLevel, getXP, getXPToNextLevel } from "~/helper/leveling"; import { rdata, rmsg } from "~/helper/res"; import { DayGameStats } from "~/models/daygamestats"; import { Game } from "~/models/game"; +import { Message } from "~/models/message"; import { PlaySession } from "~/models/playsession"; import { Playtime } from "~/models/playtime"; import { User } from "~/models/user"; @@ -71,7 +72,14 @@ export const POST = route( ); } } - + // Update notifications count + let mcount: string | number = "0"; + mcount = await Message.countDocuments({ + to: user.id, + read: false, + deleted: false, + }); + mcount = mcount.toLocaleString(); // Original playtime system let playtime = await Playtime.findOne({ game: game.id, user: user.id }); @@ -114,7 +122,7 @@ export const POST = route( const xpToNext = getXPToNextLevel(level); return rdata( - { level, xp: xp - xpToCurrent, next: xpToNext - xpToCurrent }, + { level, xp: xp - xpToCurrent, next: xpToNext - xpToCurrent, notification: mcount }, 201 ); } -- GitLab From 55285dfdcdc5952d27f309f42e212e3c5dba5fc9 Mon Sep 17 00:00:00 2001 From: Wrab Date: Sun, 24 Nov 2024 14:34:24 +0300 Subject: [PATCH 2/3] instantly update notifications in inbox --- src/components/Inbox.svelte | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/components/Inbox.svelte b/src/components/Inbox.svelte index 3ec4a006..24b9f7a2 100644 --- a/src/components/Inbox.svelte +++ b/src/components/Inbox.svelte @@ -26,6 +26,8 @@ content: "", }; + const notifications = document.querySelectorAll(".notif"); + async function read(id: string) { message = "loading"; const res = await json(`/api/messages/read?id=${id}`); @@ -42,9 +44,9 @@ const mcount = messages.reduce((n, m) => (n += !m.read), 0); if (mcount === 0) { - [...document.querySelectorAll(".notif")].forEach(e => e.remove()); + notifications.forEach(e => e.style.display = "none"); } else { - [...document.querySelectorAll(".notif")].forEach( + notifications.forEach( e => (e.textContent = mcount.toLocaleString()) ); } @@ -75,6 +77,9 @@ m.read = true; return m; }); + notifications.forEach(bubble => { + bubble.style.display = "none"; + }); } } @@ -99,6 +104,12 @@ if (m.id === message._id) m.read = false; return m; }); + + const mcount = messages.reduce((n, m) => (n += !m.read), 0); + notifications.forEach(bubble => { + bubble.textContent = mcount.toLocaleString(); + bubble.style.removeProperty("display"); + }); } } -- GitLab From 7fd3b4150dd97d0e15a21128f2175cfa7c90bcc0 Mon Sep 17 00:00:00 2001 From: Wrab Date: Tue, 3 Dec 2024 21:11:23 +0300 Subject: [PATCH 3/3] include notification count in invalid heartbeats --- src/components/GameWindow.svelte | 2 ++ src/pages/api/games/playtime/heartbeat.ts | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/GameWindow.svelte b/src/components/GameWindow.svelte index 13e5bb4e..19fc15e2 100644 --- a/src/components/GameWindow.svelte +++ b/src/components/GameWindow.svelte @@ -207,6 +207,8 @@ ); diff++; + } + if(xp.notification !== undefined){ if(lastNotificationCount !== xp.notification){ lastNotificationCount = xp.notification if (xp.notification !== "0"){ diff --git a/src/pages/api/games/playtime/heartbeat.ts b/src/pages/api/games/playtime/heartbeat.ts index 248ce0c9..68cda17d 100644 --- a/src/pages/api/games/playtime/heartbeat.ts +++ b/src/pages/api/games/playtime/heartbeat.ts @@ -93,7 +93,7 @@ export const POST = route( }); } else { if (Date.now() - user.lastHeartbeat < 59 * 1000) { - return rmsg("Invalid heartbeat", 400); + return rdata({ message: "Invalid heartbeat", notification: mcount }, 400); } // if (playtime.updatedAt) -- GitLab