Persist player position across refresh/unload

This commit is contained in:
Jage9
2026-02-20 18:19:42 -05:00
parent d61459cf72
commit 9b518d79dc
2 changed files with 23 additions and 5 deletions

View File

@@ -1,3 +1,3 @@
// Maintainer-controlled web client version. // Maintainer-controlled web client version.
// Format: YYYY.MM.DD Rn (example: 2026.02.20 R2) // 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";

View File

@@ -747,6 +747,17 @@ function squareWord(distance: number): string {
return distance === 1 ? 'square' : 'squares'; 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 { function gameLoop(): void {
if (!state.running) return; if (!state.running) return;
handleMovement(); handleMovement();
@@ -777,6 +788,7 @@ function handleMovement(): void {
state.player.x = nextX; state.player.x = nextX;
state.player.y = nextY; state.player.y = nextY;
persistPlayerPosition();
state.player.lastMoveTime = now; state.player.lastMoveTime = now;
audio.sfxMove(state.player); audio.sfxMove(state.player);
signaling.send({ type: 'update_position', x: nextX, y: nextY }); signaling.send({ type: 'update_position', x: nextX, y: nextY });
@@ -926,10 +938,7 @@ async function connect(): Promise<void> {
function disconnect(): void { function disconnect(): void {
const wasRunning = state.running; const wasRunning = state.running;
if (state.running) { if (state.running) {
localStorage.setItem( persistPlayerPosition();
'spatialChatPosition',
JSON.stringify({ x: state.player.x, y: state.player.y }),
);
} }
signaling.disconnect(); signaling.disconnect();
@@ -1504,6 +1513,7 @@ function handleListModeInput(code: string): void {
if (!peer) return; if (!peer) return;
state.player.x = peer.x; state.player.x = peer.x;
state.player.y = peer.y; state.player.y = peer.y;
persistPlayerPosition();
signaling.send({ type: 'update_position', x: peer.x, y: peer.y }); signaling.send({ type: 'update_position', x: peer.x, y: peer.y });
state.mode = 'normal'; state.mode = 'normal';
updateStatus(`Moved to ${peer.nickname}.`); updateStatus(`Moved to ${peer.nickname}.`);
@@ -1541,6 +1551,7 @@ function handleListItemsModeInput(code: string): void {
if (!item) return; if (!item) return;
state.player.x = item.x; state.player.x = item.x;
state.player.y = item.y; state.player.y = item.y;
persistPlayerPosition();
signaling.send({ type: 'update_position', x: item.x, y: item.y }); signaling.send({ type: 'update_position', x: item.x, y: item.y });
state.mode = 'normal'; state.mode = 'normal';
updateStatus(`Moved to ${itemLabel(item)}.`); updateStatus(`Moved to ${itemLabel(item)}.`);
@@ -1982,6 +1993,13 @@ function closeSettings(): void {
} }
function setupUiHandlers(): void { function setupUiHandlers(): void {
const persistOnUnload = (): void => {
if (!state.running) return;
persistPlayerPosition();
};
window.addEventListener('pagehide', persistOnUnload);
window.addEventListener('beforeunload', persistOnUnload);
dom.connectButton.addEventListener('click', () => { dom.connectButton.addEventListener('click', () => {
void connect(); void connect();
}); });