diff --git a/app/games/[gameId]/result.tsx b/app/games/[gameId]/result.tsx index 2a4ac13..ff39de0 100644 --- a/app/games/[gameId]/result.tsx +++ b/app/games/[gameId]/result.tsx @@ -1,29 +1,38 @@ "use client"; -import { Visualizer } from "./visualizer"; -import { api } from "@/convex/_generated/api"; import { Doc } from "@/convex/_generated/dataModel"; +import { api } from "@/convex/_generated/api"; import { useQuery } from "convex/react"; +import { Visualizer } from "./visualizer"; export const Result = ({ result }: { result: Doc<"results"> }) => { const map = useQuery(api.maps.getMapByLevel, { level: result.level, }); - if (!map) return null; + if (!map) { + return
Map was not found
; + } if (result.status === "inProgress") { return
Game in progress...
; } + if (result.status === "failed") { + return
{result.error}
; + } + return ( -
-
Level {map.level}
+
+
Level {map.level}
-
- {result.isWin ? "Won" : "Lost"} +
+
+ {result.isWin ? "Won" : "Lost"} +
+ {result.reasoning !== "" &&

{result.reasoning}

}
); diff --git a/convex/maps.ts b/convex/maps.ts index b6d2617..d9d2b4c 100644 --- a/convex/maps.ts +++ b/convex/maps.ts @@ -139,17 +139,9 @@ export const playMapAction = internalAction({ existingMap[0][1] = "B"; existingMap[0][2] = "B"; - const game = new ZombieSurvival(existingMap); - - while (!game.finished()) { - game.step(); - } - - const isWin = !game.getPlayer().dead(); - await ctx.runMutation(internal.results.updateResult, { resultId, - isWin, + isWin: ZombieSurvival.isWin(existingMap), reasoning: "This is a mock response", map: existingMap, }); @@ -159,23 +151,25 @@ export const playMapAction = internalAction({ try { const { solution, reasoning } = await runModel(args.modelId, map.grid); - const game = new ZombieSurvival(solution); - - while (!game.finished()) { - game.step(); - } - - const isWin = !game.getPlayer().dead(); await ctx.runMutation(internal.results.updateResult, { resultId, - isWin, + isWin: ZombieSurvival.isWin(solution), reasoning, map: solution, }); } catch (error) { - // todo: handle error - console.log(error); + const errorMessage = + error instanceof Error + ? error.message + : typeof error === "string" + ? error + : "Unexpected error happened"; + + await ctx.runMutation(internal.results.failResult, { + resultId, + error: errorMessage, + }); } }, }); diff --git a/convex/results.ts b/convex/results.ts index 3acb364..630dedc 100644 --- a/convex/results.ts +++ b/convex/results.ts @@ -31,6 +31,25 @@ export const createInitialResult = internalMutation({ }, }); +export const failResult = internalMutation({ + args: { + resultId: v.id("results"), + error: v.string(), + }, + handler: async (ctx, args) => { + const result = await ctx.db.get(args.resultId); + + if (!result) { + throw new Error("Result not found"); + } + + await ctx.db.patch(args.resultId, { + error: args.error, + status: "failed", + }); + }, +}); + export const updateResult = internalMutation({ args: { resultId: v.id("results"), diff --git a/convex/schema.ts b/convex/schema.ts index ae66f4a..4083f79 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -26,7 +26,12 @@ export default defineSchema({ level: v.number(), isWin: v.boolean(), reasoning: v.string(), + error: v.optional(v.string()), map: v.array(v.array(v.string())), - status: v.union(v.literal("inProgress"), v.literal("completed")), + status: v.union( + v.literal("inProgress"), + v.literal("completed"), + v.literal("failed"), + ), }).index("by_gameId_level", ["gameId", "level"]), });