[wip] kefir cleanup
This commit is contained in:
@@ -5,12 +5,22 @@ import {
|
||||
SimpleConfiguration,
|
||||
SimpleGameState,
|
||||
} from "@games/shared/games/simple";
|
||||
import { human } from "./human";
|
||||
import dayjs from "dayjs";
|
||||
import db from "./db";
|
||||
import { liveTable, WsOut, WsIn } from "./table";
|
||||
import { Human } from "@prisma/client";
|
||||
import { combine } from "kefir";
|
||||
import Bus from "kefir-bus";
|
||||
import { Game } from "@games/shared/games";
|
||||
|
||||
export const WS = Bus<
|
||||
{
|
||||
type: "open" | "message" | "error" | "close";
|
||||
humanKey: string;
|
||||
tableKey: string;
|
||||
},
|
||||
unknown
|
||||
>();
|
||||
|
||||
const api = new Elysia({ prefix: "/api" })
|
||||
.post("/whoami", async ({ cookie: { token } }) => {
|
||||
@@ -35,7 +45,14 @@ const api = new Elysia({ prefix: "/api" })
|
||||
|
||||
return human.key;
|
||||
})
|
||||
.use(human)
|
||||
.derive(async ({ cookie: { token }, status }) => {
|
||||
const humanKey = await db.human
|
||||
.findUnique({
|
||||
where: { token: token.value },
|
||||
})
|
||||
.then((human) => human?.key);
|
||||
return humanKey != null ? { humanKey } : status(401);
|
||||
})
|
||||
.post(
|
||||
"/setName",
|
||||
({ body: { name }, humanKey }) =>
|
||||
@@ -64,7 +81,6 @@ const api = new Elysia({ prefix: "/api" })
|
||||
return safeProfile;
|
||||
})
|
||||
)
|
||||
.get("/games", () => [{ key: "simple", name: "simple" }])
|
||||
.ws("/ws/:tableKey", {
|
||||
async open({
|
||||
data: {
|
||||
@@ -73,11 +89,7 @@ const api = new Elysia({ prefix: "/api" })
|
||||
},
|
||||
send,
|
||||
}) {
|
||||
const table = liveTable<
|
||||
SimpleConfiguration,
|
||||
SimpleGameState,
|
||||
SimpleAction
|
||||
>(tableKey);
|
||||
const table = liveTable(tableKey);
|
||||
|
||||
table.inputs.connectionChanges.emit({
|
||||
humanKey,
|
||||
@@ -87,27 +99,24 @@ const api = new Elysia({ prefix: "/api" })
|
||||
table.outputs.playersPresent.onValue((players) =>
|
||||
send({ players })
|
||||
);
|
||||
|
||||
table.outputs.playersReady
|
||||
.skipDuplicates()
|
||||
.onValue((readys) => send({ playersReady: readys }));
|
||||
|
||||
combine(
|
||||
[table.outputs.gameState],
|
||||
[table.outputs.gameConfig],
|
||||
(state, config) =>
|
||||
state &&
|
||||
config &&
|
||||
getSimplePlayerView(config, state, humanKey)
|
||||
[table.outputs.gameImpl],
|
||||
(state, { game: Game }) =>
|
||||
state && game.getView({ config, state, humanKey })
|
||||
)
|
||||
.toProperty()
|
||||
.onValue((view) => send({ view }));
|
||||
},
|
||||
|
||||
response: WsOut,
|
||||
body: WsIn,
|
||||
response: WsOut,
|
||||
|
||||
message(
|
||||
message: (
|
||||
{
|
||||
data: {
|
||||
humanKey,
|
||||
@@ -115,21 +124,9 @@ const api = new Elysia({ prefix: "/api" })
|
||||
},
|
||||
},
|
||||
body
|
||||
) {
|
||||
const {
|
||||
inputs: { readys, actions, quits },
|
||||
} = liveTable(tableKey);
|
||||
) => WS.emit({ ...body, type: "message", humanKey, tableKey }),
|
||||
|
||||
if ("ready" in body) {
|
||||
readys.emit({ humanKey, ...body });
|
||||
} else if ("action" in body) {
|
||||
actions.emit({ humanKey, ...body.action });
|
||||
} else if ("quit" in body) {
|
||||
quits.emit({ humanKey });
|
||||
}
|
||||
},
|
||||
|
||||
async close({
|
||||
close({
|
||||
data: {
|
||||
params: { tableKey },
|
||||
humanKey,
|
||||
|
||||
Reference in New Issue
Block a user