@antha/multiplayer-p2p-lock-step 0.0.4 → 0.0.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.
@@ -18,7 +18,7 @@ export declare enum P2pLockStepMessageType {
18
18
  */
19
19
  export type FrameEventDetail<MultiplayerPacket extends JsonCompatibleValue> = {
20
20
  packet: MultiplayerPacket;
21
- clientId: ClientId;
21
+ sourceClientId: ClientId;
22
22
  };
23
23
  /**
24
24
  * Message exchanged by p2p-lock-step clients.
@@ -31,7 +31,9 @@ export type P2pLockStepMessage<MultiplayerPacket extends JsonCompatibleValue> =
31
31
  type: P2pLockStepMessageType.Actions;
32
32
  sourceClientId: ClientId;
33
33
  actions: MultiplayerPacket[];
34
- } | {
34
+ }
35
+ /** Sent from the host to clients when a frame is ready. */
36
+ | {
35
37
  type: P2pLockStepMessageType.Frame;
36
38
  actions: FrameEventDetail<MultiplayerPacket>[];
37
39
  };
@@ -204,12 +206,13 @@ export declare class P2pLockStepMultiplayerController<MultiplayerPacket extends
204
206
  isConnected(): boolean;
205
207
  /** Cleanup everything. */
206
208
  destroy(): void;
207
- /**
208
- * Join or create a room.
209
- *
210
- * @throws `Error` if this controller is already connected to a room.
211
- */
209
+ /** Join or create a room. */
212
210
  joinOrCreateRoom(room: Readonly<RoomInput>): Promise<void>;
211
+ /** Join through the core room controller while preserving the wrapper connection on failure. */
212
+ protected joinRoom({ previousRoomConnection, room, }: Readonly<{
213
+ previousRoomConnection: MultiplayerRoomConnection<P2pLockStepMessage<MultiplayerPacket>> | undefined;
214
+ room: Readonly<RoomInput>;
215
+ }>): Promise<MultiplayerRoomConnection<P2pLockStepMessage<MultiplayerPacket>>>;
213
216
  /** Leave the current room or single player connection. */
214
217
  leaveRoom(): void;
215
218
  /** Forward core room-controller events into this frame-sync controller. */
@@ -198,7 +198,7 @@ export class P2pLockStepMultiplayerController extends ListenTarget {
198
198
  ...this.frameActions,
199
199
  ...actionArray.map((packet) => {
200
200
  return {
201
- clientId: this.clientId,
201
+ sourceClientId: this.clientId,
202
202
  packet,
203
203
  };
204
204
  }),
@@ -221,28 +221,39 @@ export class P2pLockStepMultiplayerController extends ListenTarget {
221
221
  this.roomController.destroy();
222
222
  super.destroy();
223
223
  }
224
- /**
225
- * Join or create a room.
226
- *
227
- * @throws `Error` if this controller is already connected to a room.
228
- */
224
+ /** Join or create a room. */
229
225
  async joinOrCreateRoom(room) {
230
- if (this.currentConnection) {
226
+ if (this.singleplayer) {
231
227
  throw new Error('Cannot join room: connection already established.');
232
228
  }
229
+ const previousRoomConnection = this.roomConnection;
233
230
  this.debugLog(`joining or creating room '${room.roomName}' (${room.roomId})`);
231
+ const roomConnection = await this.joinRoom({
232
+ previousRoomConnection,
233
+ room,
234
+ });
235
+ if (previousRoomConnection) {
236
+ globalThis.clearTimeout(this.timeoutId);
237
+ this.clientsResponded = {};
238
+ this.frameActions = [];
239
+ this.frameTickReady = true;
240
+ }
241
+ this.attachMultiplayerRoomConnection(roomConnection);
242
+ this.debugLog(`attached p2p-lock-step connection; client=${this.getClientId() || 'unknown'} host=${this.isHost()} connected=${this.isConnected()}`);
243
+ }
244
+ /** Join through the core room controller while preserving the wrapper connection on failure. */
245
+ async joinRoom({ previousRoomConnection, room, }) {
234
246
  try {
235
247
  await this.roomController.joinOrCreateRoom(room);
236
248
  this.debugLog(`room controller joined room '${room.roomName}' (${room.roomId}); client=${this.roomController.getClientId() || 'unknown'} host=${this.roomController.isHost()}`);
237
249
  if (!this.roomController.currentConnection) {
238
250
  throw new Error('Cannot start p2p-lock-step multiplayer: room connection is missing.');
239
251
  }
240
- this.attachMultiplayerRoomConnection(this.roomController.currentConnection);
241
- this.debugLog(`attached p2p-lock-step connection; client=${this.getClientId() || 'unknown'} host=${this.isHost()} connected=${this.isConnected()}`);
252
+ return this.roomController.currentConnection;
242
253
  }
243
254
  catch (error) {
244
255
  this.debugLog(`join room failed: ${String(error)}`);
245
- this.roomConnection = undefined;
256
+ this.roomConnection = previousRoomConnection;
246
257
  throw error;
247
258
  }
248
259
  }
@@ -322,7 +333,7 @@ export class P2pLockStepMultiplayerController extends ListenTarget {
322
333
  ...this.frameActions,
323
334
  ...message.actions.map((packet) => {
324
335
  return {
325
- clientId: sourceClientId,
336
+ sourceClientId,
326
337
  packet,
327
338
  };
328
339
  }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/multiplayer-p2p-lock-step",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Multiplayer mod for the Antha engine.",
5
5
  "keywords": [
6
6
  "vir",
@@ -49,7 +49,7 @@
49
49
  "istanbul-smart-text-reporter": "^1.1.5"
50
50
  },
51
51
  "peerDependencies": {
52
- "@antha/multiplayer-core": "^0.0.4"
52
+ "@antha/multiplayer-core": "^0.0.6"
53
53
  },
54
54
  "engines": {
55
55
  "node": ">=22"