diff --git a/convex/multiplayerGames.ts b/convex/multiplayerGames.ts index 39bfb1f..fc639f4 100644 --- a/convex/multiplayerGames.ts +++ b/convex/multiplayerGames.ts @@ -1,10 +1,5 @@ import { runMultiplayerModel } from "../models/multiplayer"; -import { - Direction, - ZombieSurvival, - fromDirectionString, - move, -} from "../simulator"; +import { ZombieSurvival, fromDirectionString, move } from "../simulator"; import { v } from "convex/values"; import { api, internal } from "./_generated/api"; import { internalAction, internalMutation, query } from "./_generated/server"; @@ -13,266 +8,25 @@ import { AI_MODELS, ModelSlug } from "./constants"; const HARD_CODED_PLAYER_TOKEN = "1"; const TURN_DELAY = 0; +const boardState = ` +Z.Z.Z. . . .B. . . . . . . . , +Z.Z. . . . .B. . . . . . . . , +Z. . . . .B. .1. . . . . . . , + . . . .R. . . . .R. . . . . , + . . . .R. . . . .R. . . . . , + . . . .R. . . . .R. . . . . , + . . . . . . . .B. . . . . .Z, + . . . . . . .B. . . . . .Z.Z, + . . . . . . .B. . . . .Z.Z.Z, +`; + export const startMultiplayerGame = internalMutation({ handler: async (ctx) => { const gameId = await ctx.db.insert("multiplayerGames", { - boardState: [ - [ - "Z", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - "Z", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - "Z", - "Z", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - "R", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - "R", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - "R", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - "R", - " ", - " ", - " ", - "R", - "R", - "B", - "B", - "R", - "R", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - HARD_CODED_PLAYER_TOKEN, - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - [ - "Z", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - " ", - ], - ], + boardState: boardState + .trim() + .split(",\n") + .map((it) => it.split(".")), playerMap: [ { modelSlug: AI_MODELS["gpt-4o"].slug, @@ -344,7 +98,10 @@ export const runMultiplayerGameTurn = internalAction({ boardState: map.getState(), }, ); - } else if (turn === HARD_CODED_PLAYER_TOKEN) { + } else if ( + ZombieSurvival.mapHasToken(map.getState(), turn) && + turn === HARD_CODED_PLAYER_TOKEN + ) { const model = multiplayerGame.playerMap.find( (entry) => entry.playerToken === turn, ); @@ -356,7 +113,7 @@ export const runMultiplayerGameTurn = internalAction({ const results = await runMultiplayerModel( model.modelSlug as ModelSlug, map.getState(), - HARD_CODED_PLAYER_TOKEN, + turn, ); if (results.moveDirection && results.moveDirection !== "STAY") { diff --git a/simulator/ZombieSurvival.ts b/simulator/ZombieSurvival.ts index 3f2c97c..0467b5e 100644 --- a/simulator/ZombieSurvival.ts +++ b/simulator/ZombieSurvival.ts @@ -148,6 +148,10 @@ export class ZombieSurvival { return game.getPlayer() === null || !game.getPlayer()?.dead(); } + public static mapHasToken(map: string[][], token: string): boolean { + return map.flat().includes(token); + } + public static mapHasMultiplePlayers(map: string[][]): boolean { return ( map.map((row) => row.filter((cell) => cell === "P")).flat().length > 1