@antha/multiplayer-core 0.0.1 → 0.0.2
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.
|
@@ -5,6 +5,7 @@ import { ListenTarget } from 'typed-event-target';
|
|
|
5
5
|
import { type ClientId, type RoomId } from '../multiplayer-id.js';
|
|
6
6
|
import { type RoomInput } from '../webrtc/webrtc-multiplayer-controller.js';
|
|
7
7
|
import { RoomRejectionError } from './errors.js';
|
|
8
|
+
import { type MultiplayerClientRooms } from './multiplayer-api.js';
|
|
8
9
|
import { type MultiplayerApiClient } from './multiplayer-client.js';
|
|
9
10
|
/**
|
|
10
11
|
* Connection state for {@link MultiplayerRoomController}.
|
|
@@ -173,13 +174,20 @@ declare const ControllerRoomListEvent_base: (new (eventInitDict: {
|
|
|
173
174
|
hasRoomPassword: boolean;
|
|
174
175
|
}>>>, "controller-room-list">, "type">;
|
|
175
176
|
/**
|
|
176
|
-
* This is called whenever the room list updates, even if there were no changes to the room list.
|
|
177
|
-
*
|
|
177
|
+
* This is called whenever the room list updates, even if there were no changes to the room list. By
|
|
178
|
+
* default, room list updates are off. Use `MultiplayerRoomController.startRoomUpdates` to start
|
|
179
|
+
* room list updates.
|
|
178
180
|
*
|
|
179
181
|
* @category Events
|
|
180
182
|
*/
|
|
181
183
|
export declare class ControllerRoomListEvent extends ControllerRoomListEvent_base {
|
|
182
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Callback fired whenever the room list is fetched from the multiplayer API.
|
|
187
|
+
*
|
|
188
|
+
* @category Internal
|
|
189
|
+
*/
|
|
190
|
+
export type ControllerRoomListListener = (rooms: Readonly<MultiplayerClientRooms>) => MaybePromise<void>;
|
|
183
191
|
declare const ControllerClientEvent_base: (new (eventInitDict: {
|
|
184
192
|
bubbles?: boolean;
|
|
185
193
|
cancelable?: boolean;
|
|
@@ -334,11 +342,7 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
|
|
|
334
342
|
readonly knownErrors: {
|
|
335
343
|
RoomRejectionError: typeof RoomRejectionError;
|
|
336
344
|
};
|
|
337
|
-
|
|
338
|
-
* Set to `false` to disable room updates, even when still not connected to a room in
|
|
339
|
-
* multiplayer mode.
|
|
340
|
-
*/
|
|
341
|
-
enableRoomUpdates: boolean;
|
|
345
|
+
protected isListeningToRoomUpdates: boolean;
|
|
342
346
|
/** Currently joined room id. If a room has not been joined yet, this will be empty. */
|
|
343
347
|
readonly roomId: RoomId | undefined;
|
|
344
348
|
/** The current connection state of the controller's connection to a backend API. */
|
|
@@ -357,11 +361,7 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
|
|
|
357
361
|
* playing in single player.
|
|
358
362
|
*/
|
|
359
363
|
multiplayerApiClient: Readonly<MultiplayerApiClient> | undefined;
|
|
360
|
-
/**
|
|
361
|
-
* Used to keep track of the room update interval. This will be set when the controller is
|
|
362
|
-
* constructed in multiplayer mode or when a room is left. This will be cleared when a room is
|
|
363
|
-
* joined or if the controller is destroyed.
|
|
364
|
-
*/
|
|
364
|
+
/** Used to keep track of the room update interval. */
|
|
365
365
|
protected roomUpdateIntervalId: ReturnType<typeof globalThis.setInterval> | undefined;
|
|
366
366
|
/** This is populated when `.initMultiplayer` is called. */
|
|
367
367
|
protected multiplayerParams: Readonly<MultiplayerInitParams> | undefined;
|
|
@@ -389,10 +389,17 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
|
|
|
389
389
|
constructor(params: MultiplayerRoomControllerParams<Message>);
|
|
390
390
|
/**
|
|
391
391
|
* Start multiplayer mode. This initializes
|
|
392
|
-
* {@link MultiplayerRoomController.multiplayerApiClient}
|
|
393
|
-
* {@link MultiplayerRoomController.roomUpdateIntervalId}.
|
|
392
|
+
* {@link MultiplayerRoomController.multiplayerApiClient}.
|
|
394
393
|
*/
|
|
395
394
|
initMultiplayer(params: Readonly<MultiplayerInitParams>): Promise<void>;
|
|
395
|
+
/**
|
|
396
|
+
* Listen for room list updates, including while connected to a room.
|
|
397
|
+
*
|
|
398
|
+
* If a callback is provided, it is called each time the room list is updated.
|
|
399
|
+
*/
|
|
400
|
+
startRoomUpdates(callback?: ControllerRoomListListener | undefined): import("typed-event-target").RemoveListenerCallback;
|
|
401
|
+
/** Turn off room list updates and remove callbacks added via `startRoomUpdates`. */
|
|
402
|
+
stopRoomUpdates(): void;
|
|
396
403
|
/** Send a generic message to the current room. */
|
|
397
404
|
sendMessage(message: Readonly<Message>): void;
|
|
398
405
|
/** Detects if this controller is the room host or not. */
|
|
@@ -411,6 +418,8 @@ export declare class MultiplayerRoomController<Message extends JsonCompatibleVal
|
|
|
411
418
|
leaveRoom(): void;
|
|
412
419
|
/** Set the current connection state and fire listeners. */
|
|
413
420
|
protected updateConnectionState(state: Partial<ApiAndRoomConnectionState>): void;
|
|
421
|
+
/** Stop polling the multiplayer server for room updates. */
|
|
422
|
+
protected stopRoomInterval(): void;
|
|
414
423
|
/** Starts polling the multiplayer server for room updates and fires listeners. */
|
|
415
424
|
protected startRoomInterval(): void;
|
|
416
425
|
}
|
|
@@ -42,8 +42,9 @@ export class ControllerMessageEvent extends defineTypedCustomEvent()('controller
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
* This is called whenever the room list updates, even if there were no changes to the room list.
|
|
46
|
-
*
|
|
45
|
+
* This is called whenever the room list updates, even if there were no changes to the room list. By
|
|
46
|
+
* default, room list updates are off. Use `MultiplayerRoomController.startRoomUpdates` to start
|
|
47
|
+
* room list updates.
|
|
47
48
|
*
|
|
48
49
|
* @category Events
|
|
49
50
|
*/
|
|
@@ -89,11 +90,7 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
89
90
|
RoomRejectionError,
|
|
90
91
|
};
|
|
91
92
|
knownErrors = MultiplayerRoomController.knownErrors;
|
|
92
|
-
|
|
93
|
-
* Set to `false` to disable room updates, even when still not connected to a room in
|
|
94
|
-
* multiplayer mode.
|
|
95
|
-
*/
|
|
96
|
-
enableRoomUpdates = true;
|
|
93
|
+
isListeningToRoomUpdates = false;
|
|
97
94
|
/** Currently joined room id. If a room has not been joined yet, this will be empty. */
|
|
98
95
|
roomId;
|
|
99
96
|
/** The current connection state of the controller's connection to a backend API. */
|
|
@@ -112,11 +109,7 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
112
109
|
* playing in single player.
|
|
113
110
|
*/
|
|
114
111
|
multiplayerApiClient;
|
|
115
|
-
/**
|
|
116
|
-
* Used to keep track of the room update interval. This will be set when the controller is
|
|
117
|
-
* constructed in multiplayer mode or when a room is left. This will be cleared when a room is
|
|
118
|
-
* joined or if the controller is destroyed.
|
|
119
|
-
*/
|
|
112
|
+
/** Used to keep track of the room update interval. */
|
|
120
113
|
roomUpdateIntervalId;
|
|
121
114
|
/** This is populated when `.initMultiplayer` is called. */
|
|
122
115
|
multiplayerParams;
|
|
@@ -153,8 +146,7 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
153
146
|
}
|
|
154
147
|
/**
|
|
155
148
|
* Start multiplayer mode. This initializes
|
|
156
|
-
* {@link MultiplayerRoomController.multiplayerApiClient}
|
|
157
|
-
* {@link MultiplayerRoomController.roomUpdateIntervalId}.
|
|
149
|
+
* {@link MultiplayerRoomController.multiplayerApiClient}.
|
|
158
150
|
*/
|
|
159
151
|
async initMultiplayer(params) {
|
|
160
152
|
if (this.currentConnection) {
|
|
@@ -185,7 +177,23 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
185
177
|
});
|
|
186
178
|
throw error;
|
|
187
179
|
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Listen for room list updates, including while connected to a room.
|
|
183
|
+
*
|
|
184
|
+
* If a callback is provided, it is called each time the room list is updated.
|
|
185
|
+
*/
|
|
186
|
+
startRoomUpdates(callback) {
|
|
187
|
+
this.isListeningToRoomUpdates = true;
|
|
188
188
|
this.startRoomInterval();
|
|
189
|
+
return this.listen(ControllerRoomListEvent, async (event) => {
|
|
190
|
+
await callback?.(event.detail);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
/** Turn off room list updates and remove callbacks added via `startRoomUpdates`. */
|
|
194
|
+
stopRoomUpdates() {
|
|
195
|
+
this.isListeningToRoomUpdates = false;
|
|
196
|
+
this.stopRoomInterval();
|
|
189
197
|
}
|
|
190
198
|
/** Send a generic message to the current room. */
|
|
191
199
|
sendMessage(message) {
|
|
@@ -210,7 +218,7 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
210
218
|
api: MultiplayerConnectionState.Disconnected,
|
|
211
219
|
});
|
|
212
220
|
this.currentConnection?.destroy();
|
|
213
|
-
|
|
221
|
+
this.stopRoomUpdates();
|
|
214
222
|
}
|
|
215
223
|
/**
|
|
216
224
|
* Join or create a room.
|
|
@@ -257,7 +265,6 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
257
265
|
});
|
|
258
266
|
if (connectionResult.connected) {
|
|
259
267
|
makeWritable(this).roomId = room.roomId;
|
|
260
|
-
globalThis.clearInterval(this.roomUpdateIntervalId);
|
|
261
268
|
this.updateConnectionState({
|
|
262
269
|
room: MultiplayerConnectionState.Connected,
|
|
263
270
|
});
|
|
@@ -281,7 +288,6 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
281
288
|
makeWritable(this).roomId = undefined;
|
|
282
289
|
this.currentConnection.destroy();
|
|
283
290
|
this.currentConnection = undefined;
|
|
284
|
-
this.startRoomInterval();
|
|
285
291
|
this.updateConnectionState({
|
|
286
292
|
room: MultiplayerConnectionState.Disconnected,
|
|
287
293
|
});
|
|
@@ -301,18 +307,22 @@ export class MultiplayerRoomController extends ListenTarget {
|
|
|
301
307
|
},
|
|
302
308
|
}));
|
|
303
309
|
}
|
|
310
|
+
/** Stop polling the multiplayer server for room updates. */
|
|
311
|
+
stopRoomInterval() {
|
|
312
|
+
globalThis.clearInterval(this.roomUpdateIntervalId);
|
|
313
|
+
this.roomUpdateIntervalId = undefined;
|
|
314
|
+
}
|
|
304
315
|
/** Starts polling the multiplayer server for room updates and fires listeners. */
|
|
305
316
|
startRoomInterval() {
|
|
306
|
-
|
|
317
|
+
this.stopRoomInterval();
|
|
318
|
+
if (this.isListeningToRoomUpdates && this.multiplayerApiClient) {
|
|
307
319
|
const roomUpdateMs = this.multiplayerParams?.roomUpdateInterval
|
|
308
320
|
? convertDuration(this.multiplayerParams.roomUpdateInterval, {
|
|
309
321
|
milliseconds: true,
|
|
310
322
|
}).milliseconds
|
|
311
323
|
: 10_000;
|
|
312
324
|
this.roomUpdateIntervalId = globalThis.setInterval(async () => {
|
|
313
|
-
if (this.
|
|
314
|
-
!this.multiplayerApiClient ||
|
|
315
|
-
!this.enableRoomUpdates) {
|
|
325
|
+
if (!this.isListeningToRoomUpdates || !this.multiplayerApiClient) {
|
|
316
326
|
return;
|
|
317
327
|
}
|
|
318
328
|
const output = await this.multiplayerApiClient.fetch(multiplayerRoomsEndpoint).GET({
|