From 89df5894922ef91977fb6e28ee3555a7e6d91c75 Mon Sep 17 00:00:00 2001 From: geen Date: Tue, 15 Oct 2024 23:43:29 +0530 Subject: [PATCH 1/3] added saveRound.ts and relates schemas to work with --- .gitignore | 2 ++ convex/_generated/api.d.ts | 2 ++ convex/saveRound.ts | 61 ++++++++++++++++++++++++++++++++++++++ convex/schema.ts | 18 +++++++++++ 4 files changed, 83 insertions(+) create mode 100644 convex/saveRound.ts diff --git a/.gitignore b/.gitignore index 3e7495f..9dabb99 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ next-env.d.ts # Ignored for the template, you probably want to remove it: package-lock.json + +.env.local diff --git a/convex/_generated/api.d.ts b/convex/_generated/api.d.ts index 3d230dd..83ecb5b 100644 --- a/convex/_generated/api.d.ts +++ b/convex/_generated/api.d.ts @@ -18,6 +18,7 @@ import type { import type * as auth from "../auth.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 users from "../users.js"; /** @@ -32,6 +33,7 @@ declare const fullApi: ApiFromModules<{ auth: typeof auth; http: typeof http; messages: typeof messages; + saveRound: typeof saveRound; users: typeof users; }>; export declare const api: FilterApi< diff --git a/convex/saveRound.ts b/convex/saveRound.ts new file mode 100644 index 0000000..77d7161 --- /dev/null +++ b/convex/saveRound.ts @@ -0,0 +1,61 @@ +// 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, + }); + } + } + } + + return { + success: true, + message: "Round result saved successfully", + resultId, + updatedScore, + }; + }, +}); diff --git a/convex/schema.ts b/convex/schema.ts index 6a6772e..c4d84b6 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -10,4 +10,22 @@ export default defineSchema({ userId: v.id("users"), body: v.string(), }), + + games: defineTable({ + modelId: v.string(), + // Other fields can be added as needed + }), + + results: defineTable({ + gameId: v.id("games"), + roundId: v.string(), + level: v.number(), + isWin: v.boolean(), + reasoning: v.string(), + }), + + scores: defineTable({ + modelId: v.string(), + score: v.number(), + }), }); From c4d6d0dabd7a399e8f7d73ae9fa90372a6cc1e07 Mon Sep 17 00:00:00 2001 From: geen Date: Tue, 15 Oct 2024 23:51:19 +0530 Subject: [PATCH 2/3] logic of score shanged --- convex/saveRound.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convex/saveRound.ts b/convex/saveRound.ts index 77d7161..842630e 100644 --- a/convex/saveRound.ts +++ b/convex/saveRound.ts @@ -45,7 +45,7 @@ export const saveRoundMutation = mutation({ // Create new score entry updatedScore = await ctx.db.insert("scores", { modelId: game.modelId, - score: 1, + score: 0, }); } } From 29e4ba70c6b4e3847fc6fca24252bf5d3ae9b128 Mon Sep 17 00:00:00 2001 From: geen Date: Tue, 15 Oct 2024 23:53:54 +0530 Subject: [PATCH 3/3] logic of score reversed --- convex/saveRound.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convex/saveRound.ts b/convex/saveRound.ts index 842630e..77d7161 100644 --- a/convex/saveRound.ts +++ b/convex/saveRound.ts @@ -45,7 +45,7 @@ export const saveRoundMutation = mutation({ // Create new score entry updatedScore = await ctx.db.insert("scores", { modelId: game.modelId, - score: 0, + score: 1, }); } }