Compare commits

...

19 Commits

43 changed files with 739 additions and 604 deletions

2
.gitignore vendored
View File

@@ -2,6 +2,8 @@
.vinxi .vinxi
*.db *.db
.DS_STORE
# ---> Node # ---> Node
# Logs # Logs
logs logs

View File

@@ -8,3 +8,8 @@ build:
start: start:
PORT=$(PORT) pnpm start PORT=$(PORT) pnpm start
note:
./notes/newfile
# touch ./notes/$$file.md
# code -r ./notes/$$file.md

7
deploy Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
branch=$(git branch --show-current)
git switch prod
git merge $branch
git push
git switch $branch

7
notes/newfile Executable file
View File

@@ -0,0 +1,7 @@
#!/bin/bash
ts=$(date +"%Y-%m-%d-%H%M%S")
file=./notes/$ts.md
touch $file
echo -e "# $ts\n" > $file
echo "$file:end"
code --goto "$file:2"

View File

@@ -1,7 +1,7 @@
{ {
"name": "games", "name": "games",
"type": "module", "type": "module",
"version": "0.0.6", "version": "0.0.10",
"scripts": { "scripts": {
"dev": "pnpm --parallel dev", "dev": "pnpm --parallel dev",
"build": "pnpm run -F client build", "build": "pnpm run -F client build",
@@ -12,11 +12,7 @@
"object-hash": "^3.0.0" "object-hash": "^3.0.0"
}, },
"onlyBuiltDependencies": [ "onlyBuiltDependencies": [
"@parcel/watcher", "esbuild"
"@prisma/client",
"@prisma/engines",
"esbuild",
"prisma"
] ]
}, },
"devDependencies": { "devDependencies": {

View File

@@ -8,8 +8,11 @@
}, },
"dependencies": { "dependencies": {
"@elysiajs/eden": "^1.3.2", "@elysiajs/eden": "^1.3.2",
"@solid-primitives/memo": "^1.4.3",
"@solid-primitives/scheduled": "^1.5.2", "@solid-primitives/scheduled": "^1.5.2",
"@solid-primitives/storage": "^4.3.3",
"@solidjs/router": "^0.15.3", "@solidjs/router": "^0.15.3",
"color2k": "^2.0.3",
"js-cookie": "^3.0.5", "js-cookie": "^3.0.5",
"kefir": "^3.8.8", "kefir": "^3.8.8",
"kefir-bus": "^2.3.1", "kefir-bus": "^2.3.1",

View File

@@ -11,6 +11,4 @@ const { api } = treaty<Api>(
export default api; export default api;
export const fromWebsocket = <T>(ws: any) => export const fromWebsocket = <T>(ws: any) =>
fromEvents(ws, "message").map( fromEvents(ws, "message").map((evt) => (evt as unknown as { data: T }).data);
(evt) => (evt as unknown as { data: T }).data
);

View File

@@ -1,17 +1,14 @@
import { makePersisted } from "@solid-primitives/storage";
import { Route, Router } from "@solidjs/router"; import { Route, Router } from "@solidjs/router";
import { createResource, lazy, Suspense } from "solid-js"; import pkg from "^/package.json";
import { createSignal, lazy, Suspense } from "solid-js";
import { render } from "solid-js/web"; import { render } from "solid-js/web";
import "virtual:uno.css"; import "virtual:uno.css";
import pkg from "^/package.json";
import "./style.css"; import "./style.css";
import api from "./api"; import { name, setName } from "./profile";
import { mePromise } from "./profile";
const Profile = () => { const Profile = () => {
let dialogRef!: HTMLDialogElement; let dialogRef!: HTMLDialogElement;
const [profile] = createResource(() =>
mePromise.then(() => api.profile.get())
);
return ( return (
<> <>
@@ -24,10 +21,10 @@ const Profile = () => {
<div class="fixed tr bg-emerald-100 m-2 p-4 rounded-xl border-2 shadow-md shadow-black"> <div class="fixed tr bg-emerald-100 m-2 p-4 rounded-xl border-2 shadow-md shadow-black">
Name:{" "} Name:{" "}
<input <input
value={profile()?.data?.name ?? ""} value={name()}
onChange={(e) => { onChange={(e) => {
dialogRef.close(); dialogRef.close();
void api.setName.post({ name: e.target.value }); setName(e.target.value);
}} }}
class="bg-emerald-200 border-1.5 rounded-full px-4" class="bg-emerald-200 border-1.5 rounded-full px-4"
/> />

View File

@@ -1,7 +1,7 @@
import { Component, Suspense } from "solid-js"; import { Component, Suspense } from "solid-js";
import type { Card } from "@games/shared/cards"; import type { Card } from "@games/shared/cards";
import { Clickable, Sizable, Stylable } from "./toolbox"; import { Clickable, Scalable, Stylable } from "./toolbox";
const cardToSvgFilename = (card: Card) => { const cardToSvgFilename = (card: Card) => {
if (card.kind == "joker") { if (card.kind == "joker") {
@@ -17,7 +17,12 @@ const cardToSvgFilename = (card: Card) => {
}`; }`;
}; };
export const CARD_RATIO = 1.456730769;
export const BASE_CARD_WIDTH = 100;
export default ((props) => { export default ((props) => {
const width = () => BASE_CARD_WIDTH * (props.scale ?? 1);
const height = () => width() * CARD_RATIO;
return ( return (
<Suspense> <Suspense>
<img <img
@@ -25,8 +30,8 @@ export default ((props) => {
draggable={false} draggable={false}
class={props.class} class={props.class}
style={props.style} style={props.style}
width={props.width ?? "100px"} width={`${width()}px`}
height={props.height} height={`${height()}px`}
src={ src={
props.face == "down" props.face == "down"
? "/views/back.svg" ? "/views/back.svg"
@@ -45,5 +50,5 @@ export default ((props) => {
) & ) &
Stylable & Stylable &
Clickable & Clickable &
Sizable Scalable
>; >;

View File

@@ -1,8 +1,9 @@
import type { Hand } from "@games/shared/cards"; import type { Hand } from "@games/shared/cards";
import { For } from "solid-js"; import { For } from "solid-js";
import Card from "./Card"; import Card from "./Card";
import { Stylable } from "./toolbox";
export default (props: { handCount: number }) => { export default (props: { handCount: number } & Stylable) => {
return ( return (
<For each={Array(props.handCount)}> <For each={Array(props.handCount)}>
{(_, i) => { {(_, i) => {
@@ -10,16 +11,16 @@ export default (props: { handCount: number }) => {
return ( return (
<Card <Card
face="down" face="down"
width="40px" scale={0.4}
style={{ style={{
"margin-left": "-10px", "margin-left": "-12px",
"margin-right": "-10px", "margin-right": "-12px",
transform: `rotate(${ transform: `translate(0px, ${Math.pow(
midOffset * 0.2 Math.abs(midOffset),
}rad) translate(0px, ${ 2
2 ** Math.abs(midOffset) * 2 )}px) rotate(${midOffset * 0.12}rad)`,
}px)`, "min-width": "40px",
"box-shadow": "-4px 4px 4px rgba(0, 0, 0, 0.7)", "box-shadow": "-4px 4px 6px rgba(0, 0, 0, 0.6)",
}} }}
/> />
); );

View File

@@ -1,72 +0,0 @@
import { Accessor, createContext, For, useContext } from "solid-js";
import type {
SimpleAction,
SimplePlayerView,
} from "@games/shared/games/simple";
import { me, profile } from "~/profile";
import Hand from "./Hand";
import Pile from "./Pile";
import { TableContext } from "./Table";
import { Portal } from "solid-js/web";
import FannedHand from "./FannedHand";
export const GameContext = createContext<{
view: Accessor<SimplePlayerView>;
submitAction: (action: SimpleAction) => any;
}>();
export default () => {
const table = useContext(TableContext)!;
const view = table.view as Accessor<SimplePlayerView>;
const submitAction = (action: SimpleAction) => table.sendWs({ action });
return (
<GameContext.Provider value={{ view, submitAction }}>
<Pile
count={view().deckCount}
class="cursor-pointer fixed center"
onClick={() => submitAction({ type: "draw" })}
/>
<Hand
class="fixed bc"
hand={view().myHand}
onClickCard={(card) => submitAction({ type: "discard", card })}
/>
<div class="absolute tc text-align-center">
It's{" "}
<span class="font-bold">
{view().playerTurn == me()
? "your"
: profile(view().playerTurn)()?.name + "'s"}
</span>{" "}
turn
</div>
<button
class="button fixed tl m-4 p-1"
onClick={() => {
table.sendWs({ quit: true });
}}
>
Quit
</button>
<For each={Object.entries(view().playerHandCounts)}>
{([playerKey, handCount], i) => (
<Portal
mount={document.getElementById(`player-${playerKey}`)!}
ref={(ref) => {
const midOffset =
i() + 0.5 - Object.values(view().playerHandCounts).length / 2;
ref.style = `position: absolute; display: flex; justify-content: center; top: 65%; transform: translate(${Math.abs(
midOffset * 0
)}px, 0px) rotate(${midOffset * 1}rad)`;
}}
>
<FannedHand handCount={handCount} />
</Portal>
)}
</For>
</GameContext.Provider>
);
};

View File

@@ -1,22 +1,63 @@
import { Component, For, JSX, Show } from "solid-js"; import { Component, createMemo, For, JSX, Show } from "solid-js";
import Card from "./Card"; import Card, { BASE_CARD_WIDTH, CARD_RATIO } from "./Card";
import { desaturate } from "color2k";
import { Clickable, Stylable } from "./toolbox"; import { Clickable, hashColor, Scalable, Stylable } from "./toolbox";
const cardOffset = 0.35; // Small offset for the stack effect
export default ((props) => { export default ((props) => {
const cards = createMemo(() => {
const numCards = Math.max(0, props.count - 1); // Subtract 1 for the top card
return Array.from({ length: numCards }, (_, i) => i).toReversed();
});
const width = () => BASE_CARD_WIDTH * (props.scale ?? 1);
const height = () => width() * CARD_RATIO;
const offset = () => cardOffset * (props.scale ?? 1);
return ( return (
<Show when={props.count > 0}> <Show when={props.count > 0}>
<Card <div
onClick={props.onClick} style={{
style={props.style} ...props.style,
class={props.class + " shadow-lg shadow-black"} }}
face="down" class={props.class}
/> >
<svg
class="absolute z-[-1]"
width={width() + cards().length * offset()}
height={height() + cards().length * offset()}
viewBox={`0 0 ${width() + cards().length * offset()} ${
height() + cards().length * offset()
}`}
xmlns="http://www.w3.org/2000/svg"
>
<For each={cards()}>
{(i) => {
const xOffset = (i * offset()) / 2;
const yOffset = i * offset();
const color = desaturate(hashColor(i), 0.9);
return (
<rect
x={xOffset}
y={yOffset}
width={width()}
height={height()}
rx="5" // Rounded corners
ry="5"
fill={color}
/>
);
}}
</For>
</svg>
<Card onClick={props.onClick} face="down" scale={props.scale} />
</div>
</Show> </Show>
); );
}) satisfies Component< }) satisfies Component<
{ {
count: number; count: number;
} & Stylable & } & Stylable &
Clickable Clickable &
Scalable
>; >;

View File

@@ -1,28 +1,18 @@
import { createSignal, useContext } from "solid-js"; import { onMount, useContext } from "solid-js";
import { playerColor, profile } from "~/profile"; import { playerColor } from "~/profile";
import { TableContext } from "./Table"; import { TableContext } from "./Table";
import { Stylable } from "./toolbox"; import { Stylable } from "./toolbox";
import { createObservable, createObservableWithInit } from "~/fn";
import { GameContext } from "./Game";
export default (props: { playerKey: string } & Stylable) => { export default (props: { playerKey: string } & Stylable) => {
const table = useContext(TableContext); const table = useContext(TableContext);
const playerReady =
table?.wsEvents
.filter((evt) => evt.playersReady != null)
.map((evt) => evt.playersReady![props.playerKey])
.thru((Evt) => createObservableWithInit(Evt, false)) ??
createSignal(false)[0];
const game = useContext(GameContext);
return ( return (
<div <div
id={`player-${props.playerKey}`} ref={(e) => table?.setPlayers(props.playerKey, { ref: e })}
style={{ style={{
...props.style, ...props.style,
"background-color": playerColor(props.playerKey), "background-color": playerColor(props.playerKey),
...(playerReady() && table?.view() == null ...(table?.view() == null && table?.players[props.playerKey].ready
? { ? {
border: "10px solid green", border: "10px solid green",
} }
@@ -30,8 +20,8 @@ export default (props: { playerKey: string } & Stylable) => {
}} }}
class={`${props.class} w-20 h-20 rounded-full flex justify-center items-center`} class={`${props.class} w-20 h-20 rounded-full flex justify-center items-center`}
> >
<p style={{ "font-size": "1em" }}> <p class="font-[1em] text-align-center">
{profile(props.playerKey)()?.name} {table?.players[props.playerKey].name}
</p> </p>
</div> </div>
); );

View File

@@ -1,5 +1,6 @@
import type { TWsIn, TWsOut } from "@games/server/src/table"; import type { TWsIn, TWsOut } from "@games/server/src/table";
import { fromPromise, Stream } from "kefir"; import games from "@games/shared/games/index";
import { pool, Property, Stream } from "kefir";
import { import {
Accessor, Accessor,
createContext, createContext,
@@ -7,70 +8,140 @@ import {
createSignal, createSignal,
For, For,
onCleanup, onCleanup,
onMount,
Setter,
Show, Show,
} from "solid-js"; } from "solid-js";
import { createStore, SetStoreFunction, Store } from "solid-js/store";
import { Dynamic } from "solid-js/web";
import api, { fromWebsocket } from "~/api"; import api, { fromWebsocket } from "~/api";
import { createObservable, createObservableWithInit, cx } from "~/fn"; import { createObservable, createSynced, cx, extractProperty } from "~/fn";
import { me, mePromise } from "~/profile"; import { me, name } from "~/profile";
import Game from "./Game"; import GAMES from "./games";
import Player from "./Player"; import Player from "./Player";
import games from "@games/shared/games/index";
type PlayerStore = Store<{
[key: string]: {
name: string;
ready: boolean;
ref?: HTMLDivElement;
};
}>;
export const TableContext = createContext<{ export const TableContext = createContext<{
view: Accessor<any>;
sendWs: (msg: TWsIn) => void;
wsEvents: Stream<TWsOut, any>; wsEvents: Stream<TWsOut, any>;
sendWs: (msg: TWsIn) => void;
tableRef: HTMLDivElement;
gameConfig: Accessor<any>;
setGameConfig: Setter<any>;
players: PlayerStore;
setPlayers: SetStoreFunction<PlayerStore>;
view: Accessor<any>;
}>(); }>();
export default (props: { tableKey: string }) => { export default (props: { tableKey: string }) => {
const wsPromise = new Promise< // #region Websocket declaration
ReturnType<ReturnType<typeof api.ws>["subscribe"]> let ws: ReturnType<ReturnType<typeof api.ws>["subscribe"]> | undefined =
>((res) => { undefined;
const ws = api.ws(props).subscribe(); const wsEvents = pool<TWsOut, any>();
ws.on("open", () => res(ws)); const sendWs = (msg: TWsIn) => ws?.send(msg);
ws.on("error", () => res(ws));
// #endregion
// #region inbound table properties
const [players, setPlayers] = createStore<PlayerStore>({});
wsEvents
.thru(extractProperty("playersPresent"))
.onValue((P) =>
setPlayers(
Object.fromEntries(
P.map((p) => [
p,
p in players ? players[p] : { name: "", ready: false },
])
)
)
);
wsEvents.thru(extractProperty("playerNames")).onValue((P) =>
Object.entries(P)
.filter(([player]) => player in players)
.map(([player, name]) => setPlayers(player, "name", name))
);
wsEvents.thru(extractProperty("playersReady")).onValue((P) =>
Object.entries(P)
.filter(([player]) => player in players)
.map(([player, ready]) => setPlayers(player, "ready", ready))
);
// #endregion
// #region inbound game properties
const [gameConfig, setGameConfig] = createSynced({
ws: wsEvents.thru(extractProperty("gameConfig")) as Property<
{ game: string; players: string[] },
any
>,
sendWs: (gameConfig) => sendWs({ gameConfig }),
}); });
const view = wsEvents.thru(extractProperty("view")).thru(createObservable);
const sendWs = (msg: TWsIn) => wsPromise.then((ws) => ws.send(msg)); // #endregion
const wsEvents = fromPromise(wsPromise).flatMap((ws) =>
fromWebsocket<TWsOut>(ws)
);
onCleanup(() => wsPromise.then((ws) => ws.close()));
const presenceEvents = wsEvents.filter((evt) => evt.playersPresent != null);
const gameEvents = wsEvents.filter((evt) => evt.view !== undefined);
const players = createObservableWithInit<string[]>(
presenceEvents.map((evt) => evt.playersPresent!),
[]
);
const [ready, setReady] = createSignal(false); const [ready, setReady] = createSignal(false);
mePromise.then(
(me) =>
me &&
wsEvents
.filter((evt) => evt.playersReady !== undefined)
.map((evt) => evt.playersReady?.[me] ?? false)
.onValue(setReady)
);
createEffect(() => sendWs({ ready: ready() })); onMount(() => {
const view = createObservable(gameEvents.map((evt) => evt.view)); ws = api.ws(props).subscribe();
ws.on("open", () => {
wsEvents.plug(fromWebsocket<TWsOut>(ws));
// TODO: these need to be in a tracking scope to be disposed
createEffect(() => sendWs({ ready: ready() }));
createEffect(() => sendWs({ name: name() }));
});
onCleanup(() => ws?.close());
});
const GamePicker = () => {
return (
<div class="absolute tc mt-8 flex gap-4">
<select value={gameConfig()?.game}>
<For each={Object.entries(games)}>
{([gameId]) => <option value={gameId}>{gameId}</option>}
</For>
</select>
<button onClick={() => setReady((prev) => !prev)} class="button p-1 ">
{ready() ? "Not Ready" : "Ready"}
</button>
</div>
);
};
let tableRef!: HTMLDivElement;
return ( return (
<TableContext.Provider <TableContext.Provider
value={{ value={{
sendWs,
wsEvents, wsEvents,
sendWs,
tableRef,
players,
setPlayers,
gameConfig,
setGameConfig,
view, view,
}} }}
> >
{/* Player avatars around the table */}
<div class="flex justify-around p-t-14"> <div class="flex justify-around p-t-14">
<For each={players().filter((p) => p != me())}> <For each={gameConfig()?.players.filter((p) => p != me())}>
{(player, i) => { {(player, i) => {
const verticalOffset = () => { const verticalOffset = () => {
const N = players().length - 1; const N = gameConfig()!.players.length - 1;
const x = Math.abs((2 * i() + 1) / (N * 2) - 0.5); const x = Math.abs((2 * i() + 1) / (N * 2) - 0.5);
const y = Math.sqrt(1 - x * x); const y = Math.sqrt(1 - x * x);
return 1 - y; return 1 - y;
@@ -86,8 +157,10 @@ export default (props: { tableKey: string }) => {
}} }}
</For> </For>
</div> </div>
{/* The table body itself */}
<div <div
id="table" ref={tableRef}
class={cx( class={cx(
"fixed", "fixed",
@@ -101,32 +174,26 @@ export default (props: { tableKey: string }) => {
"top-40", "top-40",
"bottom-20", "bottom-20",
"left-10", "left-[2%]",
"right-10" "right-[2%]"
)} )}
style={{ style={{
"border-radius": "50%", "border-radius": "50%",
}} }}
> >
<Show when={view() == null}> <Show when={view() == null}>
<div class="absolute tc mt-8 flex gap-4"> <GamePicker />
<select>
<For each={Object.entries(games)}>
{([gameId, game]) => <option value={gameId}>{gameId}</option>}
</For>
</select>
<button
onClick={() => setReady((prev) => !prev)}
class="button p-1 "
>
{ready() ? "Not Ready" : "Ready"}
</button>
</div>
</Show> </Show>
</div> </div>
<Show when={view() != null}>
<Game /> {/* The game being played */}
</Show> <Dynamic
component={
gameConfig()?.game ?? "" in GAMES
? GAMES[gameConfig()!.game as keyof typeof GAMES]
: undefined
}
/>
</TableContext.Provider> </TableContext.Provider>
); );
}; };

View File

@@ -0,0 +1,5 @@
import simple from "./simple";
export default {
simple,
};

View File

@@ -0,0 +1,149 @@
import type {
SimpleAction,
SimplePlayerView,
} from "@games/shared/games/simple";
import { Accessor, createEffect, For, Show, useContext } from "solid-js";
import { Portal } from "solid-js/web";
import { me } from "~/profile";
import { createObservable, extractProperty } from "../../fn";
import FannedHand from "../FannedHand";
import Hand from "../Hand";
import Pile from "../Pile";
import { TableContext } from "../Table";
export default () => {
const table = useContext(TableContext)!;
const view = table.view as Accessor<SimplePlayerView>;
const Configuration = () => (
<Show when={view() == null}>
<Portal mount={table.tableRef}>
<div class="absolute center grid grid-cols-2 gap-col-2 text-xl">
<label for="allow discards" style={{ "text-align": "right" }}>
Allow discards
</label>
<input
type="checkbox"
id="allow discards"
style={{ width: "50px" }}
checked={table.gameConfig()?.["can discard"] ?? false}
onChange={(evt) =>
table.setGameConfig({
...table.gameConfig(),
"can discard": evt.target.checked,
})
}
/>
<label for="to win" style={{ "text-align": "right" }}>
Cards to win
</label>
<input
type="number"
id="to win"
style={{
"text-align": "center",
width: "50px",
color: "var(--yellow)",
}}
value={table.gameConfig()["cards to win"]}
onChange={(evt) =>
table.setGameConfig({
...table.gameConfig(),
"cards to win": Number.parseInt(evt.target.value),
})
}
/>
</div>
</Portal>
</Show>
);
const submitAction = (action: SimpleAction) => table.sendWs({ action });
const ActiveGame = () => (
<Show when={view() != null}>
{/* Main pile in the middle of the table */}
<Pile
count={view().deckCount}
scale={0.8}
class="cursor-pointer fixed center"
onClick={() => submitAction({ type: "draw" })}
/>
{/* Your own hand */}
<Hand
class="fixed bc"
hand={view().myHand}
onClickCard={(card) => submitAction({ type: "discard", card })}
/>
{/* Other players' hands */}
<For
each={Object.entries(view().playerHandCounts).filter(
([key, _]) => key in table.players
)}
>
{([playerKey, handCount], i) => (
<Portal
mount={table.players[playerKey].ref}
ref={(ref) => {
const midOffset =
i() + 0.5 - Object.values(view().playerHandCounts).length / 2;
ref.style = `position: absolute; display: flex; justify-content: center; top: 65%;`;
}}
>
<FannedHand handCount={handCount} />
</Portal>
)}
</For>
{/* Turn indicator */}
<div
class="absolute tc text-align-center"
style={{
"background-color":
view().playerTurn == me() ? "var(--yellow)" : "transparent",
color: view().playerTurn == me() ? "var(--dark)" : "var(--light)",
}}
>
It's{" "}
<span class="font-bold">
{view().playerTurn == me()
? "your"
: table.players[view().playerTurn].name + "'s"}
</span>{" "}
turn
</div>
{/* Quit button */}
<button
class="button fixed tl m-4 p-1"
onClick={() => {
table.sendWs({ quit: true });
}}
>
Quit
</button>
</Show>
);
const results = table.wsEvents
.thru(extractProperty("results"))
.thru(createObservable);
const Results = () => (
<Show when={results() != null}>
<span class="bg-[var(--light)] text-[var(--dark)] rounded-[24px] border-2 border-[var(--dark)] absolute center p-4 shadow-lg text-[4em] text-center">
{table.players[results()!].name} won!
</span>
</Show>
);
return (
<>
<Configuration />
<ActiveGame />
<Results />
</>
);
};

View File

@@ -1,3 +1,4 @@
import hash, { NotUndefined } from "object-hash";
import { JSX } from "solid-js"; import { JSX } from "solid-js";
export type Stylable = { export type Stylable = {
@@ -15,7 +16,8 @@ export type Clickable = {
| undefined; | undefined;
}; };
export type Sizable = { export type Scalable = {
width?: string; scale?: number;
height?: string;
}; };
export const hashColor = (obj: NotUndefined) => `#${hash(obj).substring(0, 6)}`;

View File

@@ -1,5 +1,8 @@
import { Observable } from "kefir"; import { createLatest } from "@solid-primitives/memo";
import { Accessor, createSignal } from "solid-js"; import { Observable, Property, Stream } from "kefir";
import { Accessor, createEffect, createSignal } from "solid-js";
import { createStore } from "solid-js/store";
import type { ExtractPropertyType, UnionKeys } from "@games/shared/types";
declare global { declare global {
interface Array<T> { interface Array<T> {
@@ -37,3 +40,31 @@ export const createObservableWithInit = <T>(
}; };
export const cx = (...classes: string[]) => classes.join(" "); export const cx = (...classes: string[]) => classes.join(" ");
export const createObservableStore =
<T extends object = {}>(init: T) =>
(obs: Observable<T, any>) => {
const [store, setStore] = createStore<T>(init);
obs.onValue((val) => setStore(val));
return store;
};
export const extractProperty =
<T extends object, P extends UnionKeys<T>>(property: P) =>
(obs: Observable<T, any>): Property<ExtractPropertyType<T, P>, any> =>
obs
.filter((o) => property in o)
.map(
(o) => (o as { [K in P]: any })[property] as ExtractPropertyType<T, P>
)
.toProperty();
export const createSynced = <T>(p: {
ws: Stream<T, any>;
sendWs: (o: T) => void;
}) => {
const [local, setLocal] = createSignal<T>();
const remote = createObservable(p.ws.toProperty());
createEffect(() => local() !== undefined && p.sendWs(local()!));
return [createLatest([local, remote]), setLocal] as const;
};

View File

@@ -1,26 +1,14 @@
import { createResource, Resource } from "solid-js"; import { makePersisted } from "@solid-primitives/storage";
import { ApiType } from "./fn";
import api from "./api";
import hash from "object-hash"; import hash from "object-hash";
import { createResource, createSignal } from "solid-js";
import api from "./api";
export const mePromise = api.whoami.post().then((r) => r.data); export const mePromise = api.whoami.post().then((r) => r.data);
export const [me] = createResource(() => mePromise); export const [me] = createResource(() => mePromise);
const playerProfiles: {
[humanKey: string]: Resource<ApiType<typeof api.profile.get>>;
} = {};
export const profile = (humanKey: string) => {
if (!(humanKey in playerProfiles)) {
playerProfiles[humanKey] = createResource(() =>
api.profile
.get({ query: { otherHumanKey: humanKey } })
.then((r) => r.data)
)[0];
}
return playerProfiles[humanKey];
};
export const playerColor = (humanKey: string) => export const playerColor = (humanKey: string) =>
"#" + hash(humanKey).substring(0, 6); "#" + hash(humanKey).substring(0, 6);
export const [name, setName] = makePersisted(createSignal("__name__"), {
name: "name",
});

View File

@@ -3,38 +3,44 @@ import { A } from "@solidjs/router";
export default () => { export default () => {
const randomTablePath = `/t/abcd`; const randomTablePath = `/t/abcd`;
return ( return (
<div class="flex flex-col absolute center"> <>
<h1>Welcome to games.drm.dev!</h1> <div class="flex flex-col absolute center">
<p> <h1>Welcome to games.drm.dev!</h1>
This website is a real-time multiplayer platform for playing <p>
card games online. This website is a real-time multiplayer platform for playing card
</p> games online.
<br /> </p>
<p> <br />
Games happen at <strong>tables</strong>. A table is any url of <p>
the form{" "} Games happen at <strong>tables</strong>. A table is any url of the
<span class="font-mono text-[var(--light-purple)]"> form{" "}
games.drm.dev/t/ <span class="font-mono text-[var(--light-purple)]">
<span class="text-[var(--yellow)]">*</span> games.drm.dev/t/
</span> <span class="text-[var(--yellow)]">*</span>
</p> </span>
<br /> </p>
<p> <br />
Go to the same one as your friend and you will find them there! <p>Go to the same one as your friend and you will find them there!</p>
</p> <br />
<br /> <p>
<p> If you have a table key in mind (the part after /t/), then plug it in
If you have a table key in mind (the part after /t/), then plug to your URL bar! Or, here's a couple links to random tables:
it in to your URL bar! Or, here's a couple links to random </p>
tables: <br />
</p> <p>
<br /> With no one in it:{" "}
<p> <A href={randomTablePath}>
With no one in it:{" "} https://www.games.drm.dev{randomTablePath}
<A href={randomTablePath}> </A>
https://www.games.drm.dev{randomTablePath} </p>
</A> </div>
</p> <a href="https://brainmade.org" target="_blank">
</div> <img
src="https://brainmade.org/white-logo.svg"
class="fixed bl m-2"
width="80"
/>
</a>
</>
); );
}; };

View File

@@ -15,11 +15,11 @@ body {
} }
h1 { h1 {
font-size: 48px; font-size: 2em;
font-family: Garamond; font-family: Garamond;
} }
p { p {
font-size: 24px; font-size: 1em;
font-family: Garamond; font-family: Garamond;
} }
a { a {

View File

@@ -1,19 +0,0 @@
-- CreateTable
CREATE TABLE "Game" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT NOT NULL,
"rules" TEXT
);
-- CreateTable
CREATE TABLE "Instance" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"gameId" INTEGER NOT NULL,
"gameState" JSONB NOT NULL,
CONSTRAINT "Instance_gameId_fkey" FOREIGN KEY ("gameId") REFERENCES "Game" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "Game_name_key" ON "Game"("name");

View File

@@ -1,34 +0,0 @@
/*
Warnings:
- The primary key for the `Game` table will be changed. If it partially fails, the table could be left without primary key constraint.
- You are about to drop the column `id` on the `Game` table. All the data in the column will be lost.
- You are about to drop the column `gameId` on the `Instance` table. All the data in the column will be lost.
- Added the required column `key` to the `Game` table without a default value. This is not possible if the table is not empty.
- Added the required column `gameKey` to the `Instance` table without a default value. This is not possible if the table is not empty.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Game" (
"key" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT NOT NULL,
"rules" TEXT
);
INSERT INTO "new_Game" ("createdAt", "name", "rules", "updatedAt") SELECT "createdAt", "name", "rules", "updatedAt" FROM "Game";
DROP TABLE "Game";
ALTER TABLE "new_Game" RENAME TO "Game";
CREATE TABLE "new_Instance" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"gameKey" TEXT NOT NULL,
"gameState" JSONB NOT NULL,
CONSTRAINT "Instance_gameKey_fkey" FOREIGN KEY ("gameKey") REFERENCES "Game" ("key") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Instance" ("gameState", "id") SELECT "gameState", "id" FROM "Instance";
DROP TABLE "Instance";
ALTER TABLE "new_Instance" RENAME TO "Instance";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -1,20 +0,0 @@
/*
Warnings:
- The primary key for the `Instance` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Instance" (
"id" TEXT NOT NULL PRIMARY KEY,
"gameKey" TEXT NOT NULL,
"gameState" JSONB NOT NULL,
CONSTRAINT "Instance_gameKey_fkey" FOREIGN KEY ("gameKey") REFERENCES "Game" ("key") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Instance" ("gameKey", "gameState", "id") SELECT "gameKey", "gameState", "id" FROM "Instance";
DROP TABLE "Instance";
ALTER TABLE "new_Instance" RENAME TO "Instance";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -1,28 +0,0 @@
/*
Warnings:
- Added the required column `createdByKey` to the `Instance` table without a default value. This is not possible if the table is not empty.
*/
-- CreateTable
CREATE TABLE "Human" (
"key" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL
);
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Instance" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdByKey" TEXT NOT NULL,
"gameKey" TEXT NOT NULL,
"gameState" JSONB NOT NULL,
CONSTRAINT "Instance_createdByKey_fkey" FOREIGN KEY ("createdByKey") REFERENCES "Human" ("key") ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT "Instance_gameKey_fkey" FOREIGN KEY ("gameKey") REFERENCES "Game" ("key") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Instance" ("gameKey", "gameState", "id") SELECT "gameKey", "gameState", "id" FROM "Instance";
DROP TABLE "Instance";
ALTER TABLE "new_Instance" RENAME TO "Instance";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -1,12 +0,0 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Human" (
"key" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL DEFAULT ''
);
INSERT INTO "new_Human" ("key", "name") SELECT "key", "name" FROM "Human";
DROP TABLE "Human";
ALTER TABLE "new_Human" RENAME TO "Human";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -1,45 +0,0 @@
/*
Warnings:
- The required column `token` was added to the `Human` table with a prisma-level default value. This is not possible if the table is not empty. Please add this column as optional, then populate it before making it required.
*/
-- CreateTable
CREATE TABLE "_HumanToInstance" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL,
CONSTRAINT "_HumanToInstance_A_fkey" FOREIGN KEY ("A") REFERENCES "Human" ("key") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_HumanToInstance_B_fkey" FOREIGN KEY ("B") REFERENCES "Instance" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Human" (
"key" TEXT NOT NULL PRIMARY KEY,
"token" TEXT NOT NULL,
"name" TEXT NOT NULL DEFAULT '__name__',
"lastActive" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO "new_Human" ("key", "name") SELECT "key", "name" FROM "Human";
DROP TABLE "Human";
ALTER TABLE "new_Human" RENAME TO "Human";
CREATE UNIQUE INDEX "Human_token_key" ON "Human"("token");
CREATE TABLE "new_Instance" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdByKey" TEXT NOT NULL,
"gameKey" TEXT NOT NULL,
"gameState" JSONB NOT NULL,
CONSTRAINT "Instance_gameKey_fkey" FOREIGN KEY ("gameKey") REFERENCES "Game" ("key") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Instance" ("createdByKey", "gameKey", "gameState", "id") SELECT "createdByKey", "gameKey", "gameState", "id" FROM "Instance";
DROP TABLE "Instance";
ALTER TABLE "new_Instance" RENAME TO "Instance";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
-- CreateIndex
CREATE UNIQUE INDEX "_HumanToInstance_AB_unique" ON "_HumanToInstance"("A", "B");
-- CreateIndex
CREATE INDEX "_HumanToInstance_B_index" ON "_HumanToInstance"("B");

View File

@@ -1,15 +0,0 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Instance" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdByKey" TEXT NOT NULL,
"gameKey" TEXT NOT NULL,
"gameState" JSONB,
CONSTRAINT "Instance_gameKey_fkey" FOREIGN KEY ("gameKey") REFERENCES "Game" ("key") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_Instance" ("createdByKey", "gameKey", "gameState", "id") SELECT "createdByKey", "gameKey", "gameState", "id" FROM "Instance";
DROP TABLE "Instance";
ALTER TABLE "new_Instance" RENAME TO "Instance";
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -1,3 +0,0 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "sqlite"

View File

@@ -1,37 +0,0 @@
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
model Game {
key String @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
rules String?
instances Instance[]
}
model Human {
key String @id @default(cuid())
token String @unique @default(cuid())
name String @default("__name__")
lastActive DateTime @default(now())
playingInstances Instance[]
}
model Instance {
id String @id @default(cuid())
createdByKey String
gameKey String
players Human[]
game Game @relation(fields: [gameKey], references: [key])
gameState Json?
}

View File

@@ -1,15 +1,8 @@
{ {
"name": "@games/server", "name": "@games/server",
"scripts": { "scripts": {
"dev": "concurrently 'pnpm run devserver' 'pnpm run dbstudio'", "dev": "NODE_ENV=development PORT=5001 bun run --hot src/index.ts",
"devserver": "NODE_ENV=development PORT=5001 bun run --hot src/index.ts", "start": "NODE_ENV=production bun run src/index.ts"
"dbstudio": "pnpm dlx prisma studio --browser none",
"dbdeploy": "pnpm dlx prisma migrate deploy",
"dbtypes": "pnpm dlx prisma generate",
"dbsync": "pnpm dlx prisma migrate dev",
"dbwipe": "pnpm dlx prisma migrate reset",
"prod": "NODE_ENV=production bun run src/index.ts",
"start": "concurrently 'pnpm run prod' 'pnpm run dbstudio'"
}, },
"dependencies": { "dependencies": {
"@elysiajs/cors": "^1.3.3", "@elysiajs/cors": "^1.3.3",

View File

@@ -1,6 +0,0 @@
import path from "node:path";
import { defineConfig } from "prisma/config";
export default defineConfig({
schema: path.join("db", "schema.prisma"),
});

View File

@@ -1,81 +1,28 @@
import { Game } from "@games/shared/games";
import { Human } from "@prisma/client";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { Elysia, t } from "elysia"; import { Elysia } from "elysia";
import { combine } from "kefir"; import { generateTokenAndKey, resolveToken } from "./human";
import Bus from "kefir-bus";
import db from "./db";
import { liveTable, WsIn, WsOut } from "./table";
import { err } from "./logging"; import { err } from "./logging";
import { liveTable, WsIn, WsOut } from "./table";
export const WS = Bus< import type { ExtractPropertyType, UnionKeys } from "@games/shared/types";
{
type: "open" | "message" | "error" | "close";
humanKey: string;
tableKey: string;
},
unknown
>();
const api = new Elysia({ prefix: "/api" }) const api = new Elysia({ prefix: "/api" })
.post("/whoami", async ({ cookie: { token } }) => { .post("/whoami", async ({ cookie: { token } }) => {
let human: Human | null; let key: string | undefined;
if ( if (token.value == null || (key = resolveToken(token.value)) == null) {
token.value == null || const [newToken, newKey] = generateTokenAndKey();
(human = await db.human.findUnique({
where: {
token: token.value,
},
})) == null
) {
human = await db.human.create({
data: {},
});
token.set({ token.set({
value: human.token, value: newToken,
expires: dayjs().add(1, "year").toDate(), expires: dayjs().add(1, "year").toDate(),
httpOnly: true, httpOnly: true,
}); });
return newKey;
} }
return key;
return human.key;
}) })
.derive(async ({ cookie: { token }, status }) => { .derive(async ({ cookie: { token }, status }) => {
const humanKey = await db.human const humanKey = token.value && resolveToken(token.value);
.findUnique({
where: { token: token.value },
})
.then((human) => human?.key);
return humanKey != null ? { humanKey } : status(401); return humanKey != null ? { humanKey } : status(401);
}) })
.post(
"/setName",
({ body: { name }, humanKey }) =>
db.human.update({
where: {
key: humanKey,
},
data: {
name,
},
}),
{
body: t.Object({
name: t.String(),
}),
}
)
.get("/profile", ({ humanKey, query: { otherHumanKey } }) =>
db.human
.findFirst({ where: { key: otherHumanKey ?? humanKey } })
.then((human) => {
if (human == null) {
return null;
}
const { token, ...safeProfile } = human;
return safeProfile;
})
)
.ws("/ws/:tableKey", { .ws("/ws/:tableKey", {
body: WsIn, body: WsIn,
response: WsOut, response: WsOut,
@@ -98,6 +45,7 @@ const api = new Elysia({ prefix: "/api" })
...table.outputs.global, ...table.outputs.global,
...(table.outputs.player[humanKey] ?? {}), ...(table.outputs.player[humanKey] ?? {}),
}).forEach(([type, stream]) => }).forEach(([type, stream]) =>
// @ts-ignore
stream.onValue((v) => send({ [type]: v })) stream.onValue((v) => send({ [type]: v }))
); );
}, },

View File

@@ -1,5 +0,0 @@
"use server";
import { PrismaClient } from "@prisma/client";
export default new PrismaClient();

17
pkg/server/src/human.ts Normal file
View File

@@ -0,0 +1,17 @@
const tokenToHumanKey: { [token: string]: string } = {};
const playerKeys: Set<string> = new Set();
export const generateTokenAndKey = () => {
let token: string, key: string;
do {
[token, key] = [crypto.randomUUID(), crypto.randomUUID()];
tokenToHumanKey[token] = key;
playerKeys.add(key);
} while (!(token in tokenToHumanKey || playerKeys.has(key)));
return [token, key];
};
export const resolveToken = (token: string) =>
tokenToHumanKey[token] as string | undefined;
export const tokenExists = (token: string) => token in tokenToHumanKey;
export const keyExists = (key: string) => playerKeys.has(key);

View File

@@ -16,7 +16,7 @@ new Elysia()
}) })
) )
.onError(({ error }) => console.error(error)) // .onError(({ error }) => console.error(error))
.get("/ping", () => "pong") .get("/ping", () => "pong")
.use(api) .use(api)

View File

@@ -10,7 +10,7 @@ export const log = (value: unknown) => LogBus.emit(value);
export const err = (value: unknown) => export const err = (value: unknown) =>
LogBus.emitEvent({ type: "error", value }); LogBus.emitEvent({ type: "error", value });
LogStream.log(); // LogStream.log();
LogStream.onError((err) => { // LogStream.onError((err) => {
console.error(err); // console.error(err);
}); // });

View File

@@ -1,5 +1,6 @@
import GAMES, { Game, GameKey } from "@games/shared/games"; import GAMES, { Game, GameKey } from "@games/shared/games";
import { import {
invert,
isEmpty, isEmpty,
multiScan, multiScan,
partition, partition,
@@ -8,18 +9,29 @@ import {
ValueWithin, ValueWithin,
} from "@games/shared/kefirs"; } from "@games/shared/kefirs";
import { t } from "elysia"; import { t } from "elysia";
import { combine, Observable, pool, Property } from "kefir"; import { combine, constant, merge, Observable, pool, Property } from "kefir";
import Bus, { type Bus as TBus } from "kefir-bus"; import Bus, { type Bus as TBus } from "kefir-bus";
import { log } from "./logging"; import { log } from "./logging";
import simple from "@games/shared/games/simple";
export const WsOut = t.Object({ const DEFAULT_GAME_CONFIG = simple.defaultConfig;
playersPresent: t.Optional(t.Array(t.String())),
playersReady: t.Optional(t.Nullable(t.Record(t.String(), t.Boolean()))), export const WsOut = t.Union([
gameConfig: t.Optional(t.Any()), t.Object({ playersPresent: t.Array(t.String()) }),
view: t.Optional(t.Any()), t.Object({ playerNames: t.Record(t.String(), t.String()) }),
}); t.Object({ playersReady: t.Record(t.String(), t.Boolean()) }),
t.Object({
gameConfig: t.Object({ game: t.String(), players: t.Array(t.String()) }),
}),
t.Object({ view: t.Any() }),
t.Object({ results: t.Any() }),
]);
export type TWsOut = typeof WsOut.static; export type TWsOut = typeof WsOut.static;
export const WsIn = t.Union([ export const WsIn = t.Union([
t.Object({ name: t.String() }),
t.Object({
gameConfig: t.Object({ game: t.String(), players: t.Array(t.String()) }),
}),
t.Object({ ready: t.Boolean() }), t.Object({ ready: t.Boolean() }),
t.Object({ action: t.Any() }), t.Object({ action: t.Any() }),
t.Object({ quit: t.Literal(true) }), t.Object({ quit: t.Literal(true) }),
@@ -30,7 +42,8 @@ type Attributed = { humanKey: string };
type TablePayload< type TablePayload<
GameConfig = unknown, GameConfig = unknown,
GameView = unknown, GameView = unknown,
GameAction = unknown GameAction = unknown,
GameResult = unknown
> = { > = {
inputs: { inputs: {
connectionChanges: TBus< connectionChanges: TBus<
@@ -51,6 +64,8 @@ type TablePayload<
any any
>; >;
gameConfig: Property<GameConfig | null, any>; gameConfig: Property<GameConfig | null, any>;
results: Property<GameResult, any>;
playerNames: Property<{ [key: string]: string }, any>;
}; };
player: { player: {
[key: string]: { [key: string]: {
@@ -71,12 +86,18 @@ export const liveTable = <
}, },
GameState, GameState,
GameAction extends Attributed, GameAction extends Attributed,
GameView GameView,
GameResult
>( >(
key: string key: string
) => { ) => {
if (!(key in tables)) { if (!(key in tables)) {
const inputs: TablePayload<GameConfig, GameState, GameAction>["inputs"] = { const inputs: TablePayload<
GameConfig,
GameState,
GameAction,
GameResult
>["inputs"] = {
connectionChanges: Bus(), connectionChanges: Bus(),
messages: Bus(), messages: Bus(),
}; };
@@ -110,7 +131,7 @@ export const liveTable = <
.onValue(({ added, removed }) => { .onValue(({ added, removed }) => {
added.forEach((p) => { added.forEach((p) => {
playerStreams[p] = { playerStreams[p] = {
view: combine([gameState], [gameImpl], (a, b) => [a, b] as const) view: withGame(gameState)
.map( .map(
([state, game]) => ([state, game]) =>
state && (game.getView({ state, humanKey: p }) as GameView) state && (game.getView({ state, humanKey: p }) as GameView)
@@ -123,29 +144,43 @@ export const liveTable = <
}); });
}); });
const { ready, action, quit } = partition( const {
["ready", "action", "quit"], name,
ready,
action,
quit,
gameConfig: clientGameConfigs,
} = partition(
["name", "ready", "action", "quit", "gameConfig"],
messages messages
) as unknown as { ) as unknown as {
// yuck // yuck
name: Observable<Attributed & { name: string }, any>;
ready: Observable<Attributed & { ready: boolean }, any>; ready: Observable<Attributed & { ready: boolean }, any>;
action: Observable<Attributed & { action: GameAction }, any>; action: Observable<Attributed & { action: GameAction }, any>;
quit: Observable<Attributed, any>; quit: Observable<Attributed, any>;
gameConfig: Observable<Attributed & { gameConfig: GameConfig }, any>;
}; };
const gameEnds = quit.map((_) => null); const gameEnds = quit.map((_) => null);
const gameIsActivePool = pool<boolean, any>();
const gameIsActive = merge([
constant(false),
gameIsActivePool,
]).toProperty();
const playersReady = multiScan( const playersReady = multiScan(
null as { null as {
[key: string]: boolean; [key: string]: boolean;
} | null, } | null,
[ [
playersPresent, // TODO: filter to only outside active games playersPresent.filterBy(invert(gameIsActive)),
(prev, players: ValueWithin<typeof playersPresent>) => (prev, players: ValueWithin<typeof playersPresent>) =>
Object.fromEntries(players.map((p) => [p, prev?.[p] ?? false])), Object.fromEntries(players.map((p) => [p, prev?.[p] ?? false])),
], ],
[ [
ready, // TODO: filter to only outside active games ready.filterBy(invert(gameIsActive)),
(prev, evt: ValueWithin<typeof ready>) => (prev, evt: ValueWithin<typeof ready>) =>
prev?.[evt.humanKey] != null prev?.[evt.humanKey] != null
? { ? {
@@ -162,6 +197,13 @@ export const liveTable = <
] ]
).toProperty(); ).toProperty();
const playerNames = name
.scan(
(prev, n) => ({ ...prev, [n.humanKey]: n.name }),
{} as { [key: string]: string }
)
.toProperty();
const gameStarts = playersReady const gameStarts = playersReady
.filter( .filter(
(pr) => (pr) =>
@@ -175,24 +217,31 @@ export const liveTable = <
const gameImpl = gameConfig const gameImpl = gameConfig
.filter((cfg) => cfg.game in GAMES) .filter((cfg) => cfg.game in GAMES)
.map((config) => GAMES[config.game as GameKey](config)) .map((config) => GAMES[config.game as GameKey].impl(config))
.toProperty(); .toProperty();
const withGame = <T>(obs: Observable<T, any>) =>
combine([obs], [gameImpl], (o, game) => [o, game] as const);
const resultsPool = pool<GameResult | null, any>();
const results = merge([constant(null), resultsPool]).toProperty();
const gameState = multiScan( const gameState = multiScan(
null as GameState | null, null as GameState | null,
[ [
// initialize game state when started // initialize game state when started
gameImpl.sampledBy(gameStarts), gameImpl.sampledBy(gameStarts).filterBy(invert(gameIsActive)),
(prev, game: ValueWithin<typeof gameImpl>) => (prev, game: ValueWithin<typeof gameImpl>) =>
prev || (game.init() as GameState), prev || (game.init() as GameState),
], ],
[ [
combine([action], [gameImpl], (act, game) => [act, game] as const), // handle actions from players
action.filterBy(gameIsActive).thru(withGame),
( (
prev, prev,
[{ action, humanKey }, game]: [ [{ action, humanKey }, game]: [
Attributed & { action: GameAction }, Attributed & { action: GameAction },
Game ReturnType<Game["impl"]>
] ]
) => ) =>
prev && prev &&
@@ -202,28 +251,41 @@ export const liveTable = <
humanKey, humanKey,
}) as GameState), }) as GameState),
], ],
[quit, () => null] [results.filterBy(gameIsActive), () => null], // handle game ending criteria
[quit, () => null] // handle players leaving the room
).toProperty(); ).toProperty();
const gameIsActive = gameState resultsPool.plug(
.map((gs) => gs != null) gameState
.skipDuplicates() .filter((state) => state != null)
.toProperty(); .thru(withGame)
.map(([state, game]) => game.getResult(state) as unknown as GameResult)
.filter((result) => result != null)
.merge(quit.map(() => null))
);
gameIsActivePool.plug(
combine([gameState, results])
.map(([state, result]) => state != null && result == null)
.skipDuplicates()
.toProperty()
);
gameConfigPool.plug( gameConfigPool.plug(
multiScan( multiScan(
{ DEFAULT_GAME_CONFIG,
game: "simple",
players: [] as string[],
},
[ [
playersPresent.filterBy(gameIsActive.map((active) => !active)), playersPresent.filterBy(gameIsActive.thru(invert)),
(prev, players) => ({ (prev, players) => ({
...prev, ...prev,
players, players,
}), }),
],
[
clientGameConfigs.filterBy(gameIsActive.thru(invert)),
// @ts-ignore
(prev, { gameConfig }) => ({ ...gameConfig, players: prev.players }),
] ]
// TODO: Add player defined config changes
) as unknown as Observable<GameConfig, any> ) as unknown as Observable<GameConfig, any>
); );
@@ -234,6 +296,8 @@ export const liveTable = <
playersPresent, playersPresent,
playersReady, playersReady,
gameConfig, gameConfig,
results,
playerNames,
}, },
player: playerStreams, player: playerStreams,
}, },

View File

@@ -1,21 +1,30 @@
import simple from "./simple"; import simple from "./simple";
export type Game< export type Game<
S = unknown, S = unknown, // state
A = unknown, A = unknown, // action
E extends { error: any } = { error: any }, E = unknown, // error
V = unknown V = unknown, // view
R = unknown, // results
C extends { game: string; players: string[] } = {
game: string;
players: string[];
}
> = { > = {
title: string; impl: (config: C) => {
rules: string; title: string;
init: () => S; rules: string;
resolveAction: (p: { state: S; action: A; humanKey: string }) => S | E; init: () => S;
getView: (p: { state: S; humanKey: string }) => V; resolveAction: (p: { state: S; action: A; humanKey: string }) => S | E;
resolveQuit: (p: { state: S; humanKey: string }) => S; getView: (p: { state: S; humanKey: string }) => V;
resolveQuit: (p: { state: S; humanKey: string }) => S;
getResult: (state: S) => R | undefined;
};
defaultConfig: C;
}; };
export const GAMES: { export const GAMES: {
[key: string]: (config: { game: string; players: string[] }) => Game; [key: string]: Game;
} = { } = {
// renaissance, // renaissance,
simple, simple,

View File

@@ -1,10 +1,13 @@
import { Card, Hand, newDeck, Pile, shuffle, vCard } from "@games/shared/cards"; import { Card, Hand, newDeck, Pile, shuffle, vCard } from "@games/shared/cards";
import { heq } from "@games/shared/utils"; import { heq } from "@games/shared/utils";
import type { Game } from "."; import type { Game } from ".";
import { XOR } from "ts-xor";
export type SimpleConfiguration = { export type SimpleConfiguration = {
game: "simple"; game: "simple";
players: string[]; players: string[];
"can discard": boolean;
"cards to win": number;
}; };
// omniscient game state // omniscient game state
@@ -50,6 +53,16 @@ export const getSimplePlayerView = (
), ),
}); });
// type SimpleError = XOR<
// { "go away": string },
// { chill: string },
// { "ah ah": string }
// >;
type SimpleError = {
class: "go away" | "chill" | "ah ah";
message: string;
};
export const resolveSimpleAction = ({ export const resolveSimpleAction = ({
config, config,
state, state,
@@ -62,13 +75,18 @@ export const resolveSimpleAction = ({
humanKey: string; humanKey: string;
}): SimpleGameState => { }): SimpleGameState => {
const playerHand = state.playerHands[humanKey]; const playerHand = state.playerHands[humanKey];
if (playerHand == null) { if (playerHand == null) {
throw new Error( throw {
`${humanKey} is not a player in this game; they cannot perform actions` message: "You are not a part of this game!",
); class: "go away",
} satisfies SimpleError;
} }
if (humanKey != config.players[state.turnIdx]) { if (humanKey != config.players[state.turnIdx]) {
throw new Error(`It's not ${humanKey}'s turn!`); throw {
message: "It's not your turn!",
class: "chill",
} satisfies SimpleError;
} }
const numPlayers = Object.keys(state.playerHands).length; const numPlayers = Object.keys(state.playerHands).length;
@@ -87,6 +105,13 @@ export const resolveSimpleAction = ({
}; };
} else { } else {
// action.type == discard // action.type == discard
if (config["can discard"] == false) {
throw {
message: "You're not allowed to discard!",
class: "ah ah",
} satisfies SimpleError;
}
const cardIndex = playerHand.findIndex(heq(action.card)); const cardIndex = playerHand.findIndex(heq(action.card));
return { return {
deck: [action.card, ...state.deck], deck: [action.card, ...state.deck],
@@ -101,10 +126,16 @@ export const resolveSimpleAction = ({
} }
}; };
type SimpleError = { error: "whoops!" }; export type SimpleResult = string;
export default (config: SimpleConfiguration) => export default {
({ defaultConfig: {
game: "simple",
players: [],
"can discard": true,
"cards to win": 5,
},
impl: (config: SimpleConfiguration) => ({
title: "Simple", title: "Simple",
rules: "You can draw, or you can discard. Then your turn is up.", rules: "You can draw, or you can discard. Then your turn is up.",
init: () => newSimpleGameState(config), init: () => newSimpleGameState(config),
@@ -112,9 +143,16 @@ export default (config: SimpleConfiguration) =>
getView: ({ state, humanKey }) => getView: ({ state, humanKey }) =>
getSimplePlayerView(config, state, humanKey), getSimplePlayerView(config, state, humanKey),
resolveQuit: () => null, resolveQuit: () => null,
} satisfies Game< getResult: (state) =>
SimpleGameState, Object.entries(state.playerHands).find(
SimpleAction, ([_, hand]) => hand.length === config["cards to win"]
SimpleError, )?.[0],
SimplePlayerView }),
>); } satisfies Game<
SimpleGameState,
SimpleAction,
SimpleError,
SimplePlayerView,
SimpleResult,
SimpleConfiguration
>;

View File

@@ -50,3 +50,5 @@ export const setDiff = <T>(
}); });
export const set = <T>(arr: T[]) => new Set<T>(arr); export const set = <T>(arr: T[]) => new Set<T>(arr);
export const invert = <E>(obs: Observable<boolean, E>) => obs.map((o) => !o);

9
pkg/shared/types.ts Normal file
View File

@@ -0,0 +1,9 @@
export type UnionKeys<T> = T extends any ? keyof T : never;
export type ExtractPropertyType<
T,
P extends string | number | symbol
> = T extends {
[K in P]: any;
}
? T[P]
: never;

51
pnpm-lock.yaml generated
View File

@@ -20,12 +20,21 @@ importers:
'@elysiajs/eden': '@elysiajs/eden':
specifier: ^1.3.2 specifier: ^1.3.2
version: 1.3.3(elysia@1.3.20(exact-mirror@0.2.0(@sinclair/typebox@0.34.41))(file-type@21.0.0)(typescript@5.9.2)) version: 1.3.3(elysia@1.3.20(exact-mirror@0.2.0(@sinclair/typebox@0.34.41))(file-type@21.0.0)(typescript@5.9.2))
'@solid-primitives/memo':
specifier: ^1.4.3
version: 1.4.3(solid-js@1.9.9)
'@solid-primitives/scheduled': '@solid-primitives/scheduled':
specifier: ^1.5.2 specifier: ^1.5.2
version: 1.5.2(solid-js@1.9.9) version: 1.5.2(solid-js@1.9.9)
'@solid-primitives/storage':
specifier: ^4.3.3
version: 4.3.3(solid-js@1.9.9)
'@solidjs/router': '@solidjs/router':
specifier: ^0.15.3 specifier: ^0.15.3
version: 0.15.3(solid-js@1.9.9) version: 0.15.3(solid-js@1.9.9)
color2k:
specifier: ^2.0.3
version: 2.0.3
js-cookie: js-cookie:
specifier: ^3.0.5 specifier: ^3.0.5
version: 3.0.5 version: 3.0.5
@@ -587,11 +596,33 @@ packages:
resolution: {integrity: sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==} resolution: {integrity: sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==}
engines: {node: '>=18'} engines: {node: '>=18'}
'@solid-primitives/memo@1.4.3':
resolution: {integrity: sha512-CA+n9yaoqbYm+My5tY2RWb6EE16tVyehM4GzwQF4vCwvjYPAYk1JSRIVuMC0Xuj5ExD2XQJE5E2yAaKY2HTUsg==}
peerDependencies:
solid-js: ^1.6.12
'@solid-primitives/scheduled@1.5.2': '@solid-primitives/scheduled@1.5.2':
resolution: {integrity: sha512-/j2igE0xyNaHhj6kMfcUQn5rAVSTLbAX+CDEBm25hSNBmNiHLu2lM7Usj2kJJ5j36D67bE8wR1hBNA8hjtvsQA==} resolution: {integrity: sha512-/j2igE0xyNaHhj6kMfcUQn5rAVSTLbAX+CDEBm25hSNBmNiHLu2lM7Usj2kJJ5j36D67bE8wR1hBNA8hjtvsQA==}
peerDependencies: peerDependencies:
solid-js: ^1.6.12 solid-js: ^1.6.12
'@solid-primitives/storage@4.3.3':
resolution: {integrity: sha512-ACbNwMZ1s8VAvld6EUXkDkX/US3IhtlPLxg6+B2s9MwNUugwdd51I98LPEaHrdLpqPmyzqgoJe0TxEFlf3Dqrw==}
peerDependencies:
'@tauri-apps/plugin-store': '*'
solid-js: ^1.6.12
solid-start: '*'
peerDependenciesMeta:
'@tauri-apps/plugin-store':
optional: true
solid-start:
optional: true
'@solid-primitives/utils@6.3.2':
resolution: {integrity: sha512-hZ/M/qr25QOCcwDPOHtGjxTD8w2mNyVAYvcfgwzBHq2RwNqHNdDNsMZYap20+ruRwW4A3Cdkczyoz0TSxLCAPQ==}
peerDependencies:
solid-js: ^1.6.12
'@solidjs/router@0.15.3': '@solidjs/router@0.15.3':
resolution: {integrity: sha512-iEbW8UKok2Oio7o6Y4VTzLj+KFCmQPGEpm1fS3xixwFBdclFVBvaQVeibl1jys4cujfAK5Kn6+uG2uBm3lxOMw==} resolution: {integrity: sha512-iEbW8UKok2Oio7o6Y4VTzLj+KFCmQPGEpm1fS3xixwFBdclFVBvaQVeibl1jys4cujfAK5Kn6+uG2uBm3lxOMw==}
peerDependencies: peerDependencies:
@@ -907,6 +938,9 @@ packages:
color-name@1.1.4: color-name@1.1.4:
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==} resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
color2k@2.0.3:
resolution: {integrity: sha512-zW190nQTIoXcGCaU08DvVNFTmQhUpnJfVuAKfWqUQkflXKpaDdpaYoM0iluLS9lgJNHyBF58KKA2FBEwkD7wog==}
colorette@2.0.20: colorette@2.0.20:
resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==} resolution: {integrity: sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==}
@@ -2807,10 +2841,25 @@ snapshots:
'@sindresorhus/merge-streams@4.0.0': {} '@sindresorhus/merge-streams@4.0.0': {}
'@solid-primitives/memo@1.4.3(solid-js@1.9.9)':
dependencies:
'@solid-primitives/scheduled': 1.5.2(solid-js@1.9.9)
'@solid-primitives/utils': 6.3.2(solid-js@1.9.9)
solid-js: 1.9.9
'@solid-primitives/scheduled@1.5.2(solid-js@1.9.9)': '@solid-primitives/scheduled@1.5.2(solid-js@1.9.9)':
dependencies: dependencies:
solid-js: 1.9.9 solid-js: 1.9.9
'@solid-primitives/storage@4.3.3(solid-js@1.9.9)':
dependencies:
'@solid-primitives/utils': 6.3.2(solid-js@1.9.9)
solid-js: 1.9.9
'@solid-primitives/utils@6.3.2(solid-js@1.9.9)':
dependencies:
solid-js: 1.9.9
'@solidjs/router@0.15.3(solid-js@1.9.9)': '@solidjs/router@0.15.3(solid-js@1.9.9)':
dependencies: dependencies:
solid-js: 1.9.9 solid-js: 1.9.9
@@ -3209,6 +3258,8 @@ snapshots:
color-name@1.1.4: {} color-name@1.1.4: {}
color2k@2.0.3: {}
colorette@2.0.20: {} colorette@2.0.20: {}
compare-func@2.0.0: compare-func@2.0.0: