deep in kefir lore

This commit is contained in:
2025-08-22 00:19:40 -04:00
parent 7d8ac0db76
commit cc53470ddf
8 changed files with 187 additions and 173 deletions

View File

@@ -1,9 +1,14 @@
import { Elysia, t } from "elysia";
import { Action, GameState, getKnowledge, getView } from "./games/simple";
import {
SimpleAction,
SimpleGameState,
getKnowledge,
getView,
} from "./games/simple";
import { human } from "./human";
import dayjs from "dayjs";
import db from "./db";
import { liveTable } from "./table";
import { liveTable, WebsocketIncomingMessage } from "./table";
const api = new Elysia({ prefix: "/api" })
.post("/whoami", async ({ cookie: { token } }) => {
@@ -56,11 +61,34 @@ const api = new Elysia({ prefix: "/api" })
})
)
.get("/games", () => [{ key: "simple", name: "simple" }])
.ws("/:tableKey", {
.ws("/ws/:tableKey", {
response: t.Object({
players: t.Optional(t.Array(t.String())),
view: t.Optional(t.Any()),
}),
body: WebsocketIncomingMessage,
message(
{
data: {
humanKey,
params: { tableKey },
},
},
body
) {
const {
inputs: { gameProposals, gameStarts, gameActions },
} = liveTable(tableKey);
if ("proposeGame" in body) {
gameProposals.emit(body);
} else if ("startGame" in body) {
gameStarts.emit(body);
} else if ("action" in body) {
gameActions.emit(body);
}
},
async open({
data: {
@@ -69,7 +97,7 @@ const api = new Elysia({ prefix: "/api" })
},
send,
}) {
const table = liveTable<GameState, Action>(tableKey);
const table = liveTable<SimpleGameState, SimpleAction>(tableKey);
table.outputs.playersPresent.onValue((players) =>
send({ players })
@@ -81,8 +109,7 @@ const api = new Elysia({ prefix: "/api" })
getView(getKnowledge(gameState, humanKey), humanKey),
})
);
table.input.emit({ humanKey, presence: "joined" });
table.inputs.presenceChanges.emit({ humanKey, presence: "joined" });
},
async close({
data: {
@@ -90,7 +117,7 @@ const api = new Elysia({ prefix: "/api" })
humanKey,
},
}) {
liveTable(tableKey).input.emit({
liveTable(tableKey).inputs.presenceChanges.emit({
humanKey,
presence: "left",
});