[wip] so close; check the ws messages

This commit is contained in:
2025-08-30 18:24:08 -04:00
parent 782dd738cc
commit 01a12ec58a
11 changed files with 139 additions and 88 deletions

View File

@@ -6,6 +6,7 @@ import { combine } from "kefir";
import Bus from "kefir-bus";
import db from "./db";
import { liveTable, WsIn, WsOut } from "./table";
import { err } from "./logging";
export const WS = Bus<
{
@@ -76,13 +77,17 @@ const api = new Elysia({ prefix: "/api" })
})
)
.ws("/ws/:tableKey", {
async open({
body: WsIn,
response: WsOut,
open: ({
data: {
params: { tableKey },
humanKey,
},
send,
}) {
}) => {
console.log("websocket opened");
const table = liveTable(tableKey);
table.inputs.connectionChanges.emit({
@@ -90,22 +95,14 @@ const api = new Elysia({ prefix: "/api" })
presence: "joined",
});
Object.entries(table.outputs.global).forEach(([type, stream]) =>
Object.entries({
...table.outputs.global,
...(table.outputs.player[humanKey] ?? {}),
}).forEach(([type, stream]) =>
stream.onValue((v) => send({ [type]: v }))
);
combine(
[table.outputs.gameState],
[table.outputs.gameImpl],
(state, game: Game) => state && game.getView({ state, humanKey })
)
.toProperty()
.onValue((view) => send({ view }));
},
body: WsIn,
response: WsOut,
message: (
{
data: {
@@ -114,19 +111,20 @@ const api = new Elysia({ prefix: "/api" })
},
},
body
) => WS.emit({ ...body, type: "message", humanKey, tableKey }),
) => liveTable(tableKey).inputs.messages.emit({ ...body, humanKey }),
close({
close: ({
data: {
params: { tableKey },
humanKey,
},
}) {
}) =>
liveTable(tableKey).inputs.connectionChanges.emit({
humanKey,
presence: "left",
});
},
}),
error: (error) => err(error),
});
export default api;