From 43317bde8582939847d13f25b73a171fb56a89db Mon Sep 17 00:00:00 2001 From: KeshavAnandCode Date: Wed, 28 Jan 2026 19:22:51 -0600 Subject: [PATCH] bonus and timing logic a little bit better --- src/app/api/game/[code]/route.ts | 10 +- src/app/host/page.tsx | 416 ++++++++++++++++++------------- src/app/play/page.tsx | 14 +- src/components/Timer.tsx | 32 ++- src/lib/rooms.ts | 41 ++- 5 files changed, 321 insertions(+), 192 deletions(-) diff --git a/src/app/api/game/[code]/route.ts b/src/app/api/game/[code]/route.ts index a7405fa..871d7a5 100644 --- a/src/app/api/game/[code]/route.ts +++ b/src/app/api/game/[code]/route.ts @@ -1,9 +1,10 @@ import { NextResponse } from 'next/server'; -import { getRoom, buzz, startGame, loadQuestion, startTossupTimer, startBonusTimer, markTossupCorrect, markTossupWrong, markBonusCorrect, markBonusWrong, endGame } from '@/lib/rooms'; import { getRandomQuestion } from '@/lib/scibowl-api'; import { z, ZodError } from 'zod'; +import { getRoom, buzz, startGame, loadQuestion, startTossupTimer, startBonusTimer, markTossupCorrect, markTossupWrong, markBonusCorrect, markBonusWrong, moveOn, endGame } from '@/lib/rooms'; + const gameActionSchema = z.object({ - action: z.enum(['start', 'load_question', 'start_timer', 'buzz', 'tossup_correct', 'tossup_wrong', 'bonus_start_timer', 'bonus_correct', 'bonus_wrong', 'end']), + action: z.enum(['start', 'load_question', 'start_timer', 'buzz', 'tossup_correct', 'tossup_wrong', 'bonus_start_timer', 'bonus_correct', 'bonus_wrong', 'move_on', 'end']), moderatorId: z.string().optional(), playerId: z.string().optional(), }); @@ -66,6 +67,11 @@ export async function POST( success = markBonusWrong(code, moderatorId); break; + case 'move_on': + if (!moderatorId) return NextResponse.json({ error: 'Missing moderatorId' }, { status: 400 }); + success = moveOn(code, moderatorId); + break; + case 'end': if (!moderatorId) return NextResponse.json({ error: 'Missing moderatorId' }, { status: 400 }); success = endGame(code, moderatorId); diff --git a/src/app/host/page.tsx b/src/app/host/page.tsx index 6c71f36..b6cc278 100644 --- a/src/app/host/page.tsx +++ b/src/app/host/page.tsx @@ -80,16 +80,61 @@ export default function HostPage() { window.location.href = "/"; } + // Keyboard shortcuts + useEffect(() => { + if (!roomCode) return; + + const handleKeyPress = (e: KeyboardEvent) => { + if (e.code === "Space") { + e.preventDefault(); + const phase = gameState?.phase; + + if (phase === "lobby" && players.length > 0) { + gameAction("start"); + } else if (phase === "intermission") { + gameAction("load_question"); + } else if (phase === "tossup_reading") { + gameAction("start_timer"); + } else if (phase === "bonus" && !gameState?.bonusStartTime) { + gameAction("bonus_start_timer"); + } + } + + // C for correct + if (e.code === "KeyC") { + const phase = gameState?.phase; + if (phase === "tossup_buzzing" && gameState?.buzzedPlayer) { + gameAction("tossup_correct"); + } else if (phase === "bonus" && gameState?.bonusStartTime) { + gameAction("bonus_correct"); + } + } + + // W for wrong + if (e.code === "KeyW") { + const phase = gameState?.phase; + if (phase === "tossup_buzzing" && gameState?.buzzedPlayer) { + gameAction("tossup_wrong"); + } else if (phase === "bonus" && gameState?.bonusStartTime) { + gameAction("bonus_wrong"); + } + } + }; + + window.addEventListener("keydown", handleKeyPress); + return () => window.removeEventListener("keydown", handleKeyPress); + }, [roomCode, gameState, players]); + if (roomClosed) { return ( -
-
-

+
+
+

Room Closed

Go Home @@ -100,45 +145,45 @@ export default function HostPage() { if (!roomCode) { return ( -
-
-

+
+
+

Create Room

-
-