add game service for DB persistence of predictions, dishes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 11:13:57 +01:00
parent 7a330c173c
commit 1b0348de23

View File

@@ -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))
}
}