-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpocket-change.js
379 lines (325 loc) · 10.7 KB
/
pocket-change.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
import Currency from './currency.js';
import log from './logger.js';
import Settings from './settings.js';
import Validator from './validator.js';
/**
* Handles generating coin and converting to loot
*/
export default class PocketChange {
/**
* Initializes dependencies
*/
constructor() {
this._settings = new Settings();
this._validator = new Validator();
}
/**
* Takes the provided token and adds currency to it if it is valid
*
* @param {TokenDocument5e} tokenDocument - The token document for the dropped actor
*/
populateTreasureForToken(tokenDocument) {
if (!this._validator.shouldAutoGenerateCurrency(tokenDocument)) return;
log('Generating treasure');
const actor = tokenDocument.actor;
const currency = this.generateCurrency(actor);
return actor.update({ 'data.currency': currency });
}
/**
* Generates currency for the provided actor based on its challenge rating
*
* @param {Actor5e} actor - The actor to base the coin generation off of
*/
generateCurrency(actor) {
let currency;
if (this._isWithinChallengeRating(actor, 0, 4)) {
currency = this._treasureForChallengeRating0to4(actor);
} else if (this._isWithinChallengeRating(actor, 5, 10)) {
currency = this._treasureForChallengeRating5to10(actor);
} else if (this._isWithinChallengeRating(actor, 11, 16)) {
currency = this._treasureForChallengeRating11to16(actor);
} else if (this._isWithinChallengeRating(actor, 17, 100)) {
currency = this._treasureForChallengeRating17andUp(actor);
}
currency.convertCurrencies();
const converted = currency.convertToStandardCurrency();
if (this._settings.showChatMessage) {
this._showChatMessage(actor, converted);
}
return converted;
}
_isWithinChallengeRating(actor, lowerCr, upperCr) {
let cr = actor.system.details.cr;
return cr >= lowerCr && cr <= upperCr;
}
_treasureForChallengeRating0to4(actor) {
let currency = new Currency(actor);
let roll = this._rollDice('1d100');
if (roll >= 1 && roll <= 30) {
currency.addCopper(this._rollDice('5d6'));
} else if (roll >= 31 && roll <= 60) {
currency.addSilver(this._rollDice('4d6'));
} else if (roll >= 61 && roll <= 70) {
currency.addElectrum(this._rollDice('3d6'));
} else if (roll >= 71 && roll <= 95) {
currency.addGold(this._rollDice('3d6'));
} else {
currency.addPlatinum(this._rollDice('1d6'));
}
return currency;
}
_treasureForChallengeRating5to10(actor) {
let currency = new Currency(actor);
let roll = this._rollDice('1d100');
if (roll >= 1 && roll <= 30) {
currency.addCopper(this._rollDice('4d6*100'));
currency.addElectrum(this._rollDice('1d6*10'));
} else if (roll >= 31 && roll <= 60) {
currency.addSilver(this._rollDice('6d6*10'));
currency.addGold(this._rollDice('2d6*10'));
} else if (roll >= 61 && roll <= 70) {
currency.addElectrum(this._rollDice('3d6*10'));
currency.addGold(this._rollDice('2d6*10'));
} else if (roll >= 71 && roll <= 95) {
currency.addGold(this._rollDice('4d6*10'));
} else {
currency.addGold(this._rollDice('2d6*10'));
currency.addPlatinum(this._rollDice('3d6'));
}
return currency;
}
_treasureForChallengeRating11to16(actor) {
let currency = new Currency(actor);
let roll = this._rollDice('1d100');
if (roll >= 1 && roll <= 20) {
currency.addSilver(this._rollDice('4d6*100'));
currency.addGold(this._rollDice('1d6*100'));
} else if (roll >= 21 && roll <= 35) {
currency.addElectrum(this._rollDice('1d6*100'));
currency.addGold(this._rollDice('1d6*100'));
} else if (roll >= 36 && roll <= 75) {
currency.addGold(this._rollDice('2d6*100'));
currency.addPlatinum(this._rollDice('1d6*10'));
} else {
currency.addGold(this._rollDice('2d6*100'));
currency.addPlatinum(this._rollDice('2d6*10'));
}
return currency;
}
_treasureForChallengeRating17andUp(actor) {
let currency = new Currency(actor);
let roll = this._rollDice('1d100');
if (roll >= 1 && roll <= 15) {
currency.addElectrum(this._rollDice('2d6*1000'));
currency.addGold(this._rollDice('8d6*100'));
} else if (roll >= 16 && roll <= 55) {
currency.addGold(this._rollDice('1d6*1000'));
currency.addPlatinum(this._rollDice('1d6*100'));
} else {
currency.addGold(this._rollDice('1d6*1000'));
currency.addPlatinum(this._rollDice('2d6*100'));
}
return currency;
}
_rollDice(formula) {
const roll = new Roll(formula);
roll.evaluate({
async: false, // TODO eventually, this will be asynchronous and will need to handle it
});
return roll.total;
}
_showChatMessage(actor, currency) {
ChatMessage.create({
user: game.userId,
whisper: game.users.filter((user) => user.isGM).map((gm) => gm.id),
flavor: `Currency generated for ${actor.name}`,
content: this._currencyToString(currency),
});
}
_currencyToString(currency) {
return `
<table>
<tr>
<th>PP</th>
<th>GP</th>
<th>EP</th>
<th>SP</th>
<th>CP</th>
</tr>
<tr>
<td>${currency.pp}</td>
<td>${currency.gp}</td>
<td>${currency.ep}</td>
<td>${currency.sp}</td>
<td>${currency.cp}</td>
</table>
`;
}
/**
* Converts the provided token to a lootable sheet
*
* @param {object} options
* @param {Token5e} options.token - the token to convert
* @param {number} options.chanceOfDamagedItems - (optional) the chance an item is considered damaged from 0 to 1. Uses the setting if undefined
* @param {number} options.damagedItemsMultiplier - (optional) the amount to reduce the value of a damaged item by. Uses the setting if undefined
* @param {boolean} options.removeDamagedItems - (optional) if true, removes items that are damaged of common rarity
*/
async convertToLoot({
token,
chanceOfDamagedItems,
damagedItemsMultiplier,
removeDamagedItems,
}) {
chanceOfDamagedItems ??= this._settings.chanceOfDamagedItems;
damagedItemsMultiplier ??= this._settings.damagedItemsMultiplier;
removeDamagedItems ??= this._settings.removeDamagedItems;
const sheet = token.actor.sheet;
// -1 for opened before but now closed
// 0 for closed and never opened
// 1 for currently open
const priorState = sheet._state;
// Close the old sheet if it's open
await sheet.close();
let newActorData = {
flags: {
core: {
sheetClass: 'dnd5e.LootSheet5eNPC',
},
lootsheetnpc5e: {
lootsheettype: 'Loot',
},
},
};
newActorData.items = this._getLootableItems({
token,
chanceOfDamagedItems,
damagedItemsMultiplier,
removeDamagedItems,
});
// Delete all items first
await token.document.actor.deleteEmbeddedDocuments(
'Item',
Array.from(token.actor.items.keys())
);
// Update actor with the new sheet and items
await token.document.actor.update(newActorData);
// Update the document with the overlay icon and new permissions
await token.document.update({
overlayEffect: this._settings.lootIcon,
vision: false,
actorData: {
actor: {
flags: {
loot: {
playersPermission: CONST.DOCUMENT_PERMISSION_LEVELS.OBSERVER,
},
},
},
permission: this._getUpdatedUserPermissions(token),
},
});
// Deregister the old sheet class
token.actor._sheet = null;
delete token.actor.apps[sheet.appId];
if (priorState > 0) {
// Re-draw the updated sheet if it was open
token.actor.sheet.render(true);
}
}
// Remove natural weapons, natural armor, class features, spells, and feats.
_getLootableItems({
token,
chanceOfDamagedItems,
damagedItemsMultiplier,
removeDamagedItems,
}) {
return token.actor.items
.map((item) => {
return item.toObject();
})
.filter((item) => {
if (item.type == 'weapon') {
return item.system.weaponType != 'natural';
}
if (item.type == 'equipment') {
if (!item.system.armor) return true;
return item.system.armor.type != 'natural';
}
return !['class', 'spell', 'feat'].includes(item.type);
})
.filter((item) => {
if (this._isItemDamaged(item, chanceOfDamagedItems)) {
if (removeDamagedItems) return false;
item.name += ' (Damaged)';
item.system.price *= damagedItemsMultiplier;
}
return true;
})
.map((item) => {
item.system.equipped = false;
return item;
});
}
_isItemDamaged(item, chanceOfDamagedItems) {
const rarity = item.system.rarity;
if (!rarity) return false;
// Never consider items above common rarity breakable
if (rarity.toLowerCase() !== 'common' && rarity.toLowerCase() !== 'none') {
return false;
}
return Math.random() < chanceOfDamagedItems;
}
// Update permissions to observer level, so players can loot
_getUpdatedUserPermissions(token) {
let lootingUsers = game.users.filter((user) => {
return (
user.role == CONST.USER_ROLES.PLAYER ||
user.role == CONST.USER_ROLES.TRUSTED
);
});
let permissions = {};
Object.assign(permissions, token.actor.permission);
lootingUsers.forEach((user) => {
permissions[user.id] = CONST.DOCUMENT_PERMISSION_LEVELS.OBSERVER;
});
return permissions;
}
/**
* Converts the provided token from a lootable sheet
*
* @param {Token5e} token - the token to convert
*/
async convertFromLoot(token) {
const sheet = token.actor.sheet;
// -1 for opened before but now closed
// 0 for closed and never opened
// 1 for currently open
const priorState = sheet._state;
// Close the old sheet if it's open
await sheet.close();
// Set back to default class
await token.document.actor.setFlag('core', 'sheetClass', 'Default');
// Update the document without the overlay icon and remove permissions
await token.document.update({
overlayEffect: '',
vision: false,
actorData: {
actor: {
flags: {
loot: {
playersPermission: CONST.DOCUMENT_PERMISSION_LEVELS.NONE,
},
},
},
permission: CONST.DOCUMENT_PERMISSION_LEVELS.NONE,
},
});
// Deregister the old sheet class
token.actor._sheet = null;
delete token.actor.apps[sheet.appId];
if (priorState > 0) {
// Re-draw the updated sheet if it was open
token.actor.sheet.render(true);
}
}
}