Skip to content

Commit

Permalink
Move attempts to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 21, 2024
1 parent 755c4f4 commit 96a8eaa
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion app/play/[level]/[attempt]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function PlayLevelAttemptPage({
const level = Number.parseInt(params.level, 10);
const attemptNum = Number.parseInt(params.attempt, 10);

const attempt = useQuery(api.playerresults.getUserAttempt, {
const attempt = useQuery(api.attempts.getUserAttempt, {
level,
attempt: attemptNum,
});
Expand Down
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
FilterApi,
FunctionReference,
} from "convex/server";
import type * as attempts from "../attempts.js";
import type * as auth from "../auth.js";
import type * as constants from "../constants.js";
import type * as crons from "../crons.js";
Expand All @@ -40,6 +41,7 @@ import type * as users from "../users.js";
* ```
*/
declare const fullApi: ApiFromModules<{
attempts: typeof attempts;
auth: typeof auth;
constants: typeof constants;
crons: typeof crons;
Expand Down
47 changes: 47 additions & 0 deletions convex/attempts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { getAuthUserId } from "@convex-dev/auth/server";
import { v } from "convex/values";
import { api } from "./_generated/api";
import { Id } from "./_generated/dataModel";
import { query } from "./_generated/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);
},
});
44 changes: 0 additions & 44 deletions convex/playerresults.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,7 @@
import { getAuthUserId } from "@convex-dev/auth/server";
import { v } from "convex/values";
import { api } from "./_generated/api";
import { Id } from "./_generated/dataModel";
import { mutation, query } from "./_generated/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) => {
const userId = await getAuthUserId(ctx);
Expand Down

0 comments on commit 96a8eaa

Please sign in to comment.