Compare commits
15 Commits
9919b97931
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e9b145066 | |||
| 7d45a36f93 | |||
| fb3f567a5b | |||
| 41bd1fce38 | |||
| ed42c831de | |||
| ae6a79aadd | |||
| 46002403c8 | |||
| b854fec9e5 | |||
| bedafb0b7c | |||
| 9e3697ffef | |||
| b3e040f03f | |||
| 46e7b60ade | |||
| ce29ab72ae | |||
| d203fe4141 | |||
| 67fdf66cd4 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,6 +2,8 @@
|
|||||||
.vinxi
|
.vinxi
|
||||||
*.db
|
*.db
|
||||||
|
|
||||||
|
.DS_STORE
|
||||||
|
|
||||||
# ---> Node
|
# ---> Node
|
||||||
# Logs
|
# Logs
|
||||||
logs
|
logs
|
||||||
|
|||||||
5
Makefile
5
Makefile
@@ -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
7
deploy
Executable 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
7
notes/newfile
Executable 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"
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "games",
|
"name": "games",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.0.8",
|
"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",
|
||||||
|
|||||||
@@ -8,9 +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",
|
"@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",
|
||||||
|
|||||||
@@ -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
|
|
||||||
);
|
|
||||||
|
|||||||
@@ -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
|
||||||
>;
|
>;
|
||||||
|
|||||||
@@ -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)",
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
import { Accessor, createContext, For, useContext } from "solid-js";
|
|
||||||
import type {
|
|
||||||
SimpleAction,
|
|
||||||
SimplePlayerView,
|
|
||||||
SimpleResult,
|
|
||||||
} from "@games/shared/games/simple";
|
|
||||||
import { me } 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"
|
|
||||||
: table.playerNames[view().playerTurn] + "'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>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@@ -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
|
||||||
>;
|
>;
|
||||||
|
|||||||
@@ -1,28 +1,18 @@
|
|||||||
import { createSignal, useContext } from "solid-js";
|
import { onMount, useContext } from "solid-js";
|
||||||
import { playerColor } 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">
|
||||||
{table?.playerNames[props.playerKey]}
|
{table?.players[props.playerKey].name}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import type { TWsIn, TWsOut } from "@games/server/src/table";
|
import type { TWsIn, TWsOut } from "@games/server/src/table";
|
||||||
import { fromPromise, merge, Stream } from "kefir";
|
import games from "@games/shared/games/index";
|
||||||
|
import { pool, Property, Stream } from "kefir";
|
||||||
import {
|
import {
|
||||||
Accessor,
|
Accessor,
|
||||||
createContext,
|
createContext,
|
||||||
@@ -7,98 +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 {
|
import { createObservable, createSynced, cx, extractProperty } from "~/fn";
|
||||||
createObservable,
|
import { me, name } from "~/profile";
|
||||||
createObservableStore,
|
import GAMES from "./games";
|
||||||
createObservableWithInit,
|
|
||||||
cx,
|
|
||||||
} from "~/fn";
|
|
||||||
import { me, mePromise } from "~/profile";
|
|
||||||
import Game from "./Game";
|
|
||||||
import Player from "./Player";
|
import Player from "./Player";
|
||||||
import games from "@games/shared/games/index";
|
|
||||||
import { createStore, Store } from "solid-js/store";
|
|
||||||
import { name } from "~/profile";
|
|
||||||
|
|
||||||
|
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>;
|
||||||
playerNames: Store<{ [key: string]: string }>;
|
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 !== undefined
|
|
||||||
);
|
|
||||||
const gameEvents = wsEvents.filter((evt) => evt.view !== undefined);
|
|
||||||
const resultEvents = wsEvents.filter((evt) => evt.results !== undefined);
|
|
||||||
|
|
||||||
const players = createObservableWithInit<string[]>(
|
|
||||||
presenceEvents.map((evt) => evt.playersPresent!),
|
|
||||||
[]
|
|
||||||
);
|
|
||||||
const playerNames = createObservableStore(
|
|
||||||
wsEvents
|
|
||||||
.filter((evt) => evt.playerNames != null)
|
|
||||||
.map(({ playerNames }) => playerNames!)
|
|
||||||
.toProperty(),
|
|
||||||
{}
|
|
||||||
);
|
|
||||||
|
|
||||||
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(() => {
|
||||||
createEffect(() => sendWs({ name: name() }));
|
ws = api.ws(props).subscribe();
|
||||||
const view = createObservable(gameEvents.map((evt) => evt.view));
|
ws.on("open", () => {
|
||||||
const results = createObservable<string>(
|
wsEvents.plug(fromWebsocket<TWsOut>(ws));
|
||||||
merge([
|
|
||||||
gameEvents
|
|
||||||
.filter((evt) => "view" in evt && evt.view == null)
|
|
||||||
.map(() => undefined),
|
|
||||||
resultEvents.map((evt) => evt.results),
|
|
||||||
])
|
|
||||||
);
|
|
||||||
|
|
||||||
|
// 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,
|
||||||
playerNames,
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
{/* 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;
|
||||||
@@ -114,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",
|
||||||
|
|
||||||
@@ -129,37 +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
|
||||||
<Show when={results() != null}>
|
component={
|
||||||
<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">
|
gameConfig()?.game ?? "" in GAMES
|
||||||
{playerNames[results()!]} won!
|
? GAMES[gameConfig()!.game as keyof typeof GAMES]
|
||||||
</span>
|
: undefined
|
||||||
</Show>
|
}
|
||||||
|
/>
|
||||||
</TableContext.Provider>
|
</TableContext.Provider>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
5
pkg/client/src/components/games/index.ts
Normal file
5
pkg/client/src/components/games/index.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import simple from "./simple";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
simple,
|
||||||
|
};
|
||||||
149
pkg/client/src/components/games/simple.tsx
Normal file
149
pkg/client/src/components/games/simple.tsx
Normal 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 />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -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)}`;
|
||||||
|
|||||||
@@ -1,6 +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 { createStore } from "solid-js/store";
|
||||||
|
import type { ExtractPropertyType, UnionKeys } from "@games/shared/types";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Array<T> {
|
interface Array<T> {
|
||||||
@@ -39,11 +41,30 @@ export const createObservableWithInit = <T>(
|
|||||||
|
|
||||||
export const cx = (...classes: string[]) => classes.join(" ");
|
export const cx = (...classes: string[]) => classes.join(" ");
|
||||||
|
|
||||||
export const createObservableStore = <T extends object = {}>(
|
export const createObservableStore =
|
||||||
obs: Observable<T, any>,
|
<T extends object = {}>(init: T) =>
|
||||||
init: T
|
(obs: Observable<T, any>) => {
|
||||||
) => {
|
const [store, setStore] = createStore<T>(init);
|
||||||
const [store, setStore] = createStore<T>(init);
|
obs.onValue((val) => setStore(val));
|
||||||
obs.onValue((val) => setStore(val));
|
return store;
|
||||||
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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
import { createEffect, createResource, createSignal, Resource } from "solid-js";
|
|
||||||
import { ApiType } from "./fn";
|
|
||||||
import api from "./api";
|
|
||||||
import hash from "object-hash";
|
|
||||||
import { makePersisted } from "@solid-primitives/storage";
|
import { makePersisted } from "@solid-primitives/storage";
|
||||||
|
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);
|
||||||
createEffect(() => console.log(me()));
|
|
||||||
|
|
||||||
export const playerColor = (humanKey: string) =>
|
export const playerColor = (humanKey: string) =>
|
||||||
"#" + hash(humanKey).substring(0, 6);
|
"#" + hash(humanKey).substring(0, 6);
|
||||||
|
|||||||
@@ -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>
|
||||||
|
</>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,25 +1,12 @@
|
|||||||
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 { liveTable, WsIn, WsOut } from "./table";
|
|
||||||
import { err } from "./logging";
|
import { err } from "./logging";
|
||||||
import { generateTokenAndKey, resolveToken, tokenExists } from "./human";
|
import { liveTable, WsIn, WsOut } from "./table";
|
||||||
|
import type { ExtractPropertyType, UnionKeys } from "@games/shared/types";
|
||||||
export const WS = Bus<
|
|
||||||
{
|
|
||||||
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 } }) => {
|
||||||
console.log("WHOAMI");
|
|
||||||
let key: string | undefined;
|
let key: string | undefined;
|
||||||
if (token.value == null || (key = resolveToken(token.value)) == null) {
|
if (token.value == null || (key = resolveToken(token.value)) == null) {
|
||||||
const [newToken, newKey] = generateTokenAndKey();
|
const [newToken, newKey] = generateTokenAndKey();
|
||||||
@@ -58,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 }))
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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);
|
||||||
});
|
// });
|
||||||
|
|||||||
@@ -12,18 +12,26 @@ import { t } from "elysia";
|
|||||||
import { combine, constant, merge, 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())),
|
|
||||||
playerNames: t.Optional(t.Record(t.String(), t.String())),
|
export const WsOut = t.Union([
|
||||||
playersReady: t.Optional(t.Nullable(t.Record(t.String(), t.Boolean()))),
|
t.Object({ playersPresent: t.Array(t.String()) }),
|
||||||
gameConfig: t.Optional(t.Any()),
|
t.Object({ playerNames: t.Record(t.String(), t.String()) }),
|
||||||
view: t.Optional(t.Any()),
|
t.Object({ playersReady: t.Record(t.String(), t.Boolean()) }),
|
||||||
results: t.Optional(t.Any()),
|
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({ 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) }),
|
||||||
@@ -136,8 +144,14 @@ export const liveTable = <
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const { name, ready, action, quit } = partition(
|
const {
|
||||||
["name", "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
|
||||||
@@ -145,6 +159,7 @@ export const liveTable = <
|
|||||||
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);
|
||||||
@@ -202,7 +217,7 @@ 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>) =>
|
const withGame = <T>(obs: Observable<T, any>) =>
|
||||||
@@ -226,7 +241,7 @@ export const liveTable = <
|
|||||||
prev,
|
prev,
|
||||||
[{ action, humanKey }, game]: [
|
[{ action, humanKey }, game]: [
|
||||||
Attributed & { action: GameAction },
|
Attributed & { action: GameAction },
|
||||||
Game
|
ReturnType<Game["impl"]>
|
||||||
]
|
]
|
||||||
) =>
|
) =>
|
||||||
prev &&
|
prev &&
|
||||||
@@ -258,18 +273,19 @@ export const liveTable = <
|
|||||||
|
|
||||||
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>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -3,21 +3,28 @@ import simple from "./simple";
|
|||||||
export type Game<
|
export type Game<
|
||||||
S = unknown, // state
|
S = unknown, // state
|
||||||
A = unknown, // action
|
A = unknown, // action
|
||||||
E extends { error: any } = { error: any }, // error
|
E = unknown, // error
|
||||||
V = unknown, // view
|
V = unknown, // view
|
||||||
R = unknown // results
|
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;
|
||||||
getResult: (state: S) => R | undefined;
|
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,
|
||||||
|
|||||||
@@ -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],
|
||||||
@@ -103,10 +128,14 @@ export const resolveSimpleAction = ({
|
|||||||
|
|
||||||
export type SimpleResult = string;
|
export type SimpleResult = string;
|
||||||
|
|
||||||
type SimpleError = { error: "whoops!" };
|
export default {
|
||||||
|
defaultConfig: {
|
||||||
export default (config: SimpleConfiguration) =>
|
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),
|
||||||
@@ -116,12 +145,14 @@ export default (config: SimpleConfiguration) =>
|
|||||||
resolveQuit: () => null,
|
resolveQuit: () => null,
|
||||||
getResult: (state) =>
|
getResult: (state) =>
|
||||||
Object.entries(state.playerHands).find(
|
Object.entries(state.playerHands).find(
|
||||||
([_, hand]) => hand.length === 2
|
([_, hand]) => hand.length === config["cards to win"]
|
||||||
)?.[0],
|
)?.[0],
|
||||||
} satisfies Game<
|
}),
|
||||||
SimpleGameState,
|
} satisfies Game<
|
||||||
SimpleAction,
|
SimpleGameState,
|
||||||
SimpleError,
|
SimpleAction,
|
||||||
SimplePlayerView,
|
SimpleError,
|
||||||
SimpleResult
|
SimplePlayerView,
|
||||||
>);
|
SimpleResult,
|
||||||
|
SimpleConfiguration
|
||||||
|
>;
|
||||||
|
|||||||
9
pkg/shared/types.ts
Normal file
9
pkg/shared/types.ts
Normal 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;
|
||||||
22
pnpm-lock.yaml
generated
22
pnpm-lock.yaml
generated
@@ -20,6 +20,9 @@ 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)
|
||||||
@@ -29,6 +32,9 @@ importers:
|
|||||||
'@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
|
||||||
@@ -590,6 +596,11 @@ 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:
|
||||||
@@ -927,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==}
|
||||||
|
|
||||||
@@ -2827,6 +2841,12 @@ 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
|
||||||
@@ -3238,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:
|
||||||
|
|||||||
Reference in New Issue
Block a user