@journey-to-code/rooms 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +154 -2
- package/dist/client.d.ts +62 -0
- package/dist/client.js +134 -0
- package/dist/client.js.map +1 -0
- package/dist/server.d.ts +303 -0
- package/dist/server.js +875 -0
- package/dist/server.js.map +1 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mikey Nichols
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,155 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @journey-to-code/rooms
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Turn-based rooms over [`@zubra/sync`](https://github.com/journey-to-code-org/zubra/blob/main/docs/sync.md):
|
|
4
|
+
seats, moves the server decides, a private view per seat, bots, and a move log. Nothing in it is
|
|
5
|
+
about spades; a room is defined by five functions.
|
|
6
|
+
|
|
7
|
+
It lives here while spades shapes its API, and moves to its own repository once that settles.
|
|
8
|
+
|
|
9
|
+
## Defining a room
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createRooms, RefusedMove } from "@journey-to-code/rooms/server";
|
|
13
|
+
|
|
14
|
+
const rooms = createRooms({
|
|
15
|
+
seats: 4,
|
|
16
|
+
setup: (options, seed) => newGame(options, seed),
|
|
17
|
+
act: (state, seat, move) => applyAction(state, { ...move, seat }), // throw to refuse; the message goes to the player
|
|
18
|
+
view: (state, seat) => viewFor(state, seat), // what that seat may see
|
|
19
|
+
summary: (state) => ({ scores: state.scores }), // what everyone may see
|
|
20
|
+
bot: (state, seat) => botMove(state, seat), // or null when it isn't that seat's move
|
|
21
|
+
finished: (state) => state.phase === "finished", // optional: finished rooms leave memory
|
|
22
|
+
}, { log: sqliteLog("rooms.db", { keep: "latest" }) });
|
|
23
|
+
|
|
24
|
+
const id = await rooms.create(options); // open a room from server code (a house opening a table)
|
|
25
|
+
|
|
26
|
+
// Node: answers /rooms/* and /_zb/sync/*
|
|
27
|
+
http.createServer(async (req, res) => { if (!(await rooms.handle(req, res))) serveApp(req, res); });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## In the browser
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { createRoom, openRoom } from "@journey-to-code/rooms/client";
|
|
34
|
+
|
|
35
|
+
const id = await createRoom(houseRules);
|
|
36
|
+
const room = openRoom(id);
|
|
37
|
+
room.onRoom((doc) => render(doc.seats)); // who's sitting where, started or not
|
|
38
|
+
const { seat } = await room.sit(0, "brokennil"); // room.stand() gives the seat back, before the deal
|
|
39
|
+
room.onView(seat, ({ view }) => render(view)); // this seat's view only
|
|
40
|
+
await room.act({ type: "bid", bid: { kind: "nil" } });
|
|
41
|
+
room.onChat((messages) => render(messages)); // the table's chat, oldest first
|
|
42
|
+
await room.say("good luck ♠️"); // seated players; rooms.say(id, seat, text) on the server, for bots
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Bots that take a while
|
|
46
|
+
|
|
47
|
+
`bot` is called on the server's one thread, which is fine for a move that's quick to find. For one
|
|
48
|
+
that isn't, give the room `think` as well: it is asked in `bot`'s place and may answer later.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
think: (state, seat, name, settings, { signal, table }) =>
|
|
52
|
+
state.turn !== seat ? null // not this seat's move: say so at once
|
|
53
|
+
: pool.think({ state, seat }, { signal, table }), // a promise of the move (a worker thread, say)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- **The pause hides the think.** The room asks as the bot's pause (`botDelay`) begins and plays
|
|
57
|
+
the move when both are done, so a think shorter than the pause costs the table nothing.
|
|
58
|
+
- **Only on the state it was for.** If the game moved on while the bot thought (someone moved, a
|
|
59
|
+
deadline fired), the answer is dropped and the bot is asked again about the table as it is now,
|
|
60
|
+
with no second pause. If the bot was stopped (the game finished, the settings changed, its seat
|
|
61
|
+
changed hands, the table was closed, the server restarted), the answer is dropped and that's all.
|
|
62
|
+
`signal` aborts either way, for a think that can be cancelled.
|
|
63
|
+
- **Still a move like any other.** It goes through `act` and into the move log.
|
|
64
|
+
- A deadline's `move()` is still worked out there and then, on the server's thread: it never waits
|
|
65
|
+
behind anyone's think. A promise that rejects (or resolves `null`) is logged and leaves the seat
|
|
66
|
+
waiting; resolve a move, whatever happens.
|
|
67
|
+
|
|
68
|
+
## Deadlines
|
|
69
|
+
|
|
70
|
+
A room waits for its seats for ever unless its definition has a `deadline`: by when, and the move
|
|
71
|
+
the server makes for which seat if nothing else happens first. A turn timer, a flag falling on a
|
|
72
|
+
chess clock, a window for claiming a discard and "a day a move" are all this.
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
createRooms({
|
|
76
|
+
// ...
|
|
77
|
+
act(state, seat, move, time) { /* time = { at, elapsed }: see below */ },
|
|
78
|
+
deadline: (state, { settings, seats }) =>
|
|
79
|
+
state.over ? null : { after: settings.limit, seat: state.turn, move: () => botMove(state, state.turn) },
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
| | |
|
|
84
|
+
| --- | --- |
|
|
85
|
+
| `after` | Ms after the last move (or the deal). "30 seconds a turn" is `after: 30_000`; a chess clock is `after: state.bank[state.turn]`. |
|
|
86
|
+
| `at` | Instead of `after`: a server time, for a state that keeps times itself. |
|
|
87
|
+
| `seat` | Whose clock it is. The timeout move is made as this seat. |
|
|
88
|
+
| `grace` | Optional: ms the server waits past that time before it makes the move. Pages count down to the time itself; a move sent as it reaches zero has this long to arrive. For clocks of a few seconds. |
|
|
89
|
+
| `move()` | The move, worked out only when time runs out. `null`: nothing happens, and the room waits. |
|
|
90
|
+
|
|
91
|
+
- **One timer a room.** The server asks for the deadline after every move, arms one timer, and
|
|
92
|
+
puts it away when the game finishes. It's separate from the bot's timer; both can be waiting.
|
|
93
|
+
- **A timeout is a move.** It goes through `act`, into the move log, and out through `onMove`
|
|
94
|
+
(with `timeout: true`), so a replay gives the same game.
|
|
95
|
+
- **Never both.** A move puts the deadline away in the same breath that it's decided, and the
|
|
96
|
+
timer checks it's still the armed one before it moves: a move and a timeout can't both land on
|
|
97
|
+
one state. Whichever is decided first wins; the other is refused or dropped.
|
|
98
|
+
- **Moves have times.** `act`'s fourth argument is `{ at, elapsed }`: the server time the move was
|
|
99
|
+
decided, and the ms since the move before it. The log keeps `at`, and a replay hands `act` the
|
|
100
|
+
same numbers, so a state may hold clocks (a bank per seat, with an increment) as long as they
|
|
101
|
+
change only by `elapsed`. `deadline` and `act` must never read the clock themselves.
|
|
102
|
+
- **Restarts.** The clock keeps running while the server is down: a deadline counts from the last
|
|
103
|
+
move's logged time, not from the restart, so nobody gets a fresh period (and a chess bank loses
|
|
104
|
+
the downtime). Rooms that had a deadline running are restored when the server starts, without
|
|
105
|
+
waiting for a request (`await rooms.restored`). If the time has already passed, the seat gets
|
|
106
|
+
`deadlineGrace` (default 5 seconds) from the restart before the timeout move is made. A game
|
|
107
|
+
dealt before moves had times starts its clock the first time it's restored.
|
|
108
|
+
- **Settings.** A deadline may depend on the table's settings; it's worked out again when they
|
|
109
|
+
change, without restarting the clock. One that lands in the past that way gets the same grace.
|
|
110
|
+
- **Pages see when, never what.** The public room doc has `deadline: { at, seat } | null`, in
|
|
111
|
+
server time. `GET /rooms/<id>` answers with the server's `now`, and the client measures the
|
|
112
|
+
difference from the browser's clock on every `info()`:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
room.onRoom((doc) => show(doc.deadline?.seat, room.timeLeft(doc.deadline))); // ms left, by the server's clock
|
|
116
|
+
room.serverNow(); // the server's time, as near as this browser can tell
|
|
117
|
+
room.clockOffset(); // how far ahead the server's clock is, in ms
|
|
118
|
+
await room.syncClock(); // ask again (after the phone slept)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## How it keeps a hand private
|
|
122
|
+
|
|
123
|
+
| Collection | Holds | Who reads it |
|
|
124
|
+
| --- | --- | --- |
|
|
125
|
+
| `rooms/<id>` | seats, started, summary, the deadline that's running | everyone |
|
|
126
|
+
| `views/<id>/<seat>` | one seat's view | that seat's holder |
|
|
127
|
+
| `games/<id>` | options, seed, seat holders | the server |
|
|
128
|
+
| `moves/<id>` | every move and when it was decided, one field each | the server |
|
|
129
|
+
| `chat/<id>` | the table's chat, one field per message | everyone at the table |
|
|
130
|
+
|
|
131
|
+
- **Pages never write.** `authorize` refuses every page push. Moves go to `POST /rooms/<id>/act`, and the server writes the result.
|
|
132
|
+
- **A seat is a cookie.** Sitting down sets an HttpOnly `rooms_player` cookie, and `authorize` lets a request read a seat's view only when its cookie holds that seat. `newPlayerId()`, `playerCookie()` and `playerOf()` let the app use the same identity (spades uses it for house membership).
|
|
133
|
+
- **Moves can't interleave.** The server decides a move and records it with no `await` in between.
|
|
134
|
+
- **Finished games leave memory.** With `finished`, a room's state is dropped once the game ends; a later request replays it from the log.
|
|
135
|
+
- **Restarts replay.** After a restart, a room's state is rebuilt by replaying `setup` and its move log, so `act` must be deterministic.
|
|
136
|
+
|
|
137
|
+
## Letting people watch
|
|
138
|
+
|
|
139
|
+
Give the room a `watch(state, views)` and the server writes one more view after each move,
|
|
140
|
+
`views/<id>/watch`, that anyone may read (`spectators()` in the options can say otherwise). A page
|
|
141
|
+
follows it with `room.onView("watch", …)`.
|
|
142
|
+
|
|
143
|
+
`sharedView(views, { blank, from })` builds that view from every seat's: what all the seats see
|
|
144
|
+
alike is kept, and what differs between them becomes null or an empty list, so a watcher learns
|
|
145
|
+
nothing that every player doesn't already know. Name the private parts in `blank` as well (two
|
|
146
|
+
hands that happened to match in some detail would otherwise show it), and in `from` the keys that
|
|
147
|
+
say where the viewer sits.
|
|
148
|
+
|
|
149
|
+
A table that's all computer players can be opened right up instead: `exhibit(id)` lets anyone read
|
|
150
|
+
every seat's view.
|
|
151
|
+
|
|
152
|
+
## Two things learned about Sync along the way
|
|
153
|
+
|
|
154
|
+
- **The live stream is authorized when it opens.** A browser that gets its cookie after connecting (by sitting down) keeps being treated as a stranger until it reconnects; `sit()` reopens the connection for this reason.
|
|
155
|
+
- **Sync has no plain-JavaScript way to follow a document yet**; documents reach `'use collection'` components. The client wraps the store (`configure({ store })`, public API) and tells subscribers about every save. A `subscribe(collection, key)` in Sync itself would make this unnecessary.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ChatMessage, DeadlineDoc, RoomDoc, ViewDoc } from "./server.js";
|
|
2
|
+
export type { ChatMessage, DeadlineDoc };
|
|
3
|
+
export interface RoomInfo<Summary = unknown> extends RoomDoc<Summary> {
|
|
4
|
+
id: string;
|
|
5
|
+
/** The seat this browser holds, or null. */
|
|
6
|
+
seat: number | null;
|
|
7
|
+
/** The server's clock (ms) when it answered. */
|
|
8
|
+
now?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class RoomError extends Error {
|
|
11
|
+
readonly status: number;
|
|
12
|
+
name: string;
|
|
13
|
+
constructor(message: string, status: number);
|
|
14
|
+
}
|
|
15
|
+
export interface OpenRoomOptions {
|
|
16
|
+
/** Where the rooms endpoints live. Default `/rooms`. */
|
|
17
|
+
base?: string;
|
|
18
|
+
/** Where the sync log is served. Default `/_zb/sync`. */
|
|
19
|
+
sync?: string;
|
|
20
|
+
}
|
|
21
|
+
/** Create a room on the server, with its settings if the game has any. Resolves to its id. */
|
|
22
|
+
export declare function createRoom(options: unknown, { base, settings }?: OpenRoomOptions & {
|
|
23
|
+
settings?: unknown;
|
|
24
|
+
}): Promise<string>;
|
|
25
|
+
export declare function openRoom<View = unknown, Move = unknown, Summary = unknown>(id: string, { base, sync }?: OpenRoomOptions): {
|
|
26
|
+
id: string;
|
|
27
|
+
/** The public room as the server has it now, and the seat this browser holds. */
|
|
28
|
+
info: () => Promise<RoomInfo<Summary>>;
|
|
29
|
+
/**
|
|
30
|
+
* The server's clock, as near as this browser can tell (after the first info()). Count down to a
|
|
31
|
+
* room's `deadline.at` with this, never with Date.now().
|
|
32
|
+
*/
|
|
33
|
+
serverNow: () => number;
|
|
34
|
+
/** Ms left on a deadline (never below zero), by the server's clock; null when there's none. */
|
|
35
|
+
timeLeft: (deadline: DeadlineDoc | null | undefined) => number | null;
|
|
36
|
+
/** How far the server's clock is ahead of this browser's, in ms. */
|
|
37
|
+
clockOffset: () => number;
|
|
38
|
+
/** Ask the server the time again (after a long sleep, say). Resolves to the offset. */
|
|
39
|
+
syncClock: () => Promise<number>;
|
|
40
|
+
/** Follow the public room: seats, whether it started, the summary, the deadline that's running. */
|
|
41
|
+
onRoom(fn: (room: RoomDoc<Summary>) => void): () => void;
|
|
42
|
+
/** Follow one seat's view (the server only sends it to that seat's holder), or "watch": what someone watching may see. */
|
|
43
|
+
onView(seat: number | "watch", fn: (view: ViewDoc<View>) => void): () => void;
|
|
44
|
+
/** Follow the table's chat: every message so far, oldest first, each time one arrives. */
|
|
45
|
+
onChat(fn: (messages: ChatMessage[]) => void): () => void;
|
|
46
|
+
say: (text: string) => Promise<ChatMessage>;
|
|
47
|
+
sit: (seat: number, name: string) => Promise<{
|
|
48
|
+
seat: number;
|
|
49
|
+
}>;
|
|
50
|
+
addBot: (seat: number, name?: string) => Promise<unknown>;
|
|
51
|
+
/** Change the table's settings (anyone seated, mid-game too). */
|
|
52
|
+
settings: (settings: unknown) => Promise<unknown>;
|
|
53
|
+
removeBot: (seat: number) => Promise<unknown>;
|
|
54
|
+
/** Give up your seat, before the game starts. */
|
|
55
|
+
stand: () => Promise<unknown>;
|
|
56
|
+
start: () => Promise<unknown>;
|
|
57
|
+
act: (move: Move) => Promise<unknown>;
|
|
58
|
+
/** Pull now, instead of waiting for the live stream. */
|
|
59
|
+
refresh: () => Promise<void>;
|
|
60
|
+
close: () => void;
|
|
61
|
+
};
|
|
62
|
+
export type Room<View = unknown, Move = unknown, Summary = unknown> = ReturnType<typeof openRoom<View, Move, Summary>>;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// The browser side of a room: follow the public room and your own seat's view through
|
|
2
|
+
// @zubra/sync, and send moves to the server. Framework-agnostic: React, Vue, a Zubra component
|
|
3
|
+
// or plain script can all subscribe.
|
|
4
|
+
import { configure, indexedDBStore, memoryStore } from "@zubra/sync";
|
|
5
|
+
import { connect } from "@zubra/sync/connect";
|
|
6
|
+
// Sync has no plain-JavaScript subscription yet: documents reach components through
|
|
7
|
+
// 'use collection'. Its store is public API, though, and every change it receives is stored, so
|
|
8
|
+
// a store that tells us about each save is how a page without a component follows a document.
|
|
9
|
+
const listeners = new Map();
|
|
10
|
+
let watched = null;
|
|
11
|
+
function watchStore() {
|
|
12
|
+
if (watched)
|
|
13
|
+
return watched;
|
|
14
|
+
const inner = typeof indexedDB === "undefined" ? memoryStore() : indexedDBStore();
|
|
15
|
+
watched = {
|
|
16
|
+
get: (collection, key) => inner.get(collection, key),
|
|
17
|
+
async set(collection, key, fields) {
|
|
18
|
+
await inner.set(collection, key, fields);
|
|
19
|
+
const subs = listeners.get(`${collection}\u0000${key}`);
|
|
20
|
+
if (!subs?.size)
|
|
21
|
+
return;
|
|
22
|
+
const doc = (await inner.get(collection, key)) ?? {};
|
|
23
|
+
for (const fn of subs)
|
|
24
|
+
fn(doc);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
configure({ store: watched });
|
|
28
|
+
return watched;
|
|
29
|
+
}
|
|
30
|
+
function follow(collection, key, fn) {
|
|
31
|
+
const store = watchStore();
|
|
32
|
+
const id = `${collection}\u0000${key}`;
|
|
33
|
+
if (!listeners.has(id))
|
|
34
|
+
listeners.set(id, new Set());
|
|
35
|
+
listeners.get(id).add(fn);
|
|
36
|
+
void store.get(collection, key).then((doc) => doc && fn(doc));
|
|
37
|
+
return () => listeners.get(id)?.delete(fn);
|
|
38
|
+
}
|
|
39
|
+
export class RoomError extends Error {
|
|
40
|
+
status;
|
|
41
|
+
name = "RoomError";
|
|
42
|
+
constructor(message, status) {
|
|
43
|
+
super(message);
|
|
44
|
+
this.status = status;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function call(url, init) {
|
|
48
|
+
const response = await fetch(url, { credentials: "same-origin", headers: { "content-type": "application/json" }, ...init });
|
|
49
|
+
const body = (await response.json().catch(() => ({})));
|
|
50
|
+
if (!response.ok)
|
|
51
|
+
throw new RoomError(body.error ?? `Request failed (${response.status})`, response.status);
|
|
52
|
+
return body;
|
|
53
|
+
}
|
|
54
|
+
/** Create a room on the server, with its settings if the game has any. Resolves to its id. */
|
|
55
|
+
export async function createRoom(options, { base = "/rooms", settings } = {}) {
|
|
56
|
+
const { id } = await call(base, { method: "POST", body: JSON.stringify({ options, settings }) });
|
|
57
|
+
return id;
|
|
58
|
+
}
|
|
59
|
+
export function openRoom(id, { base = "/rooms", sync = "/_zb/sync" } = {}) {
|
|
60
|
+
const url = `${base}/${encodeURIComponent(id)}`;
|
|
61
|
+
watchStore();
|
|
62
|
+
const scope = { rooms: id, views: `${id}/`, chat: id };
|
|
63
|
+
let connection = connect({ url: sync, scope });
|
|
64
|
+
const post = (path, body = {}) => call(`${url}/${path}`, { method: "POST", body: JSON.stringify(body) });
|
|
65
|
+
// The live stream is authorized with the cookies it was opened with. Sitting down can give this
|
|
66
|
+
// browser its player cookie, so open the stream again to be sent this seat's view.
|
|
67
|
+
async function sit(seat, name) {
|
|
68
|
+
const result = await post("sit", { seat, name });
|
|
69
|
+
connection.stop();
|
|
70
|
+
connection = connect({ url: sync, scope });
|
|
71
|
+
return result;
|
|
72
|
+
}
|
|
73
|
+
// A deadline is a time on the server's clock, and a phone's clock can be minutes out. Every answer to
|
|
74
|
+
// info() carries the server's time: taking it as the middle of the round trip gives the difference,
|
|
75
|
+
// and the quickest round trip seen is the one to trust.
|
|
76
|
+
let offset = 0;
|
|
77
|
+
let quickest = Infinity;
|
|
78
|
+
async function info() {
|
|
79
|
+
const sent = Date.now();
|
|
80
|
+
const answer = await call(url);
|
|
81
|
+
const back = Date.now();
|
|
82
|
+
if (typeof answer.now === "number" && back - sent <= quickest) {
|
|
83
|
+
quickest = back - sent;
|
|
84
|
+
offset = answer.now - (sent + back) / 2;
|
|
85
|
+
}
|
|
86
|
+
return answer;
|
|
87
|
+
}
|
|
88
|
+
const serverNow = () => Date.now() + offset;
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
/** The public room as the server has it now, and the seat this browser holds. */
|
|
92
|
+
info,
|
|
93
|
+
/**
|
|
94
|
+
* The server's clock, as near as this browser can tell (after the first info()). Count down to a
|
|
95
|
+
* room's `deadline.at` with this, never with Date.now().
|
|
96
|
+
*/
|
|
97
|
+
serverNow,
|
|
98
|
+
/** Ms left on a deadline (never below zero), by the server's clock; null when there's none. */
|
|
99
|
+
timeLeft: (deadline) => (deadline ? Math.max(0, deadline.at - serverNow()) : null),
|
|
100
|
+
/** How far the server's clock is ahead of this browser's, in ms. */
|
|
101
|
+
clockOffset: () => offset,
|
|
102
|
+
/** Ask the server the time again (after a long sleep, say). Resolves to the offset. */
|
|
103
|
+
syncClock: () => info().then(() => offset),
|
|
104
|
+
/** Follow the public room: seats, whether it started, the summary, the deadline that's running. */
|
|
105
|
+
onRoom(fn) {
|
|
106
|
+
return follow("rooms", id, (doc) => fn(doc));
|
|
107
|
+
},
|
|
108
|
+
/** Follow one seat's view (the server only sends it to that seat's holder), or "watch": what someone watching may see. */
|
|
109
|
+
onView(seat, fn) {
|
|
110
|
+
return follow("views", `${id}/${seat}`, (doc) => doc.view !== undefined && fn(doc));
|
|
111
|
+
},
|
|
112
|
+
/** Follow the table's chat: every message so far, oldest first, each time one arrives. */
|
|
113
|
+
onChat(fn) {
|
|
114
|
+
return follow("chat", id, (doc) => fn(Object.keys(doc)
|
|
115
|
+
.filter((k) => /^m\d+$/.test(k))
|
|
116
|
+
.sort()
|
|
117
|
+
.map((k) => doc[k])));
|
|
118
|
+
},
|
|
119
|
+
say: (text) => post("say", { text }),
|
|
120
|
+
sit,
|
|
121
|
+
addBot: (seat, name) => post("bot", { seat, name }),
|
|
122
|
+
/** Change the table's settings (anyone seated, mid-game too). */
|
|
123
|
+
settings: (settings) => post("settings", { settings }),
|
|
124
|
+
removeBot: (seat) => post("bot", { seat, add: false }),
|
|
125
|
+
/** Give up your seat, before the game starts. */
|
|
126
|
+
stand: () => post("stand"),
|
|
127
|
+
start: () => post("start"),
|
|
128
|
+
act: (move) => post("act", { move }),
|
|
129
|
+
/** Pull now, instead of waiting for the live stream. */
|
|
130
|
+
refresh: () => connection.pull(),
|
|
131
|
+
close: () => connection.stop(),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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