@zhuweiyou/dsh-holdem 0.1.2 → 0.1.3

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/lib/index.js CHANGED
@@ -24,6 +24,8 @@ const MAX_FRAME_BYTES = 64 * 1024;
24
24
  const TICK_MS = 250;
25
25
  const SESSION_TTL_MS = 1440 * 60 * 1e3;
26
26
  const DISCONNECT_AFTER_MS = 45e3;
27
+ /** 没有实时通道时,HTTP 请求也算「人在」:这么久没动静才标成离线。 */
28
+ const PRESENCE_TTL_MS = 2e4;
27
29
  const LOBBY_KICK_IDLE_MS = 600 * 1e3;
28
30
  const CHAT_COOLDOWN_MS = 1200;
29
31
  const BUY_IN_BB = 100;
@@ -1446,7 +1448,7 @@ function traceWs(line) {
1446
1448
  function registerHoldemSocket(server, manager) {
1447
1449
  const wss = new WebSocketServer({
1448
1450
  noServer: true,
1449
- handleProtocols: (protocols) => protocols.has(SUBPROTOCOL) ? SUBPROTOCOL : false
1451
+ handleProtocols: (protocols) => protocols.has(SUBPROTOCOL) ? SUBPROTOCOL : ""
1450
1452
  });
1451
1453
  const sockets = /* @__PURE__ */ new Map();
1452
1454
  const off = manager.onEvent((playerId, event) => {
@@ -3168,8 +3170,8 @@ var RoomManager = class {
3168
3170
  if (cookieSession) {
3169
3171
  const player$1 = room.players.get(cookieSession.playerId);
3170
3172
  if (player$1 && !player$1.leaving) {
3171
- player$1.connected = true;
3172
3173
  player$1.browserId = input.browserId ?? player$1.browserId;
3174
+ this.touch(room, player$1);
3173
3175
  return {
3174
3176
  playerId: player$1.playerId,
3175
3177
  roomId: room.roomId,
@@ -3260,7 +3262,8 @@ var RoomManager = class {
3260
3262
  view(roomId, playerId) {
3261
3263
  const room = this.roomOf(playerId);
3262
3264
  if (!room || room.roomId !== roomId) throw new CommandError("auth", "不在这个房间里");
3263
- if (room.hostPlayerId === playerId) room.hostLastSeen = this.now();
3265
+ const player = room.players.get(playerId);
3266
+ if (player) this.touch(room, player);
3264
3267
  return this.project(room, playerId);
3265
3268
  }
3266
3269
  command(roomId, playerId, command, baseSeq, actionId) {
@@ -3268,7 +3271,7 @@ var RoomManager = class {
3268
3271
  if (!room || room.roomId !== roomId) throw new CommandError("auth", "不在这个房间里");
3269
3272
  const player = room.players.get(playerId);
3270
3273
  if (!player || player.leaving) throw new CommandError("auth", "不在这个房间里");
3271
- if (room.hostPlayerId === playerId) room.hostLastSeen = this.now();
3274
+ this.touch(room, player);
3272
3275
  if (command.type === "ping") return {
3273
3276
  seq: room.seq,
3274
3277
  view: this.project(room, playerId)
@@ -3411,13 +3414,32 @@ var RoomManager = class {
3411
3414
  roomId: entry.roomId
3412
3415
  };
3413
3416
  }
3417
+ /**
3418
+ * 实时通道打开/关闭。
3419
+ *
3420
+ * 关掉 socket 不等于立刻离线:后台轮询(HTTP)还活着的话人还在。真正判离线交给
3421
+ * `tick` 按 `PRESENCE_TTL_MS` 扫一遍,两边都没有动静才标成离线。
3422
+ */
3414
3423
  markConnected(playerId, roomId, connected) {
3415
3424
  const room = this.rooms.get(roomId);
3416
3425
  const player = room?.players.get(playerId);
3417
3426
  if (!room || !player) return;
3418
- if (room.hostPlayerId === playerId && connected) room.hostLastSeen = this.now();
3419
- if (player.connected === connected) return;
3420
- player.connected = connected;
3427
+ player.socketOpen = connected;
3428
+ if (connected) player.lastSeenAt = this.now();
3429
+ this.refreshPresence(room, player);
3430
+ }
3431
+ /** HTTP 请求也算心跳:轮询模式下不能把自己和别人都显示成离线。 */
3432
+ touch(room, player) {
3433
+ if (player.leaving) return;
3434
+ player.lastSeenAt = this.now();
3435
+ if (room.hostPlayerId === player.playerId) room.hostLastSeen = player.lastSeenAt;
3436
+ this.refreshPresence(room, player);
3437
+ }
3438
+ /** 有实时通道,或者最近有 HTTP 动静,就算在线。 */
3439
+ refreshPresence(room, player) {
3440
+ const online = !player.leaving && (player.socketOpen || this.now() - player.lastSeenAt < PRESENCE_TTL_MS);
3441
+ if (player.connected === online) return;
3442
+ player.connected = online;
3421
3443
  this.publish(room);
3422
3444
  }
3423
3445
  health() {
@@ -3576,6 +3598,7 @@ var RoomManager = class {
3576
3598
  if (room.phase === "hand" && room.hand) this.tickHand(room, now);
3577
3599
  if (room.phase === "handOver" && room.handOverAt !== null && now >= room.handOverAt) this.advance(room, now);
3578
3600
  for (const player of [...room.players.values()]) if (player.leaving && room.phase !== "hand") this.removePlayer(room, player.playerId);
3601
+ else this.refreshPresence(room, player);
3579
3602
  }
3580
3603
  this.cleanupSessions(now);
3581
3604
  }
@@ -3726,6 +3749,8 @@ var RoomManager = class {
3726
3749
  seat,
3727
3750
  ready: false,
3728
3751
  connected: true,
3752
+ socketOpen: false,
3753
+ lastSeenAt: this.now(),
3729
3754
  sittingOut: false,
3730
3755
  autoFold: false,
3731
3756
  timeoutStreak: 0,