|
import { NextRequest, NextResponse } from "next/server"; |
|
import prisma from "@/lib/prisma"; |
|
|
|
export async function POST( |
|
request: NextRequest, |
|
context: { params: { inviteCode: string } } |
|
) { |
|
try { |
|
const { inviteCode } = await context.params; |
|
|
|
|
|
const group = await prisma.group.findUnique({ |
|
where: { inviteCode }, |
|
include: { players: true }, |
|
}); |
|
|
|
if (!group) { |
|
return NextResponse.json({ error: "Groupe non trouvé" }, { status: 404 }); |
|
} |
|
|
|
|
|
const allPlayersReady = group.players.every((player) => player.isReady); |
|
if (!allPlayersReady) { |
|
return NextResponse.json( |
|
{ error: "Tous les joueurs ne sont pas prêts" }, |
|
{ status: 400 } |
|
); |
|
} |
|
|
|
|
|
if (group.players.length < 3) { |
|
return NextResponse.json( |
|
{ error: "Il faut au moins 3 joueurs pour commencer" }, |
|
{ status: 400 } |
|
); |
|
} |
|
|
|
|
|
const imposteurIndex = Math.floor(Math.random() * group.players.length); |
|
const imposteur = group.players[imposteurIndex]; |
|
|
|
|
|
const updatedGroup = await prisma.group.update({ |
|
where: { id: group.id }, |
|
data: { |
|
status: "PLAYING", |
|
imposteurId: imposteur.id, |
|
currentRound: 1, |
|
totalRounds: 3, |
|
}, |
|
include: { |
|
players: true, |
|
}, |
|
}); |
|
|
|
return NextResponse.json({ |
|
success: true, |
|
group: updatedGroup, |
|
}); |
|
} catch (error: any) { |
|
console.error("Erreur lors du démarrage de la partie:", error); |
|
return NextResponse.json( |
|
{ |
|
error: "Erreur lors du démarrage de la partie", |
|
details: error.message, |
|
}, |
|
{ status: 500 } |
|
); |
|
} |
|
} |
|
|