refactor(page): extract handlers and utils into dedicated modules
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
import { Dispatch, SetStateAction } from "react";
|
||||
import type { Screen } from "./types";
|
||||
import type { useSoloGame } from "./useSoloGame";
|
||||
import type { useMultiGame } from "./useMultiGame";
|
||||
|
||||
type UseSoloGame = ReturnType<typeof useSoloGame>;
|
||||
type UseMultiGame = ReturnType<typeof useMultiGame>;
|
||||
|
||||
type Handlers = {
|
||||
solo: UseSoloGame;
|
||||
multi: UseMultiGame;
|
||||
playerName: string;
|
||||
joinCode: string;
|
||||
setScreen: Dispatch<SetStateAction<Screen>>;
|
||||
setError: Dispatch<SetStateAction<string | null>>;
|
||||
setLoading: Dispatch<SetStateAction<boolean>>;
|
||||
};
|
||||
|
||||
export function useGameHandlers({
|
||||
solo, multi, playerName, joinCode, setScreen, setError, setLoading,
|
||||
}: Handlers) {
|
||||
async function handleCreateRoom() {
|
||||
if (!playerName.trim()) { setError("Entre ton pseudo !"); return; }
|
||||
setLoading(true); setError(null);
|
||||
const { error: err } = await multi.createRoom(playerName.trim());
|
||||
setLoading(false);
|
||||
if (err) { setError(err); return; }
|
||||
setScreen("lobby");
|
||||
}
|
||||
|
||||
async function handleJoinRoom() {
|
||||
if (!playerName.trim()) { setError("Entre ton pseudo !"); return; }
|
||||
if (joinCode.trim().length !== 4) { setError("Le code doit faire 4 lettres"); return; }
|
||||
setLoading(true); setError(null);
|
||||
const { error: err } = await multi.joinRoom(playerName.trim(), joinCode.trim().toUpperCase());
|
||||
setLoading(false);
|
||||
if (err) { setError(err); return; }
|
||||
setScreen("lobby");
|
||||
}
|
||||
|
||||
async function handleStartGame() {
|
||||
setLoading(true); setError(null);
|
||||
const { error: err } = await multi.startGame();
|
||||
setLoading(false);
|
||||
if (err) setError(err);
|
||||
}
|
||||
|
||||
async function handleNextRound() {
|
||||
await multi.nextRound();
|
||||
setScreen("lobby");
|
||||
}
|
||||
|
||||
async function handleResetGame() {
|
||||
await multi.resetGame();
|
||||
setScreen("lobby");
|
||||
}
|
||||
|
||||
function handleLeave() {
|
||||
multi.leave();
|
||||
setScreen("home");
|
||||
}
|
||||
|
||||
function handleSolo() {
|
||||
solo.reset();
|
||||
setScreen("solo");
|
||||
}
|
||||
|
||||
return {
|
||||
handleCreateRoom,
|
||||
handleJoinRoom,
|
||||
handleStartGame,
|
||||
handleNextRound,
|
||||
handleResetGame,
|
||||
handleLeave,
|
||||
handleSolo,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
export function fmt(s: number): string {
|
||||
const m = Math.floor(s / 60);
|
||||
const sec = Math.floor(s % 60);
|
||||
return `${m}:${String(sec).padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
export async function saveGame(data: {
|
||||
mode: string;
|
||||
startArticle: string;
|
||||
targetArticle: string;
|
||||
path: string[];
|
||||
clicks: number;
|
||||
timeSeconds: number;
|
||||
won: boolean;
|
||||
}) {
|
||||
try {
|
||||
await fetch("/api/games", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
} catch { /* silencieux — pas de compte ou hors ligne */ }
|
||||
}
|
||||
Reference in New Issue
Block a user