Skip to content

Commit 6efbe0f

Browse files
authored
Important changes to handling Disease and Injuries (#2283)
* change disease increment/decrement to async to ensure updates visible by caller * fix disease error while setting up test * update injury handle to match that of the disease * added action to step diseases and injuries on sheet V2 * fix error about trying to call toMessage on undefined when failing to roll disease duration
1 parent 91ca553 commit 6efbe0f

5 files changed

Lines changed: 123 additions & 71 deletions

File tree

modules/actor/sheet/actor-sheet.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,26 +1146,15 @@ export default class ActorSheetWFRP4e extends WarhammerActorSheet {
11461146
}
11471147

11481148
async _onInjuryDurationClick(ev) {
1149-
let itemId = this._getId(ev);
1150-
let injury = this.actor.items.get(itemId).toObject()
1151-
if (!isNaN(injury.system.duration.value)) {
1152-
if (ev.button == 0)
1153-
return this.actor.decrementInjury(injury)
1154-
else injury.system.duration.value++
1155-
return this.actor.updateEmbeddedDocuments("Item", [injury])
1149+
const injury = this._getDocument(ev);
1150+
1151+
if (ev.button === 0)
1152+
{
1153+
injury.system.decrement();
11561154
}
1157-
else {
1158-
try {
1159-
let roll = await new Roll(injury.system.duration.value, this.actor).roll();
1160-
roll.toMessage({speaker : {alias : this.actor.name}, flavor : injury.name})
1161-
injury.system.duration.value = roll.total;
1162-
injury.system.duration.active = true;
1163-
return this.actor.updateEmbeddedDocuments("Item", [injury])
1164-
}
1165-
catch
1166-
{
1167-
return ui.notifications.error(game.i18n.localize("ERROR.ParseInjury"))
1168-
}
1155+
else
1156+
{
1157+
injury.system.increment();
11691158
}
11701159
}
11711160

modules/model/item/disease.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -146,54 +146,53 @@ export class DiseaseModel extends BaseItemModel {
146146
throw new Error("Must provide incubation or duration as type")
147147
}
148148

149-
let roll
150-
try
149+
try
151150
{
152-
roll = await new Roll(this[type].value, this.parent).roll();
151+
let roll = await new Roll(this[type].value, this.parent).roll();
153152
let update = {[`system.${type}.value`] : roll.total};
154153
if (type == "duration")
155154
{
156155
update["system.duration.active"] = true;
157156
}
158157
await this.parent.update(update);
158+
159+
let messageData = this.getMessageData()
160+
161+
messageData.speaker.alias += " " + type;
162+
163+
roll.toMessage(messageData, {rollMode : "gmroll"})
159164
}
160165
catch (e)
161166
{
162167
ChatMessage.create(this.getMessageData(game.i18n.localize("CHAT.DiseaseRollError")));
163168
}
164-
165-
let messageData = this.getMessageData()
166-
167-
messageData.speaker.alias += " " + type;
168-
169-
roll.toMessage(messageData, {rollMode : "gmroll"})
170169
}
171170

172-
increment()
171+
async increment()
173172
{
174173
if (this.duration.active)
175174
{
176-
return this.parent.update({"system.duration.value" : Number(this.duration.value) + 1})
175+
return await this.parent.update({"system.duration.value" : Number(this.duration.value) + 1})
177176
}
178177
else
179178
{
180-
return this.parent.update({"system.incubation.value" : Number(this.incubation.value) + 1})
179+
return await this.parent.update({"system.incubation.value" : Number(this.incubation.value) + 1})
181180
}
182181
}
183182

184-
decrement()
183+
async decrement()
185184
{
186185
let update = {}
187186
if (this.duration.active)
188187
{
189188
if (isNaN(this.duration.value))
190189
{
191-
return this.start("duration");
190+
return await this.start("duration");
192191
}
193192
let duration = Number(this.duration.value) - 1;
194193
if (duration == 0)
195194
{
196-
return this.finishDuration();
195+
return await this.finishDuration();
197196
}
198197
else
199198
{
@@ -204,20 +203,20 @@ export class DiseaseModel extends BaseItemModel {
204203
{
205204
if (isNaN(this.incubation.value))
206205
{
207-
return this.start("incubation");
206+
return await this.start("incubation");
208207
}
209208
let incubation = Number(this.incubation.value) - 1;
210209
if (incubation == 0)
211210
{
212211
update = {"system.incubation.value" : incubation};
213-
this.start("duration");
212+
await this.start("duration");
214213
}
215214
else
216215
{
217216
update = {"system.incubation.value" : incubation};
218217
}
219218
}
220-
return this.parent.update(update);
219+
return await this.parent.update(update);
221220
}
222221

223222
async finishDuration() {
@@ -234,7 +233,7 @@ export class DiseaseModel extends BaseItemModel {
234233
let difficultyname = lingering.specifier;
235234
let difficulty = warhammer.utility.findKey(difficultyname, game.wfrp4e.config.difficultyNames, { caseInsensitive: true }) || "challenging"
236235

237-
let test = await this.setupSkill(game.i18n.localize("NAME.Endurance"), {appendTitle: ` - ${game.i18n.localize("NAME.Lingering")}`, fields: {difficulty : difficulty} }, {skipTargets: true});
236+
let test = await this.parent.actor.setupSkill(game.i18n.localize("NAME.Endurance"), {appendTitle: ` - ${game.i18n.localize("NAME.Lingering")}`, fields: {difficulty : difficulty} }, {skipTargets: true});
238237
await test.roll();
239238

240239
if (test.failed)

modules/model/item/injury.js

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,88 @@
1-
import { LocationalItemModel } from "./components/locational";
1+
import {LocationalItemModel} from "./components/locational";
2+
23
let fields = foundry.data.fields;
34

4-
export class InjuryModel extends LocationalItemModel
5+
export class InjuryModel extends LocationalItemModel
56
{
6-
static defineSchema()
7+
static defineSchema()
8+
{
9+
let schema = super.defineSchema();
10+
schema.penalty = new fields.SchemaField({
11+
value: new fields.StringField(),
12+
})
13+
schema.duration = new fields.SchemaField({
14+
value: new fields.StringField(),
15+
active: new fields.BooleanField(),
16+
permanent: new fields.BooleanField(),
17+
});
18+
return schema;
19+
}
20+
21+
/**
22+
* Used to identify an Item as one being a child or instance of InjuryModel
23+
*
24+
* @final
25+
* @returns {boolean}
26+
*/
27+
get isInjury() {
28+
return true;
29+
}
30+
31+
chatData() {
32+
let properties = [];
33+
properties.push(`<b>${game.i18n.localize("Location")}</b>: ${this.location.value}`);
34+
if (this.penalty.value)
35+
properties.push(`<b>${game.i18n.localize("Penalty")}</b>: ${this.penalty.value}`);
36+
return properties;
37+
}
38+
39+
async increment() {
40+
if (this.duration.active)
741
{
8-
let schema = super.defineSchema();
9-
schema.penalty = new fields.SchemaField({
10-
value : new fields.StringField(),
11-
})
12-
schema.duration = new fields.SchemaField({
13-
value : new fields.StringField(),
14-
active : new fields.BooleanField(),
15-
permanent : new fields.BooleanField(),
16-
});
17-
return schema;
42+
return await this.parent.update({"system.duration.value" : Number(this.duration.value) + 1})
1843
}
44+
}
45+
46+
async decrement() {
47+
if (isNaN(this.duration.value)) {
48+
return await this.start();
49+
}
50+
51+
let update = {};
52+
let duration = Number(this.duration.value) - 1;
1953

20-
/**
21-
* Used to identify an Item as one being a child or instance of InjuryModel
22-
*
23-
* @final
24-
* @returns {boolean}
25-
*/
26-
get isInjury() {
27-
return true;
54+
if (duration == 0) {
55+
return await this.finish();
56+
} else {
57+
update["system.duration.value"] = duration;
2858
}
2959

30-
chatData() {
31-
let properties = [];
32-
properties.push(`<b>${game.i18n.localize("Location")}</b>: ${this.location.value}`);
33-
if (this.penalty.value)
34-
properties.push(`<b>${game.i18n.localize("Penalty")}</b>: ${this.penalty.value}`);
35-
return properties;
36-
}
60+
return await this.parent.update(update);
61+
}
62+
63+
async start() {
64+
try {
65+
let roll = await new Roll(this.duration.value, this.parent.actor).roll();
66+
roll.toMessage({speaker: {alias: this.parent.actor.name}, flavor: this.parent.name});
67+
68+
return await this.parent.update({
69+
"system.duration.value": roll.total,
70+
"system.duration.active": true
71+
});
72+
} catch (error) {
73+
return ui.notifications.error(game.i18n.localize("ERROR.ParseInjury"));
74+
}
75+
}
76+
77+
async finish() {
78+
let msg = game.i18n.format("CHAT.InjuryFinish", {injury: this.parent.name});
79+
80+
ChatMessage.create(foundry.utils.mergeObject(this.getMessageData(msg), {whisper: ChatMessage.getWhisperRecipients("GM")}));
81+
82+
await this.parent.delete();
83+
}
3784

85+
getMessageData(content = "") {
86+
return {content, speaker: {alias: this.parent.name}, flavor: this.parent.actor.name};
87+
}
3888
}

src/apps/sheets/actor/standard-sheet.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default class StandardWFRP4eActorSheet extends BaseWFRP4eActorSheet
1818
dismount : this._dismount,
1919
showMount : this._showMount,
2020
randomize: this._randomize,
21+
stepAilment: {buttons: [0, 2], handler: this._onStepAilment},
2122
},
2223
}
2324

@@ -451,4 +452,19 @@ export default class StandardWFRP4eActorSheet extends BaseWFRP4eActorSheet
451452
}
452453
}
453454

455+
static async _onStepAilment(ev)
456+
{
457+
ev.stopPropagation();
458+
ev.preventDefault();
459+
let document = (await this._getDocument(ev)) || this.document;
460+
461+
if (!document) return;
462+
463+
if (ev.button === 0) {
464+
document.system.decrement();
465+
} else {
466+
document.system.increment();
467+
}
468+
}
469+
454470
}

static/templates/sheets/actor/tabs/actor-effects.hbs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
{{localize "Permanent"}}
7070
</div>
7171
{{else}}
72-
<a data-action="rollProperty" data-path="system.duration">
72+
<a class="prevent-context" data-action="stepAilment">
7373
{{this.system.duration.value}} {{localize "days"}}
7474
</a>
7575
{{/if}}
@@ -200,10 +200,8 @@
200200
</div>
201201

202202
{{#if (isGM)}}
203-
<a data-action="rollProperty"
204-
data-type="system.incubation">{{this.system.incubation.value}}</a>
205-
<a data-action="rollProperty"
206-
data-type="system.duration">{{this.system.duration.value}}
203+
<a class="prevent-context" data-action="stepAilment">{{this.system.incubation.value}}</a>
204+
<a class="prevent-context" data-action="stepAilment">{{this.system.duration.value}}
207205
{{this.system.duration.unit}}</a>
208206
{{#if this.system.diagnosed}}
209207
<a data-action="toggleProperty" data-path="system.diagnosed"><i

0 commit comments

Comments
 (0)