@rpgjs/client 5.0.0-beta.8 → 5.0.0-beta.9

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.
Files changed (115) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/Game/AnimationManager.js.map +1 -1
  3. package/dist/Game/Event.js.map +1 -1
  4. package/dist/Game/Map.d.ts +9 -1
  5. package/dist/Game/Map.js +61 -4
  6. package/dist/Game/Map.js.map +1 -1
  7. package/dist/Game/Object.js.map +1 -1
  8. package/dist/Game/Player.js.map +1 -1
  9. package/dist/Gui/Gui.js +1 -1
  10. package/dist/Gui/Gui.js.map +1 -1
  11. package/dist/Gui/NotificationManager.js.map +1 -1
  12. package/dist/Resource.js.map +1 -1
  13. package/dist/RpgClientEngine.js +7 -2
  14. package/dist/RpgClientEngine.js.map +1 -1
  15. package/dist/Sound.js.map +1 -1
  16. package/dist/_virtual/{_@oxc-project_runtime@0.128.0 → _@oxc-project_runtime@0.130.0}/helpers/decorate.js +1 -1
  17. package/dist/_virtual/{_@oxc-project_runtime@0.128.0 → _@oxc-project_runtime@0.130.0}/helpers/decorateMetadata.js +1 -1
  18. package/dist/components/animations/animation.ce.js.map +1 -1
  19. package/dist/components/animations/hit.ce.js.map +1 -1
  20. package/dist/components/animations/index.js.map +1 -1
  21. package/dist/components/character.ce.js +34 -3
  22. package/dist/components/character.ce.js.map +1 -1
  23. package/dist/components/dynamics/bar.ce.js.map +1 -1
  24. package/dist/components/dynamics/image.ce.js.map +1 -1
  25. package/dist/components/dynamics/parse-value.js.map +1 -1
  26. package/dist/components/dynamics/shape-utils.js.map +1 -1
  27. package/dist/components/dynamics/shape.ce.js.map +1 -1
  28. package/dist/components/dynamics/text.ce.js.map +1 -1
  29. package/dist/components/gui/box.ce.js.map +1 -1
  30. package/dist/components/gui/dialogbox/index.ce.js +3 -3
  31. package/dist/components/gui/dialogbox/index.ce.js.map +1 -1
  32. package/dist/components/gui/gameover.ce.js +1 -1
  33. package/dist/components/gui/gameover.ce.js.map +1 -1
  34. package/dist/components/gui/hud/hud.ce.js +1 -1
  35. package/dist/components/gui/hud/hud.ce.js.map +1 -1
  36. package/dist/components/gui/menu/equip-menu.ce.js.map +1 -1
  37. package/dist/components/gui/menu/exit-menu.ce.js.map +1 -1
  38. package/dist/components/gui/menu/items-menu.ce.js.map +1 -1
  39. package/dist/components/gui/menu/main-menu.ce.js.map +1 -1
  40. package/dist/components/gui/menu/options-menu.ce.js.map +1 -1
  41. package/dist/components/gui/menu/skills-menu.ce.js.map +1 -1
  42. package/dist/components/gui/mobile/index.js.map +1 -1
  43. package/dist/components/gui/mobile/mobile.ce.js.map +1 -1
  44. package/dist/components/gui/notification/notification.ce.js.map +1 -1
  45. package/dist/components/gui/save-load.ce.js.map +1 -1
  46. package/dist/components/gui/shop/shop.ce.js +1 -1
  47. package/dist/components/gui/shop/shop.ce.js.map +1 -1
  48. package/dist/components/gui/title-screen.ce.js +2 -2
  49. package/dist/components/gui/title-screen.ce.js.map +1 -1
  50. package/dist/components/player-components-utils.js.map +1 -1
  51. package/dist/components/player-components.ce.js.map +1 -1
  52. package/dist/components/prebuilt/hp-bar.ce.js.map +1 -1
  53. package/dist/components/prebuilt/light-halo.ce.js.map +1 -1
  54. package/dist/components/scenes/canvas.ce.js +147 -4
  55. package/dist/components/scenes/canvas.ce.js.map +1 -1
  56. package/dist/components/scenes/draw-map.ce.js +2 -8
  57. package/dist/components/scenes/draw-map.ce.js.map +1 -1
  58. package/dist/components/scenes/event-layer.ce.js.map +1 -1
  59. package/dist/core/inject.js +1 -1
  60. package/dist/core/inject.js.map +1 -1
  61. package/dist/core/setup.js +1 -1
  62. package/dist/core/setup.js.map +1 -1
  63. package/dist/decorators/spritesheet.d.ts +1 -0
  64. package/dist/decorators/spritesheet.js +11 -0
  65. package/dist/decorators/spritesheet.js.map +1 -0
  66. package/dist/index.d.ts +1 -0
  67. package/dist/index.js +3 -2
  68. package/dist/module.js +1 -1
  69. package/dist/module.js.map +1 -1
  70. package/dist/node_modules/.pnpm/{@signe_di@2.10.0 → @signe_di@3.0.1}/node_modules/@signe/di/dist/index.js +1 -1
  71. package/dist/node_modules/.pnpm/@signe_di@3.0.1/node_modules/@signe/di/dist/index.js.map +1 -0
  72. package/dist/node_modules/.pnpm/{@signe_reactive@2.10.0 → @signe_reactive@3.0.1}/node_modules/@signe/reactive/dist/index.js +1 -1
  73. package/dist/node_modules/.pnpm/@signe_reactive@3.0.1/node_modules/@signe/reactive/dist/index.js.map +1 -0
  74. package/dist/node_modules/.pnpm/@signe_room@3.0.1/node_modules/@signe/room/dist/chunk-EUXUH3YW.js +13 -0
  75. package/dist/node_modules/.pnpm/@signe_room@3.0.1/node_modules/@signe/room/dist/chunk-EUXUH3YW.js.map +1 -0
  76. package/dist/node_modules/.pnpm/{@signe_room@2.10.0 → @signe_room@3.0.1}/node_modules/@signe/room/dist/index.js +124 -39
  77. package/dist/node_modules/.pnpm/@signe_room@3.0.1/node_modules/@signe/room/dist/index.js.map +1 -0
  78. package/dist/node_modules/.pnpm/{@signe_sync@2.10.0 → @signe_sync@3.0.1}/node_modules/@signe/sync/dist/client/index.js +1 -1
  79. package/dist/node_modules/.pnpm/@signe_sync@3.0.1/node_modules/@signe/sync/dist/client/index.js.map +1 -0
  80. package/dist/node_modules/.pnpm/{@signe_sync@2.10.0 → @signe_sync@3.0.1}/node_modules/@signe/sync/dist/index.js +36 -13
  81. package/dist/node_modules/.pnpm/@signe_sync@3.0.1/node_modules/@signe/sync/dist/index.js.map +1 -0
  82. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-HAC622V3.js.map +1 -1
  83. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-S74YV6PU.js.map +1 -1
  84. package/dist/node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.js.map +1 -1
  85. package/dist/presets/animation.js.map +1 -1
  86. package/dist/presets/faceset.js.map +1 -1
  87. package/dist/presets/icon.js.map +1 -1
  88. package/dist/presets/index.js.map +1 -1
  89. package/dist/presets/lpc.js.map +1 -1
  90. package/dist/presets/rmspritesheet.js.map +1 -1
  91. package/dist/services/AbstractSocket.js.map +1 -1
  92. package/dist/services/keyboardControls.js.map +1 -1
  93. package/dist/services/loadMap.js +1 -1
  94. package/dist/services/loadMap.js.map +1 -1
  95. package/dist/services/mmorpg.js +7 -3
  96. package/dist/services/mmorpg.js.map +1 -1
  97. package/dist/services/save.js.map +1 -1
  98. package/dist/services/standalone.js +1 -1
  99. package/dist/services/standalone.js.map +1 -1
  100. package/dist/utils/getEntityProp.js.map +1 -1
  101. package/package.json +10 -10
  102. package/src/Game/Map.ts +79 -0
  103. package/src/RpgClientEngine.ts +10 -1
  104. package/src/components/character.ce +37 -8
  105. package/src/components/scenes/canvas.ce +165 -6
  106. package/src/components/scenes/draw-map.ce +2 -15
  107. package/src/components/scenes/event-layer.ce +1 -2
  108. package/src/decorators/spritesheet.ts +8 -0
  109. package/src/index.ts +1 -0
  110. package/src/services/mmorpg.ts +8 -2
  111. package/dist/node_modules/.pnpm/@signe_di@2.10.0/node_modules/@signe/di/dist/index.js.map +0 -1
  112. package/dist/node_modules/.pnpm/@signe_reactive@2.10.0/node_modules/@signe/reactive/dist/index.js.map +0 -1
  113. package/dist/node_modules/.pnpm/@signe_room@2.10.0/node_modules/@signe/room/dist/index.js.map +0 -1
  114. package/dist/node_modules/.pnpm/@signe_sync@2.10.0/node_modules/@signe/sync/dist/client/index.js.map +0 -1
  115. package/dist/node_modules/.pnpm/@signe_sync@2.10.0/node_modules/@signe/sync/dist/index.js.map +0 -1
