@hadoku/game-server 0.1.266 → 0.1.267
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/dist/play/relay-socket.d.ts +9 -2
- package/dist/play/relay-socket.js +40 -22
- package/package.json +5 -5
|
@@ -33,9 +33,14 @@
|
|
|
33
33
|
* rejoins by sending `relay:join` again on the socket it already has (`relay-channel.ts` does on
|
|
34
34
|
* every `relay:ended`): no new event and no reconnect. Until it does, that socket speaks for
|
|
35
35
|
* nothing — its sends and its disconnect belong to the match it joined, which is over.
|
|
36
|
+
*
|
|
37
|
+
* ## One tab drives a seat
|
|
38
|
+
*
|
|
39
|
+
* The newest socket of a seat drives it (`./seat-control.ts`). The one it replaced is sent `relay:role`
|
|
40
|
+
* `elsewhere` and closed, so its page can offer Play here; the same tab's dead socket is closed silently.
|
|
36
41
|
*/
|
|
37
42
|
import type { Server as SocketIOServer } from 'socket.io';
|
|
38
|
-
import type
|
|
43
|
+
import { type ParticipantId } from '@wolffm/rooms-protocol';
|
|
39
44
|
import { type RateLimitConfig } from '@wolffm/rooms-server';
|
|
40
45
|
import { type Gate } from '@wolffm/service-kernel/http';
|
|
41
46
|
import { type RelayOutcome, type RelaySession, type RelaySink } from './runtimes/relay.js';
|
|
@@ -46,6 +51,8 @@ export declare const RELAY_EVENTS: {
|
|
|
46
51
|
* numbers), or `{ winners: null }` when every seat left. To every live socket of the match,
|
|
47
52
|
* the sender of `over` and every watcher included, so the room can show the result. */
|
|
48
53
|
readonly ended: "relay:ended";
|
|
54
|
+
/** `{ matchId, role: 'elsewhere' }`: a newer tab of this player took the seat, and this socket is closed. */
|
|
55
|
+
readonly role: "relay:role";
|
|
49
56
|
};
|
|
50
57
|
/** Messages per sender: four times bomberman's own host, which sends its state at 30 Hz, so no
|
|
51
58
|
* game this relay was built for comes near it, and a bundle that floods its room does. */
|
|
@@ -86,7 +93,7 @@ export interface RelayRoster {
|
|
|
86
93
|
} | undefined;
|
|
87
94
|
/** This match's own session, wired to `sink`; `onEnded` is this file's own cleanup, called once. */
|
|
88
95
|
attach(sink: RelaySink, onEnded: (outcome: RelayOutcome | null) => void): RelaySession;
|
|
89
|
-
/** A seat's
|
|
96
|
+
/** A seat's socket joined, or the socket driving it went. Absent: nothing is told. */
|
|
90
97
|
seatConnected?(seat: number): void;
|
|
91
98
|
seatDisconnected?(seat: number): void;
|
|
92
99
|
}
|
|
@@ -33,13 +33,20 @@
|
|
|
33
33
|
* rejoins by sending `relay:join` again on the socket it already has (`relay-channel.ts` does on
|
|
34
34
|
* every `relay:ended`): no new event and no reconnect. Until it does, that socket speaks for
|
|
35
35
|
* nothing — its sends and its disconnect belong to the match it joined, which is over.
|
|
36
|
+
*
|
|
37
|
+
* ## One tab drives a seat
|
|
38
|
+
*
|
|
39
|
+
* The newest socket of a seat drives it (`./seat-control.ts`). The one it replaced is sent `relay:role`
|
|
40
|
+
* `elsewhere` and closed, so its page can offer Play here; the same tab's dead socket is closed silently.
|
|
36
41
|
*/
|
|
42
|
+
import { isTabId } from '@wolffm/rooms-protocol';
|
|
37
43
|
import { MultiRateLimiter } from '@wolffm/rooms-server';
|
|
38
44
|
import { createEdgeAuth, rankOf, tierOf } from '@wolffm/service-kernel/auth';
|
|
39
45
|
import { createGate } from '@wolffm/service-kernel/http';
|
|
40
46
|
import { identityFromHeaders } from '../room/identity.js';
|
|
41
47
|
import { handshakeEdgeTier } from '../room/socket-auth.js';
|
|
42
48
|
import { costlyActLimit } from '../http/request.js';
|
|
49
|
+
import { createSeatControl } from './seat-control.js';
|
|
43
50
|
export const RELAY_EVENTS = {
|
|
44
51
|
join: 'relay:join',
|
|
45
52
|
message: 'relay:message',
|
|
@@ -47,6 +54,8 @@ export const RELAY_EVENTS = {
|
|
|
47
54
|
* numbers), or `{ winners: null }` when every seat left. To every live socket of the match,
|
|
48
55
|
* the sender of `over` and every watcher included, so the room can show the result. */
|
|
49
56
|
ended: 'relay:ended',
|
|
57
|
+
/** `{ matchId, role: 'elsewhere' }`: a newer tab of this player took the seat, and this socket is closed. */
|
|
58
|
+
role: 'relay:role',
|
|
50
59
|
};
|
|
51
60
|
/** Messages per sender: four times bomberman's own host, which sends its state at 30 Hz, so no
|
|
52
61
|
* game this relay was built for comes near it, and a bundle that floods its room does. */
|
|
@@ -105,6 +114,11 @@ export function attachRelay(io, deps) {
|
|
|
105
114
|
(deps.gate ?? defaultRelayGate(deps)).gateSocketIo(io, { minTier: deps.minTier, surface: 'play-socket' });
|
|
106
115
|
const log = deps.log ?? (() => { });
|
|
107
116
|
const live = new Map();
|
|
117
|
+
const seats = createSeatControl();
|
|
118
|
+
const driverOf = (matchId, seat) => {
|
|
119
|
+
const id = seats.driver(matchId, seat);
|
|
120
|
+
return id === null ? undefined : io.sockets.sockets.get(id);
|
|
121
|
+
};
|
|
108
122
|
const joinLimit = costlyActLimit(deps.joinRateLimit === undefined ? RELAY_JOIN_RATE_LIMIT : deps.joinRateLimit, {
|
|
109
123
|
act: 'relay-join',
|
|
110
124
|
onRefused: () => log({ evt: 'relay-join-refused', reason: 'rate-limited' }),
|
|
@@ -117,12 +131,10 @@ export function attachRelay(io, deps) {
|
|
|
117
131
|
const found = live.get(matchId);
|
|
118
132
|
if (found !== undefined)
|
|
119
133
|
return found;
|
|
120
|
-
const seatSockets = new Map();
|
|
121
134
|
const watcherSockets = new Set();
|
|
122
135
|
const session = roster.attach({
|
|
123
136
|
toSeat: (seat, msg) => {
|
|
124
|
-
|
|
125
|
-
s.emit(RELAY_EVENTS.message, msg);
|
|
137
|
+
driverOf(matchId, seat)?.emit(RELAY_EVENTS.message, msg);
|
|
126
138
|
},
|
|
127
139
|
toWatchers: (msg) => {
|
|
128
140
|
for (const s of watcherSockets)
|
|
@@ -131,16 +143,16 @@ export function attachRelay(io, deps) {
|
|
|
131
143
|
}, (outcome) => {
|
|
132
144
|
const ending = live.get(matchId);
|
|
133
145
|
const word = { winners: outcome?.winners ?? null };
|
|
134
|
-
for (
|
|
135
|
-
|
|
136
|
-
s.emit(RELAY_EVENTS.ended, word);
|
|
146
|
+
for (let seat = 0; seat < roster.seatCount; seat += 1)
|
|
147
|
+
driverOf(matchId, seat)?.emit(RELAY_EVENTS.ended, word);
|
|
137
148
|
for (const s of ending?.watcherSockets ?? [])
|
|
138
149
|
s.emit(RELAY_EVENTS.ended, word);
|
|
139
150
|
live.delete(matchId);
|
|
151
|
+
seats.forget(matchId);
|
|
140
152
|
log({ evt: 'relay-ended', matchId, winners: word.winners });
|
|
141
153
|
deps.onEnded?.(matchId, outcome);
|
|
142
154
|
});
|
|
143
|
-
const entry = { session, roster,
|
|
155
|
+
const entry = { session, roster, watcherSockets };
|
|
144
156
|
live.set(matchId, entry);
|
|
145
157
|
return entry;
|
|
146
158
|
}
|
|
@@ -149,6 +161,8 @@ export function attachRelay(io, deps) {
|
|
|
149
161
|
// The match this socket joined, by its live entry rather than its id: a room's next match
|
|
150
162
|
// lives under the same id, and a socket that has not rejoined it speaks for nothing there.
|
|
151
163
|
let joined = null;
|
|
164
|
+
/** This socket's sends were refused for having lost its seat: said once, not once per send. */
|
|
165
|
+
let refusedElsewhere = false;
|
|
152
166
|
socket.on(RELAY_EVENTS.join, (payload, ack) => {
|
|
153
167
|
const reply = ack ?? (() => { });
|
|
154
168
|
const matchId = readMatchId(payload);
|
|
@@ -175,12 +189,14 @@ export function attachRelay(io, deps) {
|
|
|
175
189
|
match.watcherSockets.add(socket);
|
|
176
190
|
}
|
|
177
191
|
else {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
192
|
+
const tab = payload.tabId;
|
|
193
|
+
const { replaced, sameTab } = seats.take(matchId, role, socket.id, isTabId(tab) ? `${participantId}\n${tab}` : null);
|
|
194
|
+
const old = replaced === null ? undefined : io.sockets.sockets.get(replaced);
|
|
195
|
+
if (old !== undefined && !sameTab)
|
|
196
|
+
old.emit(RELAY_EVENTS.role, { matchId, role: 'elsewhere' });
|
|
197
|
+
old?.disconnect(true);
|
|
198
|
+
// Told on every seat join, the same as `MatchShell.seatConnected` itself is: harmless to
|
|
199
|
+
// tell twice, and cheaper than tracking whether the seat had a driver a moment ago.
|
|
184
200
|
match.roster.seatConnected?.(role);
|
|
185
201
|
}
|
|
186
202
|
joined = { matchId, match, seat: role };
|
|
@@ -201,6 +217,12 @@ export function attachRelay(io, deps) {
|
|
|
201
217
|
log({ evt: 'relay-send-refused', matchId: joined.matchId, reason: 'watcher' });
|
|
202
218
|
return;
|
|
203
219
|
}
|
|
220
|
+
if (!seats.holds(joined.matchId, joined.seat, socket.id)) {
|
|
221
|
+
if (!refusedElsewhere)
|
|
222
|
+
log({ evt: 'relay-send-refused', matchId: joined.matchId, reason: 'elsewhere' });
|
|
223
|
+
refusedElsewhere = true;
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
204
226
|
if (messageLimit !== null && !messageLimit.allow(participantId)) {
|
|
205
227
|
if (!throttled.has(participantId))
|
|
206
228
|
log({ evt: 'relay-send-refused', matchId: joined.matchId, reason: 'rate-limited' });
|
|
@@ -219,15 +241,11 @@ export function attachRelay(io, deps) {
|
|
|
219
241
|
if (joined.seat === null) {
|
|
220
242
|
match.watcherSockets.delete(socket);
|
|
221
243
|
}
|
|
222
|
-
else {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
// good is the roster's own rejoin-grace timer's call — told only once the
|
|
228
|
-
// seat's last live socket is actually gone, never on an earlier one among several tabs.
|
|
229
|
-
if (set === undefined || set.size === 0)
|
|
230
|
-
match.roster.seatDisconnected?.(joined.seat);
|
|
244
|
+
else if (seats.release(joined.matchId, joined.seat, socket.id)) {
|
|
245
|
+
// A plain disconnect is not a leave: rejoining is `relay:join` again on a new socket.
|
|
246
|
+
// Whether this is a leave for good is the roster's own rejoin-grace timer's call, told
|
|
247
|
+
// only when the seat's driver goes, never when a socket a newer tab replaced does.
|
|
248
|
+
match.roster.seatDisconnected?.(joined.seat);
|
|
231
249
|
}
|
|
232
250
|
log({ evt: 'relay-disconnected', matchId: joined.matchId, seat: joined.seat });
|
|
233
251
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hadoku/game-server",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "0.1.267",
|
|
4
|
+
"gitHead": "111ef88170f8ed0a5b773c74dd2015fc2869bc1d",
|
|
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.
|
|
20
|
-
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.
|
|
21
|
-
"@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.
|
|
19
|
+
"@wolffm/host-wire": "npm:@hadoku/host-wire@0.1.267",
|
|
20
|
+
"@wolffm/platform-wire": "npm:@hadoku/platform-wire@0.1.267",
|
|
21
|
+
"@wolffm/service-kernel": "npm:@hadoku/service-kernel@0.1.300",
|
|
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",
|