forked from vttred/ose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonster-sheet.js
358 lines (328 loc) · 10.4 KB
/
monster-sheet.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
import { OseActor } from "./entity.js";
import { OseActorSheet } from "./actor-sheet.js";
/**
* Extend the basic ActorSheet with some very simple modifications
*/
export class OseActorSheetMonster extends OseActorSheet {
constructor(...args) {
super(...args);
}
/* -------------------------------------------- */
/**
* Extend and override the default options used by the 5e Actor Sheet
* @returns {Object}
*/
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ["ose", "sheet", "monster", "actor"],
template: "systems/ose/dist/templates/actors/monster-sheet.html",
width: 450,
height: 560,
resizable: true,
tabs: [
{
navSelector: ".tabs",
contentSelector: ".sheet-body",
initial: "attributes",
},
],
});
}
/**
* Organize and classify Owned Items for Character sheets
* @private
*/
_prepareItems(data) {
const itemsData = this.actor.data.items;
const containerContents = {};
const attackPatterns = {};
// Partition items by category
let [weapons, items, armors, spells, containers] = itemsData.reduce(
(arr, item) => {
// Classify items into types
const containerId = item?.data?.data?.containerId;
if (containerId) {
containerContents[containerId] = [
...(containerContents[containerId] || []),
item,
];
return arr;
}
// Grab attack groups
if (["weapon", "ability"].includes(item.type)) {
if (attackPatterns[item.data.data.pattern] === undefined)
attackPatterns[item.data.data.pattern] = [];
attackPatterns[item.data.data.pattern].push(item);
}
// Classify items into types
if (item.type === "weapon") arr[0].push(item);
else if (item.type === "item") arr[1].push(item);
else if (item.type === "armor") arr[2].push(item);
else if (item.type === "spell") arr[3].push(item);
else if (item.type === "container") arr[4].push(item);
return arr;
},
[[], [], [], [], []]
);
// Sort spells by level
var sortedSpells = {};
var slots = {};
for (var i = 0; i < spells.length; i++) {
let lvl = spells[i].data.data.lvl;
if (!sortedSpells[lvl]) sortedSpells[lvl] = [];
if (!slots[lvl]) slots[lvl] = 0;
slots[lvl] += spells[i].data.data.memorized;
sortedSpells[lvl].push(spells[i]);
}
data.slots = {
used: slots,
};
containers.map((container, key, arr) => {
arr[key].data.data.itemIds = containerContents[container.id] || [];
arr[key].data.data.totalWeight = containerContents[container.id]?.reduce(
(acc, item) => {
return (
acc +
item.data?.data?.weight * (item.data?.data?.quantity?.value || 1)
);
},
0
);
return arr;
});
// Assign and return
data.owned = {
weapons: weapons,
items: items,
containers: containers,
armors: armors,
};
data.attackPatterns = attackPatterns;
data.spells = sortedSpells;
[
...Object.values(data.attackPatterns),
...Object.values(data.owned),
...Object.values(data.spells),
].forEach((o) => o.sort((a, b) => (a.data.sort || 0) - (b.data.sort || 0)));
}
/**
* Prepare data for rendering the Actor sheet
* The prepared data object contains both the actor data as well as additional sheet options
*/
getData() {
const data = super.getData();
// Prepare owned items
this._prepareItems(data);
// Settings
data.config.morale = game.settings.get("ose", "morale");
data.data.details.treasure.link = TextEditor.enrichHTML(
data.data.details.treasure.table
);
data.isNew = this.actor.isNew();
return data;
}
/**
* Monster creation helpers
*/
async generateSave() {
let choices = CONFIG.OSE.monster_saves;
let templateData = { choices: choices },
dlg = await renderTemplate(
"systems/ose/dist/templates/actors/dialogs/monster-saves.html",
templateData
);
//Create Dialog window
new Dialog(
{
title: game.i18n.localize("OSE.dialog.generateSaves"),
content: dlg,
buttons: {
ok: {
label: game.i18n.localize("OSE.Ok"),
icon: '<i class="fas fa-check"></i>',
callback: (html) => {
let hd = html.find('input[name="hd"]').val();
this.actor.generateSave(hd);
},
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("OSE.Cancel"),
},
},
default: "ok",
},
{
width: 250,
}
).render(true);
}
async _onDrop(event) {
super._onDrop(event);
let data;
try {
data = JSON.parse(event.dataTransfer.getData("text/plain"));
if (data.type !== "RollTable") return;
} catch (err) {
return false;
}
let link = "";
if (data.pack) {
let tableData = game.packs
.get(data.pack)
.index.filter((el) => el._id === data.id);
link = `@Compendium[${data.pack}.${data.id}]{${tableData[0].name}}`;
} else {
link = `@RollTable[${data.id}]`;
}
this.actor.update({ "data.details.treasure.table": link });
}
/* -------------------------------------------- */
async _chooseItemType(choices = ["weapon", "armor", "shield", "gear"]) {
let templateData = { types: choices },
dlg = await renderTemplate(
"systems/ose/dist/templates/items/entity-create.html",
templateData
);
//Create Dialog window
return new Promise((resolve) => {
new Dialog({
title: game.i18n.localize("OSE.dialog.createItem"),
content: dlg,
buttons: {
ok: {
label: game.i18n.localize("OSE.Ok"),
icon: '<i class="fas fa-check"></i>',
callback: (html) => {
resolve({
type: html.find('select[name="type"]').val(),
name: html.find('input[name="name"]').val(),
});
},
},
cancel: {
icon: '<i class="fas fa-times"></i>',
label: game.i18n.localize("OSE.Cancel"),
},
},
default: "ok",
}).render(true);
});
}
async _resetAttacks(event) {
const weapons = this.actor.data.items.filter((i) => i.type === "weapon");
for (let wp of weapons) {
const item = this.actor.items.get(wp.id);
await item.update({
data: {
counter: {
value: parseInt(wp.data.data.counter.max),
},
},
});
}
}
async _onCountChange(event) {
event.preventDefault();
const itemId = event.currentTarget.closest(".item").dataset.itemId;
const item = this.actor.items.get(itemId);
if (event.target.dataset.field == "value") {
return item.update({
"data.counter.value": parseInt(event.target.value),
});
} else if (event.target.dataset.field == "max") {
return item.update({
"data.counter.max": parseInt(event.target.value),
});
}
}
/**
* Activate event listeners using the prepared sheet HTML
* @param html {HTML} The prepared HTML object ready to be rendered into the DOM
*/
activateListeners(html) {
super.activateListeners(html);
html.find(".morale-check a").click((ev) => {
let actorObject = this.actor;
actorObject.rollMorale({ event: event });
});
html.find(".reaction-check a").click((ev) => {
let actorObject = this.actor;
actorObject.rollReaction({ event: event });
});
html.find(".appearing-check a").click((ev) => {
let actorObject = this.actor;
let check = $(ev.currentTarget).closest(".check-field").data("check");
actorObject.rollAppearing({ event: event, check: check });
});
// Everything below here is only needed if the sheet is editable
if (!this.options.editable) return;
// Update Inventory Item
html.find(".item-edit").click((ev) => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
item.sheet.render(true);
});
// Delete Inventory Item
html.find(".item-delete").click(async (ev) => {
const li = $(ev.currentTarget).parents(".item");
await this.actor.deleteEmbeddedDocuments("Item", [li.data("itemId")]);
li.slideUp(200, () => this.render(false));
});
html.find(".item-create").click((event) => {
event.preventDefault();
const header = event.currentTarget;
const type = header.dataset.type;
// item creation helper func
let createItem = function (type, name = `New ${type.capitalize()}`) {
const itemData = {
name: name ? name : `New ${type.capitalize()}`,
type: type,
data: duplicate(header.dataset),
};
delete itemData.data["type"];
return itemData;
};
// Getting back to main logic
if (type == "choice") {
const choices = header.dataset.choices.split(",");
this._chooseItemType(choices).then((dialogInput) => {
const itemData = createItem(dialogInput.type, dialogInput.name);
this.actor.createEmbeddedDocuments("Item", [itemData], {});
});
return;
}
const itemData = createItem(type);
return this.actor.createEmbeddedDocuments("Item", [itemData], {});
});
html.find(".item-reset[data-action='reset-attacks']").click((ev) => {
this._resetAttacks(ev);
});
html
.find(".counter input")
.click((ev) => ev.target.select())
.change(this._onCountChange.bind(this));
html.find(".hp-roll").click((ev) => {
let actorObject = this.actor;
actorObject.rollHP({ event: event });
});
html.find(".item-pattern").click((ev) => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("itemId"));
let currentColor = item.data.data.pattern;
let colors = Object.keys(CONFIG.OSE.colors);
let index = colors.indexOf(currentColor);
if (index + 1 == colors.length) {
index = 0;
} else {
index++;
}
item.update({
"data.pattern": colors[index],
});
});
html
.find('button[data-action="generate-saves"]')
.click(() => this.generateSave());
}
}