-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Geen-19/saveRound
#9 issue worked on to add saveRound.ts and relates schemas to work with
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters