Skip to content

Commit

Permalink
Fix simulation with OpenAI mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 16, 2024
1 parent eaf4971 commit 7706445
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
31 changes: 15 additions & 16 deletions convex/openai.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import OpenAI from "openai";
import { internalAction } from "./_generated/server";
import { v } from "convex/values";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
import { api, internal } from "./_generated/api";
import { Doc } from "./_generated/dataModel";
import { ZombieSurvival } from "../simulators/zombie-survival";
import { api, internal } from "./_generated/api";
import { internalAction } from "./_generated/server";

const ResponseSchema = z.object({
map: z.array(z.array(z.string())),
Expand Down Expand Up @@ -42,15 +42,19 @@ export const playMapAction = internalAction({

if (process.env.MOCK_OPEN_AI === "true") {
const existingMap = [...map.grid.map((row) => [...row])];

existingMap[0][0] = "P";
existingMap[0][1] = "B";
existingMap[0][2] = "B";
return {
map: existingMap,

await ctx.runMutation(internal.results.updateResult, {
resultId,
isWin: ZombieSurvival.isWin(existingMap),
reasoning: "This is a mock response",
playerCoordinates: [0, 0],
boxCoordinates: [],
};
map: existingMap,
});

return;
}

// moving here for now so that I can get this deployed without a real key
Expand All @@ -68,11 +72,11 @@ export const playMapAction = internalAction({
"R" represents rocks, which players can shoot over but zombies cannot pass through or break.
"P" represents the player, who cannot move. The player's goal is to shoot and kill zombies before they reach them.
"B" represents blocks that can be placed before the round begins to hinder the zombies. You can place up to two blocks on the map.
Your goal is to place the player ("P") and two blocks ("B") in locations that maximize the player's survival by delaying the zombies' approach.
You can shoot any zombie regardless of where it is on the grid.
Returning a 2d grid with the player and blocks placed in the optimal locations, with the coordinates player ("P") and the blocks ("B"), also provide reasoning for the choices.
You can't replace rocks R or zombies Z with blocks. If there is no room to place a block, do not place any.`,
},
{
Expand All @@ -84,16 +88,11 @@ export const playMapAction = internalAction({
});

const response = completion.choices[0].message;
if (response.parsed) {
const game = new ZombieSurvival(response.parsed.map);
while (!game.finished()) {
game.step();
}
const isWin = !game.getPlayer().dead();

if (response.parsed) {
await ctx.runMutation(internal.results.updateResult, {
resultId,
isWin,
isWin: ZombieSurvival.isWin(response.parsed.map),
reasoning: response.parsed.reasoning,
map: response.parsed.map,
});
Expand Down
10 changes: 10 additions & 0 deletions simulators/zombie-survival/ZombieSurvival.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export class ZombieSurvival {
return new ZombieSurvival(config);
}

public static isWin(config: string[][]): boolean {
const game = new ZombieSurvival(config);

while (!game.finished()) {
game.step();
}

return !game.getPlayer().dead();
}

public constructor(config: string[][]) {
if (config.length === 0 || config[0].length == 0) {
throw new Error("Config is empty");
Expand Down

0 comments on commit 7706445

Please sign in to comment.