@rpgjs/server 5.0.0-alpha.4 → 5.0.0-alpha.41

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 (101) hide show
  1. package/dist/Gui/DialogGui.d.ts +5 -0
  2. package/dist/Gui/GameoverGui.d.ts +23 -0
  3. package/dist/Gui/Gui.d.ts +6 -0
  4. package/dist/Gui/MenuGui.d.ts +22 -3
  5. package/dist/Gui/NotificationGui.d.ts +1 -2
  6. package/dist/Gui/SaveLoadGui.d.ts +13 -0
  7. package/dist/Gui/ShopGui.d.ts +28 -3
  8. package/dist/Gui/TitleGui.d.ts +23 -0
  9. package/dist/Gui/index.d.ts +10 -1
  10. package/dist/Player/BattleManager.d.ts +34 -12
  11. package/dist/Player/ClassManager.d.ts +46 -13
  12. package/dist/Player/ComponentManager.d.ts +123 -0
  13. package/dist/Player/Components.d.ts +345 -0
  14. package/dist/Player/EffectManager.d.ts +86 -0
  15. package/dist/Player/ElementManager.d.ts +104 -0
  16. package/dist/Player/GoldManager.d.ts +22 -0
  17. package/dist/Player/GuiManager.d.ts +259 -0
  18. package/dist/Player/ItemFixture.d.ts +6 -0
  19. package/dist/Player/ItemManager.d.ts +450 -9
  20. package/dist/Player/MoveManager.d.ts +324 -69
  21. package/dist/Player/ParameterManager.d.ts +344 -14
  22. package/dist/Player/Player.d.ts +460 -8
  23. package/dist/Player/SkillManager.d.ts +197 -15
  24. package/dist/Player/StateManager.d.ts +89 -25
  25. package/dist/Player/VariableManager.d.ts +74 -0
  26. package/dist/RpgServer.d.ts +502 -64
  27. package/dist/RpgServerEngine.d.ts +2 -1
  28. package/dist/decorators/event.d.ts +46 -0
  29. package/dist/decorators/map.d.ts +287 -0
  30. package/dist/index.d.ts +10 -0
  31. package/dist/index.js +21653 -20900
  32. package/dist/index.js.map +1 -1
  33. package/dist/logs/log.d.ts +2 -3
  34. package/dist/module.d.ts +43 -1
  35. package/dist/presets/index.d.ts +0 -9
  36. package/dist/rooms/BaseRoom.d.ts +132 -0
  37. package/dist/rooms/lobby.d.ts +10 -2
  38. package/dist/rooms/map.d.ts +1236 -17
  39. package/dist/services/save.d.ts +43 -0
  40. package/dist/storage/index.d.ts +1 -0
  41. package/dist/storage/localStorage.d.ts +23 -0
  42. package/package.json +14 -10
  43. package/src/Gui/DialogGui.ts +19 -4
  44. package/src/Gui/GameoverGui.ts +39 -0
  45. package/src/Gui/Gui.ts +23 -1
  46. package/src/Gui/MenuGui.ts +155 -6
  47. package/src/Gui/NotificationGui.ts +1 -2
  48. package/src/Gui/SaveLoadGui.ts +60 -0
  49. package/src/Gui/ShopGui.ts +146 -16
  50. package/src/Gui/TitleGui.ts +39 -0
  51. package/src/Gui/index.ts +15 -2
  52. package/src/Player/BattleManager.ts +91 -49
  53. package/src/Player/ClassManager.ts +118 -50
  54. package/src/Player/ComponentManager.ts +425 -19
  55. package/src/Player/Components.ts +380 -0
  56. package/src/Player/EffectManager.ts +81 -44
  57. package/src/Player/ElementManager.ts +109 -86
  58. package/src/Player/GoldManager.ts +32 -35
  59. package/src/Player/GuiManager.ts +308 -150
  60. package/src/Player/ItemFixture.ts +4 -5
  61. package/src/Player/ItemManager.ts +774 -355
  62. package/src/Player/MoveManager.ts +1544 -774
  63. package/src/Player/ParameterManager.ts +546 -104
  64. package/src/Player/Player.ts +1163 -88
  65. package/src/Player/SkillManager.ts +520 -195
  66. package/src/Player/StateManager.ts +170 -182
  67. package/src/Player/VariableManager.ts +101 -63
  68. package/src/RpgServer.ts +525 -63
  69. package/src/core/context.ts +1 -0
  70. package/src/decorators/event.ts +61 -0
  71. package/src/decorators/map.ts +327 -0
  72. package/src/index.ts +11 -1
  73. package/src/logs/log.ts +10 -3
  74. package/src/module.ts +126 -3
  75. package/src/presets/index.ts +1 -10
  76. package/src/rooms/BaseRoom.ts +232 -0
  77. package/src/rooms/lobby.ts +25 -7
  78. package/src/rooms/map.ts +2502 -194
  79. package/src/services/save.ts +147 -0
  80. package/src/storage/index.ts +1 -0
  81. package/src/storage/localStorage.ts +76 -0
  82. package/tests/battle.spec.ts +375 -0
  83. package/tests/change-map.spec.ts +72 -0
  84. package/tests/class.spec.ts +274 -0
  85. package/tests/effect.spec.ts +219 -0
  86. package/tests/element.spec.ts +221 -0
  87. package/tests/event.spec.ts +80 -0
  88. package/tests/gold.spec.ts +99 -0
  89. package/tests/item.spec.ts +609 -0
  90. package/tests/module.spec.ts +38 -0
  91. package/tests/move.spec.ts +601 -0
  92. package/tests/player-param.spec.ts +28 -0
  93. package/tests/prediction-reconciliation.spec.ts +182 -0
  94. package/tests/random-move.spec.ts +65 -0
  95. package/tests/skill.spec.ts +658 -0
  96. package/tests/state.spec.ts +467 -0
  97. package/tests/variable.spec.ts +185 -0
  98. package/tests/world-maps.spec.ts +896 -0
  99. package/vite.config.ts +16 -0
  100. package/dist/Player/Event.d.ts +0 -0
  101. package/src/Player/Event.ts +0 -0
