cooking with kefir

This commit is contained in:
2025-08-20 21:56:23 -04:00
parent 265aad4522
commit 35a5af154f
9 changed files with 245 additions and 188 deletions

View File

@@ -1,8 +1,9 @@
import { Elysia, t } from "elysia";
import { simpleApi } from "./games/simple";
import { Action, GameState, getKnowledge, getView } from "./games/simple";
import { human } from "./human";
import dayjs from "dayjs";
import db from "./db";
import { liveTable } from "./table";
const api = new Elysia({ prefix: "/api" })
.post("/whoami", async ({ cookie: { token } }) => {
@@ -48,26 +49,46 @@ const api = new Elysia({ prefix: "/api" })
})
)
.get("/games", () => [{ key: "simple", name: "simple" }])
.ws("/:tableKey", {
response: t.Object({
players: t.Optional(t.Array(t.String())),
view: t.Optional(t.Any()),
}),
.get("/instances", ({ query: { game }, humanKey }) =>
db.instance.findMany({
where: {
game: {
name: game,
},
players: {
some: {
key: humanKey,
},
},
async open({
data: {
params: { tableKey },
humanKey,
},
select: {
id: true,
},
})
)
send,
}) {
const table = liveTable<GameState, Action>(tableKey);
.use(simpleApi);
table.outputs.playersPresent.onValue((players) =>
send({ players })
);
table.outputs.gameState.onValue((gameState) =>
send({
view:
gameState &&
getView(getKnowledge(gameState, humanKey), humanKey),
})
);
table.input.emit({ humanKey, presence: "joined" });
},
async close({
data: {
params: { tableKey },
humanKey,
},
}) {
liveTable(tableKey).input.emit({
humanKey,
presence: "left",
});
},
});
export default api;
export type Api = typeof api;