diff --git a/convex/_generated/api.d.ts b/convex/_generated/api.d.ts index 2464f9b..caf6a31 100644 --- a/convex/_generated/api.d.ts +++ b/convex/_generated/api.d.ts @@ -19,6 +19,7 @@ import type * as auth from "../auth.js"; import type * as games from "../games.js"; import type * as http from "../http.js"; import type * as messages from "../messages.js"; +import type * as saveRound from "../saveRound.js"; import type * as openai from "../openai.js"; import type * as users from "../users.js"; @@ -35,6 +36,7 @@ declare const fullApi: ApiFromModules<{ games: typeof games; http: typeof http; messages: typeof messages; + saveRound: typeof saveRound; openai: typeof openai; users: typeof users; }>; diff --git a/convex/saveRound.ts b/convex/saveRound.ts new file mode 100644 index 0000000..dc84938 --- /dev/null +++ b/convex/saveRound.ts @@ -0,0 +1,62 @@ +// File: convex/saveRoundMutation.ts + +import { mutation } from "./_generated/server"; +import { v } from "convex/values"; + +export const saveRoundMutation = mutation({ + args: { + gameId: v.id("games"), + roundId: v.string(), + level: v.number(), + isWin: v.boolean(), + reasoning: v.string(), + }, + handler: async (ctx, args) => { + const { gameId, roundId, level, isWin, reasoning } = args; + + // 1. Save the round result + const resultId = await ctx.db.insert("results", { + gameId, + roundId, + level, + isWin, + reasoning, + }); + + // 2. If the game was won, update the score + let updatedScore = null; + if (isWin) { + // Find the game to get the modelId + const game = await ctx.db.get(gameId); + + if (game) { + // Get the current score or initialize it + const currentScore = await ctx.db + .query("scores") + .filter((q) => q.eq(q.field("modelId"), game.modelId)) + .first(); + + if (currentScore) { + // Update existing score + updatedScore = await ctx.db.patch(currentScore._id, { + score: currentScore.score + 1, + }); + } else { + // Create new score entry + updatedScore = await ctx.db.insert("scores", { + modelId: game.modelId, + score: 1, + ...args, + }); + } + } + } + + return { + success: true, + message: "Round result saved successfully", + resultId, + updatedScore, + }; + }, +}); diff --git a/convex/schema.ts b/convex/schema.ts index ef60d0f..a2569c7 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -27,4 +27,12 @@ export default defineSchema({ score: v.number(), level: v.number(), }).index("by_game_and_level", ["gameId", "level"]), + results: defineTable({ + gameId: v.id("games"), + roundId: v.string(), + level: v.number(), + isWin: v.boolean(), + reasoning: v.string(), + }), + });