@@ -1,15 +1,8 @@
1
- import { signal } from "../../../../../@signe_reactive@2.10.0/node_modules/@signe/reactive/dist/index.js";
2
- import { id, persist, sync } from "../../../../../@signe_sync@2.10.0/node_modules/@signe/sync/dist/index.js";
1
+ import { signal } from "../../../../../@signe_reactive@3.0.1/node_modules/@signe/reactive/dist/index.js";
2
+ import { id, persist, sync } from "../../../../../@signe_sync@3.0.1/node_modules/@signe/sync/dist/index.js";
3
+ import { __decorateClass } from "./chunk-EUXUH3YW.js";
3
4
  import { z } from "../../../../../zod@3.24.2/node_modules/zod/lib/index.js";
4
- //#region ../../node_modules/.pnpm/@signe+room@2.10.0/node_modules/@signe/room/dist/index.js
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __decorateClass = (decorators, target, key, kind) => {
8
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
9
- for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result;
10
- if (kind && result) __defProp(target, key, result);
11
- return result;
12
- };
5
+ //#region ../../node_modules/.pnpm/@signe+room@3.0.1/node_modules/@signe/room/dist/index.js
13
6
  function Request2(options, bodyValidation) {
14
7
  return function(target, propertyKey) {
15
8
  if (!target.constructor._requestMetadata) target.constructor._requestMetadata = /* @__PURE__ */ new Map();
@@ -55,33 +48,46 @@ var Storage = class {
55
48
  this.memory = /* @__PURE__ */ new Map();
56
49
  }
57
50
  async put(key, value) {
58
- this.memory.set(key, value);
51
+ if (typeof key === "string") {
52
+ this.memory.set(key, value);
53
+ return;
54
+ }
55
+ for (const [entryKey, entryValue] of Object.entries(key)) this.memory.set(entryKey, entryValue);
59
56
  }
60
57
  async get(key) {
61
58
  return this.memory.get(key);
62
59
  }
63
60
  async delete(key) {
64
- this.memory.delete(key);
61
+ if (Array.isArray(key)) {
62
+ let deleted = 0;
63
+ for (const entryKey of key) if (this.memory.delete(entryKey)) deleted += 1;
64
+ return deleted;
65
+ }
66
+ return this.memory.delete(key);
65
67
  }
66
- async list() {
67
- return this.memory;
68
+ async list(options) {
69
+ if (!options?.prefix) return this.memory;
70
+ return new Map(Array.from(this.memory.entries()).filter(([key]) => String(key).startsWith(options.prefix)));
68
71
  }
69
72
  };
70
73
  z.object({
71
74
  action: z.string(),
72
75
  value: z.any()
73
76
  });
77
+ var INTERNAL_PREFIX = "$room:";
78
+ `${INTERNAL_PREFIX}`;
79
+ `${INTERNAL_PREFIX}`;
74
80
  async function request(room, path, options = { method: "GET" }) {
75
81
  const url = new URL("http://localhost" + path);
76
82
  const request2 = new Request(url.toString(), options);
77
83
  return await room.onRequest(request2);
78
84
  }
79
85
  var MockPartyClient = class {
80
- constructor(server, id2) {
86
+ constructor(server, sessionId) {
81
87
  this.server = server;
82
88
  this.events = /* @__PURE__ */ new Map();
83
- this.id = id2 || generateShortUUID();
84
- this.conn = new MockConnection(this);
89
+ this.id = generateShortUUID();
90
+ this.conn = new MockConnection(this, sessionId || this.id);
85
91
  }
86
92
  addEventListener(event, cb) {
87
93
  if (!this.events.has(event)) this.events.set(event, []);
@@ -107,8 +113,8 @@ var MockLobby = class {
107
113
  this.server = server;
108
114
  this.lobbyId = lobbyId;
109
115
  }
110
- socket(_init) {
111
- return new MockPartyClient(this.server);
116
+ socket(init) {
117
+ return new MockPartyClient(this.server, init?.id);
112
118
  }
113
119
  async connection(idOrOptions, maybeOptions) {
114
120
  const id2 = typeof idOrOptions === "string" ? idOrOptions : idOrOptions?.id;
@@ -174,7 +180,9 @@ var MockPartyRoom = class {
174
180
  });
175
181
  }
176
182
  getConnection(id2) {
177
- return this.clients.get(id2);
183
+ let connection;
184
+ for (const client of this.clients.values()) if (client.conn.id === id2 || client.conn.sessionId === id2) connection = client.conn;
185
+ return connection;
178
186
  }
179
187
  getConnections() {
180
188
  return Array.from(this.clients.values()).map((client) => client.conn);
@@ -182,13 +190,21 @@ var MockPartyRoom = class {
182
190
  clear() {
183
191
  this.clients.clear();
184
192
  }
193
+ deleteConnection(id2, connection) {
194
+ if (connection) {
195
+ this.clients.delete(connection.id);
196
+ return;
197
+ }
198
+ for (const [connectionKey, client] of this.clients) if (client.conn.id === id2 || client.conn.sessionId === id2) this.clients.delete(connectionKey);
199
+ }
185
200
  };
186
201
  var MockConnection = class {
187
- constructor(client) {
202
+ constructor(client, sessionId) {
188
203
  this.client = client;
189
204
  this.state = {};
190
205
  this.server = client.server;
191
206
  this.id = client.id;
207
+ this.sessionId = sessionId;
192
208
  }
193
209
  setState(value) {
194
210
  this.state = value;
@@ -197,6 +213,7 @@ var MockConnection = class {
197
213
  this.client._trigger("message", data);
198
214
  }
199
215
  close() {
216
+ this.server.room.deleteConnection?.(this.id, this);
200
217
  this.server.onClose(this);
201
218
  }
202
219
  };
@@ -318,19 +335,30 @@ var guardManageWorld = async (_, req, room) => {
318
335
  if (tokenShard !== room.env.SHARD_SECRET) return false;
319
336
  return true;
320
337
  }
321
- const url = new URL(req.url);
322
- const token = req.headers.get("Authorization") ?? url.searchParams.get("world-auth-token");
338
+ const token = getAuthToken(req, new URL(req.url));
323
339
  if (!token) return false;
324
340
  const jwt = new JWTAuth(room.env.AUTH_JWT_SECRET);
325
341
  try {
326
- if (!await jwt.verify(token)) return false;
342
+ const payload = await jwt.verify(token);
343
+ if (!payload) return false;
344
+ if (!canAccessWorld(payload, room.id)) return false;
327
345
  } catch (error) {
328
346
  return false;
329
347
  }
330
348
  return true;
331
349
  };
350
+ function getAuthToken(req, url) {
351
+ const authorization = req.headers.get("Authorization");
352
+ if (authorization?.startsWith("Bearer ")) return authorization.slice(7).trim();
353
+ return authorization ?? url.searchParams.get("world-auth-token");
354
+ }
355
+ function canAccessWorld(payload, worldId) {
356
+ const worlds = payload.worlds;
357
+ if (!Array.isArray(worlds)) return false;
358
+ return worlds.some((world) => world === "*" || world === worldId);
359
+ }
332
360
  var MAX_PLAYERS_PER_SHARD = 75;
333
- z.object({
361
+ var RoomConfigSchema = z.object({
334
362
  name: z.string(),
335
363
  balancingStrategy: z.enum([
336
364
  "round-robin",
@@ -345,10 +373,13 @@ z.object({
345
373
  z.object({
346
374
  shardId: z.string(),
347
375
  roomId: z.string(),
376
+ worldId: z.string().optional(),
348
377
  url: z.string().url(),
349
378
  maxConnections: z.number().int().positive()
350
379
  });
351
- z.object({
380
+ var UpdateShardStatsSchema = z.object({
381
+ shardId: z.string(),
382
+ worldId: z.string().optional(),
352
383
  connections: z.number().int().min(0),
353
384
  status: z.enum([
354
385
  "active",
@@ -356,7 +387,7 @@ z.object({
356
387
  "draining"
357
388
  ]).optional()
358
389
  });
359
- z.object({
390
+ var ScaleRoomSchema = z.object({
360
391
  roomId: z.string(),
361
392
  targetShardCount: z.number().int().positive(),
362
393
  shardTemplate: z.object({
@@ -384,6 +415,7 @@ __decorateClass([sync()], RoomConfig.prototype, "maxShards", 2);
384
415
  var ShardInfo = class {
385
416
  constructor() {
386
417
  this.roomId = signal("");
418
+ this.worldId = signal("");
387
419
  this.url = signal("");
388
420
  this.currentConnections = signal(0);
389
421
  this.maxConnections = signal(MAX_PLAYERS_PER_SHARD);
@@ -393,6 +425,7 @@ var ShardInfo = class {
393
425
  };
394
426
  __decorateClass([id()], ShardInfo.prototype, "id", 2);
395
427
  __decorateClass([sync()], ShardInfo.prototype, "roomId", 2);
428
+ __decorateClass([sync()], ShardInfo.prototype, "worldId", 2);
396
429
  __decorateClass([sync()], ShardInfo.prototype, "url", 2);
397
430
  __decorateClass([sync({ persist: false })], ShardInfo.prototype, "currentConnections", 2);
398
431
  __decorateClass([sync()], ShardInfo.prototype, "maxConnections", 2);
@@ -409,6 +442,7 @@ var WorldRoom = class {
409
442
  const { AUTH_JWT_SECRET, SHARD_SECRET } = this.room.env;
410
443
  if (!AUTH_JWT_SECRET) throw new Error("AUTH_JWT_SECRET env variable is not set");
411
444
  if (!SHARD_SECRET) throw new Error("SHARD_SECRET env variable is not set");
445
+ this.scheduleInactiveShardCleanup();
412
446
  }
413
447
  async onJoin(user, conn, ctx) {
414
448
  const canConnect = await guardManageWorld(user, ctx.request, this.room);
@@ -421,6 +455,12 @@ var WorldRoom = class {
421
455
  if (!conn.state["isAdmin"]) return null;
422
456
  return obj;
423
457
  }
458
+ getWorldId() {
459
+ return this.room.id;
460
+ }
461
+ scheduleInactiveShardCleanup() {
462
+ setTimeout(() => this.cleanupInactiveShards(), 6e4)?.unref?.();
463
+ }
424
464
  cleanupInactiveShards() {
425
465
  const now = Date.now();
426
466
  const timeout = 300 * 1e3;
@@ -428,10 +468,19 @@ var WorldRoom = class {
428
468
  Object.values(shardsValue).forEach((shard) => {
429
469
  if (now - shard.lastHeartbeat() > timeout) delete this.shards()[shard.id];
430
470
  });
431
- setTimeout(() => this.cleanupInactiveShards(), 6e4);
471
+ this.scheduleInactiveShardCleanup();
432
472
  }
433
- async registerRoom(req) {
434
- const roomConfig = await req.json();
473
+ removeShard(shardId) {
474
+ delete this.shards()[shardId];
475
+ }
476
+ shouldCompleteDrain(shard) {
477
+ return shard.status() === "draining" && shard.currentConnections() === 0;
478
+ }
479
+ async registerRoom(req, res) {
480
+ const parseResult = RoomConfigSchema.safeParse(await req.json());
481
+ if (!parseResult.success) return res?.badRequest("Invalid room configuration", { details: parseResult.error });
482
+ const roomConfig = parseResult.data;
483
+ if (roomConfig.maxShards !== void 0 && roomConfig.minShards > roomConfig.maxShards) return res?.badRequest("minShards cannot be greater than maxShards");
435
484
  const roomId = roomConfig.name;
436
485
  if (!this.rooms()[roomId]) {
437
486
  const newRoom = new RoomConfig();
@@ -452,17 +501,25 @@ var WorldRoom = class {
452
501
  room.minShards.set(roomConfig.minShards);
453
502
  room.maxShards.set(roomConfig.maxShards);
454
503
  }
504
+ await this.ensureMinShards(roomId);
455
505
  }
456
506
  async updateShardStats(req, res) {
457
- const { shardId, connections, status } = await req.json();
507
+ const parseResult = UpdateShardStatsSchema.safeParse(await req.json());
508
+ if (!parseResult.success) return res.badRequest("Invalid shard stats", { details: parseResult.error });
509
+ const body = parseResult.data;
510
+ const { shardId, connections, status } = body;
458
511
  const shard = this.shards()[shardId];
459
512
  if (!shard) return res.notFound(`Shard ${shardId} not found`);
513
+ if (body.worldId && body.worldId !== this.getWorldId()) return res.badRequest(`Shard ${shardId} belongs to world ${body.worldId}, not ${this.getWorldId()}`);
460
514
  shard.currentConnections.set(connections);
461
515
  if (status) shard.status.set(status);
462
516
  shard.lastHeartbeat.set(Date.now());
517
+ if (this.shouldCompleteDrain(shard)) this.removeShard(shard.id);
463
518
  }
464
519
  async scaleRoom(req, res) {
465
- const { targetShardCount, shardTemplate, roomId } = await req.json();
520
+ const parseResult = ScaleRoomSchema.safeParse(await req.json());
521
+ if (!parseResult.success) return res.badRequest("Invalid scale room request", { details: parseResult.error });
522
+ const { targetShardCount, shardTemplate, roomId } = parseResult.data;
466
523
  const room = this.rooms()[roomId];
467
524
  if (!room) return res.notFound(`Room ${roomId} does not exist`);
468
525
  const roomShards = Object.values(this.shards()).filter((shard) => shard.roomId() === roomId);
@@ -472,13 +529,15 @@ var WorldRoom = class {
472
529
  currentShardCount: previousShardCount
473
530
  });
474
531
  if (targetShardCount < previousShardCount) {
475
- const shardsToRemove = [...roomShards].sort((a, b) => {
532
+ const shardsToDrain = [...roomShards].sort((a, b) => {
476
533
  if (a.status() === "draining" && b.status() !== "draining") return -1;
477
534
  if (a.status() !== "draining" && b.status() === "draining") return 1;
478
535
  return a.currentConnections() - b.currentConnections();
479
536
  }).slice(0, previousShardCount - targetShardCount);
480
- roomShards.filter((shard) => !shardsToRemove.some((s) => s.id === shard.id));
481
- for (const shard of shardsToRemove) delete this.shards()[shard.id];
537
+ for (const shard of shardsToDrain) {
538
+ shard.status.set("draining");
539
+ if (this.shouldCompleteDrain(shard)) this.removeShard(shard.id);
540
+ }
482
541
  return;
483
542
  }
484
543
  if (targetShardCount > previousShardCount) {
@@ -536,8 +595,18 @@ var WorldRoom = class {
536
595
  };
537
596
  else return { error: `Failed to create shard for room ${roomId}` };
538
597
  } else return { error: `No shards available for room ${roomId}` };
539
- const activeShards = roomShards.filter((shard) => shard && shard.status() === "active");
540
- if (activeShards.length === 0) return { error: `No active shards available for room ${roomId}` };
598
+ let activeShards = this.getAvailableShards(roomShards);
599
+ if (activeShards.length === 0) {
600
+ if (autoCreate && this.canCreateShard(room, roomShards.length)) {
601
+ const newShard = await this.createShard(roomId);
602
+ if (newShard) return {
603
+ shardId: newShard.id,
604
+ url: newShard.url()
605
+ };
606
+ }
607
+ if (roomShards.some((shard) => shard.status() === "active")) return { error: `No shard capacity available for room ${roomId}` };
608
+ return { error: `No active shards available for room ${roomId}` };
609
+ }
541
610
  const balancingStrategy = room.balancingStrategy();
542
611
  let selectedShard;
543
612
  switch (balancingStrategy) {
@@ -559,18 +628,26 @@ var WorldRoom = class {
559
628
  url: selectedShard.url()
560
629
  };
561
630
  }
631
+ getAvailableShards(shards) {
632
+ return shards.filter((shard) => shard && shard.status() === "active" && shard.currentConnections() < shard.maxConnections());
633
+ }
634
+ canCreateShard(room, currentShardCount) {
635
+ return room.maxShards() === void 0 || currentShardCount < room.maxShards();
636
+ }
562
637
  async createShard(roomId, urlTemplate, maxConnections) {
563
638
  const room = this.rooms()[roomId];
564
639
  if (!room) {
565
640
  console.error(`Cannot create shard for non-existent room: ${roomId}`);
566
641
  return null;
567
642
  }
568
- const shardId = `${roomId}:${Date.now()}-${Math.floor(Math.random() * 1e4)}`;
643
+ const worldId = this.getWorldId();
644
+ const shardId = `${roomId}:${worldId}:${Date.now()}-${Math.floor(Math.random() * 1e4)}`;
569
645
  const url = (urlTemplate || this.defaultShardUrlTemplate()).replace("{shardId}", shardId).replace("{roomId}", roomId);
570
646
  const max = maxConnections || room.maxPlayersPerShard();
571
647
  const newShard = new ShardInfo();
572
648
  newShard.id = shardId;
573
649
  newShard.roomId.set(roomId);
650
+ newShard.worldId.set(worldId);
574
651
  newShard.url.set(url);
575
652
  newShard.maxConnections.set(max);
576
653
  newShard.currentConnections.set(0);
@@ -579,6 +656,14 @@ var WorldRoom = class {
579
656
  this.shards()[shardId] = newShard;
580
657
  return newShard;
581
658
  }
659
+ async ensureMinShards(roomId) {
660
+ const room = this.rooms()[roomId];
661
+ if (!room) return;
662
+ const currentShardCount = Object.values(this.shards()).filter((shard) => shard.roomId() === roomId).length;
663
+ const targetShardCount = room.minShards();
664
+ if (currentShardCount >= targetShardCount) return;
665
+ for (let i = currentShardCount; i < targetShardCount; i++) await this.createShard(roomId);
666
+ }
582
667
  };
583
668
  __decorateClass([sync(RoomConfig)], WorldRoom.prototype, "rooms", 2);
584
669
  __decorateClass([sync(ShardInfo)], WorldRoom.prototype, "shards", 2);