@hadoku/game-server 0.0.0-stage → 0.1.6
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/dist/base-path.d.ts +29 -0
- package/dist/base-path.js +30 -0
- package/dist/busy.d.ts +40 -0
- package/dist/busy.js +23 -0
- package/dist/capacity.d.ts +9 -0
- package/dist/capacity.js +6 -0
- package/dist/config.d.ts +247 -0
- package/dist/config.js +271 -0
- package/dist/edge-auth.d.ts +43 -0
- package/dist/edge-auth.js +47 -0
- package/dist/erase/anonymise.d.ts +7 -0
- package/dist/erase/anonymise.js +7 -0
- package/dist/erase/index.d.ts +18 -0
- package/dist/erase/index.js +20 -0
- package/dist/erase/legacy-ids.d.ts +7 -0
- package/dist/erase/legacy-ids.js +24 -0
- package/dist/erase/recordings.d.ts +26 -0
- package/dist/erase/recordings.js +128 -0
- package/dist/erase/rooms.d.ts +63 -0
- package/dist/erase/rooms.js +67 -0
- package/dist/gate.d.ts +27 -0
- package/dist/gate.js +15 -0
- package/dist/http/client-events.d.ts +25 -0
- package/dist/http/client-events.js +15 -0
- package/dist/http/page.d.ts +26 -0
- package/dist/http/page.js +35 -0
- package/dist/http/prepared.d.ts +14 -0
- package/dist/http/prepared.js +16 -0
- package/dist/http/request.d.ts +59 -0
- package/dist/http/request.js +37 -0
- package/dist/http/room-core.d.ts +160 -0
- package/dist/http/room-core.js +278 -0
- package/dist/http/trim.d.ts +23 -0
- package/dist/http/trim.js +76 -0
- package/dist/http/usage.d.ts +22 -0
- package/dist/http/usage.js +16 -0
- package/dist/kernel.d.ts +4 -0
- package/dist/kernel.js +26 -0
- package/dist/log.d.ts +9 -0
- package/dist/log.js +9 -0
- package/dist/people.d.ts +105 -0
- package/dist/people.js +145 -0
- package/dist/play/clock.d.ts +39 -0
- package/dist/play/clock.js +81 -0
- package/dist/play/diagnostics.d.ts +21 -0
- package/dist/play/diagnostics.js +35 -0
- package/dist/play/engine-runtime.d.ts +200 -0
- package/dist/play/engine-runtime.js +309 -0
- package/dist/play/game-adapter.d.ts +158 -0
- package/dist/play/game-adapter.js +1 -0
- package/dist/play/host.d.ts +482 -0
- package/dist/play/host.js +950 -0
- package/dist/play/jvm-transport.d.ts +25 -0
- package/dist/play/jvm-transport.js +1 -0
- package/dist/play/local-jvm.d.ts +325 -0
- package/dist/play/local-jvm.js +829 -0
- package/dist/play/match/hosted.d.ts +92 -0
- package/dist/play/match/hosted.js +151 -0
- package/dist/play/match/outcome.d.ts +26 -0
- package/dist/play/match/outcome.js +21 -0
- package/dist/play/match/shell.d.ts +378 -0
- package/dist/play/match/shell.js +613 -0
- package/dist/play/recording/retention.d.ts +36 -0
- package/dist/play/recording/retention.js +89 -0
- package/dist/play/recording/writer.d.ts +185 -0
- package/dist/play/recording/writer.js +90 -0
- package/dist/play/relay-socket.d.ts +128 -0
- package/dist/play/relay-socket.js +194 -0
- package/dist/play/runtimes/in-process.d.ts +47 -0
- package/dist/play/runtimes/in-process.js +43 -0
- package/dist/play/runtimes/relay-match.d.ts +166 -0
- package/dist/play/runtimes/relay-match.js +240 -0
- package/dist/play/runtimes/relay.d.ts +128 -0
- package/dist/play/runtimes/relay.js +183 -0
- package/dist/play/runtimes/streamed.d.ts +116 -0
- package/dist/play/runtimes/streamed.js +148 -0
- package/dist/play/start-queue.d.ts +65 -0
- package/dist/play/start-queue.js +187 -0
- package/dist/room/avatar.d.ts +1 -0
- package/dist/room/avatar.js +28 -0
- package/dist/room/caller.d.ts +21 -0
- package/dist/room/caller.js +1 -0
- package/dist/room/constants.d.ts +27 -0
- package/dist/room/constants.js +27 -0
- package/dist/room/events.d.ts +59 -0
- package/dist/room/events.js +174 -0
- package/dist/room/identity.d.ts +89 -0
- package/dist/room/identity.js +98 -0
- package/dist/room/lobby.d.ts +426 -0
- package/dist/room/lobby.js +880 -0
- package/dist/room/membership.d.ts +64 -0
- package/dist/room/membership.js +76 -0
- package/dist/room/provisioning.d.ts +22 -0
- package/dist/room/provisioning.js +46 -0
- package/dist/room/record.d.ts +63 -0
- package/dist/room/record.js +39 -0
- package/dist/room/room-list.d.ts +67 -0
- package/dist/room/room-list.js +66 -0
- package/dist/room/seats.d.ts +39 -0
- package/dist/room/seats.js +58 -0
- package/dist/room/session.d.ts +91 -0
- package/dist/room/session.js +156 -0
- package/dist/room/socket-auth.d.ts +2 -0
- package/dist/room/socket-auth.js +10 -0
- package/dist/room/watching.d.ts +35 -0
- package/dist/room/watching.js +72 -0
- package/dist/room/watchparty-link.d.ts +1 -0
- package/dist/room/watchparty-link.js +17 -0
- package/dist/stream/alloc.d.ts +115 -0
- package/dist/stream/alloc.js +368 -0
- package/dist/stream/index.d.ts +66 -0
- package/dist/stream/index.js +293 -0
- package/dist/telemetry.d.ts +46 -0
- package/dist/telemetry.js +25 -0
- package/dist/testPort.d.ts +1 -0
- package/dist/testPort.js +28 -0
- package/package.json +35 -4
- package/README.md +0 -3
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One live tab per player per room: `@wolffm/rooms-server`'s `SessionClaims`, on this server's sockets.
|
|
3
|
+
*
|
|
4
|
+
* The operator, 2026-11-07: *"whenever a player tried to join a game when they already have an active
|
|
5
|
+
* session we should … show a prompt in the middle of the screen … asking whether they want to take
|
|
6
|
+
* over in this session? if they click yes, then we terminate their previous session."* The rule is
|
|
7
|
+
* the shared room layer's (`SessionClaims`' header, and `@wolffm/rooms-protocol`'s `session.ts` for the
|
|
8
|
+
* wire); this file is where cardtable's sockets meet it.
|
|
9
|
+
*
|
|
10
|
+
* ## Which sockets
|
|
11
|
+
*
|
|
12
|
+
* A tab's screen gate (`packages/game-ui/src/room/session.ts`) opens one socket per room and sends
|
|
13
|
+
* `session:claim` on it: that is the claim the player is asked about. The tab's other sockets for the
|
|
14
|
+
* room join the same claim when they join as a player (`attach`): the pod's draft socket
|
|
15
|
+
* (`room/socket.ts`) and the match socket (`play/socket.ts`), each naming its tab. So a take retires
|
|
16
|
+
* every connection the old tab has in the room, its live match included, and a socket of a tab that
|
|
17
|
+
* does not hold the room is refused its player's join (`session-elsewhere`) instead of silently
|
|
18
|
+
* taking the seat.
|
|
19
|
+
*
|
|
20
|
+
* ## Who
|
|
21
|
+
*
|
|
22
|
+
* A player only (`isPlayer`): a seat at one of the room's tables, a place in its pod or draft, or a
|
|
23
|
+
* seat in its match. Watchers keep as many tabs as they like, and their claim answers `watcher`
|
|
24
|
+
* holding nothing. The screen claims again whenever its room changes, so a watcher who sits down is
|
|
25
|
+
* then a player with a claim, and a player who stands up lets go of it.
|
|
26
|
+
*
|
|
27
|
+
* ## The room's key
|
|
28
|
+
*
|
|
29
|
+
* The room id the screen is in, and for a match that no room holds (a scenario, a direct game against
|
|
30
|
+
* the AI) the match's own id: the same id `App.tsx`'s `roomIdFor` gives the screen.
|
|
31
|
+
*
|
|
32
|
+
* ## Liveness
|
|
33
|
+
*
|
|
34
|
+
* Every holding session socket is sent `session:beat` every `BEAT_MS`, and its ack is a heartbeat. A
|
|
35
|
+
* beat is a message, so a hidden tab whose timers the browser throttles still answers at once; an
|
|
36
|
+
* engine.io pong is one too, for every attached socket. A holder silent past `IDLE_MS` — a closed
|
|
37
|
+
* laptop, a dropped network, a socket whose ping has not timed out yet — is replaced without asking.
|
|
38
|
+
*
|
|
39
|
+
* ## A take
|
|
40
|
+
*
|
|
41
|
+
* The old tab's sockets are told `session:taken` and closed from here. A server-side close does not
|
|
42
|
+
* auto-reconnect, so the old tab cannot loop back into the room; it offers to take the session back,
|
|
43
|
+
* which is the same claim with `take`. Every take reports `session-takeover` with the room and whether
|
|
44
|
+
* the holder was live or stale.
|
|
45
|
+
*/
|
|
46
|
+
import { SessionClaims } from '@wolffm/rooms-server';
|
|
47
|
+
import { SESSION_EVENTS, parseSessionClaim } from '@wolffm/rooms-protocol';
|
|
48
|
+
import { identityFromHeaders } from './identity.js';
|
|
49
|
+
import { jsonLogger } from '../log.js';
|
|
50
|
+
import { callerContext, report } from '../telemetry.js';
|
|
51
|
+
/** How often a holding session socket is asked whether it is alive. */
|
|
52
|
+
export const BEAT_MS = 5_000;
|
|
53
|
+
/** A holder that has answered nothing for this long is stale: two missed beats and change. */
|
|
54
|
+
export const IDLE_MS = 15_000;
|
|
55
|
+
const defaultLog = jsonLogger('cardtable-session');
|
|
56
|
+
const keyOf = (participantId, room) => `${participantId}\n${room}`;
|
|
57
|
+
export function attachSessions(io, deps) {
|
|
58
|
+
const log = deps.log ?? defaultLog;
|
|
59
|
+
const claims = new SessionClaims({ idleMs: deps.idleMs ?? IDLE_MS, ...(deps.now === undefined ? {} : { now: deps.now }) });
|
|
60
|
+
/** Session sockets (the ones that sent a claim), which answer beats. */
|
|
61
|
+
const sessionSockets = new Map();
|
|
62
|
+
/** A take's bookkeeping: close the old tab's sockets, say so, count it. */
|
|
63
|
+
const took = (room, participantId, socket, decision) => {
|
|
64
|
+
for (const id of decision.retire) {
|
|
65
|
+
const old = io.sockets.sockets.get(id);
|
|
66
|
+
if (old === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
old.emit(SESSION_EVENTS.taken, { room });
|
|
69
|
+
old.disconnect(true);
|
|
70
|
+
}
|
|
71
|
+
const holder = decision.stale ? 'stale' : 'live';
|
|
72
|
+
log({ evt: 'session-taken', room, participantId, socketId: socket.id, holder, retired: decision.retire.length });
|
|
73
|
+
report('session-takeover', { room, holder, key: room, ...callerContext(socket.request.headers) });
|
|
74
|
+
};
|
|
75
|
+
io.on('connection', (socket) => {
|
|
76
|
+
const participantId = identityFromHeaders(socket.request.headers, socket.id);
|
|
77
|
+
socket.conn.on('heartbeat', () => {
|
|
78
|
+
claims.heartbeat(socket.id);
|
|
79
|
+
});
|
|
80
|
+
socket.on(SESSION_EVENTS.claim, (raw, ack) => {
|
|
81
|
+
const answer = (message) => {
|
|
82
|
+
if (typeof ack === 'function')
|
|
83
|
+
ack(message);
|
|
84
|
+
};
|
|
85
|
+
const request = parseSessionClaim(raw);
|
|
86
|
+
if (request === null) {
|
|
87
|
+
log({ evt: 'session-malformed', participantId });
|
|
88
|
+
answer({ room: '', status: 'refused' });
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const { room, tabId } = request;
|
|
92
|
+
sessionSockets.set(socket.id, socket);
|
|
93
|
+
if (!deps.isPlayer(room, participantId)) {
|
|
94
|
+
claims.release(socket.id);
|
|
95
|
+
answer({ room, status: 'watcher' });
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const decision = claims.claim(keyOf(participantId, room), tabId, socket.id, { take: request.take === true });
|
|
99
|
+
if (decision.type === 'held') {
|
|
100
|
+
log({ evt: 'session-held', room, participantId, socketId: socket.id, take: request.take === true });
|
|
101
|
+
answer({ room, status: 'held' });
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (decision.type === 'taken')
|
|
105
|
+
took(room, participantId, socket, decision);
|
|
106
|
+
answer({ room, status: 'granted' });
|
|
107
|
+
});
|
|
108
|
+
socket.on('disconnect', () => {
|
|
109
|
+
sessionSockets.delete(socket.id);
|
|
110
|
+
claims.release(socket.id);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
const beating = setInterval(() => {
|
|
114
|
+
for (const [id, socket] of sessionSockets) {
|
|
115
|
+
if (claims.keysOf(id).length === 0)
|
|
116
|
+
continue;
|
|
117
|
+
socket.timeout(deps.beatMs ?? BEAT_MS).emit(SESSION_EVENTS.beat, (err) => {
|
|
118
|
+
if (err === null)
|
|
119
|
+
claims.heartbeat(id);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}, deps.beatMs ?? BEAT_MS);
|
|
123
|
+
beating.unref();
|
|
124
|
+
return {
|
|
125
|
+
attach(room, participantId, tabId, socket) {
|
|
126
|
+
if (!deps.isPlayer(room, participantId))
|
|
127
|
+
return true;
|
|
128
|
+
const decision = claims.claim(keyOf(participantId, room), tabId, socket.id);
|
|
129
|
+
if (decision.type === 'held') {
|
|
130
|
+
log({ evt: 'session-elsewhere', room, participantId, socketId: socket.id });
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (decision.type === 'taken')
|
|
134
|
+
took(room, participantId, socket, decision);
|
|
135
|
+
// Its close lets go of the claim through the `disconnect` every socket has above.
|
|
136
|
+
return true;
|
|
137
|
+
},
|
|
138
|
+
close() {
|
|
139
|
+
clearInterval(beating);
|
|
140
|
+
sessionSockets.clear();
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/** Whether `participantId` plays in the room: a game seat, a table seat, a pod seat, or a place in its draft and rounds. */
|
|
145
|
+
export function playsIn(room, participantId) {
|
|
146
|
+
if (room.seats.includes(participantId))
|
|
147
|
+
return true;
|
|
148
|
+
if ((room.tables ?? []).some((t) => t.players.some((p) => p.memberId === participantId)))
|
|
149
|
+
return true;
|
|
150
|
+
const pod = room.game?.pod ?? null;
|
|
151
|
+
if (pod === null)
|
|
152
|
+
return false;
|
|
153
|
+
if (pod.seats.some((s) => s?.memberId === participantId))
|
|
154
|
+
return true;
|
|
155
|
+
return pod.drafters.includes(participantId) && !(pod.dropped ?? []).includes(participantId);
|
|
156
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { tierOf } from '../edge-auth.js';
|
|
2
|
+
export function handshakeTier(headers) {
|
|
3
|
+
// `tierOf` reads exactly one header off `req.headers`, but it is typed for an
|
|
4
|
+
// express `Request` and a handshake carries a bare `IncomingMessage`. The
|
|
5
|
+
// cast is here rather than a second copy of the header read: a duplicated
|
|
6
|
+
// tier lookup is precisely how the socket path and the HTTP path come to
|
|
7
|
+
// disagree about who is allowed in, which is the bug `edge-auth.ts`'s header
|
|
8
|
+
// is written about.
|
|
9
|
+
return tierOf({ headers });
|
|
10
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The room feed, bound to the lobby: an open screen is told its room (or the room list) changed,
|
|
3
|
+
* so it never reads on a timer. The feed itself is the rooms core's (`@wolffm/rooms-server`'s
|
|
4
|
+
* `attachRoomBookFeed`, over `@wolffm/rooms-protocol`'s `feed.ts` wire): one ping per room per turn,
|
|
5
|
+
* a watch is the whole set a socket follows and replaces the last. What this file adds is
|
|
6
|
+
* cardtable's own:
|
|
7
|
+
*
|
|
8
|
+
* - **Who hears a room.** Rooms are private: a socket hears a room's ping only while its participant
|
|
9
|
+
* is a member, checked when the ping goes out (an invite link watches before its join lands).
|
|
10
|
+
* - **What drives it.** Every room write goes through the lobby's one persist, which bumps the
|
|
11
|
+
* room's `updatedAt` and says so (`Lobby.onChange`) — joins and leaves included, so a member
|
|
12
|
+
* count reaches the list on the write that changed it.
|
|
13
|
+
* - **Presence.** A hidden screen reads nothing, so the watch itself keeps a room offered on the
|
|
14
|
+
* front page: every watched room is touched for its member when the watch arrives and every
|
|
15
|
+
* `FEED_TOUCH_MS` after, for as long as the socket stays.
|
|
16
|
+
*
|
|
17
|
+
* No gate of its own: `attachDraftSocket` and `attachPlay` already put the edge's provenance and the
|
|
18
|
+
* tier floor in front of every handshake on the default namespace (`room/socket.ts`'s header).
|
|
19
|
+
*/
|
|
20
|
+
import type { Server as SocketIOServer } from 'socket.io';
|
|
21
|
+
import { type RoomFeedHandle } from '@wolffm/rooms-server';
|
|
22
|
+
/** A narrow slice of `room/lobby.ts`'s `Lobby` — declared here, not imported, so this platform file carries no edge into the tangled lobby. `FileLobby` satisfies it structurally. */
|
|
23
|
+
interface RoomFeedLobby {
|
|
24
|
+
touch(roomId: string, memberId: string): void;
|
|
25
|
+
isMember(roomId: string, memberId: string): boolean;
|
|
26
|
+
onChange(listener: (roomId: string) => void): () => void;
|
|
27
|
+
}
|
|
28
|
+
interface RoomFeedDeps {
|
|
29
|
+
lobby: RoomFeedLobby;
|
|
30
|
+
log?: (line: Record<string, unknown>) => void;
|
|
31
|
+
/** Injected for tests. */
|
|
32
|
+
touchEveryMs?: number;
|
|
33
|
+
}
|
|
34
|
+
export declare function attachRoomFeed(io: SocketIOServer, deps: RoomFeedDeps): RoomFeedHandle;
|
|
35
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The room feed, bound to the lobby: an open screen is told its room (or the room list) changed,
|
|
3
|
+
* so it never reads on a timer. The feed itself is the rooms core's (`@wolffm/rooms-server`'s
|
|
4
|
+
* `attachRoomBookFeed`, over `@wolffm/rooms-protocol`'s `feed.ts` wire): one ping per room per turn,
|
|
5
|
+
* a watch is the whole set a socket follows and replaces the last. What this file adds is
|
|
6
|
+
* cardtable's own:
|
|
7
|
+
*
|
|
8
|
+
* - **Who hears a room.** Rooms are private: a socket hears a room's ping only while its participant
|
|
9
|
+
* is a member, checked when the ping goes out (an invite link watches before its join lands).
|
|
10
|
+
* - **What drives it.** Every room write goes through the lobby's one persist, which bumps the
|
|
11
|
+
* room's `updatedAt` and says so (`Lobby.onChange`) — joins and leaves included, so a member
|
|
12
|
+
* count reaches the list on the write that changed it.
|
|
13
|
+
* - **Presence.** A hidden screen reads nothing, so the watch itself keeps a room offered on the
|
|
14
|
+
* front page: every watched room is touched for its member when the watch arrives and every
|
|
15
|
+
* `FEED_TOUCH_MS` after, for as long as the socket stays.
|
|
16
|
+
*
|
|
17
|
+
* No gate of its own: `attachDraftSocket` and `attachPlay` already put the edge's provenance and the
|
|
18
|
+
* tier floor in front of every handshake on the default namespace (`room/socket.ts`'s header).
|
|
19
|
+
*/
|
|
20
|
+
import { ROOM_FEED_CHANNEL, parseRoomWatch } from '@wolffm/rooms-protocol';
|
|
21
|
+
import { attachRoomBookFeed } from '@wolffm/rooms-server';
|
|
22
|
+
import { jsonLogger } from '@wolffm/service-kernel/log';
|
|
23
|
+
import { identityFromHeaders } from './identity.js';
|
|
24
|
+
import { ROOM_STALE_MS } from './constants.js';
|
|
25
|
+
/** Well inside `ROOM_STALE_MS`, so an open room never stops being offered. */
|
|
26
|
+
const FEED_TOUCH_MS = ROOM_STALE_MS / 6;
|
|
27
|
+
const participantOf = (socket) => identityFromHeaders(socket.request.headers, socket.id);
|
|
28
|
+
export function attachRoomFeed(io, deps) {
|
|
29
|
+
const { lobby } = deps;
|
|
30
|
+
/** Each watching socket's participant and rooms, for the touch. */
|
|
31
|
+
const watching = new Map();
|
|
32
|
+
const touch = ({ participantId, rooms }) => {
|
|
33
|
+
for (const room of rooms)
|
|
34
|
+
lobby.touch(room, participantId);
|
|
35
|
+
};
|
|
36
|
+
const touching = setInterval(() => {
|
|
37
|
+
for (const watcher of watching.values())
|
|
38
|
+
touch(watcher);
|
|
39
|
+
}, deps.touchEveryMs ?? FEED_TOUCH_MS);
|
|
40
|
+
touching.unref();
|
|
41
|
+
io.on('connection', (socket) => {
|
|
42
|
+
const participantId = participantOf(socket);
|
|
43
|
+
socket.on(ROOM_FEED_CHANNEL.watch, (raw) => {
|
|
44
|
+
const watch = parseRoomWatch(raw);
|
|
45
|
+
if (watch === null)
|
|
46
|
+
return;
|
|
47
|
+
if (watch.rooms.length === 0 && !watch.list) {
|
|
48
|
+
watching.delete(socket.id);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const watcher = { participantId, rooms: watch.rooms };
|
|
52
|
+
watching.set(socket.id, watcher);
|
|
53
|
+
touch(watcher);
|
|
54
|
+
});
|
|
55
|
+
socket.on('disconnect', () => watching.delete(socket.id));
|
|
56
|
+
});
|
|
57
|
+
const feed = attachRoomBookFeed(io, {
|
|
58
|
+
wire: { channel: ROOM_FEED_CHANNEL, parseWatch: parseRoomWatch },
|
|
59
|
+
onChange: (listener) => lobby.onChange(listener),
|
|
60
|
+
isMember: (room, participantId) => lobby.isMember(room, participantId),
|
|
61
|
+
participantId: participantOf,
|
|
62
|
+
log: deps.log ?? jsonLogger('cardtable-feed'),
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
changed: feed.changed,
|
|
66
|
+
close() {
|
|
67
|
+
feed.close();
|
|
68
|
+
clearInterval(touching);
|
|
69
|
+
watching.clear();
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function watchpartyRoomId(watchpartyRoom: string): string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The cardtable room a watchparty room's game slot is bound to.
|
|
3
|
+
*
|
|
4
|
+
* A digest, not the watchparty name. Watchparty hands the embed its room ID
|
|
5
|
+
* (the same for the leader and viewers), but that id is opaque to us — any
|
|
6
|
+
* length or alphabet — and a room id must fit `ROOM_ID_RE` and is printed in
|
|
7
|
+
* the list and every log line. Were a leader KEY ever passed by mistake
|
|
8
|
+
* (`<room>-<16 hex>`, a credential), the digest still never prints it. `wp-`
|
|
9
|
+
* keeps it apart from `r-` (minted) ids; 20 hex is 80 bits.
|
|
10
|
+
*
|
|
11
|
+
* Generic: a non-Magic room binds to a watchparty slot the same way a Magic
|
|
12
|
+
* one does (D21). Split out of `room/lobby.ts` (wave 110 §L).
|
|
13
|
+
*/
|
|
14
|
+
import { createHash } from 'node:crypto';
|
|
15
|
+
export function watchpartyRoomId(watchpartyRoom) {
|
|
16
|
+
return `wp-${createHash('sha256').update(watchpartyRoom).digest('hex').slice(0, 20)}`;
|
|
17
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first host port a room's seat may take.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately clear of everything else on this host: 6080–6081 are the static
|
|
5
|
+
* bench, and the e2e harness picks its ports from 20000 up (`e2e/ports.ts`). `container.sh` refuses
|
|
6
|
+
* anything below 6100 a second time, in case this constant is ever loosened.
|
|
7
|
+
*/
|
|
8
|
+
export declare const ROOM_STREAM_BASE_PORT: number;
|
|
9
|
+
/** Seats in one room. A pod is 8; a constructed table is 2. */
|
|
10
|
+
export declare const MAX_ROOM_SEATS = 8;
|
|
11
|
+
/** No websocket attached for this long → the containers go. See the header. */
|
|
12
|
+
export declare const IDLE_RECLAIM_MS: number;
|
|
13
|
+
export interface RoomStreamSeat {
|
|
14
|
+
seat: number;
|
|
15
|
+
/** The host port websockify is published on, 127.0.0.1 only. */
|
|
16
|
+
port: number;
|
|
17
|
+
container: string;
|
|
18
|
+
}
|
|
19
|
+
export interface RoomStreamAllocation {
|
|
20
|
+
roomKey: string;
|
|
21
|
+
network: string;
|
|
22
|
+
seats: RoomStreamSeat[];
|
|
23
|
+
createdAt: number;
|
|
24
|
+
/** When a websocket was last attached or detached. Drives the idle window. */
|
|
25
|
+
lastActiveAt: number;
|
|
26
|
+
/** Websockets attached right now. Nonzero means somebody is watching. */
|
|
27
|
+
attached: number;
|
|
28
|
+
}
|
|
29
|
+
export type ReclaimReason = 'room-gone' | 'idle' | 'released' | 'orphan';
|
|
30
|
+
/** What a client may be told about a room's streams. Never a port. */
|
|
31
|
+
export interface RoomStreamStatus {
|
|
32
|
+
roomKey: string;
|
|
33
|
+
state: 'live' | 'dead' | 'none';
|
|
34
|
+
seats: number;
|
|
35
|
+
reason?: ReclaimReason;
|
|
36
|
+
/** ISO, when it died. */
|
|
37
|
+
since?: string;
|
|
38
|
+
}
|
|
39
|
+
export type AllocateResult = {
|
|
40
|
+
ok: true;
|
|
41
|
+
allocation: RoomStreamAllocation;
|
|
42
|
+
} | {
|
|
43
|
+
ok: false;
|
|
44
|
+
reason: 'bad-room-key' | 'bad-seat-count' | 'no-ports' | 'provision-failed';
|
|
45
|
+
detail: string;
|
|
46
|
+
};
|
|
47
|
+
/** The seam `scripts/stream/container.sh` sits behind. Injectable for tests. */
|
|
48
|
+
export interface ContainerScript {
|
|
49
|
+
up(name: string, port: number, network: string): Promise<void>;
|
|
50
|
+
down(name: string): Promise<void>;
|
|
51
|
+
list(prefix: string): Promise<string[]>;
|
|
52
|
+
netDown(network: string): Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
export interface RoomStreamsOptions {
|
|
55
|
+
/** The server's own lookup: does this room still exist? Drives the sweep. */
|
|
56
|
+
roomExists: (roomKey: string) => boolean;
|
|
57
|
+
script?: ContainerScript;
|
|
58
|
+
/** Injectable so the idle window is tested with a fake clock, not a wait. */
|
|
59
|
+
now?: () => number;
|
|
60
|
+
basePort?: number;
|
|
61
|
+
poolSize?: number;
|
|
62
|
+
idleMs?: number;
|
|
63
|
+
namePrefix?: string;
|
|
64
|
+
log?: (entry: Record<string, unknown>) => void;
|
|
65
|
+
/**
|
|
66
|
+
* A room's containers actually came down — through `release()`, the idle sweep, or `sweep()`
|
|
67
|
+
* finding the room gone. The ONE place this fires from is `destroy()`, below, so a caller never
|
|
68
|
+
* has to separately watch `release()` and `sweep()` to know when a room ended; `plugins/stream.ts`
|
|
69
|
+
* uses it to tell the matching `StreamedMatchHost` (`runtimes/streamed.ts`) it is settled.
|
|
70
|
+
*/
|
|
71
|
+
onEnded?: (roomKey: string, reason: ReclaimReason) => void;
|
|
72
|
+
}
|
|
73
|
+
export interface SweepReport {
|
|
74
|
+
reclaimed: Array<{
|
|
75
|
+
roomKey: string;
|
|
76
|
+
reason: ReclaimReason;
|
|
77
|
+
}>;
|
|
78
|
+
}
|
|
79
|
+
export interface RoomStreams {
|
|
80
|
+
/** Idempotent: a second call for a live room returns what it already has. */
|
|
81
|
+
allocate(roomKey: string, seatCount: number): Promise<AllocateResult>;
|
|
82
|
+
find(roomKey: string): RoomStreamAllocation | undefined;
|
|
83
|
+
/** The host port for one seat, or null when there is no such live seat. */
|
|
84
|
+
portOf(roomKey: string, seat: number): number | null;
|
|
85
|
+
statusOf(roomKey: string): RoomStreamStatus;
|
|
86
|
+
list(): RoomStreamAllocation[];
|
|
87
|
+
noteAttach(roomKey: string): void;
|
|
88
|
+
noteDetach(roomKey: string): void;
|
|
89
|
+
release(roomKey: string, reason: ReclaimReason): Promise<void>;
|
|
90
|
+
/** Rooms that are gone, and rooms nobody is watching. Safe to call often. */
|
|
91
|
+
sweep(): Promise<SweepReport>;
|
|
92
|
+
/** Containers a previous process left behind, with no allocation for them. */
|
|
93
|
+
sweepOrphans(): Promise<SweepReport>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Where `scripts/stream/container.sh` is, found by walking up from this file.
|
|
97
|
+
*
|
|
98
|
+
* It cannot be a fixed relative path: in tests this module is
|
|
99
|
+
* `packages/game-server/src/stream/alloc.ts`, and in production it is bundled into
|
|
100
|
+
* `apps/server/dist/index.js`. Walking until the script is actually there is
|
|
101
|
+
* the one form that is right in both.
|
|
102
|
+
*/
|
|
103
|
+
export declare function findContainerScript(from?: string): string | null;
|
|
104
|
+
/** The real script, as a `ContainerScript`. `env` is merged over the process's. */
|
|
105
|
+
export declare function scriptRunner(scriptPath: string, env?: Record<string, string>): ContainerScript;
|
|
106
|
+
/**
|
|
107
|
+
* A room key must not be readable as a static stream index.
|
|
108
|
+
*
|
|
109
|
+
* `/stream/3/` is the bench's third container; `/stream/3/0/` must never be a
|
|
110
|
+
* room called `3`. Room ids are minted by the lobby and may be all digits, so
|
|
111
|
+
* the two namespaces are kept disjoint HERE — at allocation — rather than by
|
|
112
|
+
* hoping a parser stays ambiguous in the right direction.
|
|
113
|
+
*/
|
|
114
|
+
export declare function isStaticIndexLike(roomKey: string): boolean;
|
|
115
|
+
export declare function createRoomStreams(options: RoomStreamsOptions): RoomStreams;
|