@pieai/swimmer-game-server-kit 0.2.3 → 0.2.4

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/README.md CHANGED
@@ -62,7 +62,7 @@ Install the exact reviewed npm version:
62
62
  ```json
63
63
  {
64
64
  "dependencies": {
65
- "@pieai/swimmer-game-server-kit": "0.2.3"
65
+ "@pieai/swimmer-game-server-kit": "0.2.4"
66
66
  }
67
67
  }
68
68
  ```
@@ -128,6 +128,16 @@ existing creation behavior. The product owns invitation parsing, consent, protoc
128
128
  compatibility and seat/reconnection rules; a public room ID grants no identity or
129
129
  right to take an occupied seat. Actual loopback tests cover both admission paths.
130
130
 
131
+ `onConnectionChange` reports only `connected`, `reconnecting` or `closed` from
132
+ the official SDK's lifecycle events, not raw close reasons. A product that owns
133
+ explicit reauthenticated seat recovery can set `reconnectAutomatically: false`:
134
+ the SDK then makes no hidden retries, disconnected input/restart sends are dropped,
135
+ and the product may offer its own exact-room retry. Default SDK reconnection and
136
+ message buffering are otherwise unchanged. Automatic SDK recovery still requires
137
+ server-side `allowReconnection`; an application-specific seat reservation alone
138
+ does not enable that transport flow. Intentional `leave()` does not emit a user
139
+ connection-failure callback and remains safe after the transport already closed.
140
+
131
141
  ## Auth Usage
132
142
 
