feat(multi): add configurable time limit per round with countdown display

This commit is contained in:
jessy-david-dev
2026-04-11 22:41:40 +02:00
parent 54c4400393
commit b1fcbbdb04
11 changed files with 158 additions and 10 deletions
+38
View File
@@ -14,6 +14,7 @@ function dbToRoom(row: {
maxPlayers: number;
gameMode: string;
searchAllowed: boolean;
timeLimit: number;
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -30,6 +31,7 @@ function dbToRoom(row: {
maxPlayers: row.maxPlayers,
gameMode: (row.gameMode ?? "race") as Room["gameMode"],
searchAllowed: row.searchAllowed ?? false,
timeLimit: row.timeLimit ?? 0,
startArticle: row.startArticle,
targetArticle: row.targetArticle,
roundWinner: row.roundWinner,
@@ -332,6 +334,41 @@ export async function PATCH(
return Response.json({ room });
}
// Hôte change la limite de temps
case "setTimeLimit": {
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.timeLimit = typeof value === "number" ? Math.max(0, Math.floor(value)) : 0;
await saveRoom(room);
return Response.json({ room });
}
// Fin du temps imparti — déclenché par le client qui détecte l'expiration
case "timeUp": {
if (room.phase !== "playing") {
return Response.json({ room });
}
if (!room.timeLimit || !room.roundStart) {
return Response.json({ room });
}
const elapsed = Date.now() - room.roundStart;
if (elapsed < room.timeLimit * 1000 - 1000) {
// Pas encore expiré (tolérance 1s pour le lag réseau)
return Response.json({ room });
}
// Marquer les non-finis comme abandonnés
for (const p of room.players) {
if (!p.hasWon && !p.hasSurrendered) {
p.hasSurrendered = true;
}
}
assignAllFinishPoints(room); // attribue les points aux gagnants selon l'ordre
await saveRoom(room);
return Response.json({ room });
}
// Manche suivante / rejouer
case "nextRound": {
const host = room.players.find((p) => p.id === playerId);
@@ -394,6 +431,7 @@ async function saveRoom(room: Room) {
maxPlayers: room.maxPlayers,
gameMode: room.gameMode,
searchAllowed: room.searchAllowed,
timeLimit: room.timeLimit,
startArticle: room.startArticle,
targetArticle: room.targetArticle,
roundWinner: room.roundWinner,
+7 -1
View File
@@ -27,6 +27,7 @@ export type Room = {
maxPlayers: number;
gameMode: "race" | "all_finish"; // race = 1er gagne, all_finish = tout le monde joue
searchAllowed: boolean;
timeLimit: number; // secondes par manche, 0 = illimité
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -59,6 +60,7 @@ function dbToRoom(row: {
maxPlayers: number;
gameMode: string;
searchAllowed: boolean;
timeLimit: number;
startArticle: string;
targetArticle: string;
roundWinner: string | null;
@@ -75,6 +77,7 @@ function dbToRoom(row: {
maxPlayers: row.maxPlayers,
gameMode: (row.gameMode ?? "race") as Room["gameMode"],
searchAllowed: row.searchAllowed ?? false,
timeLimit: row.timeLimit ?? 0,
startArticle: row.startArticle,
targetArticle: row.targetArticle,
roundWinner: row.roundWinner,
@@ -95,11 +98,12 @@ async function pruneOldRooms() {
// Response: { room: Room, playerId: string }
export async function POST(request: NextRequest) {
const body = await request.json();
const { playerName, maxPlayers, totalRounds, gameMode } = body as {
const { playerName, maxPlayers, totalRounds, gameMode, timeLimit } = body as {
playerName: string;
maxPlayers?: number;
totalRounds?: number;
gameMode?: string;
timeLimit?: number;
};
if (
@@ -135,6 +139,7 @@ export async function POST(request: NextRequest) {
const now = BigInt(Date.now());
const clampedMode: Room["gameMode"] = gameMode === "all_finish" ? "all_finish" : "race";
const clampedTime = typeof timeLimit === "number" ? Math.max(0, Math.floor(timeLimit)) : 0;
const players: Player[] = [
{
@@ -160,6 +165,7 @@ export async function POST(request: NextRequest) {
maxPlayers: clampedMax,
gameMode: clampedMode,
searchAllowed: false,
timeLimit: clampedTime,
startArticle: "",
targetArticle: "",
roundWinner: null,