@rpgjs/server 4.3.0 → 5.0.0-alpha.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/{lib → dist}/Gui/DialogGui.d.ts +1 -2
- package/{lib → dist}/Gui/Gui.d.ts +1 -2
- package/dist/Player/BattleManager.d.ts +22 -0
- package/dist/Player/ClassManager.d.ts +18 -0
- package/dist/Player/Event.d.ts +0 -0
- package/dist/Player/ItemManager.d.ts +17 -0
- package/dist/Player/MoveManager.d.ts +177 -0
- package/dist/Player/ParameterManager.d.ts +42 -0
- package/dist/Player/Player.d.ts +73 -0
- package/dist/Player/SkillManager.d.ts +23 -0
- package/dist/Player/StateManager.d.ts +39 -0
- package/{lib → dist}/RpgServer.d.ts +32 -15
- package/dist/RpgServerEngine.d.ts +5 -0
- package/dist/core/context.d.ts +2 -0
- package/dist/core/inject.d.ts +5 -0
- package/dist/core/setup.d.ts +6 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +29673 -0
- package/dist/index.js.map +1 -0
- package/dist/module.d.ts +2 -0
- package/dist/rooms/lobby.d.ts +6 -0
- package/dist/rooms/map.d.ts +110 -0
- package/package.json +15 -37
- package/src/Gui/DialogGui.ts +3 -4
- package/src/Gui/Gui.ts +4 -6
- package/src/Player/BattleManager.ts +108 -99
- package/src/Player/ClassManager.ts +47 -46
- package/src/Player/ComponentManager.ts +26 -535
- package/src/Player/EffectManager.ts +44 -59
- package/src/Player/ElementManager.ts +70 -72
- package/src/Player/Event.ts +0 -0
- package/src/Player/GoldManager.ts +24 -6
- package/src/Player/GuiManager.ts +106 -97
- package/src/Player/ItemFixture.ts +22 -17
- package/src/Player/ItemManager.ts +234 -225
- package/src/Player/MoveManager.ts +1047 -457
- package/src/Player/ParameterManager.ts +59 -37
- package/src/Player/Player.ts +272 -1058
- package/src/Player/SkillManager.ts +146 -137
- package/src/Player/StateManager.ts +124 -98
- package/src/Player/VariableManager.ts +23 -3
- package/src/RpgServer.ts +36 -12
- package/src/RpgServerEngine.ts +7 -0
- package/src/core/context.ts +3 -0
- package/src/core/inject.ts +17 -0
- package/src/core/setup.ts +20 -0
- package/src/index.ts +9 -31
- package/src/module.ts +32 -0
- package/src/presets/index.ts +1 -3
- package/src/rooms/lobby.ts +24 -0
- package/src/rooms/map.ts +311 -0
- package/tsconfig.json +26 -23
- package/vite.config.ts +21 -0
- package/CHANGELOG.md +0 -174
- package/LICENSE +0 -19
- package/browser/manifest.json +0 -7
- package/browser/rpg.server.js +0 -22379
- package/browser/rpg.server.umd.cjs +0 -22410
- package/lib/Game/EventManager.d.ts +0 -54
- package/lib/Game/EventManager.js +0 -107
- package/lib/Game/EventManager.js.map +0 -1
- package/lib/Game/Map.d.ts +0 -191
- package/lib/Game/Map.js +0 -419
- package/lib/Game/Map.js.map +0 -1
- package/lib/Game/WorldMaps.d.ts +0 -17
- package/lib/Game/WorldMaps.js +0 -28
- package/lib/Game/WorldMaps.js.map +0 -1
- package/lib/Gui/DialogGui.js +0 -57
- package/lib/Gui/DialogGui.js.map +0 -1
- package/lib/Gui/Gui.js +0 -37
- package/lib/Gui/Gui.js.map +0 -1
- package/lib/Gui/MenuGui.js +0 -23
- package/lib/Gui/MenuGui.js.map +0 -1
- package/lib/Gui/NotificationGui.js +0 -8
- package/lib/Gui/NotificationGui.js.map +0 -1
- package/lib/Gui/ShopGui.js +0 -40
- package/lib/Gui/ShopGui.js.map +0 -1
- package/lib/Gui/index.js +0 -7
- package/lib/Gui/index.js.map +0 -1
- package/lib/Interfaces/Gui.d.ts +0 -4
- package/lib/Interfaces/Gui.js +0 -2
- package/lib/Interfaces/Gui.js.map +0 -1
- package/lib/Interfaces/StateStore.d.ts +0 -5
- package/lib/Interfaces/StateStore.js +0 -2
- package/lib/Interfaces/StateStore.js.map +0 -1
- package/lib/MatchMaker.d.ts +0 -24
- package/lib/MatchMaker.js +0 -42
- package/lib/MatchMaker.js.map +0 -1
- package/lib/Monitor/index.d.ts +0 -17
- package/lib/Monitor/index.js +0 -72
- package/lib/Monitor/index.js.map +0 -1
- package/lib/Player/BattleManager.d.ts +0 -36
- package/lib/Player/BattleManager.js +0 -101
- package/lib/Player/BattleManager.js.map +0 -1
- package/lib/Player/ClassManager.d.ts +0 -47
- package/lib/Player/ClassManager.js +0 -65
- package/lib/Player/ClassManager.js.map +0 -1
- package/lib/Player/ComponentManager.d.ts +0 -397
- package/lib/Player/ComponentManager.js +0 -505
- package/lib/Player/ComponentManager.js.map +0 -1
- package/lib/Player/EffectManager.d.ts +0 -46
- package/lib/Player/EffectManager.js +0 -75
- package/lib/Player/EffectManager.js.map +0 -1
- package/lib/Player/ElementManager.d.ts +0 -108
- package/lib/Player/ElementManager.js +0 -120
- package/lib/Player/ElementManager.js.map +0 -1
- package/lib/Player/GoldManager.d.ts +0 -17
- package/lib/Player/GoldManager.js +0 -27
- package/lib/Player/GoldManager.js.map +0 -1
- package/lib/Player/GuiManager.d.ts +0 -247
- package/lib/Player/GuiManager.js +0 -291
- package/lib/Player/GuiManager.js.map +0 -1
- package/lib/Player/ItemFixture.d.ts +0 -7
- package/lib/Player/ItemFixture.js +0 -19
- package/lib/Player/ItemFixture.js.map +0 -1
- package/lib/Player/ItemManager.d.ts +0 -331
- package/lib/Player/ItemManager.js +0 -475
- package/lib/Player/ItemManager.js.map +0 -1
- package/lib/Player/MoveManager.d.ts +0 -333
- package/lib/Player/MoveManager.js +0 -450
- package/lib/Player/MoveManager.js.map +0 -1
- package/lib/Player/ParameterManager.d.ts +0 -316
- package/lib/Player/ParameterManager.js +0 -408
- package/lib/Player/ParameterManager.js.map +0 -1
- package/lib/Player/Player.d.ts +0 -828
- package/lib/Player/Player.js +0 -927
- package/lib/Player/Player.js.map +0 -1
- package/lib/Player/SkillManager.d.ts +0 -163
- package/lib/Player/SkillManager.js +0 -223
- package/lib/Player/SkillManager.js.map +0 -1
- package/lib/Player/StateManager.d.ts +0 -185
- package/lib/Player/StateManager.js +0 -228
- package/lib/Player/StateManager.js.map +0 -1
- package/lib/Player/VariableManager.d.ts +0 -46
- package/lib/Player/VariableManager.js +0 -52
- package/lib/Player/VariableManager.js.map +0 -1
- package/lib/Query.d.ts +0 -127
- package/lib/Query.js +0 -159
- package/lib/Query.js.map +0 -1
- package/lib/RpgServer.js +0 -2
- package/lib/RpgServer.js.map +0 -1
- package/lib/Scenes/Map.d.ts +0 -136
- package/lib/Scenes/Map.js +0 -273
- package/lib/Scenes/Map.js.map +0 -1
- package/lib/decorators/event.d.ts +0 -43
- package/lib/decorators/event.js +0 -13
- package/lib/decorators/event.js.map +0 -1
- package/lib/decorators/map.d.ts +0 -178
- package/lib/decorators/map.js +0 -43
- package/lib/decorators/map.js.map +0 -1
- package/lib/entry-point.d.ts +0 -34
- package/lib/entry-point.js +0 -70
- package/lib/entry-point.js.map +0 -1
- package/lib/express/api.d.ts +0 -3
- package/lib/express/api.js +0 -105
- package/lib/express/api.js.map +0 -1
- package/lib/express/errors/NotAuthorized.d.ts +0 -4
- package/lib/express/errors/NotAuthorized.js +0 -7
- package/lib/express/errors/NotAuthorized.js.map +0 -1
- package/lib/express/errors/NotFound.d.ts +0 -4
- package/lib/express/errors/NotFound.js +0 -7
- package/lib/express/errors/NotFound.js.map +0 -1
- package/lib/express/server.d.ts +0 -18
- package/lib/express/server.js +0 -70
- package/lib/express/server.js.map +0 -1
- package/lib/index.d.ts +0 -20
- package/lib/index.js +0 -19
- package/lib/index.js.map +0 -1
- package/lib/inject.d.ts +0 -22
- package/lib/inject.js +0 -29
- package/lib/inject.js.map +0 -1
- package/lib/logs/index.js +0 -6
- package/lib/logs/index.js.map +0 -1
- package/lib/logs/item.js +0 -34
- package/lib/logs/item.js.map +0 -1
- package/lib/logs/log.js +0 -7
- package/lib/logs/log.js.map +0 -1
- package/lib/logs/skill.js +0 -19
- package/lib/logs/skill.js.map +0 -1
- package/lib/logs/state.js +0 -13
- package/lib/logs/state.js.map +0 -1
- package/lib/models/Item.d.ts +0 -10
- package/lib/models/Item.js +0 -2
- package/lib/models/Item.js.map +0 -1
- package/lib/presets/index.js +0 -65
- package/lib/presets/index.js.map +0 -1
- package/lib/server.d.ts +0 -137
- package/lib/server.js +0 -443
- package/lib/server.js.map +0 -1
- package/rpg.toml +0 -14
- package/src/Game/EventManager.ts +0 -125
- package/src/Game/Map.ts +0 -500
- package/src/Game/WorldMaps.ts +0 -45
- package/src/Interfaces/Gui.ts +0 -4
- package/src/Interfaces/StateStore.ts +0 -5
- package/src/MatchMaker.ts +0 -65
- package/src/Monitor/index.ts +0 -78
- package/src/Query.ts +0 -172
- package/src/Scenes/Map.ts +0 -310
- package/src/decorators/event.ts +0 -58
- package/src/decorators/map.ts +0 -226
- package/src/entry-point.ts +0 -111
- package/src/express/api.ts +0 -118
- package/src/express/errors/NotAuthorized.ts +0 -6
- package/src/express/errors/NotFound.ts +0 -6
- package/src/express/server.ts +0 -94
- package/src/inject.ts +0 -33
- package/src/models/Item.ts +0 -11
- package/src/server.ts +0 -459
- /package/{lib → dist}/Gui/MenuGui.d.ts +0 -0
- /package/{lib → dist}/Gui/NotificationGui.d.ts +0 -0
- /package/{lib → dist}/Gui/ShopGui.d.ts +0 -0
- /package/{lib → dist}/Gui/index.d.ts +0 -0
- /package/{lib → dist}/logs/index.d.ts +0 -0
- /package/{lib → dist}/logs/item.d.ts +0 -0
- /package/{lib → dist}/logs/log.d.ts +0 -0
- /package/{lib → dist}/logs/skill.d.ts +0 -0
- /package/{lib → dist}/logs/state.d.ts +0 -0
- /package/{lib → dist}/presets/index.d.ts +0 -0
|
@@ -1,50 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { StateManager } from './StateManager'
|
|
1
|
+
import { isInstanceOf, isString, Item, type Constructor } from "@rpgjs/common";
|
|
2
|
+
import { RpgCommonPlayer, Matter } from "@rpgjs/common";
|
|
3
|
+
import { ATK, PDEF, SDEF } from "../presets";
|
|
4
|
+
import { ItemLog } from "../logs";
|
|
5
|
+
import { ArmorInstance, ItemClass, ItemInstance, WeaponInstance } from "@rpgjs/database";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
} from '../presets'
|
|
13
|
-
|
|
14
|
-
const {
|
|
15
|
-
isString,
|
|
16
|
-
isInstanceOf,
|
|
17
|
-
applyMixins
|
|
18
|
-
} = Utils
|
|
7
|
+
// Ajout des enums manquants
|
|
8
|
+
enum Effect {
|
|
9
|
+
CAN_NOT_ITEM = 'CAN_NOT_ITEM'
|
|
10
|
+
}
|
|
19
11
|
|
|
12
|
+
enum ClassHooks {
|
|
13
|
+
canEquip = 'canEquip'
|
|
14
|
+
}
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Interface defining what MoveManager adds to a class
|
|
18
|
+
*/
|
|
19
|
+
export interface IItemManager {
|
|
20
|
+
databaseById(id: string): ItemClass;
|
|
21
|
+
}
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
type Inventory = { nb: number; item: ItemInstance };
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Move Manager mixin
|
|
27
|
+
*
|
|
28
|
+
* Adds methods to manage player movement
|
|
29
|
+
*
|
|
30
|
+
* @param Base - The base class to extend
|
|
31
|
+
* @returns A new class with move management capabilities
|
|
32
|
+
*/
|
|
33
|
+
export function WithItemManager<TBase extends Constructor<RpgCommonPlayer>>(
|
|
34
|
+
Base: TBase
|
|
35
|
+
): Constructor<IItemManager> & TBase {
|
|
36
|
+
return class extends Base implements IItemManager {
|
|
27
37
|
|
|
28
38
|
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
getItem(itemClass: ItemClass | string):
|
|
46
|
-
|
|
47
|
-
|
|
39
|
+
* Retrieves the information of an object: the number and the instance
|
|
40
|
+
* @title Get Item
|
|
41
|
+
* @method player.getItem(itemClass)
|
|
42
|
+
* @param {ItemClass | string} itemClass Identifier of the object if the parameter is a string
|
|
43
|
+
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
44
|
+
* @memberof ItemManager
|
|
45
|
+
* @example
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import Potion from 'your-database/potion'
|
|
49
|
+
*
|
|
50
|
+
* player.addItem(Potion, 5)
|
|
51
|
+
* const inventory = player.getItem(Potion)
|
|
52
|
+
* console.log(inventory) // { nb: 5, item: <instance of Potion> }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
getItem(itemClass: ItemClass | string): Item {
|
|
56
|
+
const index: number = this._getItemIndex(itemClass);
|
|
57
|
+
return this.items()[index];
|
|
48
58
|
}
|
|
49
59
|
|
|
50
60
|
/**
|
|
@@ -55,73 +65,70 @@ export class ItemManager {
|
|
|
55
65
|
* @returns {boolean}
|
|
56
66
|
* @memberof ItemManager
|
|
57
67
|
* @example
|
|
58
|
-
*
|
|
68
|
+
*
|
|
59
69
|
* ```ts
|
|
60
70
|
* import Potion from 'your-database/potion'
|
|
61
|
-
*
|
|
71
|
+
*
|
|
62
72
|
* player.hasItem(Potion) // false
|
|
63
73
|
* ```
|
|
64
74
|
*/
|
|
65
75
|
hasItem(itemClass: ItemClass | string): boolean {
|
|
66
|
-
|
|
76
|
+
return !!this.getItem(itemClass);
|
|
67
77
|
}
|
|
68
78
|
|
|
69
79
|
_getItemIndex(itemClass: ItemClass | string): number {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
80
|
+
return this.items().findIndex((it: Item): boolean => {
|
|
81
|
+
if (isString(itemClass)) {
|
|
82
|
+
return it.id() == itemClass;
|
|
83
|
+
}
|
|
84
|
+
return isInstanceOf(it, itemClass);
|
|
85
|
+
});
|
|
76
86
|
}
|
|
77
87
|
/**
|
|
78
88
|
* Add an item in the player's inventory. You can give more than one by specifying `nb`
|
|
79
|
-
*
|
|
89
|
+
*
|
|
80
90
|
* `onAdd()` method is called on the ItemClass
|
|
81
|
-
*
|
|
91
|
+
*
|
|
82
92
|
* @title Add Item
|
|
83
93
|
* @method player.addItem(item,nb=1)
|
|
84
|
-
* @param {ItemClass} itemClass
|
|
94
|
+
* @param {ItemClass} itemClass
|
|
85
95
|
* @param {number} [nb] Default 1
|
|
86
96
|
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
87
97
|
* @memberof ItemManager
|
|
88
98
|
* @example
|
|
89
|
-
*
|
|
99
|
+
*
|
|
90
100
|
* ```ts
|
|
91
101
|
* import Potion from 'your-database/potion'
|
|
92
102
|
* player.addItem(Potion, 5)
|
|
93
103
|
* ```
|
|
94
104
|
*/
|
|
95
|
-
addItem(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const { item } = this.items[itemIndex]
|
|
110
|
-
this['execMethod']('onAdd', [this], item)
|
|
111
|
-
return this.items[itemIndex]
|
|
105
|
+
addItem(itemId: string, nb: number = 1): Item {
|
|
106
|
+
const data = this.databaseById(itemId);
|
|
107
|
+
const item = this.items().find((it) => it.id() == itemId);
|
|
108
|
+
let instance: Item;
|
|
109
|
+
if (item) {
|
|
110
|
+
instance = item;
|
|
111
|
+
instance.quantity.update((it) => it + nb);
|
|
112
|
+
} else {
|
|
113
|
+
instance = new Item(data);
|
|
114
|
+
instance.id.set(itemId);
|
|
115
|
+
this.items().push(instance);
|
|
116
|
+
}
|
|
117
|
+
this["execMethod"]("onAdd", [this], instance);
|
|
118
|
+
return instance;
|
|
112
119
|
}
|
|
113
120
|
|
|
114
121
|
/**
|
|
115
122
|
* 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`
|
|
116
|
-
*
|
|
123
|
+
*
|
|
117
124
|
* `onRemove()` method is called on the ItemClass
|
|
118
|
-
*
|
|
125
|
+
*
|
|
119
126
|
* @title Remove Item
|
|
120
127
|
* @method player.removeItem(item,nb=1)
|
|
121
128
|
* @param {ItemClass | string} itemClass string is item id
|
|
122
129
|
* @param {number} [nb] Default 1
|
|
123
130
|
* @returns {{ nb: number, item: instance of ItemClass } | undefined}
|
|
124
|
-
* @throws {ItemLog} notInInventory
|
|
131
|
+
* @throws {ItemLog} notInInventory
|
|
125
132
|
* If the object is not in the inventory, an exception is raised
|
|
126
133
|
* ```
|
|
127
134
|
* {
|
|
@@ -131,10 +138,10 @@ export class ItemManager {
|
|
|
131
138
|
* ```
|
|
132
139
|
* @memberof ItemManager
|
|
133
140
|
* @example
|
|
134
|
-
*
|
|
141
|
+
*
|
|
135
142
|
* ```ts
|
|
136
143
|
* import Potion from 'your-database/potion'
|
|
137
|
-
*
|
|
144
|
+
*
|
|
138
145
|
* try {
|
|
139
146
|
* player.removeItem(Potion, 5)
|
|
140
147
|
* }
|
|
@@ -143,34 +150,36 @@ export class ItemManager {
|
|
|
143
150
|
* }
|
|
144
151
|
* ```
|
|
145
152
|
*/
|
|
146
|
-
removeItem(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
this[
|
|
160
|
-
|
|
153
|
+
removeItem(
|
|
154
|
+
itemClass: ItemClass | string,
|
|
155
|
+
nb: number = 1
|
|
156
|
+
): Item | undefined {
|
|
157
|
+
const itemIndex: number = this._getItemIndex(itemClass);
|
|
158
|
+
if (itemIndex == -1) {
|
|
159
|
+
throw ItemLog.notInInventory(itemClass);
|
|
160
|
+
}
|
|
161
|
+
const currentNb: number = this.items()[itemIndex].quantity();
|
|
162
|
+
const item = this.items()[itemIndex];
|
|
163
|
+
if (currentNb - nb <= 0) {
|
|
164
|
+
this.items().splice(itemIndex, 1);
|
|
165
|
+
} else {
|
|
166
|
+
this.items()[itemIndex].quantity.update((it) => it - nb);
|
|
167
|
+
}
|
|
168
|
+
this["execMethod"]("onRemove", [this], item);
|
|
169
|
+
return this.items()[itemIndex];
|
|
161
170
|
}
|
|
162
171
|
|
|
163
172
|
/**
|
|
164
173
|
* Purchases an item and reduces the amount of gold
|
|
165
|
-
*
|
|
174
|
+
*
|
|
166
175
|
* `onAdd()` method is called on the ItemClass
|
|
167
|
-
*
|
|
176
|
+
*
|
|
168
177
|
* @title Buy Item
|
|
169
178
|
* @method player.buyItem(item,nb=1)
|
|
170
179
|
* @param {ItemClass | string} itemClass string is item id
|
|
171
180
|
* @param {number} [nb] Default 1
|
|
172
181
|
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
173
|
-
* @throws {ItemLog} haveNotPrice
|
|
182
|
+
* @throws {ItemLog} haveNotPrice
|
|
174
183
|
* If you have not set a price on the item
|
|
175
184
|
* ```
|
|
176
185
|
* {
|
|
@@ -178,7 +187,7 @@ export class ItemManager {
|
|
|
178
187
|
* msg: '...'
|
|
179
188
|
* }
|
|
180
189
|
* ```
|
|
181
|
-
* @throws {ItemLog} notEnoughGold
|
|
190
|
+
* @throws {ItemLog} notEnoughGold
|
|
182
191
|
* If the player does not have enough money
|
|
183
192
|
* ```
|
|
184
193
|
* {
|
|
@@ -188,10 +197,10 @@ export class ItemManager {
|
|
|
188
197
|
* ```
|
|
189
198
|
* @memberof ItemManager
|
|
190
199
|
* @example
|
|
191
|
-
*
|
|
200
|
+
*
|
|
192
201
|
* ```ts
|
|
193
202
|
* import Potion from 'your-database/potion'
|
|
194
|
-
*
|
|
203
|
+
*
|
|
195
204
|
* try {
|
|
196
205
|
* player.buyItem(Potion)
|
|
197
206
|
* }
|
|
@@ -200,31 +209,30 @@ export class ItemManager {
|
|
|
200
209
|
* }
|
|
201
210
|
* ```
|
|
202
211
|
*/
|
|
203
|
-
buyItem(
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return this.addItem(ItemClass, nb)
|
|
212
|
+
buyItem(itemId: string, nb = 1): Item {
|
|
213
|
+
const data = this.databaseById(itemId);
|
|
214
|
+
if (!data.price) {
|
|
215
|
+
throw ItemLog.haveNotPrice(itemId);
|
|
216
|
+
}
|
|
217
|
+
const totalPrice = nb * data.price;
|
|
218
|
+
if (this._gold() < totalPrice) {
|
|
219
|
+
throw ItemLog.notEnoughGold(itemId, nb);
|
|
220
|
+
}
|
|
221
|
+
this._gold.update((gold) => gold - totalPrice);
|
|
222
|
+
return this.addItem(itemId, nb);
|
|
215
223
|
}
|
|
216
224
|
|
|
217
225
|
/**
|
|
218
226
|
* Sell an item and the player wins the amount of the item divided by 2
|
|
219
|
-
*
|
|
227
|
+
*
|
|
220
228
|
* `onRemove()` method is called on the ItemClass
|
|
221
|
-
*
|
|
229
|
+
*
|
|
222
230
|
* @title Sell Item
|
|
223
231
|
* @method player.sellItem(item,nb=1)
|
|
224
232
|
* @param {ItemClass | string} itemClass string is item id
|
|
225
233
|
* @param {number} [nbToSell] Default 1
|
|
226
234
|
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
227
|
-
* @throws {ItemLog} haveNotPrice
|
|
235
|
+
* @throws {ItemLog} haveNotPrice
|
|
228
236
|
* If you have not set a price on the item
|
|
229
237
|
* ```
|
|
230
238
|
* {
|
|
@@ -232,7 +240,7 @@ export class ItemManager {
|
|
|
232
240
|
* msg: '...'
|
|
233
241
|
* }
|
|
234
242
|
* ```
|
|
235
|
-
* @throws {ItemLog} notInInventory
|
|
243
|
+
* @throws {ItemLog} notInInventory
|
|
236
244
|
* If the object is not in the inventory, an exception is raised
|
|
237
245
|
* ```
|
|
238
246
|
* {
|
|
@@ -240,7 +248,7 @@ export class ItemManager {
|
|
|
240
248
|
* msg: '...'
|
|
241
249
|
* }
|
|
242
250
|
* ```
|
|
243
|
-
* @throws {ItemLog} tooManyToSell
|
|
251
|
+
* @throws {ItemLog} tooManyToSell
|
|
244
252
|
* If the number of items for sale exceeds the number of actual items in the inventory
|
|
245
253
|
* ```
|
|
246
254
|
* {
|
|
@@ -250,10 +258,10 @@ export class ItemManager {
|
|
|
250
258
|
* ```
|
|
251
259
|
* @memberof ItemManager
|
|
252
260
|
* @example
|
|
253
|
-
*
|
|
261
|
+
*
|
|
254
262
|
* ```ts
|
|
255
263
|
* import Potion from 'your-database/potion'
|
|
256
|
-
*
|
|
264
|
+
*
|
|
257
265
|
* try {
|
|
258
266
|
* player.addItem(Potion)
|
|
259
267
|
* player.sellItem(Potion)
|
|
@@ -263,82 +271,81 @@ export class ItemManager {
|
|
|
263
271
|
* }
|
|
264
272
|
* ```
|
|
265
273
|
*/
|
|
266
|
-
sellItem(
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return inventory
|
|
274
|
+
sellItem(itemId: string, nbToSell = 1): Item {
|
|
275
|
+
const data = this.databaseById(itemId);
|
|
276
|
+
const inventory = this.getItem(itemId);
|
|
277
|
+
if (!inventory) {
|
|
278
|
+
throw ItemLog.notInInventory(itemId);
|
|
279
|
+
}
|
|
280
|
+
const quantity = inventory.quantity();
|
|
281
|
+
if (quantity - nbToSell < 0) {
|
|
282
|
+
throw ItemLog.tooManyToSell(itemId, nbToSell, quantity);
|
|
283
|
+
}
|
|
284
|
+
if (!data.price) {
|
|
285
|
+
throw ItemLog.haveNotPrice(itemId);
|
|
286
|
+
}
|
|
287
|
+
this._gold.update((gold) => gold + (data.price / 2) * nbToSell);
|
|
288
|
+
this.removeItem(itemId, nbToSell);
|
|
289
|
+
return inventory;
|
|
283
290
|
}
|
|
284
291
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
292
|
+
getParamItem(name: string): number {
|
|
293
|
+
let nb = 0;
|
|
294
|
+
for (let item of this.equipments()) {
|
|
295
|
+
nb += item[name] || 0;
|
|
296
|
+
}
|
|
297
|
+
const modifier = (this as any).paramsModifier?.[name];
|
|
298
|
+
if (modifier) {
|
|
299
|
+
if (modifier.value) nb += modifier.value;
|
|
300
|
+
if (modifier.rate) nb *= modifier.rate;
|
|
301
|
+
}
|
|
302
|
+
return nb;
|
|
296
303
|
}
|
|
297
304
|
|
|
298
305
|
/**
|
|
299
306
|
* recover the attack sum of items equipped on the player.
|
|
300
|
-
*
|
|
307
|
+
*
|
|
301
308
|
* @title Get the player's attack
|
|
302
309
|
* @prop {number} player.atk
|
|
303
310
|
* @memberof ItemManager
|
|
304
311
|
*/
|
|
305
312
|
get atk(): number {
|
|
306
|
-
|
|
313
|
+
return this.getParamItem(ATK);
|
|
307
314
|
}
|
|
308
315
|
|
|
309
316
|
/**
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
317
|
+
* recover the physic defense sum of items equipped on the player.
|
|
318
|
+
*
|
|
319
|
+
* @title Get the player's pdef
|
|
320
|
+
* @prop {number} player.pdef
|
|
321
|
+
* @memberof ItemManager
|
|
322
|
+
*/
|
|
316
323
|
get pdef(): number {
|
|
317
|
-
|
|
324
|
+
return this.getParamItem(PDEF);
|
|
318
325
|
}
|
|
319
326
|
|
|
320
327
|
/**
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
328
|
+
* recover the skill defense sum of items equipped on the player.
|
|
329
|
+
*
|
|
330
|
+
* @title Get the player's sdef
|
|
331
|
+
* @prop {number} player.sdef
|
|
332
|
+
* @memberof ItemManager
|
|
333
|
+
*/
|
|
327
334
|
get sdef(): number {
|
|
328
|
-
|
|
335
|
+
return this.getParamItem(SDEF);
|
|
329
336
|
}
|
|
330
337
|
|
|
331
338
|
/**
|
|
332
339
|
* Use an object. Applies effects and states. Removes the object from the inventory then
|
|
333
|
-
*
|
|
340
|
+
*
|
|
334
341
|
* `onUse()` method is called on the ItemClass (If the use has worked)
|
|
335
342
|
* `onRemove()` method is called on the ItemClass
|
|
336
|
-
*
|
|
343
|
+
*
|
|
337
344
|
* @title Use an Item
|
|
338
345
|
* @method player.useItem(item,nb=1)
|
|
339
346
|
* @param {ItemClass | string} itemClass string is item id
|
|
340
347
|
* @returns {{ nb: number, item: instance of ItemClass }}
|
|
341
|
-
* @throws {ItemLog} restriction
|
|
348
|
+
* @throws {ItemLog} restriction
|
|
342
349
|
* If the player has the `Effect.CAN_NOT_ITEM` effect
|
|
343
350
|
* ```
|
|
344
351
|
* {
|
|
@@ -346,7 +353,7 @@ export class ItemManager {
|
|
|
346
353
|
* msg: '...'
|
|
347
354
|
* }
|
|
348
355
|
* ```
|
|
349
|
-
* @throws {ItemLog} notInInventory
|
|
356
|
+
* @throws {ItemLog} notInInventory
|
|
350
357
|
* If the object is not in the inventory, an exception is raised
|
|
351
358
|
* ```
|
|
352
359
|
* {
|
|
@@ -354,7 +361,7 @@ export class ItemManager {
|
|
|
354
361
|
* msg: '...'
|
|
355
362
|
* }
|
|
356
363
|
* ```
|
|
357
|
-
* @throws {ItemLog} notUseItem
|
|
364
|
+
* @throws {ItemLog} notUseItem
|
|
358
365
|
* If the `consumable` property is on false
|
|
359
366
|
* ```
|
|
360
367
|
* {
|
|
@@ -362,7 +369,7 @@ export class ItemManager {
|
|
|
362
369
|
* msg: '...'
|
|
363
370
|
* }
|
|
364
371
|
* ```
|
|
365
|
-
* @throws {ItemLog} chanceToUseFailed
|
|
372
|
+
* @throws {ItemLog} chanceToUseFailed
|
|
366
373
|
* Chance to use the item has failed. Chances of use is defined with `ItemClass.hitRate`
|
|
367
374
|
* ```
|
|
368
375
|
* {
|
|
@@ -371,15 +378,15 @@ export class ItemManager {
|
|
|
371
378
|
* }
|
|
372
379
|
* ```
|
|
373
380
|
* > the item is still deleted from the inventory
|
|
374
|
-
*
|
|
381
|
+
*
|
|
375
382
|
* `onUseFailed()` method is called on the ItemClass
|
|
376
|
-
*
|
|
383
|
+
*
|
|
377
384
|
* @memberof ItemManager
|
|
378
385
|
* @example
|
|
379
|
-
*
|
|
386
|
+
*
|
|
380
387
|
* ```ts
|
|
381
388
|
* import Potion from 'your-database/potion'
|
|
382
|
-
*
|
|
389
|
+
*
|
|
383
390
|
* try {
|
|
384
391
|
* player.addItem(Potion)
|
|
385
392
|
* player.useItem(Potion)
|
|
@@ -389,29 +396,29 @@ export class ItemManager {
|
|
|
389
396
|
* }
|
|
390
397
|
* ```
|
|
391
398
|
*/
|
|
392
|
-
useItem(
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
399
|
+
useItem(itemId: string): Item {
|
|
400
|
+
const inventory = this.getItem(itemId);
|
|
401
|
+
if ((this as any).hasEffect?.(Effect.CAN_NOT_ITEM)) {
|
|
402
|
+
throw ItemLog.restriction(itemId);
|
|
403
|
+
}
|
|
404
|
+
if (!inventory) {
|
|
405
|
+
throw ItemLog.notInInventory(itemId);
|
|
406
|
+
}
|
|
407
|
+
const item = inventory;
|
|
408
|
+
if ((item as any).consumable === false) {
|
|
409
|
+
throw ItemLog.notUseItem(itemId);
|
|
410
|
+
}
|
|
411
|
+
const hitRate = (item as any).hitRate ?? 1;
|
|
412
|
+
if (Math.random() > hitRate) {
|
|
413
|
+
this.removeItem(itemId);
|
|
414
|
+
this["execMethod"]("onUseFailed", [this], item);
|
|
415
|
+
throw ItemLog.chanceToUseFailed(itemId);
|
|
416
|
+
}
|
|
417
|
+
(this as any).applyEffect?.(item);
|
|
418
|
+
(this as any).applyStates?.(this, item);
|
|
419
|
+
this["execMethod"]("onUse", [this], item);
|
|
420
|
+
this.removeItem(itemId);
|
|
421
|
+
return inventory;
|
|
415
422
|
}
|
|
416
423
|
|
|
417
424
|
/**
|
|
@@ -463,41 +470,43 @@ export class ItemManager {
|
|
|
463
470
|
* }
|
|
464
471
|
* ```
|
|
465
472
|
*/
|
|
466
|
-
equip(
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
473
|
+
equip(
|
|
474
|
+
itemId: string,
|
|
475
|
+
equip: boolean = true
|
|
476
|
+
): void {
|
|
477
|
+
const inventory: Item = this.getItem(itemId);
|
|
478
|
+
if (!inventory) {
|
|
479
|
+
throw ItemLog.notInInventory(itemId);
|
|
480
|
+
}
|
|
481
|
+
const data = this.databaseById(itemId);
|
|
482
|
+
if (data._type == "item") {
|
|
483
|
+
throw ItemLog.invalidToEquiped(itemId);
|
|
484
|
+
}
|
|
474
485
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
486
|
+
if (this._class && this._class()[ClassHooks.canEquip]) {
|
|
487
|
+
const canEquip = this["execMethodSync"](
|
|
488
|
+
ClassHooks.canEquip,
|
|
489
|
+
[inventory, this],
|
|
490
|
+
this._class()
|
|
491
|
+
);
|
|
492
|
+
if (!canEquip) {
|
|
493
|
+
throw ItemLog.canNotEquip(itemId);
|
|
480
494
|
}
|
|
495
|
+
}
|
|
481
496
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
this['execMethod']('onEquip', [this, equip], item)
|
|
497
|
+
const item = inventory;
|
|
498
|
+
|
|
499
|
+
if ((item as any).equipped && equip) {
|
|
500
|
+
throw ItemLog.isAlreadyEquiped(itemId);
|
|
501
|
+
}
|
|
502
|
+
(item as any).equipped = equip;
|
|
503
|
+
if (!equip) {
|
|
504
|
+
const index = this.equipments().findIndex((it) => it.id() == item.id());
|
|
505
|
+
this.equipments().splice(index, 1);
|
|
506
|
+
} else {
|
|
507
|
+
this.equipments().push(item);
|
|
508
|
+
}
|
|
509
|
+
this["execMethod"]("onEquip", [this, equip], item);
|
|
496
510
|
}
|
|
511
|
+
};
|
|
497
512
|
}
|
|
498
|
-
|
|
499
|
-
applyMixins(ItemManager, [GoldManager, StateManager, EffectManager])
|
|
500
|
-
|
|
501
|
-
export interface ItemManager extends GoldManager, StateManager, EffectManager {
|
|
502
|
-
databaseById(itemClass: any),
|
|
503
|
-
}
|