bug fixes

This commit is contained in:
2025-08-25 18:36:59 -04:00
parent a117f6703f
commit 6c45e7b114
5 changed files with 78 additions and 39 deletions

View File

@@ -74,33 +74,38 @@ const api = new Elysia({ prefix: "/api" })
},
send,
}) {
const table = liveTable<
SimpleConfiguration,
SimpleGameState,
SimpleAction
>(tableKey);
console.log(humanKey, "connected");
try {
const table = liveTable<
SimpleConfiguration,
SimpleGameState,
SimpleAction
>(tableKey);
table.outputs.playersPresent.onValue((players) =>
send({ players })
);
table.outputs.playersPresent.onValue((players) =>
send({ players })
);
table.outputs.playersReady.onValue((readys) =>
send({ playersReady: readys })
);
table.outputs.playersReady.onValue((readys) =>
send({ playersReady: readys })
);
combine(
[table.outputs.gameState],
[table.outputs.gameConfig],
(state, config) =>
state &&
config &&
getSimplePlayerView(config, state, humanKey)
).onValue((view) => send({ view }));
combine(
[table.outputs.gameState],
[table.outputs.gameConfig],
(state, config) =>
state &&
config &&
getSimplePlayerView(config, state, humanKey)
).onValue((view) => send({ view }));
table.inputs.connectionChanges.emit({
humanKey,
presence: "joined",
});
table.inputs.connectionChanges.emit({
humanKey,
presence: "joined",
});
} catch (err) {
console.error(err);
}
},
response: WsOut,
@@ -139,6 +144,10 @@ const api = new Elysia({ prefix: "/api" })
presence: "left",
});
},
// error(err) {
// console.error("ERROR IN WEBSOCKET", JSON.stringify(err, null, 2));
// },
});
export default api;

View File

@@ -11,9 +11,9 @@ const app = new Elysia()
origin: ["http://localhost:3000", "https://games.drm.dev"],
})
)
.onRequest(({ request }) => {
console.log(request.method, request.url);
})
// .onRequest(({ request }) => {
// console.log(request.method, request.url);
// })
.onError(({ error }) => {
console.error(error);
return error;

View File

@@ -12,7 +12,7 @@ import { transform } from "./kefir-extension";
export const WsOut = t.Object({
players: t.Optional(t.Array(t.String())),
playersReady: t.Optional(t.Record(t.String(), t.String())),
playersReady: t.Optional(t.Record(t.String(), t.Boolean())),
view: t.Optional(t.Any()),
});
export type TWsOut = typeof WsOut.static;
@@ -60,7 +60,6 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
quits: Bus(),
};
const { connectionChanges, readys, actions, quits } = inputs;
// =======
const playersPresent = connectionChanges
@@ -97,7 +96,11 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
);
const gameStarts = playersReady
.filter((pr) => Object.values(pr).every((ready) => ready))
.filter(
(pr) =>
Object.values(pr).length > 0 &&
Object.values(pr).every((ready) => ready)
)
.map((_) => null);
const gameConfig = playersPresent.map((players) => ({
@@ -147,9 +150,11 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
// cleanup
tables[key].outputs.playersPresent
.debounce(30000)
.debounce(30000, { immediate: false })
.filter((players) => players.length === 0)
.skip(1)
.onValue((_) => {
console.log("DELETING LIVE TABLE");
delete tables[key];
});
}