-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
248 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
This is a community project for my discord, so please join if you want to participate. | ||
|
||
You can still contribute without being part of discord, but it would be nice. | ||
|
||
## Join the community | ||
|
||
Want to help build on this project? | ||
|
||
- Join the [WDC Discord](https://discord.gg/N2uEyp7Rfu) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"use client"; | ||
|
||
import { Map } from "@/app/map"; | ||
import { api } from "@/convex/_generated/api"; | ||
import { useAction } from "convex/react"; | ||
import React, { useState } from "react"; | ||
|
||
function convertStringMapToNumberGrid(map: string[][]): number[][] { | ||
return map.map((row) => | ||
row.map((cell) => { | ||
switch (cell) { | ||
case "P": | ||
return 3; // Player | ||
case "Z": | ||
return 1; // Zombie | ||
case "R": | ||
return 2; // Rocks | ||
case "B": | ||
return 4; // Block | ||
case " ": | ||
return 0; // Empty Space | ||
default: | ||
return 0; // Default to empty space | ||
} | ||
}), | ||
); | ||
} | ||
|
||
function convertNumberGridToStringMap(grid: number[][]): string[][] { | ||
return grid.map((row) => | ||
row.map((cell) => { | ||
switch (cell) { | ||
case 3: | ||
return "P"; // Player | ||
case 1: | ||
return "Z"; // Zombie | ||
case 2: | ||
return "R"; // Rocks | ||
case 4: | ||
return "B"; // Block | ||
case 0: | ||
return " "; // Empty Space | ||
default: | ||
return " "; // Default to empty space | ||
} | ||
}), | ||
); | ||
} | ||
|
||
const MapTester = ({ map }: { map: string[][] }) => { | ||
const getPlayerMap = useAction(api.openai.playerMap); | ||
const [resMap, setResMap] = useState<null | number[][]>(); | ||
const [loading, setLoading] = useState(false); | ||
|
||
const handleClick = async () => { | ||
setLoading(true); | ||
try { | ||
const res = await getPlayerMap({ | ||
level: 1, | ||
mapId: "zombiemap", | ||
map: convertStringMapToNumberGrid(map), | ||
}); | ||
|
||
// type safe resulting object | ||
console.log(res); | ||
setResMap(res?.map); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button onClick={handleClick} disabled={loading}> | ||
Test Output | ||
</button> | ||
{loading && <p>Loading result map...</p>} | ||
{resMap && <Map map={convertNumberGridToStringMap(resMap)} />} | ||
</div> | ||
); | ||
}; | ||
|
||
export default MapTester; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { v } from "convex/values"; | ||
import { internal } from "../_generated/api"; | ||
import { internalAction } from "../_generated/server"; | ||
|
||
export const playMapAction = internalAction({ | ||
args: { | ||
mapId: v.id("maps"), | ||
gameId: v.id("games"), | ||
modelId: v.string(), | ||
level: v.number(), | ||
}, | ||
handler: async (ctx, args) => { | ||
await ctx.runMutation(internal.games.mutations.insertScore, { | ||
modelId: args.modelId, | ||
gameId: args.gameId, | ||
level: args.level, | ||
}); | ||
|
||
console.log( | ||
`Playing map ${args.mapId} for game ${args.gameId} at level ${args.level}`, | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { v } from "convex/values"; | ||
import { internal } from "../_generated/api"; | ||
import { internalMutation } from "../_generated/server"; | ||
|
||
export const createNewGame = internalMutation({ | ||
args: { modelId: v.string() }, | ||
handler: async (ctx, args) => { | ||
const gameId = await ctx.db.insert("games", { | ||
modelId: args.modelId, | ||
currentLevel: 1, | ||
status: "in_progress", | ||
}); | ||
|
||
const firstMap = await ctx.db | ||
.query("maps") | ||
.withIndex("by_level", (q) => q.eq("level", 1)) | ||
.first(); | ||
|
||
if (!firstMap) { | ||
throw new Error("No map found for level 1"); | ||
} | ||
|
||
// not sure if this is the way to do this | ||
await ctx.scheduler.runAfter(0, internal.games.actions.playMapAction, { | ||
mapId: firstMap._id, | ||
gameId, | ||
modelId: args.modelId, | ||
level: 1, | ||
}); | ||
|
||
return gameId; | ||
}, | ||
}); | ||
|
||
export const insertScore = internalMutation({ | ||
args: { | ||
modelId: v.string(), | ||
gameId: v.id("games"), | ||
level: v.number(), | ||
}, | ||
handler: async ({ db }, args) => { | ||
await db.insert("scores", { | ||
modelId: args.modelId, | ||
gameId: args.gameId, | ||
score: 0, | ||
level: args.level, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import OpenAI from "openai"; | ||
import { action, query } from "./_generated/server"; | ||
import { v } from "convex/values"; | ||
import { z } from "zod"; | ||
import { zodResponseFormat } from "openai/helpers/zod"; | ||
|
||
const openai = new OpenAI(); | ||
|
||
const ResponseSchema = z.object({ | ||
map: z.array(z.array(z.number())), | ||
reasoning: z.string(), | ||
playerCoordinates: z.array(z.number()), | ||
BoxCoordinates: z.array(z.array(z.number())), | ||
}); | ||
|
||
export const playerMap = action({ | ||
args: { | ||
map: v.array(v.array(v.number())), | ||
mapId: v.string(), | ||
level: v.number(), | ||
}, | ||
handler: async (_, args) => { | ||
try { | ||
const completion = await openai.beta.chat.completions.parse({ | ||
model: "gpt-4o-2024-08-06", | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: `You're given a 2d grid of nums such that. | ||
0 represents an empty space. | ||
1 represents a zombie. Zombies move one Manhattan step every turn and aim to reach the player. | ||
2 represents rocks, which players can shoot over but zombies cannot pass through or break. | ||
3 represents the player, who cannot move. The player's goal is to shoot and kill zombies before they reach them. | ||
4 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 (3) and two blocks (4) in locations that maximize the player's survival by delaying the zombies' approach while allowing the player clear lines of sight to shoot them before they get too close. | ||
Returning a 2d grid with the player and blocks placed in the optimal locations, with the coordinates player (3) and the blocks (4), also provide reasoning for the choices.`, | ||
}, | ||
{ | ||
role: "user", | ||
content: JSON.stringify(args.map), | ||
}, | ||
], | ||
response_format: zodResponseFormat(ResponseSchema, "game_map"), | ||
}); | ||
|
||
const map_response = completion.choices[0].message; | ||
if (map_response.parsed) { | ||
return map_response.parsed; | ||
} else if (map_response.refusal) { | ||
const refusal_res = map_response.refusal; | ||
throw new Error(`Refusal: ${refusal_res}`); | ||
} | ||
} catch (error) { | ||
// todo: handle error | ||
console.log(error); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters