-
Notifications
You must be signed in to change notification settings - Fork 0
/
poc-dynamicItemModifiers.js
382 lines (351 loc) · 12.5 KB
/
poc-dynamicItemModifiers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Actor data (mockup)
* each modable value is an object of the kind
*
* _propertyName : {
* base: Number,
* modifiers: []
* }
*
* In the Actor's constructor, for each of those objects, getters and setters are generated as
*
* .propertyName
*
* without the underscore.
*/
const actorData = {
name: 'John Doe',
items: [],
abilities: {
str: {
label: 'Strength',
_value: {
base: 0,
modifiers: []
},
_save: {
base: 0,
modifiers: []
}
},
dex: {
label: 'Dexterity',
_value: {
base: 0,
modifiers: []
},
_save: {
base: 0,
modifiers: []
}
},
con: {
label: 'Constitution',
_value: {
base: 0,
modifiers: []
},
_save: {
base: 0,
modifiers: []
}
}
},
attributes: {
ac: {
label: 'Armor Class',
_value: {
base: 0,
modifiers: []
}
}
}
}
/**
* Item data is a mockup again
* They have modification-instructions in the
*
* .provides[]
*
* Array.
*
* They consist of
* - type : bonus | set
* - target: a pointer to the object-path-property to modify
* - and a value
*
* If the item .isActive === true when adding it to the actor
* or when enabling the item while this actor owns the item,
* the modifiers are inserted into the modifiers-Array of the Actors property depicted by the target-property here
* Note the wildcards, arrays and object property-collections are supported
*
* Upon disabling, the modifier is removed again
*/
const itemData = {
name: 'Cloak of Protection',
type: 'Equipment',
isActive: false,
provides: [
{
type: 'bonus',
target: 'abilities.*.save',
value: 1
},
{
type: 'bonus',
target: 'attributes.ac.value',
value: 1
}
]
}
/**
* Class Actor mockup
*/
class Actor {
constructor(data) {
this.prepareData(data);
// create getters/ setters
this._createAccessors(this.data);
}
prepareData(actorData) {
this.data = actorData;
}
// add an item to the actor's inventory
// check if it's active, then provide the modifiers in the respective target(s)
createOwnedItem(item) {
this.data.items.push(item);
if (item.data.isActive === true) {
if (item.provides.length !== 0) {
// insert the provider
item.data.provides.forEach(provider => {
this._insertProvider(
this.data,
provider.target,
{
type: provider.type,
value: provider.bonus,
sourceType: item.type,
sourceName: item.name
}
);
});
}
}
}
// enabling/activating an item within the actor's inventory
// check for modifiers and providing those, again
enableItem(itemId) {
if (itemId < this.data.items.length && this.data.items[itemId].data.isActive === false) {
// enableing the item
this.data.items[itemId].data.isActive = true;
if (this.data.items[itemId].data.provides.length !== 0) {
// insert the provider
this.data.items[itemId].data.provides.forEach(provider => {
this._insertProvider(
this.data,
provider.target,
{
type: provider.type,
value: provider.value,
sourceType: this.data.items[itemId].data.type,
sourceName: this.data.items[itemId].data.name
}
);
});
}
}
}
// disabling/deactivating an item within the actor's inventory
// check for modifiers and remove those
disableItem(itemId) {
if (itemId < this.data.items.length && this.data.items[itemId].data.isActive === true) {
// enableing the item
this.data.items[itemId].data.isActive = false;
if (this.data.items[itemId].data.provides.length !== 0) {
// insert the provider
this.data.items[itemId].data.provides.forEach(provider => {
this._removeProvider(
this.data,
provider.target,
{
type: provider.type,
value: provider.value,
sourceType: this.data.items[itemId].data.type,
sourceName: this.data.items[itemId].data.name
}
);
});
}
}
}
/**
* Traverses an object recursively and adds getters and setters to object properties that matches a specific structure
*
* ._property {
* base: Number,
* modifiers: []
* }
*
* @param {object} obj Object to traverse to add getters and setters
*/
_createAccessors(obj) {
for (let prop in obj) {
if (typeof obj[prop] === "object") {
if (Array.isArray(obj[prop])) {
for (let i = 0; i < obj[prop].length; i++) {
this._createAccessors(obj[prop][i]);
}
} else {
if (prop.indexOf('_') === 0 && obj[prop].hasOwnProperty('base') && obj[prop].hasOwnProperty('modifiers')) {
// create get and set accessors
//Object.defineProperty(obj, '_' + prop, {
Object.defineProperty(obj, prop.substr(1), {
get: () => {
// get 'set-base' properties first
// then 'set'
// then bonus/malus
let val = obj[prop].base;
// get the highest set modifier
let set = obj[prop].modifiers.filter(modifier => modifier.type === 'set').reduce((prev, cur) => { return prev > cur.value ? prev : cur.value }, 0);
// get the sum of all bonus modifiers
let bonus = obj[prop].modifiers.filter(modifier => modifier.type === 'bonus').reduce((prev, cur) => prev + cur.value, 0);
// Priority:
// Set tops the base-value
// Then boni are added
// Malus = bonus with negative value
let result = set !== 0 ? set + bonus : val + bonus;
// Professional debugging
/*
console.log("Base Value: " + val);
console.log("Set: " + set);
console.log("Bonus: " + bonus);
console.log("=> " + result);
*/
return result;
},
// upon set, we set the base value and do not touch the modifiers
set: (value) => {
obj[prop].base = value;
}
});
} else {
// not there yet, crawl happily through the object
this._createAccessors(obj[prop]);
}
}
}
}
}
// helper for inserting the providers
_insertProvider(obj, xpath, provider) {
this._processProvider(obj, xpath, provider, true);
}
// helper for removing the providers
_removeProvider(obj, xpath, provider) {
this._processProvider(obj, xpath, provider, false);
}
/**
* Traverses an object recursively and follows the path depicted by the xpath parameter
* WHenever it finds an modable attribute (see the structure of an modable attribute above), it adds the given provider into the modifiers[] array of said attribute
* Then the getters/ setters do their job to calculate the accumlated value
*
* @param {object} obj the object to crawl through
* @param {String} xpath the property path of the actor this provides a modification to
* @param {object} provider an object in the following structure: { type: 'bonus' | 'set', value: Number, sourceType: 'e.g. Equipement, sourceName: 'e.g Item Name' }
* @param {boolean} mode is true for insertion of the modifiers and false for removing them
*/
_processProvider(obj, xpath, provider, mode = true) {
// if we reached the last bit of the xpath and there's a object property with the xpath's name
if (xpath.indexOf('.') === -1 && obj.hasOwnProperty('_' + xpath)) {
xpath = '_' + xpath;
// if that's a mod-able value within this object
// those start with an underscore, which is **not reflected in the xpath** (easy of use for the user)
// AND have the properties 'base' and 'modifiers[]'
if (obj[xpath].hasOwnProperty('base') && obj[xpath].hasOwnProperty('modifiers') && Array.isArray(obj[xpath].modifiers)) {
if (mode === true) {
obj[xpath].modifiers.push(provider);
} else {
for (let i = 0; i < obj[xpath].modifiers.length; i++) {
if (
obj[xpath].modifiers[i].type === provider.type &&
obj[xpath].modifiers[i].value === provider.value &&
obj[xpath].modifiers[i].sourceType === provider.sourceType &&
obj[xpath].modifiers[i].sourceName === provider.sourceName) {
obj[xpath].modifiers.splice(i, 1);
break;
}
}
}
}
} else {
// traverse deeper
let parts = xpath.split('.');
let property = parts.shift();
let remainder = parts.join('.');
// Wildcard property -> arrays or object property collections can be collectively addressed
if (property === '*') {
// it's an array, insert the provider in each of the array's elements
if (Array.isArray(obj)) {
for (let i = 0; i < obj[propety].length; i++) {
this._processProvider(obj[property][i], remainder, provider, mode);
}
} else {
// it's an object, insert the provider in each of the object's properties
if (typeof obj === 'object') {
// traverse all object properties
for (let prop in obj) {
this._processProvider(obj[prop], remainder, provider, mode);
}
} // else: just a single property, doesn't match with the complete xpath, so noop
}
} else {
if (obj.hasOwnProperty(property)) {
// traverse deeper
this._processProvider(obj[property], remainder, provider, mode);
}
}
}
}
}
class Item {
constructor(data) {
this.prepareData(data);
}
prepareData(itemData) {
this.data = itemData;
}
enable() {
this.data.isActive === true;
}
disable() {
this.data.isActive === false;
}
}
console.log("Creating Item (not active) and player");
var item = new Item(itemData);
var player = new Actor(actorData);
console.log("-----------------------------------------------------");
console.log("Adding the item to the player's inventory, item is still disabled");
player.createOwnedItem(item);
console.log("AC: " + player.data.attributes.ac.value);
console.log("STR Save " + player.data.abilities.str.save);
console.log("DEX Save " + player.data.abilities.dex.save);
console.log("CON Save " + player.data.abilities.con.save);
console.log("-----------------------------------------------------");
console.log("Enabling the item");
player.enableItem(0);
console.log("Item");
console.log(player.data.items[0]);
console.log("AC: " + player.data.attributes.ac.value);
console.log("STR Save " + player.data.abilities.str.save);
console.log("DEX Save " + player.data.abilities.dex.save);
console.log("CON Save " + player.data.abilities.con.save);
console.log("-----------------------------------------------------");
console.log("Disabling the item");
player.disableItem(0);
console.log("Item");
console.log(player.data.items[0]);
console.log("AC: " + player.data.attributes.ac.value);
console.log("STR Save " + player.data.abilities.str.save);
console.log("DEX Save " + player.data.abilities.dex.save);
console.log("CON Save " + player.data.abilities.con.save);