From 9b518d79dc30c0a981443d790a11ff477d219cc3 Mon Sep 17 00:00:00 2001 From: Jage9 Date: Fri, 20 Feb 2026 18:19:42 -0500 Subject: [PATCH] Persist player position across refresh/unload --- client/public/version.js | 2 +- client/src/main.ts | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/client/public/version.js b/client/public/version.js index eef478c..c449489 100644 --- a/client/public/version.js +++ b/client/public/version.js @@ -1,3 +1,3 @@ // Maintainer-controlled web client version. // Format: YYYY.MM.DD Rn (example: 2026.02.20 R2) -window.CHGRID_WEB_VERSION = "2026.02.20 R68"; +window.CHGRID_WEB_VERSION = "2026.02.20 R69"; diff --git a/client/src/main.ts b/client/src/main.ts index 944fe0a..391c9e7 100644 --- a/client/src/main.ts +++ b/client/src/main.ts @@ -747,6 +747,17 @@ function squareWord(distance: number): string { return distance === 1 ? 'square' : 'squares'; } +function persistPlayerPosition(): void { + try { + localStorage.setItem( + 'spatialChatPosition', + JSON.stringify({ x: state.player.x, y: state.player.y }), + ); + } catch { + // Ignore storage failures (private mode/quota/blocked storage). + } +} + function gameLoop(): void { if (!state.running) return; handleMovement(); @@ -777,6 +788,7 @@ function handleMovement(): void { state.player.x = nextX; state.player.y = nextY; + persistPlayerPosition(); state.player.lastMoveTime = now; audio.sfxMove(state.player); signaling.send({ type: 'update_position', x: nextX, y: nextY }); @@ -926,10 +938,7 @@ async function connect(): Promise { function disconnect(): void { const wasRunning = state.running; if (state.running) { - localStorage.setItem( - 'spatialChatPosition', - JSON.stringify({ x: state.player.x, y: state.player.y }), - ); + persistPlayerPosition(); } signaling.disconnect(); @@ -1504,6 +1513,7 @@ function handleListModeInput(code: string): void { if (!peer) return; state.player.x = peer.x; state.player.y = peer.y; + persistPlayerPosition(); signaling.send({ type: 'update_position', x: peer.x, y: peer.y }); state.mode = 'normal'; updateStatus(`Moved to ${peer.nickname}.`); @@ -1541,6 +1551,7 @@ function handleListItemsModeInput(code: string): void { if (!item) return; state.player.x = item.x; state.player.y = item.y; + persistPlayerPosition(); signaling.send({ type: 'update_position', x: item.x, y: item.y }); state.mode = 'normal'; updateStatus(`Moved to ${itemLabel(item)}.`); @@ -1982,6 +1993,13 @@ function closeSettings(): void { } function setupUiHandlers(): void { + const persistOnUnload = (): void => { + if (!state.running) return; + persistPlayerPosition(); + }; + window.addEventListener('pagehide', persistOnUnload); + window.addEventListener('beforeunload', persistOnUnload); + dom.connectButton.addEventListener('click', () => { void connect(); });