Skip to content

Commit

Permalink
Merge branch 'main' of github.com:webdevcody/survive-the-night-sim
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Oct 16, 2024
2 parents 89edde3 + e8b074a commit 7813185
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 296 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Before you can run the project, you'll need to setup a

This starter project works with [convex](https://www.convex.dev) so to run you need to use the [.env.example](.env.example) file to create your own .env file.

In convex, please add the following environment variables:
If you want to mock all models - in convex, please add the following environment variables:

- `npx convex env set MOCK_OPEN_AI true`
- `npx convex env set MOCK_MODELS true`

Add optional environment variable/s for simulating real AI models without mockup responses(when mockup flags are set to FALSE):

- `npx convex env set GEMINI_API_KEY YOUR_API_KEY`
- `npx convex env set OPENAI_API_KEY YOUR_API_KEY`

also, you may need to run, but I think the initial setup does that.
Expand Down
4 changes: 0 additions & 4 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import type {
import type * as auth from "../auth.js";
import type * as constants from "../constants.js";
import type * as games from "../games.js";
import type * as gemini from "../gemini.js";
import type * as http from "../http.js";
import type * as init from "../init.js";
import type * as maps from "../maps.js";
import type * as openai from "../openai.js";
import type * as results from "../results.js";
import type * as scores from "../scores.js";
import type * as users from "../users.js";
Expand All @@ -39,11 +37,9 @@ declare const fullApi: ApiFromModules<{
auth: typeof auth;
constants: typeof constants;
games: typeof games;
gemini: typeof gemini;
http: typeof http;
init: typeof init;
maps: typeof maps;
openai: typeof openai;
results: typeof results;
scores: typeof scores;
users: typeof users;
Expand Down
8 changes: 4 additions & 4 deletions convex/constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export const AI_MODELS = [
{
model: "gpt-4o",
name: "OpenAI - 4o Mini",
},
{
model: "gemini-1.5-pro",
name: "Gemini - 1.5 Pro",
},
{
model: "gpt-4o",
name: "OpenAI - 4o Mini",
},
];

export const AI_MODEL_IDS = AI_MODELS.map((model) => model.model);
18 changes: 5 additions & 13 deletions convex/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,11 @@ export const startNewGame = mutation({
throw new Error("No map found for level 1");
}

if (args.modelId === "gpt-4o") {
await ctx.scheduler.runAfter(0, internal.openai.playMapAction, {
gameId,
modelId: args.modelId,
level: 1,
});
} else if (args.modelId === "gemini-1.5-pro") {
await ctx.scheduler.runAfter(0, internal.gemini.playMapAction, {
gameId,
modelId: args.modelId,
level: 1,
});
}
await ctx.scheduler.runAfter(0, internal.maps.playMapAction, {
gameId,
modelId: args.modelId,
level: 1,
});

return gameId;
},
Expand Down
140 changes: 0 additions & 140 deletions convex/gemini.ts

This file was deleted.

83 changes: 82 additions & 1 deletion convex/maps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import OpenAI from "openai";
import { internalAction, internalMutation, query } from "./_generated/server";
import { v } from "convex/values";
import { internalMutation, query } from "./_generated/server";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
import { Doc } from "./_generated/dataModel";
import { ZombieSurvival } from "../simulators/zombie-survival";
import { api, internal } from "./_generated/api";
import { runModel } from "../models";

const MAPS = [
{
Expand Down Expand Up @@ -98,3 +105,77 @@ export const getMapByLevel = query({
.first();
},
});

export const playMapAction = internalAction({
args: {
level: v.number(),
gameId: v.id("games"),
modelId: v.string(),
},
handler: async (ctx, args) => {
const resultId = await ctx.runMutation(
internal.results.createInitialResult,
{
gameId: args.gameId,
level: args.level,
},
);

const map: Doc<"maps"> | null = (await ctx.runQuery(
api.maps.getMapByLevel,
{
level: args.level,
},
)) as any;

if (!map) {
throw new Error("Map not found");
}

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

existingMap[0][0] = "P";
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,
reasoning: "This is a mock response",
map: existingMap,
});

return;
}

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,
reasoning,
map: solution,
});
} catch (error) {
// todo: handle error
console.log(error);
}
},
});
Loading

0 comments on commit 7813185

Please sign in to comment.