From 9dcbd401f15e3a412f937927fddb8a56fc84c6d0 Mon Sep 17 00:00:00 2001 From: TK-613 Date: Sun, 21 Dec 2025 09:42:24 +0900 Subject: [PATCH] feature:add endpoint for recode winner --- apps/api/src/index.ts | 19 ++++++++++++++++++- packages/schema/src/index.ts | 1 + packages/schema/src/initGame.ts | 2 +- packages/schema/src/recodeWinner.ts | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 packages/schema/src/recodeWinner.ts diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 85178da..32443d2 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -8,12 +8,14 @@ import { ResolveActionInputSchema, initGameSchema, UserCreatedInputSchema, + RecodeWinnerSchema, } from "@repo/schema"; import { createGeminiClient } from "./lib/gemini"; import { transformVoiceInput } from "./lib/voice-input-transformer"; import { resolveAction } from "./lib/resolve-action"; import { drizzle } from "drizzle-orm/d1"; -import { users } from "./db/schema"; +import { eq } from "drizzle-orm"; +import { users, games } from "./db/schema"; import { createGame } from "./lib/create-game"; type Bindings = { @@ -95,6 +97,21 @@ const api = new Hono<{ Bindings: Bindings }>() const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; return c.json({ error: errorMessage }, 500); } + }) + .patch("/v1/recodeWinner", zValidator("json", RecodeWinnerSchema), async (c) => { + try { + const db = drizzle(c.env.DB); + const winnerData = c.req.valid("json"); + await db + .update(games) + .set({ winner: winnerData.winner }) + .where(eq(games.id, winnerData.game_id)); + + return c.json({ success: true }, 200); + } catch (error) { + const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; + return c.json({ success: false, error: errorMessage }, 500); + } }); // Main app diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index 54b03e1..8d70232 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -7,3 +7,4 @@ export * from "./personality"; export * from "./backend-chess"; export * from "./resolve-action"; export * from "./initGame"; +export * from "./recodeWinner"; diff --git a/packages/schema/src/initGame.ts b/packages/schema/src/initGame.ts index a6d6b0c..65a1781 100644 --- a/packages/schema/src/initGame.ts +++ b/packages/schema/src/initGame.ts @@ -10,7 +10,7 @@ export const initGameSchema = z.object({ export type initGame = z.infer; export const initGameResponseSchema = z.object({ - game_id: z.uuid(), + game_id: z.number(), personalitys: z.array(personalitySchema).length(32), }); diff --git a/packages/schema/src/recodeWinner.ts b/packages/schema/src/recodeWinner.ts new file mode 100644 index 0000000..ed26400 --- /dev/null +++ b/packages/schema/src/recodeWinner.ts @@ -0,0 +1,6 @@ +import { z } from "zod"; + +export const RecodeWinnerSchema = z.object({ + game_id: z.number(), + winner: z.enum(["player", "enemy", "draw"]).nullable(), +});