@@ -1,17 +1,458 @@
1
- import { Constructor, RpgCommonPlayer } from '@rpgjs/common';
1
+ import { Item, PlayerCtor } from '../../../common/src';
2
2
  import { ItemClass } from '@rpgjs/database';
3
+ import { RpgPlayer } from './Player';
3
4
  /**
4
- * Interface defining what MoveManager adds to a class
5
+ * Interface defining the hooks that can be implemented on item classes or objects
6
+ *
7
+ * These hooks are called at specific moments during the item lifecycle:
8
+ * - `onAdd`: When the item is added to the player's inventory
9
+ * - `onUse`: When the item is successfully used
10
+ * - `onUseFailed`: When the item usage fails (e.g., chance roll failed)
11
+ * - `onRemove`: When the item is removed from the inventory
12
+ * - `onEquip`: When the item is equipped or unequipped
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const itemHooks: ItemHooks = {
17
+ * onAdd(player) {
18
+ * console.log('Item added to inventory');
19
+ * },
20
+ * onUse(player) {
21
+ * player.hp += 100;
22
+ * }
23
+ * };
24
+ * ```
5
25
  */
6
- export interface IItemManager {
7
- databaseById(id: string): ItemClass;
26
+ export interface ItemHooks {
27
+ /**
28
+ * Called when the item is added to the player's inventory
29
+ *
30
+ * @param player - The player receiving the item
31
+ */
32
+ onAdd?: (player: RpgPlayer) => void | Promise<void>;
33
+ /**
34
+ * Called when the item is successfully used
35
+ *
36
+ * @param player - The player using the item
37
+ */
38
+ onUse?: (player: RpgPlayer) => void | Promise<void>;
39
+ /**
40
+ * Called when the item usage fails (e.g., chance roll failed)
41
+ *
42
+ * @param player - The player attempting to use the item
43
+ */
44
+ onUseFailed?: (player: RpgPlayer) => void | Promise<void>;
45
+ /**
46
+ * Called when the item is removed from the inventory
47
+ *
48
+ * @param player - The player losing the item
49
+ */
50
+ onRemove?: (player: RpgPlayer) => void | Promise<void>;
51
+ /**
52
+ * Called when the item is equipped or unequipped
53
+ *
54
+ * @param player - The player equipping/unequipping the item
55
+ * @param equip - true if equipping, false if unequipping
56
+ */
57
+ onEquip?: (player: RpgPlayer, equip: boolean) => void | Promise<void>;
58
+ }
59
+ /**
60
+ * Base properties that can be included in an item object
61
+ *
62
+ * This interface defines the common properties that items can have.
63
+ * Use this as a base and extend it with specific item types.
64
+ *
65
+ * @template T - Additional properties specific to the item type
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * interface PotionData extends ItemData {
70
+ * hpValue: number;
71
+ * mpValue: number;
72
+ * }
73
+ *
74
+ * const potion: ItemObject<PotionData> = {
75
+ * name: 'Health Potion',
76
+ * description: 'Restores 100 HP',
77
+ * price: 200,
78
+ * hpValue: 100,
79
+ * mpValue: 0,
80
+ * onUse(player) {
81
+ * player.hp += this.hpValue;
82
+ * }
83
+ * };
84
+ * ```
85
+ */
86
+ export interface ItemData {
87
+ /** Item name */
88
+ name?: string;
89
+ /** Item description */
90
+ description?: string;
91
+ /** Item price */
92
+ price?: number;
93
+ /** HP value restored when used */
94
+ hpValue?: number;
95
+ /** MP/SP value restored when used */
96
+ mpValue?: number;
97
+ /** Chance to successfully use the item (0-1) */
98
+ hitRate?: number;
99
+ /** Whether the item is consumable */
100
+ consumable?: boolean;
101
+ /** States to add when used */
102
+ addStates?: any[];
103
+ /** States to remove when used */
104
+ removeStates?: any[];
105
+ /** Elemental properties */
106
+ elements?: any[];
107
+ /** Parameter modifiers */
108
+ paramsModifier?: Record<string, any>;
109
+ /** Item type (for equipment validation) */
110
+ _type?: 'item' | 'weapon' | 'armor';
8
111
  }
9
112
  /**
10
- * Move Manager mixin
113
+ * Item object type that combines data properties with hooks
114
+ *
115
+ * This type allows you to create item objects directly without needing a class.
116
+ * The object can contain both item data properties and lifecycle hooks.
117
+ *
118
+ * @template T - Additional properties specific to the item type (extends ItemData)
11
119
  *
12
- * Adds methods to manage player movement
120
+ * @example
121
+ * ```ts
122
+ * const potion: ItemObject = {
123
+ * name: 'Health Potion',
124
+ * description: 'Restores 100 HP',
125
+ * price: 200,
126
+ * hpValue: 100,
127
+ * consumable: true,
128
+ * onAdd(player) {
129
+ * console.log('Potion added!');
130
+ * },
131
+ * onUse(player) {
132
+ * player.hp += 100;
133
+ * }
134
+ * };
13
135
  *
14
- * @param Base - The base class to extend
15
- * @returns A new class with move management capabilities
136
+ * player.addItem(potion);
137
+ * ```
16
138
  */
17
- export declare function WithItemManager<TBase extends Constructor<RpgCommonPlayer>>(Base: TBase): Constructor<IItemManager> & TBase;
139
+ export type ItemObject<T extends ItemData = ItemData> = T & ItemHooks & {
140
+ /** Item identifier (required if not using class or string) */
141
+ id?: string;
142
+ };
143
+ /**
144
+ * Item Manager Mixin
145
+ *
146
+ * Provides comprehensive item management capabilities to any class. This mixin handles
147
+ * inventory management, item usage, equipment, buying/selling, and item effects.
148
+ * It manages the complete item system including restrictions, transactions, and equipment.
149
+ *
150
+ * @param Base - The base class to extend with item management
151
+ * @returns Extended class with item management methods
152
+ *
153
+ * @example
154
+ * ```ts
155
+ * class MyPlayer extends WithItemManager(BasePlayer) {
156
+ * constructor() {
157
+ * super();
158
+ * // Item system is automatically initialized
159
+ * }
160
+ * }
161
+ *
162
+ * const player = new MyPlayer();
163
+ * player.addItem('potion', 5);
164
+ * player.useItem('potion');
165
+ * ```
166
+ */
167
+ export declare function WithItemManager<TBase extends PlayerCtor>(Base: TBase): TBase;
168
+ /**
169
+ * Interface for Item Manager functionality
170
+ *
171
+ * Provides comprehensive item management capabilities including inventory management,
172
+ * item usage, equipment, buying/selling, and item effects. This interface defines
173
+ * the public API of the ItemManager mixin.
174
+ */
175
+ export interface IItemManager {
176
+ /**
177
+ * Retrieves the information of an object: the number and the instance
178
+ *
179
+ * The returned Item instance contains the quantity information accessible via `quantity()` method.
180
+ *
181
+ * @param itemClass - Item class or string identifier. If string, it's the item ID
182
+ * @returns Item instance containing quantity and item data
183
+ *
184
+ * @example
185
+ * ```ts
186
+ * import Potion from 'your-database/potion'
187
+ *
188
+ * player.addItem(Potion, 5)
189
+ * const inventory = player.getItem(Potion)
190
+ * console.log(inventory.quantity()) // 5
191
+ * console.log(inventory) // <instance of Item>
192
+ * ```
193
+ */
194
+ getItem(itemClass: ItemClass | string): Item;
195
+ /**
196
+ * Check if the player has the item in his inventory
197
+ *
198
+ * @param itemClass - Item class or string identifier. If string, it's the item ID
199
+ * @returns `true` if player has the item, `false` otherwise
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * import Potion from 'your-database/potion'
204
+ *
205
+ * player.hasItem(Potion) // false
206
+ * player.addItem(Potion, 1)
207
+ * player.hasItem(Potion) // true
208
+ * ```
209
+ */
210
+ hasItem(itemClass: ItemClass | string): boolean;
211
+ /**
212
+ * Add an item in the player's inventory
213
+ *
214
+ * You can add items using:
215
+ * - Item class (automatically registered in database if needed)
216
+ * - Item object (automatically registered in database if needed)
217
+ * - String ID (must be pre-registered in database)
218
+ *
219
+ * The `onAdd()` method is called on the ItemClass or ItemObject when the item is added.
220
+ *
221
+ * @param item - Item class, object, or string identifier
222
+ * @param nb - Number of items to add (default: 1)
223
+ * @returns The item instance added to inventory
224
+ *
225
+ * @example
226
+ * ```ts
227
+ * import Potion from 'your-database/potion'
228
+ *
229
+ * // Add using class
230
+ * player.addItem(Potion, 5)
231
+ *
232
+ * // Add using string ID (must be registered in database)
233
+ * player.addItem('Potion', 3)
234
+ *
235
+ * // Add using object
236
+ * player.addItem({
237
+ * id: 'custom-potion',
238
+ * name: 'Custom Potion',
239
+ * price: 200,
240
+ * onAdd(player) {
241
+ * console.log('Custom potion added!')
242
+ * }
243
+ * }, 2)
244
+ * ```
245
+ */
246
+ addItem(item: ItemClass | ItemObject | string, nb?: number): Item;
247
+ /**
248
+ * Deletes an item from inventory
249
+ *
250
+ * Decreases the quantity by `nb`. If the quantity falls to 0 or below, the item is removed from the inventory.
251
+ * The method returns `undefined` if the item is completely removed.
252
+ *
253
+ * The `onRemove()` method is called on the ItemClass when the item is removed.
254
+ *
255
+ * @param itemClass - Item class or string identifier. If string, it's the item ID
256
+ * @param nb - Number of items to remove (default: 1)
257
+ * @returns Item instance or `undefined` if the item was completely removed
258
+ * @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
259
+ * - `id`: `ITEM_NOT_INVENTORY`
260
+ * - `msg`: Error message
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * import Potion from 'your-database/potion'
265
+ *
266
+ * try {
267
+ * player.removeItem(Potion, 5)
268
+ * } catch (err) {
269
+ * console.log(err) // { id: 'ITEM_NOT_INVENTORY', msg: '...' }
270
+ * }
271
+ * ```
272
+ */
273
+ removeItem(itemClass: ItemClass | string, nb?: number): Item | undefined;
274
+ /**
275
+ * Purchases an item and reduces the amount of gold
276
+ *
277
+ * The player's gold is reduced by `nb * item.price`. The item is then added to the inventory.
278
+ * The `onAdd()` method is called on the ItemClass when the item is added.
279
+ *
280
+ * @param item - Item class, object, or string identifier
281
+ * @param nb - Number of items to buy (default: 1)
282
+ * @returns Item instance added to inventory
283
+ * @throws {Object} ItemLog.haveNotPrice - If the item has no price set
284
+ * - `id`: `NOT_PRICE`
285
+ * - `msg`: Error message
286
+ * @throws {Object} ItemLog.notEnoughGold - If the player doesn't have enough gold
287
+ * - `id`: `NOT_ENOUGH_GOLD`
288
+ * - `msg`: Error message
289
+ *
290
+ * @example
291
+ * ```ts
292
+ * import Potion from 'your-database/potion'
293
+ *
294
+ * try {
295
+ * player.buyItem(Potion)
296
+ * } catch (err) {
297
+ * if (err.id === 'NOT_ENOUGH_GOLD') {
298
+ * console.log('Not enough gold!')
299
+ * } else if (err.id === 'NOT_PRICE') {
300
+ * console.log('Item has no price!')
301
+ * }
302
+ * }
303
+ * ```
304
+ */
305
+ buyItem(item: ItemClass | ItemObject | string, nb?: number): Item;
306
+ /**
307
+ * Sell an item and the player wins the amount of the item divided by 2
308
+ *
309
+ * The player receives `(item.price / 2) * nbToSell` gold. The item is removed from the inventory.
310
+ * The `onRemove()` method is called on the ItemClass when the item is removed.
311
+ *
312
+ * @param itemClass - Item class or string identifier. If string, it's the item ID
313
+ * @param nbToSell - Number of items to sell (default: 1)
314
+ * @returns Item instance that was sold
315
+ * @throws {Object} ItemLog.haveNotPrice - If the item has no price set
316
+ * - `id`: `NOT_PRICE`
317
+ * - `msg`: Error message
318
+ * @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
319
+ * - `id`: `ITEM_NOT_INVENTORY`
320
+ * - `msg`: Error message
321
+ * @throws {Object} ItemLog.tooManyToSell - If trying to sell more items than available
322
+ * - `id`: `TOO_MANY_ITEM_TO_SELL`
323
+ * - `msg`: Error message
324
+ *
325
+ * @example
326
+ * ```ts
327
+ * import Potion from 'your-database/potion'
328
+ *
329
+ * try {
330
+ * player.addItem(Potion)
331
+ * player.sellItem(Potion)
332
+ * } catch (err) {
333
+ * console.log(err)
334
+ * }
335
+ * ```
336
+ */
337
+ sellItem(itemClass: ItemClass | string, nbToSell?: number): Item;
338
+ /**
339
+ * Use an object. Applies effects and states. Removes the object from the inventory
340
+ *
341
+ * When an item is used:
342
+ * - Effects are applied to the player (HP/MP restoration, etc.)
343
+ * - States are applied/removed as defined in the item
344
+ * - The item is removed from inventory (consumed)
345
+ *
346
+ * If the item has a `hitRate` property (0-1), there's a chance the usage might fail.
347
+ * If usage fails, the item is still removed and `onUseFailed()` is called instead of `onUse()`.
348
+ *
349
+ * The `onUse()` method is called on the ItemClass if the use was successful.
350
+ * The `onUseFailed()` method is called on the ItemClass if the chance roll failed.
351
+ * The `onRemove()` method is called on the ItemClass when the item is removed.
352
+ *
353
+ * @param itemClass - Item class or string identifier. If string, it's the item ID
354
+ * @returns Item instance that was used
355
+ * @throws {Object} ItemLog.restriction - If the player has the `Effect.CAN_NOT_ITEM` effect
356
+ * - `id`: `RESTRICTION_ITEM`
357
+ * - `msg`: Error message
358
+ * @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
359
+ * - `id`: `ITEM_NOT_INVENTORY`
360
+ * - `msg`: Error message
361
+ * @throws {Object} ItemLog.notUseItem - If the item's `consumable` property is `false`
362
+ * - `id`: `NOT_USE_ITEM`
363
+ * - `msg`: Error message
364
+ * @throws {Object} ItemLog.chanceToUseFailed - If the chance to use the item failed (hitRate roll failed)
365
+ * - `id`: `USE_CHANCE_ITEM_FAILED`
366
+ * - `msg`: Error message
367
+ * - Note: The item is still deleted from the inventory even if usage failed
368
+ *
369
+ * @example
370
+ * ```ts
371
+ * import Potion from 'your-database/potion'
372
+ *
373
+ * try {
374
+ * player.addItem(Potion)
375
+ * player.useItem(Potion)
376
+ * } catch (err) {
377
+ * if (err.id === 'USE_CHANCE_ITEM_FAILED') {
378
+ * console.log('Item usage failed due to chance roll')
379
+ * } else {
380
+ * console.log(err)
381
+ * }
382
+ * }
383
+ * ```
384
+ */
385
+ useItem(itemClass: ItemClass | string): Item;
386
+ /**
387
+ * Equips a weapon or armor on a player
388
+ *
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.
391
+ *
392
+ * The `onEquip()` method is called on the ItemClass when the item is equipped or unequipped.
393
+ *
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`)
396
+ * @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
397
+ * - `id`: `ITEM_NOT_INVENTORY`
398
+ * - `msg`: Error message
399
+ * @throws {Object} ItemLog.invalidToEquiped - If the item is not a weapon or armor (item._type is "item")
400
+ * - `id`: `INVALID_ITEM_TO_EQUIP`
401
+ * - `msg`: Error message
402
+ * @throws {Object} ItemLog.isAlreadyEquiped - If the item is already equipped
403
+ * - `id`: `ITEM_ALREADY_EQUIPED`
404
+ * - `msg`: Error message
405
+ *
406
+ * @example
407
+ * ```ts
408
+ * try {
409
+ * player.addItem('sword')
410
+ * player.equip('sword')
411
+ * // Later, unequip it
412
+ * player.equip('sword', false)
413
+ * } catch (err) {
414
+ * console.log(err)
415
+ * }
416
+ * ```
417
+ */
418
+ equip(itemId: string, equip?: boolean | 'auto'): void;
419
+ /**
420
+ * Get the player's attack (sum of items equipped)
421
+ *
422
+ * Returns the total attack value from all equipped items on the player.
423
+ *
424
+ * @returns Total attack value from equipped items
425
+ *
426
+ * @example
427
+ * ```ts
428
+ * console.log(player.atk) // 150 (sum of all equipped weapons/armors attack)
429
+ * ```
430
+ */
431
+ readonly atk: number;
432
+ /**
433
+ * Get the player's physical defense (sum of items equipped)
434
+ *
435
+ * Returns the total physical defense value from all equipped items on the player.
436
+ *
437
+ * @returns Total physical defense value from equipped items
438
+ *
439
+ * @example
440
+ * ```ts
441
+ * console.log(player.pdef) // 80 (sum of all equipped armors physical defense)
442
+ * ```
443
+ */
444
+ readonly pdef: number;
445
+ /**
446
+ * Get the player's skill defense (sum of items equipped)
447
+ *
448
+ * Returns the total skill defense value from all equipped items on the player.
449
+ *
450
+ * @returns Total skill defense value from equipped items
451
+ *
452
+ * @example
453
+ * ```ts
454
+ * console.log(player.sdef) // 60 (sum of all equipped armors skill defense)
455
+ * ```
456
+ */
457
+ readonly sdef: number;
458
+ }