@rpgjs/server 5.0.0-alpha.32 → 5.0.0-alpha.33

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.
@@ -4,8 +4,12 @@ export type ShopSellList = Record<string, number> | Array<{
4
4
  id: string;
5
5
  multiplier: number;
6
6
  }>;
7
+ export type ShopItemInput = string | {
8
+ id?: string;
9
+ [key: string]: any;
10
+ };
7
11
  export interface ShopGuiOptions {
8
- items: any[];
12
+ items: ShopItemInput[];
9
13
  sell?: ShopSellList;
10
14
  sellMultiplier?: number;
11
15
  message?: string;
@@ -386,12 +386,13 @@ export interface IItemManager {
386
386
  /**
387
387
  * Equips a weapon or armor on a player
388
388
  *
389
- * Think first to add the item in the inventory with the `addItem()` method before equipping the item.
389
+ * Think first to add the item in the inventory with the `addItem()` method before equipping the item,
390
+ * or pass `"auto"` to add the item if it is missing and equip it.
390
391
  *
391
392
  * The `onEquip()` method is called on the ItemClass when the item is equipped or unequipped.
392
393
  *
393
- * @param itemClass - Item class or string identifier. If string, it's the item ID
394
- * @param equip - Equip the item if `true`, unequip if `false` (default: `true`)
394
+ * @param itemId - Item identifier to resolve from the database
395
+ * @param equip - Equip the item if `true`, unequip if `false`, or `"auto"` to add then equip (default: `true`)
395
396
  * @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
396
397
  * - `id`: `ITEM_NOT_INVENTORY`
397
398
  * - `msg`: Error message
@@ -404,19 +405,17 @@ export interface IItemManager {
404
405
  *
405
406
  * @example
406
407
  * ```ts
407
- * import Sword from 'your-database/sword'
408
- *
409
408
  * try {
410
- * player.addItem(Sword)
411
- * player.equip(Sword)
409
+ * player.addItem('sword')
410
+ * player.equip('sword')
412
411
  * // Later, unequip it
413
- * player.equip(Sword, false)
412
+ * player.equip('sword', false)
414
413
  * } catch (err) {
415
414
  * console.log(err)
416
415
  * }
417
416
  * ```
418
417
  */
419
- equip(itemClass: ItemClass | string, equip?: boolean): void;
418
+ equip(itemId: string, equip?: boolean | 'auto'): void;
420
419
  /**
421
420
  * Get the player's attack (sum of items equipped)
422
421
  *
package/dist/index.js CHANGED
@@ -8543,9 +8543,9 @@ var Server = class {
8543
8543
  sessionState,
8544
8544
  room: this.room
8545
8545
  })) ?? userSnapshot;
8546
- load(user, hydratedSnapshot, true);
8547
8546
  signal2()[publicId] = user;
8548
- await this.room.storage.put(`${usersPropName}.${publicId}`, hydratedSnapshot);
8547
+ load(user, hydratedSnapshot, true);
8548
+ await this.room.storage.put(`${usersPropName}.${publicId}`, userSnapshot);
8549
8549
  }
8550
8550
  }
8551
8551
  const transferToken = generateShortUUID$1();
@@ -21549,13 +21549,17 @@ function WithItemManager(Base) {
21549
21549
  this.removeItem(itemClass);
21550
21550
  return inventory;
21551
21551
  }
21552
- equip(itemClass, equip = true) {
21553
- const itemId = isString(itemClass) ? itemClass : itemClass.name;
21554
- const inventory = this.getItem(itemClass);
21552
+ equip(itemId, equip = true) {
21553
+ const autoAdd = equip === "auto";
21554
+ const equipState = equip === "auto" ? true : equip;
21555
+ const data = this.databaseById(itemId);
21556
+ let inventory = this.getItem(itemId);
21557
+ if (!inventory && autoAdd) {
21558
+ inventory = this.addItem(itemId, 1);
21559
+ }
21555
21560
  if (!inventory) {
21556
21561
  throw ItemLog.notInInventory(itemId);
21557
21562
  }
21558
- const data = this.databaseById(itemId);
21559
21563
  if (data._type == "item") {
21560
21564
  throw ItemLog.invalidToEquiped(itemId);
21561
21565
  }
@@ -21570,18 +21574,18 @@ function WithItemManager(Base) {
21570
21574
  }
21571
21575
  }
21572
21576
  const item = inventory;
21573
- if (item.equipped && equip) {
21577
+ if (item.equipped && equipState) {
21574
21578
  throw ItemLog.isAlreadyEquiped(itemId);
21575
21579
  }
21576
- item.equipped = equip;
21577
- if (!equip) {
21580
+ item.equipped = equipState;
21581
+ if (!equipState) {
21578
21582
  const index = this.equipments().findIndex((it) => it.id() == item.id());
21579
21583
  this.equipments().splice(index, 1);
21580
21584
  } else {
21581
21585
  this.equipments().push(item);
21582
21586
  }
21583
21587
  const hookTarget = item._itemInstance || item;
21584
- this["execMethod"]("onEquip", [this, equip], hookTarget);
21588
+ this["execMethod"]("onEquip", [this, equipState], hookTarget);
21585
21589
  }
21586
21590
  };
21587
21591
  }
@@ -22180,8 +22184,11 @@ function WithClassManager(Base) {
22180
22184
  this.addParameter(param, actor.parameters[param]);
22181
22185
  }
22182
22186
  for (let item of actor.startingEquipment) {
22183
- this.addItem(item);
22184
- this.equip(item, true);
22187
+ const inventory = this.addItem(item);
22188
+ const itemId = inventory?.id?.();
22189
+ if (itemId) {
22190
+ this.equip(itemId, true);
22191
+ }
22185
22192
  }
22186
22193
  if (actor.class) this.setClass(actor.class);
22187
22194
  this["execMethod"]("onSet", [this], actor);