playsocketjs 2.2.1 → 2.3.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/README.md CHANGED
@@ -221,7 +221,8 @@ Creates a new PlaySocket Server instance with configuration options.
221
221
  - `clientJoinedRoom`: Client joined a room – note that clients can only leave by disconnecting (returns the client's ID `string`, room ID `string`)
222
222
  - `roomCreated`: Client created a room (returns room ID `string`)
223
223
  - `roomDestroyed`: Room was destroyed, this happens when all participants leave (returns room ID `string`)
224
- - `roomCreationRequested`: Room creation requested by client (returns `object` containing the client's ID `string`, room ID `string` and the initialStorage `object`) – if you return an `object` in the callback, it will take that as the initial storage instead
224
+ - `roomCreationRequested`: Room creation requested by client (returns `object` containing the client's ID `string`, room ID `string` and the initialStorage `object`) – if you return an `object` in the callback, it will take that as the initial storage instead. If you return `false`, the creation will be denied
225
+ - `storageUpdated`: Room storage property updated (returns `object` containing the ID of the client who requested the update `string`, room ID `string`, the update `object`, and the storage `object`)
225
226
  - `storageUpdateRequested`: Room storage property update requested by client (returns `object` containing the client's ID `string`, room ID `string` and the update `object`) – if you return `false` in the callback, the update will be blocked
226
227
  - `requestReceived`: Request from client was received by the server (returns `object` containing client's ID `string`, if in room – room ID `string`, request name `string` and optional passed data of type `any`)
227
228
 
@@ -2399,6 +2399,13 @@ function requireServer () {
2399
2399
  // Event callback with potential initial storage modifications
2400
2400
  const reviewedStorage = await this.#triggerEvent("roomCreationRequested", { roomId: newRoomId, clientId: ws.clientId, initialStorage: structuredClone({ ...data.initialStorage }) });
2401
2401
  if (typeof reviewedStorage === 'object') data.initialStorage = reviewedStorage;
2402
+ if (reviewedStorage === false) {
2403
+ ws.send(encode({
2404
+ type: 'room_creation_failed',
2405
+ reason: 'Room creation denied.'
2406
+ }), { binary: true });
2407
+ return;
2408
+ }
2402
2409
 
2403
2410
  // Check if client/creator is still connected
2404
2411
  if (!this.#clients.has(ws.clientId)) {
@@ -2493,7 +2500,6 @@ function requireServer () {
2493
2500
  // Increment version for this room
2494
2501
  const currentVersion = this.#roomVersions.get(updateRoomId) + 1;
2495
2502
  this.#roomVersions.set(updateRoomId, currentVersion);
2496
- if (this.#debug) console.log("Property update received and imported:", data.update);
2497
2503
 
2498
2504
  updateRoom.participants?.forEach(p => {
2499
2505
  const client = this.#clients.get(p);
@@ -2505,6 +2511,9 @@ function requireServer () {
2505
2511
  }), { binary: true });
2506
2512
  }
2507
2513
  });
2514
+
2515
+ this.#triggerEvent("storageUpdated", { roomId: updateRoomId, clientId: ws.clientId, update: structuredClone(data.update), storage: this.getRoomStorage(updateRoomId) });
2516
+ if (this.#debug) console.log("Property update received and imported:", data.update);
2508
2517
  }
2509
2518
  break;
2510
2519
 
@@ -2653,7 +2662,7 @@ function requireServer () {
2653
2662
  * @param {Function} callback - Callback function
2654
2663
  */
2655
2664
  onEvent(event, callback) {
2656
- const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "roomCreationRequested", "requestReceived", "roomCreated", "storageUpdateRequested", "roomDestroyed"];
2665
+ const validEvents = ["clientRegistered", "clientRegistrationRequested", "clientDisconnected", "clientJoinedRoom", "roomCreated", "roomCreationRequested", "requestReceived", "storageUpdated", "storageUpdateRequested", "roomDestroyed"];
2657
2666
  if (!validEvents.includes(event)) return console.warn(`Invalid PlaySocket event type "${event}"`);
2658
2667
  if (!this.#callbacks.has(event)) this.#callbacks.set(event, []);
2659
2668
  this.#callbacks.get(event).push(callback);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "playsocketjs",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "WebSocket wrapper for creating simple multiplayer systems with ease.",
5
5
  "module": "dist/playsocket-client.js",
6
6
  "exports": {