cooking with kefir
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user