@rpgjs/server 5.0.0-alpha.8 → 5.0.0-beta.1
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/dist/Gui/DialogGui.d.ts +5 -0
- package/dist/Gui/GameoverGui.d.ts +23 -0
- package/dist/Gui/Gui.d.ts +6 -0
- package/dist/Gui/MenuGui.d.ts +22 -3
- package/dist/Gui/NotificationGui.d.ts +1 -2
- package/dist/Gui/SaveLoadGui.d.ts +13 -0
- package/dist/Gui/ShopGui.d.ts +28 -3
- package/dist/Gui/TitleGui.d.ts +23 -0
- package/dist/Gui/index.d.ts +10 -1
- package/dist/Player/BattleManager.d.ts +44 -32
- package/dist/Player/ClassManager.d.ts +24 -4
- package/dist/Player/ComponentManager.d.ts +100 -7
- package/dist/Player/Components.d.ts +345 -0
- package/dist/Player/EffectManager.d.ts +50 -4
- package/dist/Player/ElementManager.d.ts +77 -4
- package/dist/Player/GoldManager.d.ts +1 -1
- package/dist/Player/GuiManager.d.ts +233 -5
- package/dist/Player/ItemFixture.d.ts +1 -1
- package/dist/Player/ItemManager.d.ts +431 -4
- package/dist/Player/MoveManager.d.ts +301 -34
- package/dist/Player/ParameterManager.d.ts +364 -28
- package/dist/Player/Player.d.ts +558 -14
- package/dist/Player/SkillManager.d.ts +187 -13
- package/dist/Player/StateManager.d.ts +75 -4
- package/dist/Player/VariableManager.d.ts +62 -4
- package/dist/RpgServer.d.ts +278 -63
- package/dist/RpgServerEngine.d.ts +2 -1
- package/dist/decorators/event.d.ts +46 -0
- package/dist/decorators/map.d.ts +299 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +17920 -29866
- package/dist/index.js.map +1 -1
- package/dist/logs/log.d.ts +2 -3
- package/dist/module-CaCW1SDh.js +11018 -0
- package/dist/module-CaCW1SDh.js.map +1 -0
- package/dist/module.d.ts +43 -1
- package/dist/node/connection.d.ts +51 -0
- package/dist/node/index.d.ts +5 -0
- package/dist/node/index.js +551 -0
- package/dist/node/index.js.map +1 -0
- package/dist/node/map.d.ts +16 -0
- package/dist/node/room.d.ts +21 -0
- package/dist/node/transport.d.ts +28 -0
- package/dist/node/types.d.ts +47 -0
- package/dist/presets/index.d.ts +0 -9
- package/dist/rooms/BaseRoom.d.ts +132 -0
- package/dist/rooms/lobby.d.ts +10 -2
- package/dist/rooms/map.d.ts +1359 -32
- package/dist/services/save.d.ts +43 -0
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/localStorage.d.ts +23 -0
- package/package.json +25 -10
- package/src/Gui/DialogGui.ts +19 -4
- package/src/Gui/GameoverGui.ts +39 -0
- package/src/Gui/Gui.ts +23 -1
- package/src/Gui/MenuGui.ts +155 -6
- package/src/Gui/NotificationGui.ts +1 -2
- package/src/Gui/SaveLoadGui.ts +60 -0
- package/src/Gui/ShopGui.ts +146 -16
- package/src/Gui/TitleGui.ts +39 -0
- package/src/Gui/index.ts +15 -2
- package/src/Player/BattleManager.ts +39 -56
- package/src/Player/ClassManager.ts +82 -74
- package/src/Player/ComponentManager.ts +401 -37
- package/src/Player/Components.ts +380 -0
- package/src/Player/EffectManager.ts +50 -96
- package/src/Player/ElementManager.ts +74 -152
- package/src/Player/GuiManager.ts +284 -149
- package/src/Player/ItemManager.ts +747 -341
- package/src/Player/MoveManager.ts +1532 -750
- package/src/Player/ParameterManager.ts +636 -106
- package/src/Player/Player.ts +1273 -79
- package/src/Player/SkillManager.ts +558 -197
- package/src/Player/StateManager.ts +131 -258
- package/src/Player/VariableManager.ts +85 -157
- package/src/RpgServer.ts +293 -62
- package/src/decorators/event.ts +61 -0
- package/src/decorators/map.ts +343 -0
- package/src/index.ts +11 -1
- package/src/logs/log.ts +10 -3
- package/src/module.ts +126 -3
- package/src/node/connection.ts +254 -0
- package/src/node/index.ts +22 -0
- package/src/node/map.ts +328 -0
- package/src/node/room.ts +63 -0
- package/src/node/transport.ts +532 -0
- package/src/node/types.ts +61 -0
- package/src/presets/index.ts +1 -10
- package/src/rooms/BaseRoom.ts +232 -0
- package/src/rooms/lobby.ts +25 -7
- package/src/rooms/map.ts +2682 -206
- package/src/services/save.ts +147 -0
- package/src/storage/index.ts +1 -0
- package/src/storage/localStorage.ts +76 -0
- package/tests/battle.spec.ts +375 -0
- package/tests/change-map.spec.ts +72 -0
- package/tests/class.spec.ts +274 -0
- package/tests/custom-websocket.spec.ts +127 -0
- package/tests/effect.spec.ts +219 -0
- package/tests/element.spec.ts +221 -0
- package/tests/event.spec.ts +80 -0
- package/tests/gold.spec.ts +99 -0
- package/tests/item.spec.ts +609 -0
- package/tests/module.spec.ts +38 -0
- package/tests/move.spec.ts +601 -0
- package/tests/node-transport.spec.ts +223 -0
- package/tests/player-param.spec.ts +45 -0
- package/tests/prediction-reconciliation.spec.ts +182 -0
- package/tests/random-move.spec.ts +65 -0
- package/tests/skill.spec.ts +658 -0
- package/tests/state.spec.ts +467 -0
- package/tests/variable.spec.ts +185 -0
- package/tests/world-maps.spec.ts +896 -0
- package/vite.config.ts +36 -3
- package/dist/Player/Event.d.ts +0 -0
- package/src/Player/Event.ts +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isInstanceOf, isString, Item,
|
|
2
|
-
import {
|
|
3
|
-
import { ATK, PDEF, SDEF } from "../presets";
|
|
1
|
+
import { isInstanceOf, isString, Item, type PlayerCtor} from "@rpgjs/common";
|
|
2
|
+
import { ATK, PDEF, SDEF } from "@rpgjs/common";
|
|
4
3
|
import { ItemLog } from "../logs";
|
|
5
|
-
import {
|
|
4
|
+
import type { ItemClass, ItemInstance } from "@rpgjs/database";
|
|
5
|
+
import { RpgPlayer } from "./Player";
|
|
6
6
|
|
|
7
7
|
// Ajout des enums manquants
|
|
8
8
|
enum Effect {
|
|
@@ -15,6 +15,152 @@ enum ClassHooks {
|
|
|
15
15
|
|
|
16
16
|
type Inventory = { nb: number; item: ItemInstance };
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Interface defining the hooks that can be implemented on item classes or objects
|
|
20
|
+
*
|
|
21
|
+
* These hooks are called at specific moments during the item lifecycle:
|
|
22
|
+
* - `onAdd`: When the item is added to the player's inventory
|
|
23
|
+
* - `onUse`: When the item is successfully used
|
|
24
|
+
* - `onUseFailed`: When the item usage fails (e.g., chance roll failed)
|
|
25
|
+
* - `onRemove`: When the item is removed from the inventory
|
|
26
|
+
* - `onEquip`: When the item is equipped or unequipped
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const itemHooks: ItemHooks = {
|
|
31
|
+
* onAdd(player) {
|
|
32
|
+
* console.log('Item added to inventory');
|
|
33
|
+
* },
|
|
34
|
+
* onUse(player) {
|
|
35
|
+
* player.hp += 100;
|
|
36
|
+
* }
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export interface ItemHooks {
|
|
41
|
+
/**
|
|
42
|
+
* Called when the item is added to the player's inventory
|
|
43
|
+
*
|
|
44
|
+
* @param player - The player receiving the item
|
|
45
|
+
*/
|
|
46
|
+
onAdd?: (player: RpgPlayer) => void | Promise<void>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Called when the item is successfully used
|
|
50
|
+
*
|
|
51
|
+
* @param player - The player using the item
|
|
52
|
+
*/
|
|
53
|
+
onUse?: (player: RpgPlayer) => void | Promise<void>;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Called when the item usage fails (e.g., chance roll failed)
|
|
57
|
+
*
|
|
58
|
+
* @param player - The player attempting to use the item
|
|
59
|
+
*/
|
|
60
|
+
onUseFailed?: (player: RpgPlayer) => void | Promise<void>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Called when the item is removed from the inventory
|
|
64
|
+
*
|
|
65
|
+
* @param player - The player losing the item
|
|
66
|
+
*/
|
|
67
|
+
onRemove?: (player: RpgPlayer) => void | Promise<void>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Called when the item is equipped or unequipped
|
|
71
|
+
*
|
|
72
|
+
* @param player - The player equipping/unequipping the item
|
|
73
|
+
* @param equip - true if equipping, false if unequipping
|
|
74
|
+
*/
|
|
75
|
+
onEquip?: (player: RpgPlayer, equip: boolean) => void | Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Base properties that can be included in an item object
|
|
80
|
+
*
|
|
81
|
+
* This interface defines the common properties that items can have.
|
|
82
|
+
* Use this as a base and extend it with specific item types.
|
|
83
|
+
*
|
|
84
|
+
* @template T - Additional properties specific to the item type
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* interface PotionData extends ItemData {
|
|
89
|
+
* hpValue: number;
|
|
90
|
+
* mpValue: number;
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* const potion: ItemObject<PotionData> = {
|
|
94
|
+
* name: 'Health Potion',
|
|
95
|
+
* description: 'Restores 100 HP',
|
|
96
|
+
* price: 200,
|
|
97
|
+
* hpValue: 100,
|
|
98
|
+
* mpValue: 0,
|
|
99
|
+
* onUse(player) {
|
|
100
|
+
* player.hp += this.hpValue;
|
|
101
|
+
* }
|
|
102
|
+
* };
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export interface ItemData {
|
|
106
|
+
/** Item name */
|
|
107
|
+
name?: string;
|
|
108
|
+
/** Item description */
|
|
109
|
+
description?: string;
|
|
110
|
+
/** Item price */
|
|
111
|
+
price?: number;
|
|
112
|
+
/** HP value restored when used */
|
|
113
|
+
hpValue?: number;
|
|
114
|
+
/** MP/SP value restored when used */
|
|
115
|
+
mpValue?: number;
|
|
116
|
+
/** Chance to successfully use the item (0-1) */
|
|
117
|
+
hitRate?: number;
|
|
118
|
+
/** Whether the item is consumable */
|
|
119
|
+
consumable?: boolean;
|
|
120
|
+
/** States to add when used */
|
|
121
|
+
addStates?: any[];
|
|
122
|
+
/** States to remove when used */
|
|
123
|
+
removeStates?: any[];
|
|
124
|
+
/** Elemental properties */
|
|
125
|
+
elements?: any[];
|
|
126
|
+
/** Parameter modifiers */
|
|
127
|
+
paramsModifier?: Record<string, any>;
|
|
128
|
+
/** Item type (for equipment validation) */
|
|
129
|
+
_type?: 'item' | 'weapon' | 'armor';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Item object type that combines data properties with hooks
|
|
134
|
+
*
|
|
135
|
+
* This type allows you to create item objects directly without needing a class.
|
|
136
|
+
* The object can contain both item data properties and lifecycle hooks.
|
|
137
|
+
*
|
|
138
|
+
* @template T - Additional properties specific to the item type (extends ItemData)
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* const potion: ItemObject = {
|
|
143
|
+
* name: 'Health Potion',
|
|
144
|
+
* description: 'Restores 100 HP',
|
|
145
|
+
* price: 200,
|
|
146
|
+
* hpValue: 100,
|
|
147
|
+
* consumable: true,
|
|
148
|
+
* onAdd(player) {
|
|
149
|
+
* console.log('Potion added!');
|
|
150
|
+
* },
|
|
151
|
+
* onUse(player) {
|
|
152
|
+
* player.hp += 100;
|
|
153
|
+
* }
|
|
154
|
+
* };
|
|
155
|
+
*
|
|
156
|
+
* player.addItem(potion);
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
export type ItemObject<T extends ItemData = ItemData> = T & ItemHooks & {
|
|
160
|
+
/** Item identifier (required if not using class or string) */
|
|
161
|
+
id?: string;
|
|
162
|
+
};
|
|
163
|
+
|
|
18
164
|
/**
|
|
19
165
|
* Item Manager Mixin
|
|
20
166
|
*
|
|
@@ -42,43 +188,11 @@ type Inventory = { nb: number; item: ItemInstance };
|
|
|
42
188
|
export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
43
189
|
return class extends Base {
|
|
44
190
|
|
|
45
|
-
/**
|
|
46
|
-
* Retrieves the information of an object: the number and the instance
|
|
47
|
-
* @title Get Item
|
|
48
|
-
* @method player.getItem(itemClass)
|
|
49
|
-
* @param {ItemClass | string} itemClass Identifier of the object if the parameter is a string
|
|
50
|
-
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
51
|
-
* @memberof ItemManager
|
|
52
|
-
* @example
|
|
53
|
-
*
|
|
54
|
-
* ```ts
|
|
55
|
-
* import Potion from 'your-database/potion'
|
|
56
|
-
*
|
|
57
|
-
* player.addItem(Potion, 5)
|
|
58
|
-
* const inventory = player.getItem(Potion)
|
|
59
|
-
* console.log(inventory) // { nb: 5, item: <instance of Potion> }
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
191
|
getItem(itemClass: ItemClass | string): Item {
|
|
63
192
|
const index: number = this._getItemIndex(itemClass);
|
|
64
193
|
return (this as any).items()[index];
|
|
65
194
|
}
|
|
66
195
|
|
|
67
|
-
/**
|
|
68
|
-
* Check if the player has the item in his inventory.
|
|
69
|
-
* @title Has Item
|
|
70
|
-
* @method player.hasItem(itemClass)
|
|
71
|
-
* @param {ItemClass | string} itemClass Identifier of the object if the parameter is a string
|
|
72
|
-
* @returns {boolean}
|
|
73
|
-
* @memberof ItemManager
|
|
74
|
-
* @example
|
|
75
|
-
*
|
|
76
|
-
* ```ts
|
|
77
|
-
* import Potion from 'your-database/potion'
|
|
78
|
-
*
|
|
79
|
-
* player.hasItem(Potion) // false
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
196
|
hasItem(itemClass: ItemClass | string): boolean {
|
|
83
197
|
return !!this.getItem(itemClass);
|
|
84
198
|
}
|
|
@@ -91,72 +205,237 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
91
205
|
return isInstanceOf(it, itemClass);
|
|
92
206
|
});
|
|
93
207
|
}
|
|
208
|
+
|
|
209
|
+
private _getItemMap(required: boolean = true) {
|
|
210
|
+
// Use this.map directly to support both RpgMap and LobbyRoom
|
|
211
|
+
const map = (this as any).getCurrentMap?.() || (this as any).map;
|
|
212
|
+
if (required && (!map || !map.database)) {
|
|
213
|
+
throw new Error('Player must be on a map to add items');
|
|
214
|
+
}
|
|
215
|
+
return map;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private _resolveItemInput(
|
|
219
|
+
item: ItemClass | ItemObject | string,
|
|
220
|
+
map: any,
|
|
221
|
+
databaseByIdOverride?: (id: string) => any
|
|
222
|
+
) {
|
|
223
|
+
let itemId: string;
|
|
224
|
+
let data: any;
|
|
225
|
+
let itemInstance: any = null;
|
|
226
|
+
|
|
227
|
+
if (isString(item)) {
|
|
228
|
+
itemId = item as string;
|
|
229
|
+
data = databaseByIdOverride
|
|
230
|
+
? databaseByIdOverride(itemId)
|
|
231
|
+
: (this as any).databaseById(itemId);
|
|
232
|
+
} else if (typeof item === 'function' || (item as any).prototype) {
|
|
233
|
+
itemId = (item as any).name;
|
|
234
|
+
|
|
235
|
+
const existingData = map.database()[itemId];
|
|
236
|
+
if (existingData) {
|
|
237
|
+
data = existingData;
|
|
238
|
+
} else {
|
|
239
|
+
map.addInDatabase(itemId, item as ItemClass);
|
|
240
|
+
data = item as ItemClass;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
itemInstance = new (item as ItemClass)();
|
|
244
|
+
} else {
|
|
245
|
+
const itemObj = item as ItemObject;
|
|
246
|
+
itemId = itemObj.id || `item-${Date.now()}`;
|
|
247
|
+
|
|
248
|
+
const existingData = map.database()[itemId];
|
|
249
|
+
if (existingData) {
|
|
250
|
+
data = { ...existingData, ...itemObj };
|
|
251
|
+
map.addInDatabase(itemId, data, { force: true });
|
|
252
|
+
} else {
|
|
253
|
+
map.addInDatabase(itemId, itemObj);
|
|
254
|
+
data = itemObj;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
itemInstance = itemObj;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return { itemId, data, itemInstance };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
private _createItemInstance(
|
|
264
|
+
itemId: string,
|
|
265
|
+
data: any,
|
|
266
|
+
nb: number,
|
|
267
|
+
itemInstance: any
|
|
268
|
+
): Item {
|
|
269
|
+
const instance = new Item(data);
|
|
270
|
+
instance.id.set(itemId);
|
|
271
|
+
instance.quantity.set(nb);
|
|
272
|
+
|
|
273
|
+
if (itemInstance) {
|
|
274
|
+
(instance as any)._itemInstance = itemInstance;
|
|
275
|
+
if (itemInstance.onAdd) {
|
|
276
|
+
instance.onAdd = itemInstance.onAdd.bind(itemInstance);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return instance;
|
|
281
|
+
}
|
|
282
|
+
|
|
94
283
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* `onAdd()` method is called on the ItemClass
|
|
98
|
-
*
|
|
99
|
-
* @title Add Item
|
|
100
|
-
* @method player.addItem(item,nb=1)
|
|
101
|
-
* @param {ItemClass} itemClass
|
|
102
|
-
* @param {number} [nb] Default 1
|
|
103
|
-
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
104
|
-
* @memberof ItemManager
|
|
105
|
-
* @example
|
|
106
|
-
*
|
|
107
|
-
* ```ts
|
|
108
|
-
* import Potion from 'your-database/potion'
|
|
109
|
-
* player.addItem(Potion, 5)
|
|
110
|
-
* ```
|
|
284
|
+
* Create an item instance without inventory changes or hook execution.
|
|
111
285
|
*/
|
|
112
|
-
|
|
113
|
-
const
|
|
114
|
-
const
|
|
286
|
+
createItemInstance(item: ItemClass | ItemObject | string, nb: number = 1) {
|
|
287
|
+
const map = this._getItemMap();
|
|
288
|
+
const { itemId, data, itemInstance } = this._resolveItemInput(item, map);
|
|
289
|
+
const instance = this._createItemInstance(itemId, data, nb, itemInstance);
|
|
290
|
+
return { itemId, data, itemInstance, instance };
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Resolve item snapshot entries into Item instances without side effects.
|
|
295
|
+
*/
|
|
296
|
+
resolveItemsSnapshot(snapshot: { items?: any[] }, mapOverride?: any) {
|
|
297
|
+
if (!snapshot || !Array.isArray(snapshot.items)) {
|
|
298
|
+
return snapshot;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const map = mapOverride ?? this._getItemMap(false);
|
|
302
|
+
if (!map || !map.database) {
|
|
303
|
+
return snapshot;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const databaseByIdOverride = (id: string) => {
|
|
307
|
+
const data = map.database()[id];
|
|
308
|
+
if (!data) {
|
|
309
|
+
throw new Error(
|
|
310
|
+
`The ID=${id} data is not found in the database. Add the data in the property "database"`
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
return data;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
const items = snapshot.items.map((entry: any) => {
|
|
317
|
+
const itemId = isString(entry) ? entry : entry?.id;
|
|
318
|
+
if (!itemId) {
|
|
319
|
+
return entry;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const nb =
|
|
323
|
+
!isString(entry) && typeof entry?.nb === 'number'
|
|
324
|
+
? entry.nb
|
|
325
|
+
: !isString(entry) && typeof entry?.quantity === 'number'
|
|
326
|
+
? entry.quantity
|
|
327
|
+
: 1;
|
|
328
|
+
|
|
329
|
+
const { data, itemInstance } = this._resolveItemInput(
|
|
330
|
+
itemId,
|
|
331
|
+
map,
|
|
332
|
+
databaseByIdOverride
|
|
333
|
+
);
|
|
334
|
+
return this._createItemInstance(itemId, data, nb, itemInstance);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
return { ...snapshot, items };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Resolve equipment snapshot entries into Item instances without side effects.
|
|
342
|
+
*/
|
|
343
|
+
resolveEquipmentsSnapshot(snapshot: { equipments?: any[]; items?: any[] }, mapOverride?: any) {
|
|
344
|
+
if (!snapshot || !Array.isArray(snapshot.equipments)) {
|
|
345
|
+
return snapshot;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const map = mapOverride ?? this._getItemMap(false);
|
|
349
|
+
if (!map || !map.database) {
|
|
350
|
+
return snapshot;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const databaseByIdOverride = (id: string) => {
|
|
354
|
+
const data = map.database()[id];
|
|
355
|
+
if (!data) {
|
|
356
|
+
throw new Error(
|
|
357
|
+
`The ID=${id} data is not found in the database. Add the data in the property "database"`
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
return data;
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const resolvedItems = Array.isArray(snapshot.items) ? snapshot.items : [];
|
|
364
|
+
const getItemId = (entry: any) => {
|
|
365
|
+
if (isString(entry)) return entry;
|
|
366
|
+
if (typeof entry?.id === 'function') return entry.id();
|
|
367
|
+
return entry?.id;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
const equipments = snapshot.equipments.map((entry: any) => {
|
|
371
|
+
const itemId = getItemId(entry);
|
|
372
|
+
if (!itemId) {
|
|
373
|
+
return entry;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const existing = resolvedItems.find((item: any) => {
|
|
377
|
+
const existingId = getItemId(item);
|
|
378
|
+
return existingId === itemId;
|
|
379
|
+
});
|
|
380
|
+
if (existing) {
|
|
381
|
+
return existing;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const { data, itemInstance } = this._resolveItemInput(
|
|
385
|
+
itemId,
|
|
386
|
+
map,
|
|
387
|
+
databaseByIdOverride
|
|
388
|
+
);
|
|
389
|
+
return this._createItemInstance(itemId, data, 1, itemInstance);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
return { ...snapshot, equipments };
|
|
393
|
+
}
|
|
394
|
+
addItem(item: ItemClass | ItemObject | string, nb: number = 1): Item {
|
|
395
|
+
const map = this._getItemMap();
|
|
396
|
+
const { itemId, data, itemInstance } = this._resolveItemInput(item, map);
|
|
397
|
+
|
|
398
|
+
// Find existing item in inventory
|
|
399
|
+
const existingItem = (this as any).items().find((it: Item) => it.id() == itemId);
|
|
115
400
|
let instance: Item;
|
|
116
|
-
|
|
117
|
-
|
|
401
|
+
|
|
402
|
+
if (existingItem) {
|
|
403
|
+
// Item already exists, update quantity and merge properties
|
|
404
|
+
instance = existingItem;
|
|
118
405
|
instance.quantity.update((it) => it + nb);
|
|
406
|
+
|
|
407
|
+
// Update item properties from merged data (e.g., name, description, price)
|
|
408
|
+
if (data.name !== undefined) {
|
|
409
|
+
instance.name.set(data.name);
|
|
410
|
+
}
|
|
411
|
+
if (data.description !== undefined) {
|
|
412
|
+
instance.description.set(data.description);
|
|
413
|
+
}
|
|
414
|
+
if (data.price !== undefined) {
|
|
415
|
+
instance.price.set(data.price);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// Update stored instance if it's an object with hooks
|
|
419
|
+
if (itemInstance && typeof itemInstance === 'object' && !(itemInstance instanceof Function)) {
|
|
420
|
+
(instance as any)._itemInstance = itemInstance;
|
|
421
|
+
// Update hooks if they exist
|
|
422
|
+
if (itemInstance.onAdd) {
|
|
423
|
+
instance.onAdd = itemInstance.onAdd.bind(itemInstance);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
119
426
|
} else {
|
|
120
|
-
|
|
121
|
-
instance.
|
|
427
|
+
// Create new item instance
|
|
428
|
+
instance = this._createItemInstance(itemId, data, nb, itemInstance);
|
|
122
429
|
(this as any).items().push(instance);
|
|
123
430
|
}
|
|
124
|
-
|
|
431
|
+
|
|
432
|
+
// Call onAdd hook - use stored instance if available
|
|
433
|
+
const hookTarget = (instance as any)._itemInstance || instance;
|
|
434
|
+
// Only call onAdd if it exists and is a function
|
|
435
|
+
(this as any)["execMethod"]("onAdd", [this], hookTarget);
|
|
125
436
|
return instance;
|
|
126
437
|
}
|
|
127
438
|
|
|
128
|
-
/**
|
|
129
|
-
* Deletes an item. Decreases the value `nb`. If the number falls to 0, then the item is removed from the inventory. The method then returns `undefined`
|
|
130
|
-
*
|
|
131
|
-
* `onRemove()` method is called on the ItemClass
|
|
132
|
-
*
|
|
133
|
-
* @title Remove Item
|
|
134
|
-
* @method player.removeItem(item,nb=1)
|
|
135
|
-
* @param {ItemClass | string} itemClass string is item id
|
|
136
|
-
* @param {number} [nb] Default 1
|
|
137
|
-
* @returns {{ nb: number, item: instance of ItemClass } | undefined}
|
|
138
|
-
* @throws {ItemLog} notInInventory
|
|
139
|
-
* If the object is not in the inventory, an exception is raised
|
|
140
|
-
* ```
|
|
141
|
-
* {
|
|
142
|
-
* id: ITEM_NOT_INVENTORY,
|
|
143
|
-
* msg: '...'
|
|
144
|
-
* }
|
|
145
|
-
* ```
|
|
146
|
-
* @memberof ItemManager
|
|
147
|
-
* @example
|
|
148
|
-
*
|
|
149
|
-
* ```ts
|
|
150
|
-
* import Potion from 'your-database/potion'
|
|
151
|
-
*
|
|
152
|
-
* try {
|
|
153
|
-
* player.removeItem(Potion, 5)
|
|
154
|
-
* }
|
|
155
|
-
* catch (err) {
|
|
156
|
-
* console.log(err)
|
|
157
|
-
* }
|
|
158
|
-
* ```
|
|
159
|
-
*/
|
|
160
439
|
removeItem(
|
|
161
440
|
itemClass: ItemClass | string,
|
|
162
441
|
nb: number = 1
|
|
@@ -172,52 +451,35 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
172
451
|
} else {
|
|
173
452
|
this.items()[itemIndex].quantity.update((it) => it - nb);
|
|
174
453
|
}
|
|
175
|
-
|
|
454
|
+
// Call onRemove hook - use stored instance if available
|
|
455
|
+
const hookTarget = (item as any)._itemInstance || item;
|
|
456
|
+
if (hookTarget && typeof hookTarget.onRemove === 'function') {
|
|
457
|
+
this["execMethod"]("onRemove", [this], hookTarget);
|
|
458
|
+
}
|
|
176
459
|
return this.items()[itemIndex];
|
|
177
460
|
}
|
|
178
461
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
* {
|
|
201
|
-
* id: NOT_ENOUGH_GOLD,
|
|
202
|
-
* msg: '...'
|
|
203
|
-
* }
|
|
204
|
-
* ```
|
|
205
|
-
* @memberof ItemManager
|
|
206
|
-
* @example
|
|
207
|
-
*
|
|
208
|
-
* ```ts
|
|
209
|
-
* import Potion from 'your-database/potion'
|
|
210
|
-
*
|
|
211
|
-
* try {
|
|
212
|
-
* player.buyItem(Potion)
|
|
213
|
-
* }
|
|
214
|
-
* catch (err) {
|
|
215
|
-
* console.log(err)
|
|
216
|
-
* }
|
|
217
|
-
* ```
|
|
218
|
-
*/
|
|
219
|
-
buyItem(itemId: string, nb = 1): Item {
|
|
220
|
-
const data = (this as any).databaseById(itemId);
|
|
462
|
+
buyItem(item: ItemClass | ItemObject | string, nb = 1): Item {
|
|
463
|
+
let itemId: string;
|
|
464
|
+
let data: any;
|
|
465
|
+
|
|
466
|
+
if (isString(item)) {
|
|
467
|
+
itemId = item as string;
|
|
468
|
+
data = (this as any).databaseById(itemId);
|
|
469
|
+
} else if (typeof item === 'function' || (item as any).prototype) {
|
|
470
|
+
itemId = (item as any).name;
|
|
471
|
+
data = (this as any).databaseById(itemId);
|
|
472
|
+
} else {
|
|
473
|
+
const itemObj = item as ItemObject;
|
|
474
|
+
itemId = itemObj.id || `item-${Date.now()}`;
|
|
475
|
+
try {
|
|
476
|
+
const dbData = (this as any).databaseById(itemId);
|
|
477
|
+
data = { ...dbData, ...itemObj };
|
|
478
|
+
} catch {
|
|
479
|
+
data = itemObj;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
221
483
|
if (!data.price) {
|
|
222
484
|
throw ItemLog.haveNotPrice(itemId);
|
|
223
485
|
}
|
|
@@ -226,61 +488,13 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
226
488
|
throw ItemLog.notEnoughGold(itemId, nb);
|
|
227
489
|
}
|
|
228
490
|
this._gold.update((gold) => gold - totalPrice);
|
|
229
|
-
return this.addItem(
|
|
491
|
+
return this.addItem(item, nb);
|
|
230
492
|
}
|
|
231
493
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
*
|
|
235
|
-
* `onRemove()` method is called on the ItemClass
|
|
236
|
-
*
|
|
237
|
-
* @title Sell Item
|
|
238
|
-
* @method player.sellItem(item,nb=1)
|
|
239
|
-
* @param {ItemClass | string} itemClass string is item id
|
|
240
|
-
* @param {number} [nbToSell] Default 1
|
|
241
|
-
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
242
|
-
* @throws {ItemLog} haveNotPrice
|
|
243
|
-
* If you have not set a price on the item
|
|
244
|
-
* ```
|
|
245
|
-
* {
|
|
246
|
-
* id: NOT_PRICE,
|
|
247
|
-
* msg: '...'
|
|
248
|
-
* }
|
|
249
|
-
* ```
|
|
250
|
-
* @throws {ItemLog} notInInventory
|
|
251
|
-
* If the object is not in the inventory, an exception is raised
|
|
252
|
-
* ```
|
|
253
|
-
* {
|
|
254
|
-
* id: ITEM_NOT_INVENTORY,
|
|
255
|
-
* msg: '...'
|
|
256
|
-
* }
|
|
257
|
-
* ```
|
|
258
|
-
* @throws {ItemLog} tooManyToSell
|
|
259
|
-
* If the number of items for sale exceeds the number of actual items in the inventory
|
|
260
|
-
* ```
|
|
261
|
-
* {
|
|
262
|
-
* id: TOO_MANY_ITEM_TO_SELL,
|
|
263
|
-
* msg: '...'
|
|
264
|
-
* }
|
|
265
|
-
* ```
|
|
266
|
-
* @memberof ItemManager
|
|
267
|
-
* @example
|
|
268
|
-
*
|
|
269
|
-
* ```ts
|
|
270
|
-
* import Potion from 'your-database/potion'
|
|
271
|
-
*
|
|
272
|
-
* try {
|
|
273
|
-
* player.addItem(Potion)
|
|
274
|
-
* player.sellItem(Potion)
|
|
275
|
-
* }
|
|
276
|
-
* catch (err) {
|
|
277
|
-
* console.log(err)
|
|
278
|
-
* }
|
|
279
|
-
* ```
|
|
280
|
-
*/
|
|
281
|
-
sellItem(itemId: string, nbToSell = 1): Item {
|
|
494
|
+
sellItem(itemClass: ItemClass | string, nbToSell = 1): Item {
|
|
495
|
+
const itemId = isString(itemClass) ? itemClass : (itemClass as any).name;
|
|
282
496
|
const data = (this as any).databaseById(itemId);
|
|
283
|
-
const inventory = this.getItem(
|
|
497
|
+
const inventory = this.getItem(itemClass);
|
|
284
498
|
if (!inventory) {
|
|
285
499
|
throw ItemLog.notInInventory(itemId);
|
|
286
500
|
}
|
|
@@ -292,14 +506,20 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
292
506
|
throw ItemLog.haveNotPrice(itemId);
|
|
293
507
|
}
|
|
294
508
|
this._gold.update((gold) => gold + (data.price / 2) * nbToSell);
|
|
295
|
-
this.removeItem(
|
|
509
|
+
this.removeItem(itemClass, nbToSell);
|
|
296
510
|
return inventory;
|
|
297
511
|
}
|
|
298
512
|
|
|
299
513
|
getParamItem(name: string): number {
|
|
300
514
|
let nb = 0;
|
|
301
515
|
for (let item of this.equipments()) {
|
|
302
|
-
|
|
516
|
+
// Retrieve item data from database to get properties like atk, pdef, sdef
|
|
517
|
+
try {
|
|
518
|
+
const itemData = (this as any).databaseById(item.id());
|
|
519
|
+
nb += itemData[name] || 0;
|
|
520
|
+
} catch {
|
|
521
|
+
// If item not in database, skip it
|
|
522
|
+
}
|
|
303
523
|
}
|
|
304
524
|
const modifier = (this as any).paramsModifier?.[name];
|
|
305
525
|
if (modifier) {
|
|
@@ -309,183 +529,71 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
309
529
|
return nb;
|
|
310
530
|
}
|
|
311
531
|
|
|
312
|
-
/**
|
|
313
|
-
* recover the attack sum of items equipped on the player.
|
|
314
|
-
*
|
|
315
|
-
* @title Get the player's attack
|
|
316
|
-
* @prop {number} player.atk
|
|
317
|
-
* @memberof ItemManager
|
|
318
|
-
*/
|
|
319
532
|
get atk(): number {
|
|
320
533
|
return this.getParamItem(ATK);
|
|
321
534
|
}
|
|
322
535
|
|
|
323
|
-
/**
|
|
324
|
-
* recover the physic defense sum of items equipped on the player.
|
|
325
|
-
*
|
|
326
|
-
* @title Get the player's pdef
|
|
327
|
-
* @prop {number} player.pdef
|
|
328
|
-
* @memberof ItemManager
|
|
329
|
-
*/
|
|
330
536
|
get pdef(): number {
|
|
331
537
|
return this.getParamItem(PDEF);
|
|
332
538
|
}
|
|
333
539
|
|
|
334
|
-
/**
|
|
335
|
-
* recover the skill defense sum of items equipped on the player.
|
|
336
|
-
*
|
|
337
|
-
* @title Get the player's sdef
|
|
338
|
-
* @prop {number} player.sdef
|
|
339
|
-
* @memberof ItemManager
|
|
340
|
-
*/
|
|
341
540
|
get sdef(): number {
|
|
342
541
|
return this.getParamItem(SDEF);
|
|
343
542
|
}
|
|
344
543
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
* `onUse()` method is called on the ItemClass (If the use has worked)
|
|
349
|
-
* `onRemove()` method is called on the ItemClass
|
|
350
|
-
*
|
|
351
|
-
* @title Use an Item
|
|
352
|
-
* @method player.useItem(item,nb=1)
|
|
353
|
-
* @param {ItemClass | string} itemClass string is item id
|
|
354
|
-
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
355
|
-
* @throws {ItemLog} restriction
|
|
356
|
-
* If the player has the `Effect.CAN_NOT_ITEM` effect
|
|
357
|
-
* ```
|
|
358
|
-
* {
|
|
359
|
-
* id: RESTRICTION_ITEM,
|
|
360
|
-
* msg: '...'
|
|
361
|
-
* }
|
|
362
|
-
* ```
|
|
363
|
-
* @throws {ItemLog} notInInventory
|
|
364
|
-
* If the object is not in the inventory, an exception is raised
|
|
365
|
-
* ```
|
|
366
|
-
* {
|
|
367
|
-
* id: ITEM_NOT_INVENTORY,
|
|
368
|
-
* msg: '...'
|
|
369
|
-
* }
|
|
370
|
-
* ```
|
|
371
|
-
* @throws {ItemLog} notUseItem
|
|
372
|
-
* If the `consumable` property is on false
|
|
373
|
-
* ```
|
|
374
|
-
* {
|
|
375
|
-
* id: NOT_USE_ITEM,
|
|
376
|
-
* msg: '...'
|
|
377
|
-
* }
|
|
378
|
-
* ```
|
|
379
|
-
* @throws {ItemLog} chanceToUseFailed
|
|
380
|
-
* Chance to use the item has failed. Chances of use is defined with `ItemClass.hitRate`
|
|
381
|
-
* ```
|
|
382
|
-
* {
|
|
383
|
-
* id: USE_CHANCE_ITEM_FAILED,
|
|
384
|
-
* msg: '...'
|
|
385
|
-
* }
|
|
386
|
-
* ```
|
|
387
|
-
* > the item is still deleted from the inventory
|
|
388
|
-
*
|
|
389
|
-
* `onUseFailed()` method is called on the ItemClass
|
|
390
|
-
*
|
|
391
|
-
* @memberof ItemManager
|
|
392
|
-
* @example
|
|
393
|
-
*
|
|
394
|
-
* ```ts
|
|
395
|
-
* import Potion from 'your-database/potion'
|
|
396
|
-
*
|
|
397
|
-
* try {
|
|
398
|
-
* player.addItem(Potion)
|
|
399
|
-
* player.useItem(Potion)
|
|
400
|
-
* }
|
|
401
|
-
* catch (err) {
|
|
402
|
-
* console.log(err)
|
|
403
|
-
* }
|
|
404
|
-
* ```
|
|
405
|
-
*/
|
|
406
|
-
useItem(itemId: string): Item {
|
|
407
|
-
const inventory = this.getItem(itemId);
|
|
544
|
+
useItem(itemClass: ItemClass | string): Item {
|
|
545
|
+
const itemId = isString(itemClass) ? itemClass : (itemClass as any).name;
|
|
546
|
+
const inventory = this.getItem(itemClass);
|
|
408
547
|
if ((this as any).hasEffect?.(Effect.CAN_NOT_ITEM)) {
|
|
409
548
|
throw ItemLog.restriction(itemId);
|
|
410
549
|
}
|
|
411
550
|
if (!inventory) {
|
|
412
551
|
throw ItemLog.notInInventory(itemId);
|
|
413
552
|
}
|
|
414
|
-
|
|
415
|
-
|
|
553
|
+
|
|
554
|
+
// Retrieve item data from database to check consumable and hitRate
|
|
555
|
+
const itemData = (this as any).databaseById(itemId);
|
|
556
|
+
const consumable = itemData?.consumable;
|
|
557
|
+
|
|
558
|
+
// If consumable is explicitly false, throw error
|
|
559
|
+
if (consumable === false) {
|
|
560
|
+
throw ItemLog.notUseItem(itemId);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// If consumable is undefined and item is not of type 'item', it's not consumable
|
|
564
|
+
if (consumable === undefined && itemData?._type && itemData._type !== 'item') {
|
|
416
565
|
throw ItemLog.notUseItem(itemId);
|
|
417
566
|
}
|
|
418
|
-
|
|
567
|
+
|
|
568
|
+
const hitRate = itemData?.hitRate ?? 1;
|
|
569
|
+
const hookTarget = (inventory as any)._itemInstance || inventory;
|
|
570
|
+
|
|
419
571
|
if (Math.random() > hitRate) {
|
|
420
|
-
this.removeItem(
|
|
421
|
-
this["execMethod"]("onUseFailed", [this],
|
|
572
|
+
this.removeItem(itemClass);
|
|
573
|
+
this["execMethod"]("onUseFailed", [this], hookTarget);
|
|
422
574
|
throw ItemLog.chanceToUseFailed(itemId);
|
|
423
575
|
}
|
|
424
|
-
(this as any).applyEffect?.(
|
|
425
|
-
(this as any).applyStates?.(this,
|
|
426
|
-
this["execMethod"]("onUse", [this],
|
|
427
|
-
this.removeItem(
|
|
576
|
+
(this as any).applyEffect?.(itemData);
|
|
577
|
+
(this as any).applyStates?.(this, itemData);
|
|
578
|
+
this["execMethod"]("onUse", [this], hookTarget);
|
|
579
|
+
this.removeItem(itemClass);
|
|
428
580
|
return inventory;
|
|
429
581
|
}
|
|
430
582
|
|
|
431
|
-
/**
|
|
432
|
-
* Equips a weapon or armor on a player. Think first to add the item in the inventory with the `addItem()` method before equipping the item.
|
|
433
|
-
*
|
|
434
|
-
* `onEquip()` method is called on the ItemClass
|
|
435
|
-
*
|
|
436
|
-
* @title Equip Weapon or Armor
|
|
437
|
-
* @method player.equip(itemClass,equip=true)
|
|
438
|
-
* @param {ItemClass | string} itemClass string is item id
|
|
439
|
-
* @param {number} [equip] Equip the object if true or un-equipped if false
|
|
440
|
-
* @returns {void}
|
|
441
|
-
* @throws {ItemLog} notInInventory
|
|
442
|
-
* If the item is not in the inventory
|
|
443
|
-
* ```
|
|
444
|
-
{
|
|
445
|
-
id: ITEM_NOT_INVENTORY,
|
|
446
|
-
msg: '...'
|
|
447
|
-
}
|
|
448
|
-
```
|
|
449
|
-
* @throws {ItemLog} invalidToEquiped
|
|
450
|
-
If the item is not by a weapon or armor
|
|
451
|
-
```
|
|
452
|
-
{
|
|
453
|
-
id: INVALID_ITEM_TO_EQUIP,
|
|
454
|
-
msg: '...'
|
|
455
|
-
}
|
|
456
|
-
```
|
|
457
|
-
* @throws {ItemLog} isAlreadyEquiped
|
|
458
|
-
If the item Is already equipped
|
|
459
|
-
```
|
|
460
|
-
{
|
|
461
|
-
id: ITEM_ALREADY_EQUIPED,
|
|
462
|
-
msg: '...'
|
|
463
|
-
}
|
|
464
|
-
```
|
|
465
|
-
* @memberof ItemManager
|
|
466
|
-
* @example
|
|
467
|
-
*
|
|
468
|
-
* ```ts
|
|
469
|
-
* import Sword from 'your-database/sword'
|
|
470
|
-
*
|
|
471
|
-
* try {
|
|
472
|
-
* player.addItem(Sword)
|
|
473
|
-
* player.equip(Sword)
|
|
474
|
-
* }
|
|
475
|
-
* catch (err) {
|
|
476
|
-
* console.log(err)
|
|
477
|
-
* }
|
|
478
|
-
* ```
|
|
479
|
-
*/
|
|
480
583
|
equip(
|
|
481
584
|
itemId: string,
|
|
482
|
-
equip: boolean = true
|
|
585
|
+
equip: boolean | 'auto' = true
|
|
483
586
|
): void {
|
|
484
|
-
const
|
|
587
|
+
const autoAdd = equip === 'auto';
|
|
588
|
+
const equipState = equip === 'auto' ? true : equip;
|
|
589
|
+
const data = (this as any).databaseById(itemId);
|
|
590
|
+
let inventory: Item = this.getItem(itemId);
|
|
591
|
+
if (!inventory && autoAdd) {
|
|
592
|
+
inventory = this.addItem(itemId, 1);
|
|
593
|
+
}
|
|
485
594
|
if (!inventory) {
|
|
486
595
|
throw ItemLog.notInInventory(itemId);
|
|
487
596
|
}
|
|
488
|
-
const data = (this as any).databaseById(itemId);
|
|
489
597
|
if (data._type == "item") {
|
|
490
598
|
throw ItemLog.invalidToEquiped(itemId);
|
|
491
599
|
}
|
|
@@ -503,23 +611,321 @@ export function WithItemManager<TBase extends PlayerCtor>(Base: TBase) {
|
|
|
503
611
|
|
|
504
612
|
const item = inventory;
|
|
505
613
|
|
|
506
|
-
if ((item as any).equipped &&
|
|
614
|
+
if ((item as any).equipped && equipState) {
|
|
507
615
|
throw ItemLog.isAlreadyEquiped(itemId);
|
|
508
616
|
}
|
|
509
|
-
(item as any).equipped =
|
|
510
|
-
if (!
|
|
617
|
+
(item as any).equipped = equipState;
|
|
618
|
+
if (!equipState) {
|
|
511
619
|
const index = this.equipments().findIndex((it) => it.id() == item.id());
|
|
512
620
|
this.equipments().splice(index, 1);
|
|
513
621
|
} else {
|
|
514
622
|
this.equipments().push(item);
|
|
515
623
|
}
|
|
516
|
-
|
|
624
|
+
// Call onEquip hook - use stored instance if available
|
|
625
|
+
const hookTarget = (item as any)._itemInstance || item;
|
|
626
|
+
this["execMethod"]("onEquip", [this, equipState], hookTarget);
|
|
517
627
|
}
|
|
518
628
|
} as unknown as TBase;
|
|
519
629
|
}
|
|
520
630
|
|
|
521
631
|
/**
|
|
522
|
-
*
|
|
523
|
-
*
|
|
632
|
+
* Interface for Item Manager functionality
|
|
633
|
+
*
|
|
634
|
+
* Provides comprehensive item management capabilities including inventory management,
|
|
635
|
+
* item usage, equipment, buying/selling, and item effects. This interface defines
|
|
636
|
+
* the public API of the ItemManager mixin.
|
|
524
637
|
*/
|
|
525
|
-
export
|
|
638
|
+
export interface IItemManager {
|
|
639
|
+
/**
|
|
640
|
+
* Retrieves the information of an object: the number and the instance
|
|
641
|
+
*
|
|
642
|
+
* The returned Item instance contains the quantity information accessible via `quantity()` method.
|
|
643
|
+
*
|
|
644
|
+
* @param itemClass - Item class or string identifier. If string, it's the item ID
|
|
645
|
+
* @returns Item instance containing quantity and item data
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```ts
|
|
649
|
+
* import Potion from 'your-database/potion'
|
|
650
|
+
*
|
|
651
|
+
* player.addItem(Potion, 5)
|
|
652
|
+
* const inventory = player.getItem(Potion)
|
|
653
|
+
* console.log(inventory.quantity()) // 5
|
|
654
|
+
* console.log(inventory) // <instance of Item>
|
|
655
|
+
* ```
|
|
656
|
+
*/
|
|
657
|
+
getItem(itemClass: ItemClass | string): Item;
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Check if the player has the item in his inventory
|
|
661
|
+
*
|
|
662
|
+
* @param itemClass - Item class or string identifier. If string, it's the item ID
|
|
663
|
+
* @returns `true` if player has the item, `false` otherwise
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```ts
|
|
667
|
+
* import Potion from 'your-database/potion'
|
|
668
|
+
*
|
|
669
|
+
* player.hasItem(Potion) // false
|
|
670
|
+
* player.addItem(Potion, 1)
|
|
671
|
+
* player.hasItem(Potion) // true
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
hasItem(itemClass: ItemClass | string): boolean;
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* Add an item in the player's inventory
|
|
678
|
+
*
|
|
679
|
+
* You can add items using:
|
|
680
|
+
* - Item class (automatically registered in database if needed)
|
|
681
|
+
* - Item object (automatically registered in database if needed)
|
|
682
|
+
* - String ID (must be pre-registered in database)
|
|
683
|
+
*
|
|
684
|
+
* The `onAdd()` method is called on the ItemClass or ItemObject when the item is added.
|
|
685
|
+
*
|
|
686
|
+
* @param item - Item class, object, or string identifier
|
|
687
|
+
* @param nb - Number of items to add (default: 1)
|
|
688
|
+
* @returns The item instance added to inventory
|
|
689
|
+
*
|
|
690
|
+
* @example
|
|
691
|
+
* ```ts
|
|
692
|
+
* import Potion from 'your-database/potion'
|
|
693
|
+
*
|
|
694
|
+
* // Add using class
|
|
695
|
+
* player.addItem(Potion, 5)
|
|
696
|
+
*
|
|
697
|
+
* // Add using string ID (must be registered in database)
|
|
698
|
+
* player.addItem('Potion', 3)
|
|
699
|
+
*
|
|
700
|
+
* // Add using object
|
|
701
|
+
* player.addItem({
|
|
702
|
+
* id: 'custom-potion',
|
|
703
|
+
* name: 'Custom Potion',
|
|
704
|
+
* price: 200,
|
|
705
|
+
* onAdd(player) {
|
|
706
|
+
* console.log('Custom potion added!')
|
|
707
|
+
* }
|
|
708
|
+
* }, 2)
|
|
709
|
+
* ```
|
|
710
|
+
*/
|
|
711
|
+
addItem(item: ItemClass | ItemObject | string, nb?: number): Item;
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Deletes an item from inventory
|
|
715
|
+
*
|
|
716
|
+
* Decreases the quantity by `nb`. If the quantity falls to 0 or below, the item is removed from the inventory.
|
|
717
|
+
* The method returns `undefined` if the item is completely removed.
|
|
718
|
+
*
|
|
719
|
+
* The `onRemove()` method is called on the ItemClass when the item is removed.
|
|
720
|
+
*
|
|
721
|
+
* @param itemClass - Item class or string identifier. If string, it's the item ID
|
|
722
|
+
* @param nb - Number of items to remove (default: 1)
|
|
723
|
+
* @returns Item instance or `undefined` if the item was completely removed
|
|
724
|
+
* @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
|
|
725
|
+
* - `id`: `ITEM_NOT_INVENTORY`
|
|
726
|
+
* - `msg`: Error message
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* import Potion from 'your-database/potion'
|
|
731
|
+
*
|
|
732
|
+
* try {
|
|
733
|
+
* player.removeItem(Potion, 5)
|
|
734
|
+
* } catch (err) {
|
|
735
|
+
* console.log(err) // { id: 'ITEM_NOT_INVENTORY', msg: '...' }
|
|
736
|
+
* }
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
removeItem(itemClass: ItemClass | string, nb?: number): Item | undefined;
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Purchases an item and reduces the amount of gold
|
|
743
|
+
*
|
|
744
|
+
* The player's gold is reduced by `nb * item.price`. The item is then added to the inventory.
|
|
745
|
+
* The `onAdd()` method is called on the ItemClass when the item is added.
|
|
746
|
+
*
|
|
747
|
+
* @param item - Item class, object, or string identifier
|
|
748
|
+
* @param nb - Number of items to buy (default: 1)
|
|
749
|
+
* @returns Item instance added to inventory
|
|
750
|
+
* @throws {Object} ItemLog.haveNotPrice - If the item has no price set
|
|
751
|
+
* - `id`: `NOT_PRICE`
|
|
752
|
+
* - `msg`: Error message
|
|
753
|
+
* @throws {Object} ItemLog.notEnoughGold - If the player doesn't have enough gold
|
|
754
|
+
* - `id`: `NOT_ENOUGH_GOLD`
|
|
755
|
+
* - `msg`: Error message
|
|
756
|
+
*
|
|
757
|
+
* @example
|
|
758
|
+
* ```ts
|
|
759
|
+
* import Potion from 'your-database/potion'
|
|
760
|
+
*
|
|
761
|
+
* try {
|
|
762
|
+
* player.buyItem(Potion)
|
|
763
|
+
* } catch (err) {
|
|
764
|
+
* if (err.id === 'NOT_ENOUGH_GOLD') {
|
|
765
|
+
* console.log('Not enough gold!')
|
|
766
|
+
* } else if (err.id === 'NOT_PRICE') {
|
|
767
|
+
* console.log('Item has no price!')
|
|
768
|
+
* }
|
|
769
|
+
* }
|
|
770
|
+
* ```
|
|
771
|
+
*/
|
|
772
|
+
buyItem(item: ItemClass | ItemObject | string, nb?: number): Item;
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* Sell an item and the player wins the amount of the item divided by 2
|
|
776
|
+
*
|
|
777
|
+
* The player receives `(item.price / 2) * nbToSell` gold. The item is removed from the inventory.
|
|
778
|
+
* The `onRemove()` method is called on the ItemClass when the item is removed.
|
|
779
|
+
*
|
|
780
|
+
* @param itemClass - Item class or string identifier. If string, it's the item ID
|
|
781
|
+
* @param nbToSell - Number of items to sell (default: 1)
|
|
782
|
+
* @returns Item instance that was sold
|
|
783
|
+
* @throws {Object} ItemLog.haveNotPrice - If the item has no price set
|
|
784
|
+
* - `id`: `NOT_PRICE`
|
|
785
|
+
* - `msg`: Error message
|
|
786
|
+
* @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
|
|
787
|
+
* - `id`: `ITEM_NOT_INVENTORY`
|
|
788
|
+
* - `msg`: Error message
|
|
789
|
+
* @throws {Object} ItemLog.tooManyToSell - If trying to sell more items than available
|
|
790
|
+
* - `id`: `TOO_MANY_ITEM_TO_SELL`
|
|
791
|
+
* - `msg`: Error message
|
|
792
|
+
*
|
|
793
|
+
* @example
|
|
794
|
+
* ```ts
|
|
795
|
+
* import Potion from 'your-database/potion'
|
|
796
|
+
*
|
|
797
|
+
* try {
|
|
798
|
+
* player.addItem(Potion)
|
|
799
|
+
* player.sellItem(Potion)
|
|
800
|
+
* } catch (err) {
|
|
801
|
+
* console.log(err)
|
|
802
|
+
* }
|
|
803
|
+
* ```
|
|
804
|
+
*/
|
|
805
|
+
sellItem(itemClass: ItemClass | string, nbToSell?: number): Item;
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Use an object. Applies effects and states. Removes the object from the inventory
|
|
809
|
+
*
|
|
810
|
+
* When an item is used:
|
|
811
|
+
* - Effects are applied to the player (HP/MP restoration, etc.)
|
|
812
|
+
* - States are applied/removed as defined in the item
|
|
813
|
+
* - The item is removed from inventory (consumed)
|
|
814
|
+
*
|
|
815
|
+
* If the item has a `hitRate` property (0-1), there's a chance the usage might fail.
|
|
816
|
+
* If usage fails, the item is still removed and `onUseFailed()` is called instead of `onUse()`.
|
|
817
|
+
*
|
|
818
|
+
* The `onUse()` method is called on the ItemClass if the use was successful.
|
|
819
|
+
* The `onUseFailed()` method is called on the ItemClass if the chance roll failed.
|
|
820
|
+
* The `onRemove()` method is called on the ItemClass when the item is removed.
|
|
821
|
+
*
|
|
822
|
+
* @param itemClass - Item class or string identifier. If string, it's the item ID
|
|
823
|
+
* @returns Item instance that was used
|
|
824
|
+
* @throws {Object} ItemLog.restriction - If the player has the `Effect.CAN_NOT_ITEM` effect
|
|
825
|
+
* - `id`: `RESTRICTION_ITEM`
|
|
826
|
+
* - `msg`: Error message
|
|
827
|
+
* @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
|
|
828
|
+
* - `id`: `ITEM_NOT_INVENTORY`
|
|
829
|
+
* - `msg`: Error message
|
|
830
|
+
* @throws {Object} ItemLog.notUseItem - If the item's `consumable` property is `false`
|
|
831
|
+
* - `id`: `NOT_USE_ITEM`
|
|
832
|
+
* - `msg`: Error message
|
|
833
|
+
* @throws {Object} ItemLog.chanceToUseFailed - If the chance to use the item failed (hitRate roll failed)
|
|
834
|
+
* - `id`: `USE_CHANCE_ITEM_FAILED`
|
|
835
|
+
* - `msg`: Error message
|
|
836
|
+
* - Note: The item is still deleted from the inventory even if usage failed
|
|
837
|
+
*
|
|
838
|
+
* @example
|
|
839
|
+
* ```ts
|
|
840
|
+
* import Potion from 'your-database/potion'
|
|
841
|
+
*
|
|
842
|
+
* try {
|
|
843
|
+
* player.addItem(Potion)
|
|
844
|
+
* player.useItem(Potion)
|
|
845
|
+
* } catch (err) {
|
|
846
|
+
* if (err.id === 'USE_CHANCE_ITEM_FAILED') {
|
|
847
|
+
* console.log('Item usage failed due to chance roll')
|
|
848
|
+
* } else {
|
|
849
|
+
* console.log(err)
|
|
850
|
+
* }
|
|
851
|
+
* }
|
|
852
|
+
* ```
|
|
853
|
+
*/
|
|
854
|
+
useItem(itemClass: ItemClass | string): Item;
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* Equips a weapon or armor on a player
|
|
858
|
+
*
|
|
859
|
+
* Think first to add the item in the inventory with the `addItem()` method before equipping the item,
|
|
860
|
+
* or pass `"auto"` to add the item if it is missing and equip it.
|
|
861
|
+
*
|
|
862
|
+
* The `onEquip()` method is called on the ItemClass when the item is equipped or unequipped.
|
|
863
|
+
*
|
|
864
|
+
* @param itemId - Item identifier to resolve from the database
|
|
865
|
+
* @param equip - Equip the item if `true`, unequip if `false`, or `"auto"` to add then equip (default: `true`)
|
|
866
|
+
* @throws {Object} ItemLog.notInInventory - If the item is not in the inventory
|
|
867
|
+
* - `id`: `ITEM_NOT_INVENTORY`
|
|
868
|
+
* - `msg`: Error message
|
|
869
|
+
* @throws {Object} ItemLog.invalidToEquiped - If the item is not a weapon or armor (item._type is "item")
|
|
870
|
+
* - `id`: `INVALID_ITEM_TO_EQUIP`
|
|
871
|
+
* - `msg`: Error message
|
|
872
|
+
* @throws {Object} ItemLog.isAlreadyEquiped - If the item is already equipped
|
|
873
|
+
* - `id`: `ITEM_ALREADY_EQUIPED`
|
|
874
|
+
* - `msg`: Error message
|
|
875
|
+
*
|
|
876
|
+
* @example
|
|
877
|
+
* ```ts
|
|
878
|
+
* try {
|
|
879
|
+
* player.addItem('sword')
|
|
880
|
+
* player.equip('sword')
|
|
881
|
+
* // Later, unequip it
|
|
882
|
+
* player.equip('sword', false)
|
|
883
|
+
* } catch (err) {
|
|
884
|
+
* console.log(err)
|
|
885
|
+
* }
|
|
886
|
+
* ```
|
|
887
|
+
*/
|
|
888
|
+
equip(itemId: string, equip?: boolean | 'auto'): void;
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Get the player's attack (sum of items equipped)
|
|
892
|
+
*
|
|
893
|
+
* Returns the total attack value from all equipped items on the player.
|
|
894
|
+
*
|
|
895
|
+
* @returns Total attack value from equipped items
|
|
896
|
+
*
|
|
897
|
+
* @example
|
|
898
|
+
* ```ts
|
|
899
|
+
* console.log(player.atk) // 150 (sum of all equipped weapons/armors attack)
|
|
900
|
+
* ```
|
|
901
|
+
*/
|
|
902
|
+
readonly atk: number;
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* Get the player's physical defense (sum of items equipped)
|
|
906
|
+
*
|
|
907
|
+
* Returns the total physical defense value from all equipped items on the player.
|
|
908
|
+
*
|
|
909
|
+
* @returns Total physical defense value from equipped items
|
|
910
|
+
*
|
|
911
|
+
* @example
|
|
912
|
+
* ```ts
|
|
913
|
+
* console.log(player.pdef) // 80 (sum of all equipped armors physical defense)
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
readonly pdef: number;
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Get the player's skill defense (sum of items equipped)
|
|
920
|
+
*
|
|
921
|
+
* Returns the total skill defense value from all equipped items on the player.
|
|
922
|
+
*
|
|
923
|
+
* @returns Total skill defense value from equipped items
|
|
924
|
+
*
|
|
925
|
+
* @example
|
|
926
|
+
* ```ts
|
|
927
|
+
* console.log(player.sdef) // 60 (sum of all equipped armors skill defense)
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
readonly sdef: number;
|
|
931
|
+
}
|