fix: sync totalRounds changes to server, filter geographic admin articles, validate user exists before saving game
This commit is contained in:
@@ -20,6 +20,14 @@ export async function POST(req: NextRequest) {
|
||||
won: boolean;
|
||||
};
|
||||
|
||||
const userExists = await prisma.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: { id: true },
|
||||
});
|
||||
if (!userExists) {
|
||||
return NextResponse.json({ error: "Utilisateur introuvable" }, { status: 401 });
|
||||
}
|
||||
|
||||
const game = await prisma.game.create({
|
||||
data: {
|
||||
userId: session.user.id,
|
||||
|
||||
@@ -69,7 +69,10 @@ function calcPlayerPoints(player: Player, room: Room): number {
|
||||
let timePts: number;
|
||||
if (room.timeLimit > 0) {
|
||||
const timeLeft = room.timeLimit * 1000 - elapsed;
|
||||
timePts = Math.max(0, Math.floor((timeLeft / (room.timeLimit * 1000)) * 10));
|
||||
timePts = Math.max(
|
||||
0,
|
||||
Math.floor((timeLeft / (room.timeLimit * 1000)) * 10),
|
||||
);
|
||||
} else {
|
||||
timePts = Math.max(0, 10 - Math.floor(elapsed / 30000));
|
||||
}
|
||||
@@ -355,7 +358,10 @@ export async function PATCH(
|
||||
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 });
|
||||
return Response.json(
|
||||
{ error: "Seul l'hôte peut modifier ce paramètre" },
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
if (value === "race" || value === "all_finish") {
|
||||
room.gameMode = value;
|
||||
@@ -364,18 +370,38 @@ export async function PATCH(
|
||||
return Response.json({ room });
|
||||
}
|
||||
|
||||
// Hôte change le nombre de manches
|
||||
case "setTotalRounds": {
|
||||
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 (typeof value === "number") {
|
||||
room.totalRounds = Math.min(Math.max(Math.floor(value), 1), 10);
|
||||
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);
|
||||
if (!host?.isHost) {
|
||||
return Response.json({ error: "Seul l'hôte peut modifier ce paramètre" }, { status: 403 });
|
||||
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;
|
||||
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
|
||||
// Fin du temps imparti - déclenché par le client qui détecte l'expiration
|
||||
case "timeUp": {
|
||||
if (room.phase !== "playing") {
|
||||
return Response.json({ room });
|
||||
|
||||
Reference in New Issue
Block a user