Skip to content

Commit

Permalink
Simplify hardcoded multiplayer board state
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Nov 4, 2024
1 parent e11d28d commit ab86b26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 265 deletions.
287 changes: 22 additions & 265 deletions convex/multiplayerGames.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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,
);
Expand All @@ -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") {
Expand Down
4 changes: 4 additions & 0 deletions simulator/ZombieSurvival.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ab86b26

Please sign in to comment.