fix(multi): sync game mode changes to server in lobby
This commit is contained in:
@@ -127,7 +127,7 @@ export async function PATCH(
|
||||
article?: string;
|
||||
startArticle?: string;
|
||||
targetArticle?: string;
|
||||
value?: boolean;
|
||||
value?: boolean | number | string;
|
||||
};
|
||||
|
||||
// Nettoyer les joueurs inactifs uniquement hors partie (lobby/résultats)
|
||||
@@ -351,6 +351,19 @@ export async function PATCH(
|
||||
return Response.json({ room });
|
||||
}
|
||||
|
||||
// Hôte change le mode de jeu
|
||||
case "setGameMode": {
|
||||
const host = room.players.find((p) => p.id === playerId);
|
||||
if (!host?.isHost) {
|
||||
return Response.json({ error: "Seul l'hôte peut modifier ce paramètre" }, { status: 403 });
|
||||
}
|
||||
if (value === "race" || value === "all_finish") {
|
||||
room.gameMode = value;
|
||||
await saveRoom(room);
|
||||
}
|
||||
return Response.json({ room });
|
||||
}
|
||||
|
||||
// Hôte change la limite de temps
|
||||
case "setTimeLimit": {
|
||||
const host = room.players.find((p) => p.id === playerId);
|
||||
|
||||
@@ -18,6 +18,7 @@ type LobbyScreenProps = {
|
||||
setTotalRounds: (v: number) => void;
|
||||
gameMode: Room["gameMode"];
|
||||
setGameMode: (v: Room["gameMode"]) => void;
|
||||
onSetGameMode: (v: Room["gameMode"]) => void;
|
||||
onSetSearchAllowed: (v: boolean) => void;
|
||||
onSetTimeLimit: (v: number) => void;
|
||||
};
|
||||
@@ -37,6 +38,7 @@ export function LobbyScreen({
|
||||
setTotalRounds,
|
||||
gameMode,
|
||||
setGameMode,
|
||||
onSetGameMode,
|
||||
onSetSearchAllowed,
|
||||
onSetTimeLimit,
|
||||
}: LobbyScreenProps) {
|
||||
@@ -151,7 +153,7 @@ export function LobbyScreen({
|
||||
<span className="text-xs text-[#888]">Mode de jeu</span>
|
||||
<select
|
||||
value={gameMode}
|
||||
onChange={(e) => setGameMode(e.target.value as Room["gameMode"])}
|
||||
onChange={(e) => { const v = e.target.value as Room["gameMode"]; setGameMode(v); onSetGameMode(v); }}
|
||||
className="w-full min-h-11 px-2 bg-[#0f0f0f] border border-[#2e2e2e] rounded-xl text-sm text-[#f0f0f0] outline-none focus:border-[#7c3aed] cursor-pointer transition-colors"
|
||||
>
|
||||
<option value="race">Course - le premier arrivé gagne</option>
|
||||
|
||||
@@ -158,6 +158,7 @@ export function ScreenRouter({
|
||||
setTotalRounds={setTotalRounds}
|
||||
gameMode={gameMode}
|
||||
setGameMode={setGameMode}
|
||||
onSetGameMode={(v) => multi.setGameMode(v)}
|
||||
onSetSearchAllowed={(v) => multi.setSearchAllowed(v)}
|
||||
onSetTimeLimit={(v) => multi.setTimeLimit(v)}
|
||||
/>
|
||||
|
||||
@@ -361,6 +361,16 @@ export function useMultiGame() {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [room, playerId]);
|
||||
|
||||
async function setGameMode(value: "race" | "all_finish") {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "setGameMode", playerId, value }),
|
||||
});
|
||||
if (res.ok) setRoom((await res.json() as { room: Room }).room);
|
||||
}
|
||||
|
||||
async function setTimeLimit(value: number) {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
@@ -468,6 +478,7 @@ export function useMultiGame() {
|
||||
canGoBack: historyRef.current.length > 1,
|
||||
surrender,
|
||||
timeLeft,
|
||||
setGameMode,
|
||||
setTimeLimit,
|
||||
setSearchAllowed,
|
||||
restore,
|
||||
|
||||
Reference in New Issue
Block a user