feat(multi): add configurable time limit per round with countdown display
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -17,6 +17,7 @@ type GameScreenProps = {
|
||||
clicks: number;
|
||||
elapsed: string;
|
||||
countdown: number | null;
|
||||
timeLeft: number | null;
|
||||
onNavigate: (title: string) => void;
|
||||
onGoBack: () => void;
|
||||
canGoBack: boolean;
|
||||
@@ -37,6 +38,7 @@ export function GameScreen({
|
||||
clicks,
|
||||
elapsed,
|
||||
countdown,
|
||||
timeLeft,
|
||||
onNavigate,
|
||||
onGoBack,
|
||||
canGoBack,
|
||||
@@ -261,10 +263,15 @@ export function GameScreen({
|
||||
<Breadcrumbs history={history} endRef={breadcrumbEndRef} />
|
||||
</div>
|
||||
<div className="flex items-center gap-2 sm:gap-3 shrink-0">
|
||||
<div className="flex items-center gap-2 sm:gap-3 text-[10px] sm:text-xs font-bold text-[#888] tabular-nums">
|
||||
<span>{elapsed}</span>
|
||||
<span>{clicks} clics</span>
|
||||
<span className="hidden sm:inline">{myPlayer?.score ?? 0} pts</span>
|
||||
<div className="flex items-center gap-2 sm:gap-3 text-[10px] sm:text-xs font-bold tabular-nums">
|
||||
{timeLeft !== null && (
|
||||
<span className={`font-black ${timeLeft <= 10 ? "text-red-400 animate-pulse" : timeLeft <= 30 ? "text-orange-400" : "text-[#888]"}`}>
|
||||
{timeLeft}s
|
||||
</span>
|
||||
)}
|
||||
<span className="text-[#888]">{elapsed}</span>
|
||||
<span className="text-[#888]">{clicks} clics</span>
|
||||
<span className="hidden sm:inline text-[#888]">{myPlayer?.score ?? 0} pts</span>
|
||||
</div>
|
||||
{!myFinished && canGoBack && (
|
||||
<button
|
||||
|
||||
@@ -19,6 +19,7 @@ type LobbyScreenProps = {
|
||||
gameMode: Room["gameMode"];
|
||||
setGameMode: (v: Room["gameMode"]) => void;
|
||||
onSetSearchAllowed: (v: boolean) => void;
|
||||
onSetTimeLimit: (v: number) => void;
|
||||
};
|
||||
|
||||
export function LobbyScreen({
|
||||
@@ -37,6 +38,7 @@ export function LobbyScreen({
|
||||
gameMode,
|
||||
setGameMode,
|
||||
onSetSearchAllowed,
|
||||
onSetTimeLimit,
|
||||
}: LobbyScreenProps) {
|
||||
const isHost = room.players.find((p) => p.id === playerId)?.isHost ?? false;
|
||||
const [copied, setCopied] = useState(false);
|
||||
@@ -188,6 +190,21 @@ export function LobbyScreen({
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1.5">
|
||||
<span className="text-xs text-[#888]">Temps par manche</span>
|
||||
<select
|
||||
value={room.timeLimit}
|
||||
onChange={(e) => onSetTimeLimit(Number(e.target.value))}
|
||||
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={0}>Illimité</option>
|
||||
{[60, 120, 180, 300, 600].map((s) => (
|
||||
<option key={s} value={s}>
|
||||
{s < 60 ? `${s}s` : `${s / 60} min`}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</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]"}`}
|
||||
|
||||
@@ -159,6 +159,7 @@ export function ScreenRouter({
|
||||
gameMode={gameMode}
|
||||
setGameMode={setGameMode}
|
||||
onSetSearchAllowed={(v) => multi.setSearchAllowed(v)}
|
||||
onSetTimeLimit={(v) => multi.setTimeLimit(v)}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -175,6 +176,7 @@ export function ScreenRouter({
|
||||
clicks={multi.clicks}
|
||||
elapsed={fmt(multi.elapsed)}
|
||||
countdown={multi.countdown}
|
||||
timeLeft={multi.timeLeft}
|
||||
onNavigate={multi.navigate}
|
||||
onGoBack={multi.goBack}
|
||||
canGoBack={multi.canGoBack}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -864,6 +864,7 @@ export const RoomScalarFieldEnum = {
|
||||
maxPlayers: 'maxPlayers',
|
||||
gameMode: 'gameMode',
|
||||
searchAllowed: 'searchAllowed',
|
||||
timeLimit: 'timeLimit',
|
||||
startArticle: 'startArticle',
|
||||
targetArticle: 'targetArticle',
|
||||
roundWinner: 'roundWinner',
|
||||
|
||||
@@ -121,6 +121,7 @@ export const RoomScalarFieldEnum = {
|
||||
maxPlayers: 'maxPlayers',
|
||||
gameMode: 'gameMode',
|
||||
searchAllowed: 'searchAllowed',
|
||||
timeLimit: 'timeLimit',
|
||||
startArticle: 'startArticle',
|
||||
targetArticle: 'targetArticle',
|
||||
roundWinner: 'roundWinner',
|
||||
|
||||
@@ -30,6 +30,7 @@ export type RoomAvgAggregateOutputType = {
|
||||
round: number | null
|
||||
totalRounds: number | null
|
||||
maxPlayers: number | null
|
||||
timeLimit: number | null
|
||||
countdownStart: number | null
|
||||
roundStart: number | null
|
||||
createdAt: number | null
|
||||
@@ -39,6 +40,7 @@ export type RoomSumAggregateOutputType = {
|
||||
round: number | null
|
||||
totalRounds: number | null
|
||||
maxPlayers: number | null
|
||||
timeLimit: number | null
|
||||
countdownStart: bigint | null
|
||||
roundStart: bigint | null
|
||||
createdAt: bigint | null
|
||||
@@ -52,6 +54,7 @@ export type RoomMinAggregateOutputType = {
|
||||
maxPlayers: number | null
|
||||
gameMode: string | null
|
||||
searchAllowed: boolean | null
|
||||
timeLimit: number | null
|
||||
startArticle: string | null
|
||||
targetArticle: string | null
|
||||
roundWinner: string | null
|
||||
@@ -69,6 +72,7 @@ export type RoomMaxAggregateOutputType = {
|
||||
maxPlayers: number | null
|
||||
gameMode: string | null
|
||||
searchAllowed: boolean | null
|
||||
timeLimit: number | null
|
||||
startArticle: string | null
|
||||
targetArticle: string | null
|
||||
roundWinner: string | null
|
||||
@@ -87,6 +91,7 @@ export type RoomCountAggregateOutputType = {
|
||||
maxPlayers: number
|
||||
gameMode: number
|
||||
searchAllowed: number
|
||||
timeLimit: number
|
||||
startArticle: number
|
||||
targetArticle: number
|
||||
roundWinner: number
|
||||
@@ -102,6 +107,7 @@ export type RoomAvgAggregateInputType = {
|
||||
round?: true
|
||||
totalRounds?: true
|
||||
maxPlayers?: true
|
||||
timeLimit?: true
|
||||
countdownStart?: true
|
||||
roundStart?: true
|
||||
createdAt?: true
|
||||
@@ -111,6 +117,7 @@ export type RoomSumAggregateInputType = {
|
||||
round?: true
|
||||
totalRounds?: true
|
||||
maxPlayers?: true
|
||||
timeLimit?: true
|
||||
countdownStart?: true
|
||||
roundStart?: true
|
||||
createdAt?: true
|
||||
@@ -124,6 +131,7 @@ export type RoomMinAggregateInputType = {
|
||||
maxPlayers?: true
|
||||
gameMode?: true
|
||||
searchAllowed?: true
|
||||
timeLimit?: true
|
||||
startArticle?: true
|
||||
targetArticle?: true
|
||||
roundWinner?: true
|
||||
@@ -141,6 +149,7 @@ export type RoomMaxAggregateInputType = {
|
||||
maxPlayers?: true
|
||||
gameMode?: true
|
||||
searchAllowed?: true
|
||||
timeLimit?: true
|
||||
startArticle?: true
|
||||
targetArticle?: true
|
||||
roundWinner?: true
|
||||
@@ -159,6 +168,7 @@ export type RoomCountAggregateInputType = {
|
||||
maxPlayers?: true
|
||||
gameMode?: true
|
||||
searchAllowed?: true
|
||||
timeLimit?: true
|
||||
startArticle?: true
|
||||
targetArticle?: true
|
||||
roundWinner?: true
|
||||
@@ -264,6 +274,7 @@ export type RoomGroupByOutputType = {
|
||||
maxPlayers: number
|
||||
gameMode: string
|
||||
searchAllowed: boolean
|
||||
timeLimit: number
|
||||
startArticle: string
|
||||
targetArticle: string
|
||||
roundWinner: string | null
|
||||
@@ -305,6 +316,7 @@ export type RoomWhereInput = {
|
||||
maxPlayers?: Prisma.IntFilter<"Room"> | number
|
||||
gameMode?: Prisma.StringFilter<"Room"> | string
|
||||
searchAllowed?: Prisma.BoolFilter<"Room"> | boolean
|
||||
timeLimit?: Prisma.IntFilter<"Room"> | number
|
||||
startArticle?: Prisma.StringFilter<"Room"> | string
|
||||
targetArticle?: Prisma.StringFilter<"Room"> | string
|
||||
roundWinner?: Prisma.StringNullableFilter<"Room"> | string | null
|
||||
@@ -323,6 +335,7 @@ export type RoomOrderByWithRelationInput = {
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
gameMode?: Prisma.SortOrder
|
||||
searchAllowed?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
startArticle?: Prisma.SortOrder
|
||||
targetArticle?: Prisma.SortOrder
|
||||
roundWinner?: Prisma.SortOrderInput | Prisma.SortOrder
|
||||
@@ -344,6 +357,7 @@ export type RoomWhereUniqueInput = Prisma.AtLeast<{
|
||||
maxPlayers?: Prisma.IntFilter<"Room"> | number
|
||||
gameMode?: Prisma.StringFilter<"Room"> | string
|
||||
searchAllowed?: Prisma.BoolFilter<"Room"> | boolean
|
||||
timeLimit?: Prisma.IntFilter<"Room"> | number
|
||||
startArticle?: Prisma.StringFilter<"Room"> | string
|
||||
targetArticle?: Prisma.StringFilter<"Room"> | string
|
||||
roundWinner?: Prisma.StringNullableFilter<"Room"> | string | null
|
||||
@@ -362,6 +376,7 @@ export type RoomOrderByWithAggregationInput = {
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
gameMode?: Prisma.SortOrder
|
||||
searchAllowed?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
startArticle?: Prisma.SortOrder
|
||||
targetArticle?: Prisma.SortOrder
|
||||
roundWinner?: Prisma.SortOrderInput | Prisma.SortOrder
|
||||
@@ -388,6 +403,7 @@ export type RoomScalarWhereWithAggregatesInput = {
|
||||
maxPlayers?: Prisma.IntWithAggregatesFilter<"Room"> | number
|
||||
gameMode?: Prisma.StringWithAggregatesFilter<"Room"> | string
|
||||
searchAllowed?: Prisma.BoolWithAggregatesFilter<"Room"> | boolean
|
||||
timeLimit?: Prisma.IntWithAggregatesFilter<"Room"> | number
|
||||
startArticle?: Prisma.StringWithAggregatesFilter<"Room"> | string
|
||||
targetArticle?: Prisma.StringWithAggregatesFilter<"Room"> | string
|
||||
roundWinner?: Prisma.StringNullableWithAggregatesFilter<"Room"> | string | null
|
||||
@@ -406,6 +422,7 @@ export type RoomCreateInput = {
|
||||
maxPlayers?: number
|
||||
gameMode?: string
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: number
|
||||
startArticle?: string
|
||||
targetArticle?: string
|
||||
roundWinner?: string | null
|
||||
@@ -424,6 +441,7 @@ export type RoomUncheckedCreateInput = {
|
||||
maxPlayers?: number
|
||||
gameMode?: string
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: number
|
||||
startArticle?: string
|
||||
targetArticle?: string
|
||||
roundWinner?: string | null
|
||||
@@ -442,6 +460,7 @@ export type RoomUpdateInput = {
|
||||
maxPlayers?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
gameMode?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
searchAllowed?: Prisma.BoolFieldUpdateOperationsInput | boolean
|
||||
timeLimit?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
startArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
targetArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
roundWinner?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
|
||||
@@ -460,6 +479,7 @@ export type RoomUncheckedUpdateInput = {
|
||||
maxPlayers?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
gameMode?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
searchAllowed?: Prisma.BoolFieldUpdateOperationsInput | boolean
|
||||
timeLimit?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
startArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
targetArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
roundWinner?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
|
||||
@@ -478,6 +498,7 @@ export type RoomCreateManyInput = {
|
||||
maxPlayers?: number
|
||||
gameMode?: string
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: number
|
||||
startArticle?: string
|
||||
targetArticle?: string
|
||||
roundWinner?: string | null
|
||||
@@ -496,6 +517,7 @@ export type RoomUpdateManyMutationInput = {
|
||||
maxPlayers?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
gameMode?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
searchAllowed?: Prisma.BoolFieldUpdateOperationsInput | boolean
|
||||
timeLimit?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
startArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
targetArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
roundWinner?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
|
||||
@@ -514,6 +536,7 @@ export type RoomUncheckedUpdateManyInput = {
|
||||
maxPlayers?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
gameMode?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
searchAllowed?: Prisma.BoolFieldUpdateOperationsInput | boolean
|
||||
timeLimit?: Prisma.IntFieldUpdateOperationsInput | number
|
||||
startArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
targetArticle?: Prisma.StringFieldUpdateOperationsInput | string
|
||||
roundWinner?: Prisma.NullableStringFieldUpdateOperationsInput | string | null
|
||||
@@ -532,6 +555,7 @@ export type RoomCountOrderByAggregateInput = {
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
gameMode?: Prisma.SortOrder
|
||||
searchAllowed?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
startArticle?: Prisma.SortOrder
|
||||
targetArticle?: Prisma.SortOrder
|
||||
roundWinner?: Prisma.SortOrder
|
||||
@@ -545,6 +569,7 @@ export type RoomAvgOrderByAggregateInput = {
|
||||
round?: Prisma.SortOrder
|
||||
totalRounds?: Prisma.SortOrder
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
countdownStart?: Prisma.SortOrder
|
||||
roundStart?: Prisma.SortOrder
|
||||
createdAt?: Prisma.SortOrder
|
||||
@@ -558,6 +583,7 @@ export type RoomMaxOrderByAggregateInput = {
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
gameMode?: Prisma.SortOrder
|
||||
searchAllowed?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
startArticle?: Prisma.SortOrder
|
||||
targetArticle?: Prisma.SortOrder
|
||||
roundWinner?: Prisma.SortOrder
|
||||
@@ -575,6 +601,7 @@ export type RoomMinOrderByAggregateInput = {
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
gameMode?: Prisma.SortOrder
|
||||
searchAllowed?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
startArticle?: Prisma.SortOrder
|
||||
targetArticle?: Prisma.SortOrder
|
||||
roundWinner?: Prisma.SortOrder
|
||||
@@ -588,6 +615,7 @@ export type RoomSumOrderByAggregateInput = {
|
||||
round?: Prisma.SortOrder
|
||||
totalRounds?: Prisma.SortOrder
|
||||
maxPlayers?: Prisma.SortOrder
|
||||
timeLimit?: Prisma.SortOrder
|
||||
countdownStart?: Prisma.SortOrder
|
||||
roundStart?: Prisma.SortOrder
|
||||
createdAt?: Prisma.SortOrder
|
||||
@@ -624,6 +652,7 @@ export type RoomSelect<ExtArgs extends runtime.Types.Extensions.InternalArgs = r
|
||||
maxPlayers?: boolean
|
||||
gameMode?: boolean
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: boolean
|
||||
startArticle?: boolean
|
||||
targetArticle?: boolean
|
||||
roundWinner?: boolean
|
||||
@@ -642,6 +671,7 @@ export type RoomSelectCreateManyAndReturn<ExtArgs extends runtime.Types.Extensio
|
||||
maxPlayers?: boolean
|
||||
gameMode?: boolean
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: boolean
|
||||
startArticle?: boolean
|
||||
targetArticle?: boolean
|
||||
roundWinner?: boolean
|
||||
@@ -660,6 +690,7 @@ export type RoomSelectUpdateManyAndReturn<ExtArgs extends runtime.Types.Extensio
|
||||
maxPlayers?: boolean
|
||||
gameMode?: boolean
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: boolean
|
||||
startArticle?: boolean
|
||||
targetArticle?: boolean
|
||||
roundWinner?: boolean
|
||||
@@ -678,6 +709,7 @@ export type RoomSelectScalar = {
|
||||
maxPlayers?: boolean
|
||||
gameMode?: boolean
|
||||
searchAllowed?: boolean
|
||||
timeLimit?: boolean
|
||||
startArticle?: boolean
|
||||
targetArticle?: boolean
|
||||
roundWinner?: boolean
|
||||
@@ -687,7 +719,7 @@ export type RoomSelectScalar = {
|
||||
updatedAt?: boolean
|
||||
}
|
||||
|
||||
export type RoomOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"code" | "players" | "phase" | "round" | "totalRounds" | "maxPlayers" | "gameMode" | "searchAllowed" | "startArticle" | "targetArticle" | "roundWinner" | "countdownStart" | "roundStart" | "createdAt" | "updatedAt", ExtArgs["result"]["room"]>
|
||||
export type RoomOmit<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = runtime.Types.Extensions.GetOmit<"code" | "players" | "phase" | "round" | "totalRounds" | "maxPlayers" | "gameMode" | "searchAllowed" | "timeLimit" | "startArticle" | "targetArticle" | "roundWinner" | "countdownStart" | "roundStart" | "createdAt" | "updatedAt", ExtArgs["result"]["room"]>
|
||||
|
||||
export type $RoomPayload<ExtArgs extends runtime.Types.Extensions.InternalArgs = runtime.Types.Extensions.DefaultArgs> = {
|
||||
name: "Room"
|
||||
@@ -701,6 +733,7 @@ export type $RoomPayload<ExtArgs extends runtime.Types.Extensions.InternalArgs =
|
||||
maxPlayers: number
|
||||
gameMode: string
|
||||
searchAllowed: boolean
|
||||
timeLimit: number
|
||||
startArticle: string
|
||||
targetArticle: string
|
||||
roundWinner: string | null
|
||||
@@ -1139,6 +1172,7 @@ export interface RoomFieldRefs {
|
||||
readonly maxPlayers: Prisma.FieldRef<"Room", 'Int'>
|
||||
readonly gameMode: Prisma.FieldRef<"Room", 'String'>
|
||||
readonly searchAllowed: Prisma.FieldRef<"Room", 'Boolean'>
|
||||
readonly timeLimit: Prisma.FieldRef<"Room", 'Int'>
|
||||
readonly startArticle: Prisma.FieldRef<"Room", 'String'>
|
||||
readonly targetArticle: Prisma.FieldRef<"Room", 'String'>
|
||||
readonly roundWinner: Prisma.FieldRef<"Room", 'String'>
|
||||
|
||||
@@ -30,6 +30,8 @@ export function useMultiGame() {
|
||||
const timerStartedRef = useRef(false);
|
||||
|
||||
const [countdown, setCountdown] = useState<number | null>(null);
|
||||
const [timeLeft, setTimeLeft] = useState<number | null>(null);
|
||||
const timeLimitTimerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const countdownRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
||||
const prevPhaseRef = useRef<string | null>(null);
|
||||
@@ -145,9 +147,23 @@ export function useMultiGame() {
|
||||
historyRef.current = [room.startArticle];
|
||||
setHistory([room.startArticle]);
|
||||
loadArticle(room.startArticle);
|
||||
// Démarrer le timer de temps limité si configuré
|
||||
if (timeLimitTimerRef.current) clearInterval(timeLimitTimerRef.current);
|
||||
if (room.timeLimit > 0) {
|
||||
const tick = () => {
|
||||
const left = Math.ceil(((room.roundStart ?? Date.now()) + room.timeLimit * 1000 - Date.now()) / 1000);
|
||||
setTimeLeft(left <= 0 ? 0 : left);
|
||||
};
|
||||
tick();
|
||||
timeLimitTimerRef.current = setInterval(tick, 200);
|
||||
} else {
|
||||
setTimeLeft(null);
|
||||
}
|
||||
}
|
||||
if (room.phase === "results" && prevPhase !== "results") {
|
||||
timer.stop();
|
||||
if (timeLimitTimerRef.current) { clearInterval(timeLimitTimerRef.current); timeLimitTimerRef.current = null; }
|
||||
setTimeLeft(null);
|
||||
}
|
||||
if (room.round !== prevRound && room.phase === "playing") {
|
||||
loadArticle(room.startArticle);
|
||||
@@ -155,6 +171,19 @@ export function useMultiGame() {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [room]);
|
||||
|
||||
// Temps limité : envoie timeUp quand le compteur atteint 0
|
||||
useEffect(() => {
|
||||
if (timeLeft !== 0 || !room || room.phase !== "playing" || !playerId || !room.timeLimit) return;
|
||||
fetch(`/api/rooms/${room.code}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "timeUp", playerId }),
|
||||
}).then((r) => r.json()).then((d) => {
|
||||
if ((d as { room: Room }).room) setRoom((d as { room: Room }).room);
|
||||
}).catch(() => {});
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [timeLeft]);
|
||||
|
||||
// Countdown -> playing transition
|
||||
useEffect(() => {
|
||||
if (!room || room.phase !== "countdown" || !playerId) return;
|
||||
@@ -332,6 +361,16 @@ export function useMultiGame() {
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [room, playerId]);
|
||||
|
||||
async function setTimeLimit(value: number) {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "setTimeLimit", playerId, value }),
|
||||
});
|
||||
if (res.ok) setRoom((await res.json() as { room: Room }).room);
|
||||
}
|
||||
|
||||
async function setSearchAllowed(value: boolean) {
|
||||
if (!room || !playerId) return;
|
||||
const res = await fetch(`/api/rooms/${room.code}`, {
|
||||
@@ -428,6 +467,8 @@ export function useMultiGame() {
|
||||
goBack,
|
||||
canGoBack: historyRef.current.length > 1,
|
||||
surrender,
|
||||
timeLeft,
|
||||
setTimeLimit,
|
||||
setSearchAllowed,
|
||||
restore,
|
||||
retryLoad: () => title && loadArticle(title),
|
||||
|
||||
@@ -51,6 +51,7 @@ model Room {
|
||||
maxPlayers Int @default(16)
|
||||
gameMode String @default("race")
|
||||
searchAllowed Boolean @default(false)
|
||||
timeLimit Int @default(0)
|
||||
startArticle String @default("")
|
||||
targetArticle String @default("")
|
||||
roundWinner String?
|
||||
|
||||
Reference in New Issue
Block a user