diff --git a/src/tictactoe/providers/NakamaProvider.tsx b/src/tictactoe/providers/NakamaProvider.tsx index 2daab52..5531361 100644 --- a/src/tictactoe/providers/NakamaProvider.tsx +++ b/src/tictactoe/providers/NakamaProvider.tsx @@ -47,6 +47,8 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) { const [session, setSession] = useState(null); const [socket, setSocket] = useState(null); const [matchId, setMatchId] = useState(null); + const lastModeRef = React.useRef(null); + const socketRef = React.useRef(null); // ---------------------------------------------------- // LOGIN @@ -62,17 +64,32 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) { const s = client.createSocket(undefined, undefined); // no SSL on localhost await s.connect(newSession, true); setSocket(s); + socketRef.current = s; console.log("[Nakama] WebSocket connected"); // MATCHMAKER MATCHED CALLBACK s.onmatchmakermatched = async (matched: MatchmakerMatched) => { + // 1) If match_id is empty → server rejected the group. + if (!matched.match_id) { + console.warn("[Nakama] Match rejected by server. Auto-requeueing..."); + + if (lastModeRef.current) { + try { + await joinMatchmaker(lastModeRef.current); + } catch (e) { + console.error("[Nakama] Requeue failed:", e); + } + } + + return; + } + + // 2) Valid match: continue as usual. console.log("[Nakama] MATCHED:", matched); - - setMatchId(matched.match_id); - try { await s.joinMatch(matched.match_id); + setMatchId(matched.match_id); console.log("[Nakama] Auto-joined match:", matched.match_id); } catch (err) { console.error("[Nakama] Failed to join match:", err); @@ -84,6 +101,7 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) { // MATCHMAKING // ---------------------------------------------------- async function joinMatchmaker(mode: string) { + const socket = socketRef.current; if (!socket) throw new Error("socket missing"); console.log(`[Nakama] Matchmaking... with +mode:"${mode}"`); @@ -94,6 +112,8 @@ export function NakamaProvider({ children }: { children: React.ReactNode }) { { mode } // stringProperties ); + lastModeRef.current = mode; + return ticket.ticket; }