@hadoku/game-ui 0.1.262 → 0.1.263

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.
@@ -17,6 +17,10 @@ export interface RelayChannel extends MatchChannel {
17
17
  onResult(handler: (winners: readonly number[] | null) => void): () => void;
18
18
  /** What the connection is doing, told on every change: a drop, a planned restart, a rejoin and its answer. */
19
19
  onConnection(handler: (facts: ConnectionFacts) => void): () => void;
20
+ /** A newer tab of this player took the seat: this channel is down until `playHere()`. */
21
+ onRole(handler: (role: 'elsewhere') => void): () => void;
22
+ /** Takes the seat back after `elsewhere`: connects again and joins, and the other tab is told `elsewhere` in turn. */
23
+ playHere(): void;
20
24
  }
21
25
  export interface RelayChannelOptions {
22
26
  /** The server's origin, e.g. `http://localhost:9008`. Omitted: same origin (production). */
@@ -3,7 +3,7 @@
3
3
  * hands a bundle as `ctx.channel`) implemented over the relay socket
4
4
  * (`packages/game-server/src/play/relay-socket.ts`): a socket.io connection on the SAME path and origin
5
5
  * every other socket on this page already uses (`../room/socket-options.ts`), joined with
6
- * `{ matchId: roomKey }`.
6
+ * `{ matchId: roomKey, tabId }` (this tab's own id, `../room/tab-id.ts`).
7
7
  *
8
8
  * Identity, the gate and the seal are the server's own `relay-socket.ts`'s job, exactly as the
9
9
  * play socket's own `attachPlay` — nothing here carries a credential of its own, same as
@@ -14,8 +14,12 @@
14
14
  * reconnect and after every match's end, relays `relay:message` both ways, and says what its connection is doing. A watcher's own `send` is a silent no-op from
15
15
  * out here too, but only because the SERVER drops it without an error — nothing here tracks
16
16
  * whether this socket holds a seat.
17
+ *
18
+ * When a newer tab of this player takes the seat, the server says `relay:role` `elsewhere` and closes
19
+ * this socket: the channel stays down, with no reconnect and no drop told, until `playHere()`.
17
20
  */
18
21
  import { defaultSocketPath, openSocket, socketOptions } from "../room/socket-options.js";
22
+ import { settledTabId } from "../room/tab-id.js";
19
23
  import { trackConnection } from "../track-connection.js";
20
24
  /**
21
25
  * Mirrors `packages/game-server/src/play/relay-socket.ts`'s own `RELAY_EVENTS`. The client has no package
@@ -24,7 +28,7 @@ import { trackConnection } from "../track-connection.js";
24
28
  * `relay:join` the server answers would simply go unrecognised at the join ack's own shape,
25
29
  * which `relay-fixture.spec.ts` (e2e) exercises end to end.
26
30
  */
27
- const RELAY_EVENTS = { join: 'relay:join', message: 'relay:message', ended: 'relay:ended' };
31
+ const RELAY_EVENTS = { join: 'relay:join', message: 'relay:message', ended: 'relay:ended', role: 'relay:role' };
28
32
  function winnersOf(raw) {
29
33
  return Array.isArray(raw) && raw.every((w) => typeof w === 'number') ? raw : null;
30
34
  }
@@ -41,7 +45,11 @@ export function openRelayChannel(matchId, options = {}) {
41
45
  const endedHandlers = new Set();
42
46
  const resultHandlers = new Set();
43
47
  const connectionHandlers = new Set();
48
+ const roleHandlers = new Set();
44
49
  let closed = false;
50
+ let elsewhere = false;
51
+ /** This tab's id, once settled: every join waits for it. */
52
+ let tabId = null;
45
53
  let rejoining = false;
46
54
  /** A result is shown: the next message sent or heard clears it. */
47
55
  let showing = false;
@@ -55,10 +63,10 @@ export function openRelayChannel(matchId, options = {}) {
55
63
  tellResult(null);
56
64
  };
57
65
  const join = () => {
58
- if (closed)
66
+ if (closed || elsewhere || tabId === null)
59
67
  return;
60
68
  // A match that ended before this join answers with its result (`relay-socket.ts`'s ack).
61
- socket.emit(RELAY_EVENTS.join, { matchId }, (ack) => {
69
+ socket.emit(RELAY_EVENTS.join, { matchId, tabId }, (ack) => {
62
70
  if (closed || ack?.ok !== true)
63
71
  return;
64
72
  const answered = rejoining;
@@ -69,8 +77,8 @@ export function openRelayChannel(matchId, options = {}) {
69
77
  tellResult(winnersOf(ack.ended.winners));
70
78
  });
71
79
  };
72
- // The first join goes out at once (socket.io holds it until the connection opens); a reconnect (a
73
- // dropped network, a deploy) joins again on the new one, as a brand new peer to `relay-socket.ts`.
80
+ // The first join goes out once this tab's id is settled (socket.io holds it until the connection opens);
81
+ // a reconnect (a dropped network, a deploy) joins again on the new one, as a brand new peer to `relay-socket.ts`.
74
82
  const connection = trackConnection(socket, {
75
83
  onRejoin: () => {
76
84
  rejoining = true;
@@ -80,14 +88,17 @@ export function openRelayChannel(matchId, options = {}) {
80
88
  // With no snapshot to wait for, a rejoin (`rejoining`: sent on a new connection, not yet answered) is
81
89
  // the whole of coming back, so no "back" line stays over a quiet room. The seats are the room's to show.
82
90
  const tellConnection = () => {
83
- if (closed)
91
+ if (closed || elsewhere)
84
92
  return;
85
93
  const facts = { connected: connection.connected, everConnected: connection.everConnected, serverUpdating: connection.serverUpdating, joins: rejoining ? connection.joins : 1, applied: 0, appliedAtLastJoin: 0, resyncPending: false, seatId: null, lastVerdict: null };
86
94
  for (const handler of [...connectionHandlers])
87
95
  handler(facts);
88
96
  };
89
97
  connection.subscribe(tellConnection);
90
- join();
98
+ void settledTabId().then((id) => {
99
+ tabId = id;
100
+ join();
101
+ });
91
102
  socket.on(RELAY_EVENTS.message, (msg) => {
92
103
  if (closed)
93
104
  return;
@@ -108,6 +119,15 @@ export function openRelayChannel(matchId, options = {}) {
108
119
  handler(winners);
109
120
  tellResult(winners);
110
121
  });
122
+ socket.on(RELAY_EVENTS.role, (word) => {
123
+ if (closed || word?.matchId !== matchId || word.role !== 'elsewhere')
124
+ return;
125
+ // Down before the server's close arrives, so that close is no drop to tell and no reconnect.
126
+ elsewhere = true;
127
+ socket.disconnect();
128
+ for (const handler of [...roleHandlers])
129
+ handler('elsewhere');
130
+ });
111
131
  return {
112
132
  onEnded(handler) {
113
133
  if (closed)
@@ -127,8 +147,21 @@ export function openRelayChannel(matchId, options = {}) {
127
147
  connectionHandlers.add(handler);
128
148
  return () => connectionHandlers.delete(handler);
129
149
  },
130
- send(msg) {
150
+ onRole(handler) {
131
151
  if (closed)
152
+ return () => { };
153
+ roleHandlers.add(handler);
154
+ return () => roleHandlers.delete(handler);
155
+ },
156
+ playHere() {
157
+ if (closed || !elsewhere)
158
+ return;
159
+ elsewhere = false;
160
+ socket.connect(); // joins again through `trackConnection`'s `onRejoin`
161
+ },
162
+ send(msg) {
163
+ // While another tab plays, a send would wait in socket.io's buffer and reach the match after Play here.
164
+ if (closed || elsewhere)
132
165
  return;
133
166
  underway();
134
167
  socket.emit(RELAY_EVENTS.message, msg);
@@ -147,6 +180,7 @@ export function openRelayChannel(matchId, options = {}) {
147
180
  endedHandlers.clear();
148
181
  resultHandlers.clear();
149
182
  connectionHandlers.clear();
183
+ roleHandlers.clear();
150
184
  socket.disconnect();
151
185
  },
152
186
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hadoku/game-ui",
3
- "version": "0.1.262",
4
- "gitHead": "33e217cc0042251707965b5aaac76997909d9fdf",
3
+ "version": "0.1.263",
4
+ "gitHead": "111ef88170f8ed0a5b773c74dd2015fc2869bc1d",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "description": "The browser parts any hosted game shares, above ui-shell: the room shell and session, seats and looks, the room route, the front page's generic pieces (gate, polls, gallery, patch notes), the bundle slot and relay channel a game's client mounts in, settings and trim screens, the HTTP and socket helpers, and the generic components (modal, popover, avatar, action button, radio cards). Knows no game; Magic's UI (games/magic/ui) plugs in, composed by the site (apps/ui). MIT; imports only react, react-dom, socket.io-client, @wolffm/* and the platform packages.",
@@ -53,8 +53,8 @@
53
53
  "react-dom": "^19.2.8"
54
54
  },
55
55
  "dependencies": {
56
- "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.266",
57
- "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.266",
56
+ "@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.267",
57
+ "@wolffm/ui-shell": "npm:@hadoku/ui-shell@0.1.267",
58
58
  "@wolffm/prefs-client": "npm:@hadoku/prefs-client@^1.2.4",
59
59
  "@wolffm/rooms-client": "npm:@hadoku/rooms-client@^0.45.158",
60
60
  "@wolffm/rooms-protocol": "npm:@hadoku/rooms-protocol@^0.45.158",