Skip to content

Commit

Permalink
fixing error
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 19, 2024
1 parent afce648 commit 40eb410
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 112 deletions.
2 changes: 1 addition & 1 deletion components/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getCellImage(cell: string) {
height: "100%",
position: "relative",
top: "-20px",
left: "4px",
left: "16px",
}}
/>
);
Expand Down
18 changes: 16 additions & 2 deletions components/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Button } from "@/components/ui/button";
import {
type Entity,
EntityType,
Position,
ZombieSurvival,
} from "@/simulators/zombie-survival";
import {
Expand Down Expand Up @@ -33,6 +34,17 @@ function getEntityImage(
}
}

function getImageOffset(entity: Entity): Position {
if (entity.getType() === EntityType.Zombie) {
return { x: 16, y: 0 };
}
return { x: 0, y: 0 };
}

function cloneMap(map: string[][]): string[][] {
return JSON.parse(JSON.stringify(map));
}

export function Visualizer({
autoReplay = false,
autoStart = false,
Expand Down Expand Up @@ -148,10 +160,12 @@ export function Visualizer({
? 0.5
: 1;

const offset = getImageOffset(entity);

ctx.drawImage(
entityImage,
entityPosition.x * cellSizeNum,
entityPosition.y * cellSizeNum,
entityPosition.x * cellSizeNum + offset.x,
entityPosition.y * cellSizeNum + offset.y,
cellSizeNum,
cellSizeNum,
);
Expand Down
90 changes: 0 additions & 90 deletions convex/README.md

This file was deleted.

41 changes: 28 additions & 13 deletions convex/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { v } from "convex/values";
import { internalMutation, query } from "./_generated/server";
import { api } from "./_generated/api";

export const getGlobalRankings = query({
handler: async ({ db }) => {
Expand Down Expand Up @@ -45,41 +46,55 @@ export const updateRankings = internalMutation({
isWin: v.boolean(),
},
handler: async (ctx, args) => {
const activePrompt = await ctx.runQuery(api.prompts.getActivePrompt);

if (!activePrompt) {
throw new Error("Active prompt not found");
}

const globalRanking = await ctx.db
.query("globalrankings")
.withIndex("by_modelId", (q) => q.eq("modelId", args.modelId))
.collect();
.withIndex("by_modelId_promptId", (q) =>
q.eq("modelId", args.modelId).eq("promptId", activePrompt._id),
)
.first();

const levelRanking = await ctx.db
.query("levelrankings")
.withIndex("by_modelId_level", (q) =>
q.eq("modelId", args.modelId).eq("level", args.level),
.withIndex("by_modelId_level_promptId", (q) =>
q
.eq("modelId", args.modelId)
.eq("level", args.level)
.eq("promptId", activePrompt._id),
)
.collect();
.first();

if (globalRanking.length === 0) {
if (!globalRanking) {
await ctx.db.insert("globalrankings", {
modelId: args.modelId,
wins: args.isWin ? 1 : 0,
losses: args.isWin ? 0 : 1,
promptId: activePrompt._id,
});
} else {
await ctx.db.patch(globalRanking[0]._id, {
wins: globalRanking[0].wins + (args.isWin ? 1 : 0),
losses: globalRanking[0].losses + (args.isWin ? 0 : 1),
await ctx.db.patch(globalRanking._id, {
wins: globalRanking.wins + (args.isWin ? 1 : 0),
losses: globalRanking.losses + (args.isWin ? 0 : 1),
});
}

if (levelRanking.length === 0) {
if (!levelRanking) {
await ctx.db.insert("levelrankings", {
modelId: args.modelId,
level: args.level,
wins: args.isWin ? 1 : 0,
losses: args.isWin ? 0 : 1,
promptId: activePrompt._id,
});
} else {
await ctx.db.patch(levelRanking[0]._id, {
wins: levelRanking[0].wins + (args.isWin ? 1 : 0),
losses: levelRanking[0].losses + (args.isWin ? 0 : 1),
await ctx.db.patch(levelRanking._id, {
wins: levelRanking.wins + (args.isWin ? 1 : 0),
losses: levelRanking.losses + (args.isWin ? 0 : 1),
});
}
},
Expand Down
8 changes: 5 additions & 3 deletions convex/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default defineSchema({
}).index("by_level", ["level"]),
scores: defineTable({
modelId: v.string(),
promptId: v.id("prompts"),
promptId: v.optional(v.id("prompts")),
score: v.number(),
}).index("by_modelId", ["modelId"]),
models: defineTable({
Expand All @@ -46,13 +46,15 @@ export default defineSchema({
modelId: v.string(),
wins: v.number(),
losses: v.number(),
}).index("by_modelId", ["modelId"]),
promptId: v.optional(v.id("prompts")),
}).index("by_modelId_promptId", ["modelId", "promptId"]),
levelrankings: defineTable({
modelId: v.string(),
level: v.number(),
wins: v.number(),
promptId: v.optional(v.id("prompts")),
losses: v.number(),
}).index("by_modelId_level", ["modelId", "level"]),
}).index("by_modelId_level_promptId", ["modelId", "level", "promptId"]),
attempts: defineTable({
grid: v.array(v.array(v.string())),
didWin: v.boolean(),
Expand Down
3 changes: 1 addition & 2 deletions convex/scores.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { v } from "convex/values";
import { internalMutation } from "./_generated/server";
import { useQuery } from "convex/react";
import { api } from "./_generated/api";

export const incrementScore = internalMutation({
Expand All @@ -13,7 +12,7 @@ export const incrementScore = internalMutation({
.filter((q) => q.eq(q.field("modelId"), args.modelId))
.first();

const activePrompt = useQuery(api.prompts.getActivePrompt);
const activePrompt = await ctx.runQuery(api.prompts.getActivePrompt);

if (!score && activePrompt) {
await ctx.db.insert("scores", {
Expand Down
2 changes: 1 addition & 1 deletion public/entities/zombie_alive_1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 40eb410

Please sign in to comment.