Skip to content

Commit

Permalink
Merge upstream/main
Browse files Browse the repository at this point in the history
  • Loading branch information
delasy committed Oct 16, 2024
2 parents 80e1c85 + b95d0ff commit 0fbd7d1
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 58 deletions.
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,9 +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.
## How to Contribute

## Join the community
if you see an issue, leave a comment saying "I want to work on this", then I will assign it to you.

Want to help build on this project?
## Join the Discord

- Join the [WDC Discord](https://discord.gg/N2uEyp7Rfu)
The [WDC Discord](https://discord.gg/N2uEyp7Rfu) is where we are discussing this project. We recommend you join if you want to participate.
69 changes: 50 additions & 19 deletions app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/* To change the theme colors, change the values below
or use the "Copy code" button at https://ui.shadcn.com/themes */

@layer base {
:root {
--background: 0 0% 100%;
Expand Down Expand Up @@ -32,27 +33,54 @@
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.light {
--background: 0 0% 100%;
--foreground: 222.2 84% 4.9%;
--card: 0 0% 100%;
--card-foreground: 222.2 84% 4.9%;
--popover: 0 0% 100%;
--popover-foreground: 222.2 84% 4.9%;
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;
--secondary: 210 40% 96.1%;
--secondary-foreground: 222.2 47.4% 11.2%;
--muted: 210 40% 96.1%;
--muted-foreground: 215.4 16.3% 46.9%;
--accent: 210 40% 96.1%;
--accent-foreground: 222.2 47.4% 11.2%;
--destructive: 0 84.2% 60.2%;
--destructive-foreground: 210 40% 98%;
--border: 222.2 84% 4.9%;
--input: 214.3 31.8% 91.4%;
--ring: 222.2 84% 4.9%;
--radius: 0.5rem;
--chart-1: 12 76% 61%;
--chart-2: 173 58% 39%;
--chart-3: 197 37% 24%;
--chart-4: 43 74% 66%;
--chart-5: 27 87% 67%;
}

.dark {
--background: 20 14.3% 4.1%;
--foreground: 60 9.1% 97.8%;
--card: 20 14.3% 4.1%;
--card-foreground: 60 9.1% 97.8%;
--popover: 20 14.3% 4.1%;
--popover-foreground: 60 9.1% 97.8%;
--primary: 60 9.1% 97.8%;
--primary-foreground: 24 9.8% 10%;
--secondary: 12 6.5% 15.1%;
--secondary-foreground: 60 9.1% 97.8%;
--muted: 12 6.5% 15.1%;
--muted-foreground: 24 5.4% 63.9%;
--accent: 12 6.5% 15.1%;
--accent-foreground: 60 9.1% 97.8%;
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
--card: 222.2 84% 4.9%;
--card-foreground: 210 40% 98%;
--popover: 222.2 84% 4.9%;
--popover-foreground: 210 40% 98%;
--primary: 210 40% 98%;
--primary-foreground: 222.2 47.4% 11.2%;
--secondary: 217.2 32.6% 17.5%;
--secondary-foreground: 210 40% 98%;
--muted: 217.2 32.6% 17.5%;
--muted-foreground: 215 20.2% 65.1%;
--accent: 217.2 32.6% 17.5%;
--accent-foreground: 210 40% 98%;
--destructive: 0 62.8% 30.6%;
--destructive-foreground: 60 9.1% 97.8%;
--border: 12 6.5% 15.1%;
--input: 12 6.5% 15.1%;
--ring: 24 5.7% 82.9%;
--destructive-foreground: 210 40% 98%;
--border: 217.2 32.6% 17.5%;
--input: 217.2 32.6% 17.5%;
--ring: 212.7 26.8% 83.9;
--chart-1: 220 70% 50%;
--chart-2: 160 60% 45%;
--chart-3: 30 80% 55%;
Expand All @@ -68,4 +96,7 @@
body {
@apply bg-gradient-to-b from-slate-950 to-slate-900 text-foreground;
}
.light body {
@apply bg-gradient-to-b from-blue-300 to-blue-100;
}
}
20 changes: 13 additions & 7 deletions app/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";
import { useAuthActions } from "@convex-dev/auth/react";
import { GitHubLogoIcon } from "@radix-ui/react-icons";
import { useConvexAuth } from "convex/react";
import { ThemeToggle } from "@/components/ThemeToggle";

function SignInWithGitHub() {
const { signIn } = useAuthActions();
Expand All @@ -25,14 +26,14 @@ export default function Header() {
const { isAuthenticated } = useConvexAuth();

return (
<header className="flex justify-between items-center py-4 px-6 shadow-sm border-b bg-slate-950">
<header className="flex justify-between items-center py-4 px-6 shadow-sm border-b">
<Link href="/" className="flex items-center">
<Image src="/logo.png" alt="Logo" width={32} height={32} />
<span className="ml-2 text-xl font-bold">SurviveTheNight</span>
</Link>

<div className="flex items-center">
<span className="mr-2 text-sm text-gray-200">Synced using Convex</span>
<span className="mr-2 text-sm">Synced using Convex</span>
<Link
href="https://www.convex.dev"
target="_blank"
Expand All @@ -42,11 +43,16 @@ export default function Header() {
</Link>
</div>

{!isAuthenticated ? (
<SignInWithGitHub />
) : (
<Button onClick={() => void signOut()}>Sign Out</Button>
)}
<div className="flex">
<div className="flex hover:bg-slate-500 mr-3 rounded-md px-1">
<ThemeToggle/>
</div>
{!isAuthenticated ? (
<SignInWithGitHub />
) : (
<Button onClick={() => void signOut()}>Sign Out</Button>
)}
</div>
</header>
);
}
17 changes: 10 additions & 7 deletions components/ThemeToggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { useTheme } from "next-themes";
export function ThemeToggle() {
const { theme, setTheme } = useTheme();
return (
<ToggleGroup type="single" size="sm" onValueChange={setTheme} value={theme}>
<ToggleGroupItem value="light" aria-label="Light">
<SunIcon />
</ToggleGroupItem>
<ToggleGroupItem value="dark" aria-label="Dark">
<MoonIcon />
</ToggleGroupItem>
<ToggleGroup type="single" size="sm" onValueChange={setTheme} value={theme} className="">
{theme == "light" ? (
<ToggleGroupItem value="dark" aria-label="Dark">
<MoonIcon />
</ToggleGroupItem>
) : (
<ToggleGroupItem value="light" aria-label="Light">
<SunIcon />
</ToggleGroupItem>
)}
<ToggleGroupItem value="system" aria-label="System">
<DesktopIcon />
</ToggleGroupItem>
Expand Down
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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";
Expand All @@ -37,6 +38,7 @@ 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;
Expand Down
4 changes: 4 additions & 0 deletions convex/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export const AI_MODELS = [
model: "gpt-4o",
name: "OpenAI - 4o Mini",
},
{
model: "gemini-1.5-pro",
name: "Gemini - 1.5 Pro",
},
];

export const AI_MODEL_IDS = AI_MODELS.map((model) => model.model);
140 changes: 140 additions & 0 deletions convex/gemini.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { GoogleGenerativeAI, SchemaType } from "@google/generative-ai";
import { action, internalAction } from "./_generated/server";
import { v } from "convex/values";
import { api, internal } from "./_generated/api";
import { Doc } from "./_generated/dataModel";
import { ZombieSurvival } from "../simulators/zombie-survival";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY as string);

const schema = {
description: "Game Round Results",
type: SchemaType.OBJECT,
properties: {
map: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.STRING,
},
},
description: "The resulting map after the placements",
},
reasoning: {
type: SchemaType.STRING,
description: "The reasoning behind the move",
},
playerCoordinates: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.NUMBER,
},
description: "The player's coordinates",
},
boxCoordinates: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.ARRAY,
items: {
type: SchemaType.NUMBER,
},
},
description: "The box coordinates",
},
},
required: ["map", "reasoning", "playerCoordinates", "boxCoordinates"],
};

