Skip to content

Commit

Permalink
getting the multiplayer game to work
Browse files Browse the repository at this point in the history
  • Loading branch information
webdevcody committed Nov 4, 2024
1 parent 3d9e6b6 commit fa2027d
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 129 deletions.
32 changes: 17 additions & 15 deletions convex/constants.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
export const AI_MODELS = [
{
model: "gemini-1.5-pro",
name: "Google - Gemini 1.5 Pro",
},
{
model: "gpt-4o",
export const AI_MODELS = {
"gpt-4o": {
slug: "gpt-4o",
name: "OpenAI - GPT-4o",
},
{
model: "claude-3.5-sonnet",
"claude-3.5-sonnet": {
slug: "claude-3.5-sonnet",
name: "Anthropic - Claude 3.5 Sonnet",
},
{
model: "perplexity-llama-3.1",
"perplexity-llama-3.1": {
slug: "perplexity-llama-3.1",
name: "Perplexity - Llama 3.1",
},
{
model: "mistral-large-2",
"mistral-large-2": {
slug: "mistral-large-2",
name: "Mistral - Large 2",
},
];
"gemini-1.5-pro": {
slug: "gemini-1.5-pro",
name: "Google - Gemini 1.5 Pro",
},
} as const;

export type ModelSlug = (typeof AI_MODELS)[keyof typeof AI_MODELS]["slug"];

export const AI_MODEL_IDS = AI_MODELS.map((model) => model.model);
export const AI_MODEL_SLUGS = Object.keys(AI_MODELS) as ModelSlug[];

// how long between each level when the AI models start playing.
// spacing out the levels to make it easier to watch in the games list and reduce ai token usage.
Expand Down
4 changes: 2 additions & 2 deletions convex/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v } from "convex/values";
import { api, internal } from "./_generated/api";
import { Id } from "./_generated/dataModel";
import { internalMutation, mutation, query } from "./_generated/server";
import { AI_MODEL_IDS } from "./constants";
import { AI_MODEL_SLUGS, ModelSlug } from "./constants";

export const testModel = mutation({
args: {
Expand Down Expand Up @@ -31,7 +31,7 @@ export const startNewGame = internalMutation({
modelId: v.string(),
},
handler: async (ctx, args) => {
if (!AI_MODEL_IDS.includes(args.modelId)) {
if (!AI_MODEL_SLUGS.includes(args.modelId as ModelSlug)) {
throw new Error("Invalid model ID");
}

Expand Down
18 changes: 10 additions & 8 deletions convex/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export const runActiveModelsGames = internalMutation({

await Promise.all(
models.map((model) =>
ctx.runMutation(internal.games.startNewGame, { modelId: model.slug }),
ctx.runMutation(internal.games.startNewGame, {
modelId: model.slug,
}),
),
);
},
Expand All @@ -25,16 +27,16 @@ export const seedModels = internalMutation({
const models = await ctx.db.query("models").collect();
const promises = [];

for (const model of AI_MODELS) {
const existingModel = models.find((it) => it.slug === model.model);
for (const model of Object.values(AI_MODELS)) {
const existingModel = models.find((it) => it.slug === model.slug);

if (existingModel !== undefined) {
continue;
}

promises.push(
ctx.db.insert("models", {
slug: model.model,
slug: model.slug,
name: model.name,
active: false,
}),
Expand All @@ -45,18 +47,18 @@ export const seedModels = internalMutation({
},
});

export const getActiveModelByName = query({
export const getActiveModelBySlug = query({
args: {
name: v.string(),
slug: v.string(),
},
handler: async (ctx, args) => {
const record = await ctx.db
.query("models")
.withIndex("by_slug", (q) => q.eq("slug", args.name))
.withIndex("by_slug", (q) => q.eq("slug", args.slug))
.first();

if (record === null) {
throw new Error(`Model with name '${args.name}' was not found`);
throw new Error(`Model with name '${args.slug}' was not found`);
}

return record;
Expand Down
Loading

0 comments on commit fa2027d

Please sign in to comment.