when in doubt make it a property I guess
This commit is contained in:
@@ -74,38 +74,35 @@ const api = new Elysia({ prefix: "/api" })
|
||||
},
|
||||
send,
|
||||
}) {
|
||||
console.log(humanKey, "connected");
|
||||
try {
|
||||
const table = liveTable<
|
||||
SimpleConfiguration,
|
||||
SimpleGameState,
|
||||
SimpleAction
|
||||
>(tableKey);
|
||||
const table = liveTable<
|
||||
SimpleConfiguration,
|
||||
SimpleGameState,
|
||||
SimpleAction
|
||||
>(tableKey);
|
||||
|
||||
table.outputs.playersPresent.onValue((players) =>
|
||||
send({ players })
|
||||
);
|
||||
table.inputs.connectionChanges.emit({
|
||||
humanKey,
|
||||
presence: "joined",
|
||||
});
|
||||
|
||||
table.outputs.playersReady.onValue((readys) =>
|
||||
send({ playersReady: readys })
|
||||
);
|
||||
table.outputs.playersPresent.onValue((players) =>
|
||||
send({ players })
|
||||
);
|
||||
|
||||
combine(
|
||||
[table.outputs.gameState],
|
||||
[table.outputs.gameConfig],
|
||||
(state, config) =>
|
||||
state &&
|
||||
config &&
|
||||
getSimplePlayerView(config, state, humanKey)
|
||||
).onValue((view) => send({ view }));
|
||||
table.outputs.playersReady.onValue((readys) =>
|
||||
send({ playersReady: readys })
|
||||
);
|
||||
|
||||
table.inputs.connectionChanges.emit({
|
||||
humanKey,
|
||||
presence: "joined",
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
combine(
|
||||
[table.outputs.gameState],
|
||||
[table.outputs.gameConfig],
|
||||
(state, config) =>
|
||||
state &&
|
||||
config &&
|
||||
getSimplePlayerView(config, state, humanKey)
|
||||
)
|
||||
.toProperty()
|
||||
.onValue((view) => send({ view }));
|
||||
},
|
||||
|
||||
response: WsOut,
|
||||
@@ -144,10 +141,6 @@ const api = new Elysia({ prefix: "/api" })
|
||||
presence: "left",
|
||||
});
|
||||
},
|
||||
|
||||
// error(err) {
|
||||
// console.error("ERROR IN WEBSOCKET", JSON.stringify(err, null, 2));
|
||||
// },
|
||||
});
|
||||
|
||||
export default api;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { t } from "elysia";
|
||||
import { combine, Property } from "kefir";
|
||||
import { combine, pool, Property } from "kefir";
|
||||
import Bus, { type Bus as TBus } from "kefir-bus";
|
||||
import {
|
||||
newSimpleGameState,
|
||||
@@ -75,7 +75,8 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
|
||||
(evt.presence == "joined" ? 1 : -1),
|
||||
};
|
||||
}, {} as { [key: string]: number })
|
||||
.map((counts) => Object.keys(counts));
|
||||
.map((counts) => Object.keys(counts))
|
||||
.toProperty();
|
||||
|
||||
const playersReady = transform(
|
||||
{} as { [key: string]: boolean },
|
||||
@@ -93,7 +94,9 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
|
||||
? { ...prev, [evt.humanKey]: evt.ready }
|
||||
: prev,
|
||||
]
|
||||
);
|
||||
)
|
||||
.toProperty()
|
||||
.log("playersReady");
|
||||
|
||||
const gameStarts = playersReady
|
||||
.filter(
|
||||
@@ -101,22 +104,28 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
|
||||
Object.values(pr).length > 0 &&
|
||||
Object.values(pr).every((ready) => ready)
|
||||
)
|
||||
.map((_) => null);
|
||||
.map((_) => null)
|
||||
.log("gameStarts");
|
||||
|
||||
const gameConfig = playersPresent.map((players) => ({
|
||||
game: "simple",
|
||||
players,
|
||||
}));
|
||||
const gameConfigPool = pool<
|
||||
{
|
||||
game: string;
|
||||
players: string[];
|
||||
},
|
||||
never
|
||||
>();
|
||||
|
||||
const gameConfig = gameConfigPool.toProperty();
|
||||
|
||||
const gameState = transform(
|
||||
null as SimpleGameState | null,
|
||||
[
|
||||
combine([gameStarts], [gameConfig], (_, config) => config),
|
||||
combine([gameStarts], [gameConfigPool], (_, config) => config),
|
||||
(prev, startConfig: SimpleConfiguration) =>
|
||||
prev == null ? newSimpleGameState(startConfig) : prev,
|
||||
],
|
||||
[
|
||||
combine([actions], [gameConfig], (action, config) => ({
|
||||
combine([actions], [gameConfigPool], (action, config) => ({
|
||||
action,
|
||||
config,
|
||||
})),
|
||||
@@ -138,6 +147,21 @@ export const liveTable = <GameConfig, GameState, GameAction>(key: string) => {
|
||||
]
|
||||
).toProperty();
|
||||
|
||||
const gameIsActive = gameState
|
||||
.map((gs) => gs != null)
|
||||
.skipDuplicates()
|
||||
.toProperty()
|
||||
.log("gameIsActive");
|
||||
|
||||
gameConfigPool.plug(
|
||||
playersPresent
|
||||
.filterBy(gameIsActive.map((active) => !active))
|
||||
.map((players) => ({
|
||||
game: "simple",
|
||||
players,
|
||||
}))
|
||||
);
|
||||
|
||||
tables[key] = {
|
||||
inputs,
|
||||
outputs: {
|
||||
|
||||
Reference in New Issue
Block a user