diff --git a/Frontend/src/lib/save-sync.svelte.ts b/Frontend/src/lib/save-sync.svelte.ts index e6b5481..3583a29 100644 --- a/Frontend/src/lib/save-sync.svelte.ts +++ b/Frontend/src/lib/save-sync.svelte.ts @@ -26,12 +26,17 @@ let lastSave: string | null = null; let loaded = false; let saveInterval: ReturnType | null = null; +// Snapshot the $state proxy into a plain object for serialization +function snapshotState(): Record { + return JSON.parse(JSON.stringify(game.state)); +} + export async function saveToServer() { if (!authStore.user || !game.ready) return; const result = await apiRequest('/save', { method: 'POST', body: JSON.stringify({ - gameState: game.state, + gameState: snapshotState(), playTimeSeconds: game.playSeconds, }), }); @@ -103,7 +108,7 @@ export function setupVisibilitySync() { window.addEventListener('beforeunload', () => { if (!authStore.user || !game.ready) return; const payload = JSON.stringify({ - gameState: game.state, + gameState: snapshotState(), playTimeSeconds: game.playSeconds, }); fetch(`${BACKEND_URL}/api/save`, {