133
143
  ```ts
package/dist/client.d.ts CHANGED
@@ -2,16 +2,22 @@ import { type Room } from '@colyseus/sdk';
2
2
  import { type SwimmerGameMessages } from './definition.js';
3
3
  export interface SwimmerGameClient<TInput, TSnapshot, TPlayerId extends string = string> {
4
4
  readonly playerId: TPlayerId | null;
5
+ readonly connectionState: SwimmerConnectionState;
5
6
  readonly room: Room<unknown, TSnapshot>;
6
7
  leave(): Promise<void>;
7
8
  sendInput(input: TInput): void;
8
9
  sendRestart(): void;
9
10
  }
11
+ export type SwimmerConnectionState = 'connected' | 'reconnecting' | 'closed';
10
12
  export interface ConnectSwimmerGameClientOptions<TSnapshot extends object, TPlayerId extends string = string> {
11
13
  endpoint: string;
12
14
  /** Join this existing room through the SDK's joinById. Failure must never
13
15
  * create a replacement room. Omit for the existing joinOrCreate behavior. */
14
16
  roomId?: string;
17
+ /** Keep SDK defaults unless the product owns explicit, reauthenticated
18
+ * exact-room recovery. False also drops gameplay sends while disconnected. */
19
+ reconnectAutomatically?: boolean;
20
+ onConnectionChange?: (state: SwimmerConnectionState) => void;
15
21
  joinOptions?: Record<string, unknown>;
16
22
  messages?: Partial<SwimmerGameMessages>;
17
23
  onError?: (message: string) => void;
package/dist/client.js CHANGED
@@ -14,6 +14,28 @@ export async function connectSwimmerGameClient(options) {
14
14
  ? await client.joinOrCreate(options.roomName, options.joinOptions ?? {})
15
15
  : await client.joinById(options.roomId, options.joinOptions ?? {});
16
16
  let playerId = null;
17
+ let connectionState = 'connected';
18
+ let leaving = false;
19
+ if (options.reconnectAutomatically !== undefined)
20
+ room.reconnection.enabled = options.reconnectAutomatically;
21
+ const changed = (next) => {
22
+ if (connectionState === next)
23
+ return;
24
+ connectionState = next;
25
+ if (next === 'closed')
26
+ playerId = null;
27
+ if (!leaving)
28
+ options.onConnectionChange?.(next);
29
+ };
30
+ // The SDK emits drop even when retry is disabled; do not promise automatic
31
+ // recovery in that mode or report the following leave as another failure.
32
+ room.onDrop(() => changed(options.reconnectAutomatically === false ? 'closed' : 'reconnecting'));
33
+ room.onReconnect(() => {
34
+ changed('connected');
35
+ if (!leaving)
36
+ room.send(messages.identify);
37
+ });
38
+ room.onLeave(() => changed('closed'));
17
39
  room.onMessage(messages.welcome, (payload) => {
18
40
  playerId = payload.playerId;
19
41
  options.onWelcome?.(payload);
@@ -26,18 +48,29 @@ export async function connectSwimmerGameClient(options) {
26
48
  options.onError?.(payload.message ?? 'Unknown game server error.');
27
49
  });
28
50
  room.send(messages.identify);
51
+ options.onConnectionChange?.(connectionState);
29
52
  return {
30
53
  get playerId() {
31
54
  return playerId;
32
55
  },
56
+ get connectionState() {
57
+ return connectionState;
58
+ },
33
59
  room,
34
60
  async leave() {
61
+ leaving = true;
62
+ if (connectionState === 'closed')
63
+ return;
35
64
  await room.leave();
36
65
  },
37
66
  sendInput(input) {
67
+ if (options.reconnectAutomatically === false && connectionState !== 'connected')
68
+ return;
38
69
  room.send(messages.input, { input });
39
70
  },
40
71
  sendRestart() {
72
+ if (options.reconnectAutomatically === false && connectionState !== 'connected')
73
+ return;
41
74
  room.send(messages.restart);
42
75
  },
43
76
  };
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAa,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,0BAA0B,EAA4B,MAAM,iBAAiB,CAAC;AA0BvF,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAK5C,OAA8D;IAE9D,MAAM,QAAQ,GAAG,0BAA0B,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9D,IACE,OAAO,CAAC,MAAM,KAAK,SAAS;QAC5B,CAAC,OAAO,OAAO,CAAC,MAAM,KAAK,QAAQ;YACjC,OAAO,CAAC,MAAM,CAAC,MAAM,KAAK,CAAC;YAC3B,OAAO,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG;YAC3B,CAAC,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,EAC3C,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;IACD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,IAAI,GACR,OAAO,CAAC,MAAM,KAAK,SAAS;QAC1B,CAAC,CAAC,MAAM,MAAM,CAAC,YAAY,CAAY,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,WAAW,IAAI,EAAE,CAAC;QACnF,CAAC,CAAC,MAAM,MAAM,CAAC,QAAQ,CAAY,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,IAAI,E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E,CAAC,CAAC,CAAC;IACH,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC7B,OAAO,CAAC,kBAAkB,EAAE,CAAC,eAAe,CAAC,CAAC;IAE9C,OAAO;QACL,IAAI,QAAQ;YACV,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,IAAI,eAAe;YACjB,OAAO,eAAe,CAAC;QACzB,CAAC;QACD,IAAI;QACJ,KAAK,CAAC,KAAK;YACT,OAAO,GAAG,IAAI,CAAC;YACf,IAAI,eAAe,KAAK,QAAQ;gBAAE,OAAO;YACzC,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACrB,CAAC;QACD,SAAS,CAAC,KAAa;YACrB,IAAI,OAAO,CAAC,sBAAsB,KAAK,KAAK,IAAI,eAAe,KAAK,WAAW;gBAAE,OAAO;YACxF,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACvC,CAAC;QACD,WAAW;YACT,IAAI,OAAO,CAAC,sBAAsB,KAAK,KAAK,IAAI,eAAe,KAAK,WAAW;gBAAE,OAAO;YACxF,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;KACF,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pieai/swimmer-game-server-kit",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Shared Colyseus game server kit for PieAI Swimmer-based games.",
5
5
  "keywords": [
6
6
  "colyseus",