@antha/multiplayer-p2p-authoritative-host 0.0.3 → 0.0.5

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.
@@ -1,6 +1,6 @@
1
1
  import { type ApiAndRoomConnectionState, type ClientId, ControllerClientEvent, ControllerConnectionEvent, ControllerRoomListEvent, type ControllerRoomListListener, type MultiplayerInitParams, type MultiplayerRoomConnection, MultiplayerRoomController, type RoomInput, RoomRejectionError } from '@antha/multiplayer-core';
2
2
  import { type JsonCompatibleValue, type MaybePromise, type PartialWithUndefined } from '@augment-vir/common';
3
- import { ListenTarget, type TypedCustomEventInit } from 'typed-event-target';
3
+ import { ListenTarget, type RemoveListenerCallback, type TypedCustomEventInit } from 'typed-event-target';
4
4
  /**
5
5
  * Message type for {@link P2pAuthoritativeHostMessage}.
6
6
  *
@@ -152,7 +152,9 @@ export declare class P2pAuthoritativeHostMultiplayerController<Input extends Jso
152
152
  *
153
153
  * If a callback is provided, it is called each time the room list is updated.
154
154
  */
155
- startRoomUpdates(callback?: ControllerRoomListListener | undefined): import("typed-event-target").RemoveListenerCallback;
155
+ startRoomUpdates(callback: ControllerRoomListListener): RemoveListenerCallback;
156
+ startRoomUpdates(callback?: undefined): undefined;
157
+ startRoomUpdates(callback?: ControllerRoomListListener | undefined): RemoveListenerCallback | undefined;
156
158
  /** Turn off room list updates and remove callbacks added via `startRoomUpdates`. */
157
159
  stopRoomUpdates(): void;
158
160
  /** Currently joined room id. If a room has not been joined yet, this will be empty. */
@@ -204,12 +206,13 @@ export declare class P2pAuthoritativeHostMultiplayerController<Input extends Jso
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<P2pAuthoritativeHostMessage<Input, State>> | undefined;
214
+ room: Readonly<RoomInput>;
215
+ }>): Promise<MultiplayerRoomConnection<P2pAuthoritativeHostMessage<Input, State>>>;
213
216
  /** Leave the current room or single player connection. */
214
217
  leaveRoom(): void;
215
218
  /** Forward core room-controller events into this state-sync controller. */
@@ -1,5 +1,5 @@
1
1
  import { ControllerClientEvent, ControllerConnectionEvent, ControllerMessageEvent, ControllerRoomListEvent, createMultiplayerId, emptyApiAndRoomConnectionState, MultiplayerConnectionState, MultiplayerRoomController, RoomRejectionError, } from '@antha/multiplayer-core';
2
- import { defineTypedCustomEvent, ListenTarget } from 'typed-event-target';
2
+ import { defineTypedCustomEvent, ListenTarget, } from 'typed-event-target';
3
3
  /**
4
4
  * Message type for {@link P2pAuthoritativeHostMessage}.
5
5
  *
@@ -66,11 +66,6 @@ export class P2pAuthoritativeHostMultiplayerController extends ListenTarget {
66
66
  get clientId() {
67
67
  return this.roomConnection?.clientId || this.localClientId;
68
68
  }
69
- /**
70
- * Listen for room list updates, including while connected to a room.
71
- *
72
- * If a callback is provided, it is called each time the room list is updated.
73
- */
74
69
  startRoomUpdates(callback) {
75
70
  return this.roomController.startRoomUpdates(callback);
76
71
  }
@@ -194,24 +189,29 @@ export class P2pAuthoritativeHostMultiplayerController extends ListenTarget {
194
189
  this.roomController.destroy();
195
190
  super.destroy();
196
191
  }
197
- /**
198
- * Join or create a room.
199
- *
200
- * @throws `Error` if this controller is already connected to a room.
201
- */
192
+ /** Join or create a room. */
202
193
  async joinOrCreateRoom(room) {
203
- if (this.currentConnection) {
194
+ if (this.singleplayer) {
204
195
  throw new Error('Cannot join room: connection already established.');
205
196
  }
197
+ const previousRoomConnection = this.roomConnection;
198
+ const roomConnection = await this.joinRoom({
199
+ previousRoomConnection,
200
+ room,
201
+ });
202
+ this.attachMultiplayerRoomConnection(roomConnection);
203
+ }
204
+ /** Join through the core room controller while preserving the wrapper connection on failure. */
205
+ async joinRoom({ previousRoomConnection, room, }) {
206
206
  try {
207
207
  await this.roomController.joinOrCreateRoom(room);
208
208
  if (!this.roomController.currentConnection) {
209
209
  throw new Error('Cannot start p2p-authoritative-host multiplayer: room connection is missing.');
210
210
  }
211
- this.attachMultiplayerRoomConnection(this.roomController.currentConnection);
211
+ return this.roomController.currentConnection;
212
212
  }
213
213
  catch (error) {
214
- this.roomConnection = undefined;
214
+ this.roomConnection = previousRoomConnection;
215
215
  throw error;
216
216
  }
217
217
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antha/multiplayer-p2p-authoritative-host",
3
- "version": "0.0.3",
3
+ "version": "0.0.5",
4
4
  "description": "P2P authoritative-host multiplayer strategy for the Antha engine.",
5
5
  "keywords": [
6
6
  "vir",
@@ -48,7 +48,7 @@
48
48
  "istanbul-smart-text-reporter": "^1.1.5"
49
49
  },
50
50
  "peerDependencies": {
51
- "@antha/multiplayer-core": "^0.0.3"
51
+ "@antha/multiplayer-core": "^0.0.5"
52
52
  },
53
53
  "engines": {
54
54
  "node": ">=22"