@signe/room 2.0.0 → 2.1.0

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/src/world.ts CHANGED
@@ -6,6 +6,7 @@ import * as Party from "./types/party";
6
6
  import { guardManageWorld } from "./world.guard";
7
7
  import { response } from "./utils";
8
8
  import { RoomInterceptorPacket, RoomOnJoin } from "./interfaces";
9
+ import { ServerResponse } from "./request/response";
9
10
 
10
11
  // Types definitions
11
12
  type BalancingStrategy = 'round-robin' | 'least-connections' | 'random';
@@ -173,13 +174,13 @@ export class WorldRoom implements RoomInterceptorPacket, RoomOnJoin {
173
174
  method: 'POST',
174
175
  })
175
176
  @Guard([guardManageWorld])
176
- async updateShardStats(req: Party.Request) {
177
+ async updateShardStats(req: Party.Request, res: ServerResponse) {
177
178
  const body: { shardId: string; connections: number; status: ShardStatus } = await req.json();
178
179
  const { shardId, connections, status } = body;
179
180
  const shard = this.shards()[shardId];
180
181
 
181
182
  if (!shard) {
182
- return { error: `Shard ${shardId} not found` };
183
+ return res.notFound(`Shard ${shardId} not found`);
183
184
  }
184
185
 
185
186
  shard.currentConnections.set(connections);
@@ -194,14 +195,14 @@ export class WorldRoom implements RoomInterceptorPacket, RoomOnJoin {
194
195
  method: 'POST',
195
196
  })
196
197
  @Guard([guardManageWorld])
197
- async scaleRoom(req: Party.Request) {
198
+ async scaleRoom(req: Party.Request, res: ServerResponse) {
198
199
  const data: z.infer<typeof ScaleRoomSchema> = await req.json();
199
200
  const { targetShardCount, shardTemplate, roomId } = data;
200
201
 
201
202
  // Validate room exists
202
203
  const room = this.rooms()[roomId];
203
204
  if (!room) {
204
- return { error: `Room ${roomId} does not exist` };
205
+ return res.notFound(`Room ${roomId} does not exist`);
205
206
  }
206
207
 
207
208
  const roomShards = Object.values(this.shards())
@@ -211,11 +212,10 @@ export class WorldRoom implements RoomInterceptorPacket, RoomOnJoin {
211
212
 
212
213
  // Check max shards constraint
213
214
  if (room.maxShards() !== undefined && targetShardCount > room.maxShards()!) {
214
- return {
215
- error: `Cannot scale beyond maximum allowed shards (${room.maxShards()})`,
215
+ return res.badRequest(`Cannot scale beyond maximum allowed shards (${room.maxShards()})`, {
216
216
  roomId,
217
217
  currentShardCount: previousShardCount
218
- };
218
+ });
219
219
  }
220
220
 
221
221
  // Handle scaling down
@@ -268,26 +268,26 @@ export class WorldRoom implements RoomInterceptorPacket, RoomOnJoin {
268
268
  path: 'connect',
269
269
  method: 'POST',
270
270
  })
271
- async connect(req: Party.Request) {
271
+ async connect(req: Party.Request, res: ServerResponse) {
272
272
  try {
273
273
  // Extract request data
274
274
  let data: { roomId: string; autoCreate?: boolean };
275
-
275
+
276
276
  try {
277
277
  // Handle potential empty body or malformed JSON
278
278
  const body = await req.text();
279
279
  if (!body || body.trim() === '') {
280
- return response(400, { error: "Request body is empty" });
280
+ return res.badRequest("Request body is empty");
281
281
  }
282
282
 
283
283
  data = JSON.parse(body);
284
284
  } catch (parseError) {
285
- return response(400, { error: "Invalid JSON in request body" });
285
+ return res.badRequest("Invalid JSON in request body");
286
286
  }
287
287
 
288
288
  // Verify roomId is provided
289
289
  if (!data.roomId) {
290
- return response(400, { error: "roomId parameter is required" });
290
+ return res.badRequest("roomId parameter is required");
291
291
  }
292
292
 
293
293
  // Determine if auto-creation is enabled (default to true)
@@ -298,18 +298,18 @@ export class WorldRoom implements RoomInterceptorPacket, RoomOnJoin {
298
298
 
299
299
  // Check for errors
300
300
  if ('error' in result) {
301
- return response(404, { error: result.error });
301
+ return res.notFound(result.error);
302
302
  }
303
303
 
304
304
  // Return shard information to the client
305
- return response(200, {
305
+ return res.success({
306
306
  success: true,
307
307
  shardId: result.shardId,
308
308
  url: result.url
309
309
  });
310
310
  } catch (error) {
311
311
  console.error('Error connecting to shard:', error);
312
- return response(500, { error: "Internal server error", details: error instanceof Error ? error.message : String(error) });
312
+ return res.serverError();
313
313
  }
314
314
  }
315
315