feat: server-authoritative save — wait for server before game starts
Some checks failed
CI/CD — Build & Deploy / Build & Deploy (push) Failing after 18s
Some checks failed
CI/CD — Build & Deploy / Build & Deploy (push) Failing after 18s
- Store starts in ready:false — no game actions until authority loaded - useSaveSync signals serverLoaded (success, empty, or unreachable) - GameSync orchestrates: logged in → wait server, guest → localStorage - Home shows loading screen until ready - localStorage = cache only, server = authority for logged-in users
This commit is contained in:
@@ -1,22 +1,47 @@
|
||||
// GameSync.tsx — Bridge useSaveSync ↔ Zustand store
|
||||
// Monter une seule fois dans App. Silencieux en mode invité (pas de token).
|
||||
// Serveur = autorité. Attend la save serveur avant de rendre le jeu jouable.
|
||||
// Guest mode (pas connecté) : init depuis localStorage immédiatement.
|
||||
|
||||
import { useCallback } from "react";
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
import { useGameStore } from "../store/useGameStore";
|
||||
import { useAuth } from "../context/AuthContext";
|
||||
import { useSaveSync } from "../hooks/useSaveSync";
|
||||
|
||||
export function GameSync() {
|
||||
const state = useGameStore((s) => s.state);
|
||||
const ready = useGameStore((s) => s.ready);
|
||||
const loadFromServer = useGameStore((s) => s.loadFromServer);
|
||||
const initGuest = useGameStore((s) => s.initGuest);
|
||||
const playSeconds = useGameStore((s) => s.playSeconds);
|
||||
const { user, loading: authLoading } = useAuth();
|
||||
const initDone = useRef(false);
|
||||
|
||||
const getGameState = useCallback(() => state, [state]);
|
||||
|
||||
useSaveSync({
|
||||
const { serverLoaded } = useSaveSync({
|
||||
getGameState,
|
||||
onLoad: loadFromServer,
|
||||
playTimeSeconds: playSeconds,
|
||||
});
|
||||
|
||||
// Once auth resolves: if no user or no server save → init guest
|
||||
useEffect(() => {
|
||||
if (authLoading || initDone.current || ready) return;
|
||||
|
||||
// Not logged in → guest mode immediately
|
||||
if (!user) {
|
||||
initDone.current = true;
|
||||
initGuest();
|
||||
return;
|
||||
}
|
||||
|
||||
// Logged in but server save loaded (or confirmed empty) → useSaveSync handles it
|
||||
// If serverLoaded is true and store isn't ready yet, it means server had no save
|
||||
if (serverLoaded && !ready) {
|
||||
initDone.current = true;
|
||||
initGuest(); // use localStorage as starting point, server will save it on next sync
|
||||
}
|
||||
}, [authLoading, user, serverLoaded, ready, initGuest]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user