const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
generationConfig: {
responseMimeType: "application/json",
responseSchema: schema,
},
});

type playMapActionResponse = {
map: string[][];
reasoning: string;
playerCoordinates: number[];
boxCoordinates: number[][];
};

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_GEMINI === "true") {
const existingMap = [...map.grid.map((row) => [...row])];
existingMap[0][0] = "P";
existingMap[0][1] = "B";
existingMap[0][2] = "B";
return {
map: existingMap,
reasoning: "This is a mock response",
playerCoordinates: [0, 0],
boxCoordinates: [],
};
}

const result = await model.generateContent(
`You're given a 2d grid of nums such that.
" " represents an empty space.
"Z" represents a zombie. Zombies move one Manhattan step every turn and aim to reach the player.
"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.
Grid: ${JSON.stringify(map)}`,
);

// todo: check if the response is valid acc to types and the player and box coordinates are valid,
// as sometimes the model returns a state that's erroring out in the simulator

const parsedResponse = JSON.parse(
result.response.text(),
) as playMapActionResponse;

const game = new ZombieSurvival(parsedResponse.map);
while (!game.finished()) {
game.step();
}
const isWin = !game.getPlayer().dead();

await ctx.runMutation(internal.results.updateResult, {
resultId,
isWin,
reasoning: parsedResponse.reasoning,
map: parsedResponse.map,
});
},
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@auth/core": "^0.34.2",
"@convex-dev/auth": "^0.0.71",
"@google/generative-ai": "^0.21.0",
"@radix-ui/react-dropdown-menu": "^2.1.1",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-select": "^2.1.2",
Expand Down
4 changes: 0 additions & 4 deletions simulators/zombie-survival/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ export interface Position {
x: number;
y: number;
}

export function positionAsNumber(position: Position): number {
return position.x + position.y;
}
Loading

0 comments on commit 0fbd7d1

Please sign in to comment.