@hadoku/game-server 0.1.264 → 0.1.265

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.
@@ -0,0 +1,22 @@
1
+ /** What a socket is to a match: it drives a seat, it lost its seat to a newer window, or it has no seat. */
2
+ export type SeatRole = 'player' | 'elsewhere' | 'watcher';
3
+ export interface SeatControl {
4
+ /**
5
+ * `socketId` (of tab `tab`, or null when unnamed) drives `seat` of `matchId` from now: the socket
6
+ * it took the seat from, and whether that was the same tab. A caller qualifies `tab` with the
7
+ * participant, so two accounts' tabs never match.
8
+ */
9
+ take(matchId: string, seat: number, socketId: string, tab: string | null): {
10
+ replaced: string | null;
11
+ sameTab: boolean;
12
+ };
13
+ /** Whether `socketId` drives `seat` now. */
14
+ holds(matchId: string, seat: number, socketId: string): boolean;
15
+ /** The socket driving `seat` now, or null when none does. */
16
+ driver(matchId: string, seat: number): string | null;
17
+ /** `socketId` closed: true when it was driving `seat`, which now has no driver. */
18
+ release(matchId: string, seat: number, socketId: string): boolean;
19
+ /** The match is gone. */
20
+ forget(matchId: string): void;
21
+ }
22
+ export declare function createSeatControl(): SeatControl;
@@ -0,0 +1,46 @@
1
+ /*
2
+ * One driving socket per seat, and it is the newest.
3
+ *
4
+ * Two windows that could both act on one seat would make a mess of the game, so only one may. And
5
+ * an active player never watches their own game, so the window a player opens last is the one they
6
+ * play in: its join takes the seat, and the window it took it from is told `elsewhere` and let go
7
+ * of. That page offers Play here, which is the same join the other way round.
8
+ *
9
+ * The same tab coming back (a socket cut without a close reaching the server, then the tab's own
10
+ * reconnect) takes the place of its own dead socket the same way, and is told nothing: that socket
11
+ * has no page left to tell.
12
+ *
13
+ * This is the rule alone: who drives each seat of each match. Telling a socket its role, closing it
14
+ * and refusing its input are the caller's, which owns the sockets.
15
+ */
16
+ export function createSeatControl() {
17
+ /** Each match's seats, each with its driving socket and that socket's tab. */
18
+ const matches = new Map();
19
+ const driving = (matchId, seat) => matches.get(matchId)?.get(seat);
20
+ return {
21
+ take(matchId, seat, socketId, tab) {
22
+ const seats = matches.get(matchId) ?? new Map();
23
+ matches.set(matchId, seats);
24
+ const before = seats.get(seat);
25
+ seats.set(seat, { socketId, tab });
26
+ // A socket joining again takes nothing from anyone.
27
+ if (before === undefined || before.socketId === socketId)
28
+ return { replaced: null, sameTab: false };
29
+ return { replaced: before.socketId, sameTab: tab !== null && before.tab === tab };
30
+ },
31
+ holds: (matchId, seat, socketId) => driving(matchId, seat)?.socketId === socketId,
32
+ driver: (matchId, seat) => driving(matchId, seat)?.socketId ?? null,
33
+ release(matchId, seat, socketId) {
34
+ const seats = matches.get(matchId);
35
+ if (seats?.get(seat)?.socketId !== socketId)
36
+ return false;
37
+ seats.delete(seat);
38
+ if (seats.size === 0)
39
+ matches.delete(matchId);
40
+ return true;
41
+ },
42
+ forget(matchId) {
43
+ matches.delete(matchId);
44
+ },
45
+ };
46
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-server",
3
- "version": "0.1.264",
4
- "gitHead": "150a898357756dfd5044fb375b226ad1f456d792",
3
+ "version": "0.1.265",
4
+ "gitHead": "8b28f20837041731519c303b5c9aff80deed9780",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The server parts any hosted game shares, above the kernel: the app config and edge auth, the room core (identity, seats, sessions, membership, the room list, the generic room routes), match hosting (the host contract, local and remote JVMs, placement, the start queue, recordings, the in-process and relay runtimes), streams, erase, telemetry and usage. Knows no game; Magic plugs in from apps/server. MIT; imports only node built-ins, express, socket.io, @wolffm/* and the platform packages.",
@@ -16,9 +16,9 @@
16
16
  "play/remote/"
17
17
  ],
18
18
  "dependencies": {
19
- "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.264",
20
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.264",
21
- "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.297",
19
+ "@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.265",
20
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.265",
21
+ "@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.298",
22
22
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.4",
23
23
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.158",
24
24
  "@wolffm/rooms-server": "npm:@hadoku/rooms-server@^0.45.158",