From 1b0348de23e889702b5f658ee330a36243772952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20F=C3=B6rtsch?= Date: Thu, 12 Mar 2026 11:13:57 +0100 Subject: [PATCH] add game service for DB persistence of predictions, dishes Co-Authored-By: Claude Opus 4.6 --- packages/server/src/games/game-service.ts | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 packages/server/src/games/game-service.ts diff --git a/packages/server/src/games/game-service.ts b/packages/server/src/games/game-service.ts new file mode 100644 index 0000000..39fac23 --- /dev/null +++ b/packages/server/src/games/game-service.ts @@ -0,0 +1,64 @@ +import { eq, and } from "drizzle-orm" +import type { Database } from "../db/client" +import { predictions, dishes, dishGuesses } from "../db/schema" + +export class GameService { + constructor(private db: Database) {} + + async persistPrediction(data: { + playerId: string + roomId: string + predictedWinner: string + top3: string[] + nulPointsPick: string + }) { + // Delete existing prediction for this player+room, then insert + await this.db + .delete(predictions) + .where(and(eq(predictions.playerId, data.playerId), eq(predictions.roomId, data.roomId))) + await this.db.insert(predictions).values({ + playerId: data.playerId, + roomId: data.roomId, + predictedWinner: data.predictedWinner, + top3: data.top3, + nulPointsPick: data.nulPointsPick, + }) + } + + async persistDish(data: { + id: string + roomId: string + name: string + correctCountry: string + }) { + await this.db.insert(dishes).values({ + id: data.id, + roomId: data.roomId, + name: data.name, + correctCountry: data.correctCountry, + }) + } + + async persistDishGuess(data: { + playerId: string + dishId: string + guessedCountry: string + }) { + // Delete existing guess for this player+dish, then insert + await this.db + .delete(dishGuesses) + .where(and(eq(dishGuesses.playerId, data.playerId), eq(dishGuesses.dishId, data.dishId))) + await this.db.insert(dishGuesses).values({ + playerId: data.playerId, + dishId: data.dishId, + guessedCountry: data.guessedCountry, + }) + } + + async markDishesRevealed(roomId: string) { + await this.db + .update(dishes) + .set({ revealed: true }) + .where(eq(dishes.roomId, roomId)) + } +}