fix(multi): restrict search and game settings to room host only

This commit is contained in:
jessy-david-dev
2026-04-11 22:02:06 +02:00
parent 0401cdffe6
commit ede083f881
12 changed files with 126 additions and 34 deletions
+45 -13
View File
@@ -13,6 +13,7 @@ function dbToRoom(row: {
totalRounds: number;
maxPlayers: number;
gameMode: string;
searchAllowed: boolean;
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -28,10 +29,12 @@ function dbToRoom(row: {
totalRounds: row.totalRounds,
maxPlayers: row.maxPlayers,
gameMode: (row.gameMode ?? "race") as Room["gameMode"],
searchAllowed: row.searchAllowed ?? false,
startArticle: row.startArticle,
targetArticle: row.targetArticle,
roundWinner: row.roundWinner,
countdownStart: row.countdownStart !== null ? Number(row.countdownStart) : null,
countdownStart:
row.countdownStart !== null ? Number(row.countdownStart) : null,
roundStart: row.roundStart !== null ? Number(row.roundStart) : null,
createdAt: Number(row.createdAt),
};
@@ -82,7 +85,9 @@ export async function PATCH(
{ params }: { params: Promise<{ code: string }> },
) {
const { code } = await params;
const row = await prisma.room.findUnique({ where: { code: code.toUpperCase() } });
const row = await prisma.room.findUnique({
where: { code: code.toUpperCase() },
});
if (!row) {
return Response.json({ error: "Room introuvable" }, { status: 404 });
@@ -91,15 +96,23 @@ export async function PATCH(
const room = dbToRoom(row);
const body = await request.json();
const { action, playerId, playerName, article, startArticle, targetArticle } =
body as {
action: string;
playerId?: string;
playerName?: string;
article?: string;
startArticle?: string;
targetArticle?: string;
};
const {
action,
playerId,
playerName,
article,
startArticle,
targetArticle,
value,
} = body as {
action: string;
playerId?: string;
playerName?: string;
article?: string;
startArticle?: string;
targetArticle?: string;
value?: boolean;
};
// Nettoyer les joueurs inactifs avant chaque action
room.players = prunePlayers(room.players);
@@ -279,7 +292,9 @@ export async function PATCH(
if (room.gameMode === "race") {
// En mode course, forfait ne termine pas la manche — on attend qu'un vrai gagnant arrive
// Sauf si tous ont abandonné
const anyoneStillPlaying = room.players.some((p) => !p.hasWon && !p.hasSurrendered);
const anyoneStillPlaying = room.players.some(
(p) => !p.hasWon && !p.hasSurrendered,
);
if (!anyoneStillPlaying) {
room.phase = "results";
// Pas de roundWinner si tout le monde a abandonné
@@ -295,6 +310,21 @@ export async function PATCH(
return Response.json({ room });
}
// Hôte toggle recherche Ctrl+F
case "setSearchAllowed": {
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 },
);
}
room.searchAllowed =
typeof value === "boolean" ? value : !room.searchAllowed;
await saveRoom(room);
return Response.json({ room });
}
// Manche suivante / rejouer
case "nextRound": {
const host = room.players.find((p) => p.id === playerId);
@@ -356,10 +386,12 @@ async function saveRoom(room: Room) {
totalRounds: room.totalRounds,
maxPlayers: room.maxPlayers,
gameMode: room.gameMode,
searchAllowed: room.searchAllowed,
startArticle: room.startArticle,
targetArticle: room.targetArticle,
roundWinner: room.roundWinner,
countdownStart: room.countdownStart !== null ? BigInt(room.countdownStart) : null,
countdownStart:
room.countdownStart !== null ? BigInt(room.countdownStart) : null,
roundStart: room.roundStart !== null ? BigInt(room.roundStart) : null,
},
});
+4
View File
@@ -25,6 +25,7 @@ export type Room = {
totalRounds: number;
maxPlayers: number;
gameMode: "race" | "all_finish"; // race = 1er gagne, all_finish = tout le monde joue
searchAllowed: boolean;
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -56,6 +57,7 @@ function dbToRoom(row: {
totalRounds: number;
maxPlayers: number;
gameMode: string;
searchAllowed: boolean;
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -71,6 +73,7 @@ function dbToRoom(row: {
totalRounds: row.totalRounds,
maxPlayers: row.maxPlayers,
gameMode: (row.gameMode ?? "race") as Room["gameMode"],
searchAllowed: row.searchAllowed ?? false,
startArticle: row.startArticle,
targetArticle: row.targetArticle,
roundWinner: row.roundWinner,
@@ -154,6 +157,7 @@ export async function POST(request: NextRequest) {
totalRounds: clampedRounds,
maxPlayers: clampedMax,
gameMode: clampedMode,
searchAllowed: false,
startArticle: "",
targetArticle: "",
roundWinner: null,
+2
View File
@@ -4,6 +4,7 @@ import { useEffect, useRef } from "react";
import { ArticleView } from "./ArticleView";
import { Breadcrumbs } from "./Breadcrumbs";
import type { Room } from "../api/rooms/route";
import { useCtrlFBlock } from "../../lib/useSearchAllowed";
type GameScreenProps = {
room: Room;
@@ -28,6 +29,7 @@ export function GameScreen({
countdown, onNavigate, onRetry, onNextRound, onResetGame, onSurrender,
}: GameScreenProps) {
const breadcrumbEndRef = useRef<HTMLDivElement>(null);
useCtrlFBlock(room.searchAllowed);
const myPlayer = room.players.find((p) => p.id === playerId);
const isHost = myPlayer?.isHost ?? false;
const myFinished = myPlayer?.hasWon || myPlayer?.hasSurrendered;
+19 -13
View File
@@ -1,7 +1,6 @@
"use client";
import { useState } from "react";
import { useSearchAllowed } from "../../lib/useSearchAllowed";
import type { Room } from "../api/rooms/route";
type LobbyScreenProps = {
@@ -19,6 +18,7 @@ type LobbyScreenProps = {
setTotalRounds: (v: number) => void;
gameMode: Room["gameMode"];
setGameMode: (v: Room["gameMode"]) => void;
onSetSearchAllowed: (v: boolean) => void;
};
export function LobbyScreen({
@@ -36,11 +36,11 @@ export function LobbyScreen({
setTotalRounds,
gameMode,
setGameMode,
onSetSearchAllowed,
}: LobbyScreenProps) {
const isHost = room.players.find((p) => p.id === playerId)?.isHost ?? false;
const [copied, setCopied] = useState(false);
const [blurred, setBlurred] = useState(true);
const { allowed: searchAllowed, toggle: toggleSearch } = useSearchAllowed();
function copyCode() {
navigator.clipboard.writeText(room.code).then(() => {
@@ -186,20 +186,26 @@ export function LobbyScreen({
</select>
</div>
</div>
<button
onClick={() => onSetSearchAllowed(!room.searchAllowed)}
className={`w-full min-h-11 rounded-xl text-sm font-semibold border transition-colors cursor-pointer flex items-center justify-between px-4 ${room.searchAllowed ? "bg-[#7c3aed]/10 border-[#7c3aed] text-[#a78bfa]" : "bg-[#1a1a1a] border-[#2e2e2e] text-[#888]"}`}
>
<span>🔍 Recherche Ctrl+F</span>
<span className={`text-xs font-bold px-2 py-0.5 rounded-full ${room.searchAllowed ? "bg-[#7c3aed]/30 text-[#a78bfa]" : "bg-[#242424] text-[#555]"}`}>
{room.searchAllowed ? "Autorisée" : "Bloquée"}
</span>
</button>
</div>
)}
<button
onClick={toggleSearch}
className={`w-full min-h-11 rounded-xl text-sm font-semibold border transition-colors cursor-pointer flex items-center justify-between px-4 ${searchAllowed ? "bg-[#7c3aed]/10 border-[#7c3aed] text-[#a78bfa]" : "bg-[#1a1a1a] border-[#2e2e2e] text-[#888]"}`}
>
<span>🔍 Recherche Ctrl+F</span>
<span
className={`text-xs font-bold px-2 py-0.5 rounded-full ${searchAllowed ? "bg-[#7c3aed]/30 text-[#a78bfa]" : "bg-[#242424] text-[#555]"}`}
>
{searchAllowed ? "Autorisée" : "Bloquée"}
</span>
</button>
{!isHost && (
<div className={`w-full min-h-11 rounded-xl text-sm font-semibold border flex items-center justify-between px-4 ${room.searchAllowed ? "bg-[#7c3aed]/10 border-[#7c3aed] text-[#a78bfa]" : "bg-[#1a1a1a] border-[#2e2e2e] text-[#888]"}`}>
<span>🔍 Recherche Ctrl+F</span>
<span className={`text-xs font-bold px-2 py-0.5 rounded-full ${room.searchAllowed ? "bg-[#7c3aed]/30 text-[#a78bfa]" : "bg-[#242424] text-[#555]"}`}>
{room.searchAllowed ? "Autorisée" : "Bloquée"}
</span>
</div>
)}
{room.round > 0 && (
<p className="text-xs text-[#888] text-center">
+1
View File
@@ -112,6 +112,7 @@ export function ScreenRouter({
maxPlayers={maxPlayers} setMaxPlayers={setMaxPlayers}
totalRounds={totalRounds} setTotalRounds={setTotalRounds}
gameMode={gameMode} setGameMode={setGameMode}
onSetSearchAllowed={(v) => multi.setSearchAllowed(v)}
/>
);