From 6a122d211cf56938d8d2de0a5a89c2dfe7433783 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Sat, 19 Oct 2024 22:52:25 +0300 Subject: [PATCH 1/4] Move attempts visualizations to separate page --- app/play/[level]/[attempt]/page.tsx | 61 +++++++++++++++++++++++++++++ app/play/[level]/page.tsx | 27 +++++++------ convex/playerresults.ts | 46 +++++++++++++++++++++- 3 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 app/play/[level]/[attempt]/page.tsx diff --git a/app/play/[level]/[attempt]/page.tsx b/app/play/[level]/[attempt]/page.tsx new file mode 100644 index 0000000..1b65d0b --- /dev/null +++ b/app/play/[level]/[attempt]/page.tsx @@ -0,0 +1,61 @@ +"use client"; + +import React from "react"; +import { Authenticated, useQuery } from "convex/react"; +import { ChevronLeftIcon } from "@radix-ui/react-icons"; +import Link from "next/link"; +import { Button } from "@/components/ui/button"; +import { Visualizer } from "@/components/Visualizer"; +import { api } from "@/convex/_generated/api"; + +export default function PlayLevelAttemptPage({ + params, +}: { + params: { level: string; attempt: string }; +}) { + const level = Number.parseInt(params.level, 10); + const attemptNum = Number.parseInt(params.attempt, 10); + + const attempt = useQuery(api.playerresults.getUserAttempt, { + level, + attempt: attemptNum, + }); + + if (attempt === undefined) { + return

Loading...

; + } else if (attempt === null) { + return

Attempt Not Found

; + } + + return ( + +
+
+ +
+

Night #{level}

+

Attempt #{attemptNum}

+ +
+ +
+ {attempt.didWin ? "You Survived!" : "You Died!"} +
+
+
+
+ ); +} diff --git a/app/play/[level]/page.tsx b/app/play/[level]/page.tsx index 2078ce4..5ac0648 100644 --- a/app/play/[level]/page.tsx +++ b/app/play/[level]/page.tsx @@ -281,21 +281,24 @@ export default function PlayLevelPage({ {tries && tries.attempts && tries.attempts.length > 0 && ( <>
Tries
-
- {tries.attempts.map((attempt) => ( -
+ {tries.attempts.map((attempt, idx) => ( +
+ Attempt #{idx + 1} + + ))}
diff --git a/convex/playerresults.ts b/convex/playerresults.ts index 1b700e0..e4ae0cd 100644 --- a/convex/playerresults.ts +++ b/convex/playerresults.ts @@ -1,6 +1,50 @@ +import { getAuthUserId } from "@convex-dev/auth/server"; import { v } from "convex/values"; +import { Id } from "./_generated/dataModel"; +import { api } from "./_generated/api"; import { mutation, query } from "./_generated/server"; -import { getAuthUserId } from "@convex-dev/auth/server"; + +export const getUserAttempt = query({ + args: { + attempt: v.number(), + level: v.number(), + }, + handler: async (ctx, args) => { + const userId = await getAuthUserId(ctx); + + if (!userId) { + return null; + } + + const map = await ctx.runQuery(api.maps.getMapByLevel, { + level: args.level, + }); + + if (map === null) { + return null; + } + + const userResults = await ctx.db + .query("userResults") + .withIndex("by_mapId_userId", (q) => + q.eq("mapId", map._id).eq("userId", userId), + ) + .collect(); + + if (userResults.length !== 1) { + return null; + } + + const userResult = userResults[0]; + + if (userResult.attempts.length < args.attempt) { + return null; + } + + const attemptId: Id<"attempts"> = userResult.attempts[args.attempt - 1]; + return await ctx.db.get(attemptId); + }, +}); export const getUserMapStatus = query({ handler: async (ctx) => { From 04e8289f1ab2d68c1409c78a3667f81b6ddea9d6 Mon Sep 17 00:00:00 2001 From: Aaron Delasy Date: Sun, 20 Oct 2024 14:07:17 +0300 Subject: [PATCH 2/4] Fix TW classes order --- app/play/[level]/[attempt]/page.tsx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/play/[level]/[attempt]/page.tsx b/app/play/[level]/[attempt]/page.tsx index 1b65d0b..8804786 100644 --- a/app/play/[level]/[attempt]/page.tsx +++ b/app/play/[level]/[attempt]/page.tsx @@ -29,18 +29,18 @@ export default function PlayLevelAttemptPage({ return ( -
-
-
-

Night #{level}

-

Attempt #{attemptNum}

+

Night #{level}

+

Attempt #{attemptNum}

-
+
Date: Sun, 20 Oct 2024 14:51:53 +0300 Subject: [PATCH 3/4] Fix code style --- app/play/[level]/[attempt]/page.tsx | 4 ++-- app/play/[level]/page.tsx | 6 +++--- convex/playerresults.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/play/[level]/[attempt]/page.tsx b/app/play/[level]/[attempt]/page.tsx index 8804786..6d5a642 100644 --- a/app/play/[level]/[attempt]/page.tsx +++ b/app/play/[level]/[attempt]/page.tsx @@ -1,11 +1,11 @@ "use client"; import React from "react"; -import { Authenticated, useQuery } from "convex/react"; import { ChevronLeftIcon } from "@radix-ui/react-icons"; +import { Authenticated, useQuery } from "convex/react"; import Link from "next/link"; -import { Button } from "@/components/ui/button"; import { Visualizer } from "@/components/Visualizer"; +import { Button } from "@/components/ui/button"; import { api } from "@/convex/_generated/api"; export default function PlayLevelAttemptPage({ diff --git a/app/play/[level]/page.tsx b/app/play/[level]/page.tsx index 50f2275..1409584 100644 --- a/app/play/[level]/page.tsx +++ b/app/play/[level]/page.tsx @@ -277,8 +277,8 @@ export default function PlayLevelPage({ {tries && tries.attempts && tries.attempts.length > 0 && ( <> -
Tries
-
+
Tries
+
{tries.attempts.map((attempt, idx) => ( -
-

Night #{level}

-

Attempt #{attemptNum}

+
+
+ +
+

Night #{level}

+

Attempt #{attemptNum}

-
- -
- {attempt.didWin ? "You Survived!" : "You Died!"} -
+
+ +
+ {attempt.didWin ? "You Survived!" : "You Died!"}
- +
); }