-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtraining.js
executable file
·1338 lines (1212 loc) · 50.2 KB
/
training.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Imports
import AuditLog from "./audit-log.js";
import { DWTForm } from "./downtime.js";
import { GMConfig } from "./gmConfig.js";
let activateDowntimeTab = false;
// Register Game Settings
Hooks.once("init", () => {
game.settings.registerMenu("downtime-ethck", "config", {
name: "Config",
label: "Access Config Menu",
hint: "Access the configuration menu to find additional options.",
icon: "fas fa-desktop",
type: GMConfig,
restricted: true,
});
game.settings.register("downtime-ethck", "enableTraining", {
name: "Show Training Tab on PCs",
hint: "Display the training tab for Player Characters",
scope: "world",
config: true,
default: true,
type: Boolean,
});
game.settings.register("downtime-ethck", "enableTrainingNpc", {
name: "Show Training Tab on NPCs",
hint: "Display the training tab for Non-Player Characters",
scope: "world",
config: true,
default: true,
type: Boolean,
});
game.settings.register("downtime-ethck", "aboutTimeCompat", {
name: "Enable About Time Compatibility",
hint: "Allows for About Time month/day to appear in Activity Log",
scope: "world",
config: true,
default: false,
type: Boolean,
});
game.settings.register("downtime-ethck", "crashCompat", {
name: "Enable Compatibility for Crash's 5e Downtime Tracking",
hint: "Allows for Crash's Downtime and this module to reside in the same tab.",
scope: "world",
config: true,
default: true,
type: Boolean,
});
game.settings.register("downtime-ethck", "crashCompatMessage", {
name: "Add Extra Text To Distingusih between Downtime and Training?",
hint: "Determines if extra text shows up between crash's tacking and training and this mod.",
scope: "world",
config: true,
default: true,
type: Boolean,
});
game.settings.register("downtime-ethck", "betterRollsCompat", {
name: "Enable Compatibility for Better Rolls",
hint: "Allows for Better Rolls alternate roll format to be used.",
scope: "world",
config: true,
default: false,
type: Boolean,
});
game.settings.register("downtime-ethck", "tabName", {
name: "Tab Name",
hint: "Name for the custom downtime tab",
scope: "world",
config: true,
default: "Downtime",
type: String,
});
game.settings.register("downtime-ethck", "dcRollMode", {
name: "DC Roll Mode",
hint: "Roll Mode used when downtime activities' DCs are called",
scope: "world",
config: true,
type: String,
choices: {
gmroll: "Private GM Roll (Player can see)",
blindroll: "Blind Roll (Player can't see)",
},
default: "blindroll",
});
game.settings.register("downtime-ethck", "extraSheetWidth", {
name: "Extra Sheet Width",
hint: "# of pixels to increase width of sheet by.",
scope: "client",
config: true,
default: 50,
type: Number,
});
game.settings.register("downtime-ethck", "activities", {
scope: "world",
config: false,
default: [],
});
game.settings.register("downtime-ethck", "changes", {
scope: "world",
config: false,
default: {},
});
game.settings.register("downtime-ethck", "migrated", {
scope: "world",
config: false,
default: { status: false, version: "0.3.3" },
});
// Autocomplete Inline Properties AIP
// Define the config for our package
const aipConfig = {
packageName: "downtime-ethck",
sheetClasses: [
{
name: "DWTForm",
fieldConfigs: [
{
selector: `.ethck-downtime-form #rollable #CUSTOM`,
showButton: true,
allowHotkey: true,
dataMode: CONST.AIP?.DATA_MODE.CUSTOM,
customDataGetter: (sheet) => {
return rollContext(sheet.actor);
},
customInlinePrefix: "@",
},
],
},
],
};
// Add our config
CONFIG.AIP?.PACKAGE_CONFIG.push(aipConfig);
});
Hooks.once("ready", () => {
_downtimeMigrate();
});
// The Meat And Potatoes
async function addTrainingTab(app, html, data) {
// Determine if we should show the downtime tab
let showTrainingTab = false;
if (data.isCharacter) {
showTrainingTab = game.settings.get("downtime-ethck", "enableTraining");
} else if (data.isNPC) {
showTrainingTab = game.settings.get("downtime-ethck", "enableTrainingNpc");
}
if (showTrainingTab) {
// Get our actor
let actor = game.actors.contents.find((a) => a._id === data.actor._id);
// actor isn't loaded in the world, or doesn't exist
// i.e. compendiums
if (actor === undefined) {
return;
}
// Make sure flags exist if they don't already
if (actor.flags["downtime-ethck"] === undefined || actor.flags["downtime-ethck"] === null) {
await actor.setFlag("downtime-ethck", "trainingItems", []);
await actor.setFlag("downtime-ethck", "changes", []);
}
let flags = actor.getFlag("downtime-ethck", "trainingItems");
let CRASH_COMPAT = false;
const crash5eTraining = game.modules.get("5e-training");
if (
crash5eTraining !== undefined &&
crash5eTraining.active === true &&
game.settings.get("downtime-ethck", "crashCompat")
) {
// 0.4.6 changed how the tab is rendered, so our new logic requires this (10/23/2020)
if (isNewerVersion(crash5eTraining.version, "0.4.6")) {
// version must be GREATER to return true.
CRASH_COMPAT = true;
} else {
ui.notifications.warn(
"Please update Crash's 5e Downtime Tracking to version 0.4.7 or greater to enable compaitbility."
);
}
} else {
// Update the nav menu
let tabs = html.find('.tabs[data-group="primary"]');
if (!tabs.find('.item[data-tab="downtime"]').length) {
//Prevent addition of tab more than once
let trainingTabBtn;
if (game.system.id === "dnd5e") {
trainingTabBtn = $('<a class="item" data-tab="downtime"><i class="fas fa-beer-mug"></i></a>');
} else {
let tabName = game.settings.get("downtime-ethck", "tabName");
trainingTabBtn = $('<a class="item" data-tab="downtime">' + tabName + "</a>");
}
tabs.append(trainingTabBtn);
}
}
if (game.system.id === "dnd5e") {
const skills = CONFIG.DND5E.skills;
} else if (game.system.id === "pf1") {
const skills = CONFIG.PF1.skills;
}
// Create the tab content
let sheet;
if (game.system.id === "dnd5e") {
sheet = html.find(".tab-body");
} else if (game.system.id === "pf1") {
sheet = html.find(".primary-body");
// template expects flags to be up a level, so copy them over.
data.actor.flags["downtime-ethck"] = data.actor.flags["downtime-ethck"];
}
// Compile our template
let ethckDowntimeTabHtml = $(
await renderTemplate("modules/downtime-ethck/templates/training-section.html", {
activities: game.settings.get("downtime-ethck", "activities"),
actorAct: data,
isGM: game.user.isGM,
})
);
// attach to sheet
let downtimeHTML = await compileDowntimeTab(CRASH_COMPAT, ethckDowntimeTabHtml, sheet);
downtimeHandler(downtimeHTML, actor, app);
// Set Training Tab as Active
html.find('.tabs .item[data-tab="downtime"]').click((ev) => {
app.activateDowntimeTab = true;
});
// Unset Training Tab as Active
html.find('.tabs .item:not(.tabs .item[data-tab="downtime"])').click((ev) => {
app.activateDowntimeTab = false;
});
}
}
Hooks.on(`renderActorSheet`, (app, html, data) => {
// Borrowed from Crash's 5e-training to allow choice
// of whether to be on same line or not.
let widenSheet = adjustSheetWidth(app);
if (widenSheet) {
let newPos = { width: app.position.width + game.settings.get("downtime-ethck", "extraSheetWidth") };
app.setPosition(newPos);
}
addTrainingTab(app, html, data).then(function () {
if (app.activateDowntimeTab) {
if (game.system.id === "dnd5e") {
app._tabs[0].activate("downtime");
} else if (game.system.id === "pf1") {
app._tabsAlt.activate("downtime");
}
}
});
});
async function outputRolls(actor, activity, event, trainingIdx, res, materials) {
let cmsg = "";
let cmsgResult = "";
let triggeredComp = false;
if (activity.type === "SUCCESS_COUNT") {
let booleanResults = [0, 0];
res.map((pair) => {
if (pair[0] >= pair[1]) {
//Rolled is greater than dc
booleanResults[0] += 1;
} else {
//Rolled is less than dc
booleanResults[1] += 1;
}
});
cmsg = "With " + booleanResults[0] + " successes and " + booleanResults[1] + " failures.";
activity.result?.forEach((result) => {
if (result.min <= booleanResults[0] && result.max >= booleanResults[0]) {
cmsgResult = result.details;
if (result.triggerComplication) triggeredComp = true;
}
});
} else if (activity.type === "ROLL_TOTAL") {
let rollTotal = 0;
if (res.length > 1) {
// Take the first number of each roll and add them all together
// Rolls are [roll, dc]
rollTotal = res.reduce((sum, roll) => sum + roll[0], 0);
} else {
rollTotal = res[0][0];
}
activity.result.forEach((result) => {
if (rollTotal >= parseInt(result.min) && rollTotal <= parseInt(result.max)) {
cmsgResult = result.details;
if (result.triggerComplication) triggeredComp = true;
}
});
} else if (activity.type === "NO_ROLL") {
// Do Nothing
}
// Add in materials, if any.
cmsg = materials ? cmsg + "\n Used " + materials : cmsg;
const cmsgTemplate = await renderTemplate("modules/downtime-ethck/templates/chatMessage.html", {
img: activity.chat_icon,
text: cmsg,
result: cmsgResult,
});
// Determine if we whisper this message, and who to
const cmsgVis = activity.options.rolls_are_private || game.settings.get("core", "rollMode") === "gmroll";
const gmUserIds = game.users.filter((user) => user.role === 4).map((gmUser) => gmUser.id);
// Results message
ChatMessage.create({
user: game.user.id,
speaker: ChatMessage.getSpeaker({ actor }),
content: cmsgTemplate,
flavor: "has completed the downtime activity of " + activity.name,
type: CONST.CHAT_MESSAGE_TYPES.IC,
// Current user + all gm users
whisper: cmsgVis ? [game.user.id, ...gmUserIds] : [],
});
// Test if complications are being used
if (activity.complication !== undefined) {
const num = Math.floor(Math.random() * 100) + 1; // 1-100
if (triggeredComp || num <= parseInt(activity.complication.chance)) {
// Complication has occured
let tableRes = game.tables.get(activity.complication.roll_table);
// Also outputs chat message, YAY!
let opts = {};
if (activity.options.complications_are_private === true) {
opts["rollMode"] = "blindroll";
}
tableRes.draw(opts);
}
}
let timestamp = Date.now();
// About Time Compat TIME
if (game.settings.get("downtime-ethck", "aboutTimeCompat")) {
const aboutTime = game.modules.get("about-time");
if (aboutTime !== undefined && aboutTime.active === true) {
timestamp = game.Gametime.DTNow().longDate().date;
}
} else {
timestamp = new Date(timestamp).toDateString();
}
// Activity log format
const change = {
timestamp: timestamp,
user: game.user.name,
activityName: activity.name,
result: cmsgResult,
timeTaken: activity.options.days_used,
materials: materials,
};
// Handle flags
let flags = actor.getFlag("downtime-ethck", "changes");
if (!flags) flags = [];
flags.push(change);
await actor.unsetFlag("downtime-ethck", "changes");
await actor.setFlag("downtime-ethck", "changes", flags);
}
/*
Just make a quick simple roll (typically hidden) to find the DC, if needed.
return: Roll()
*/
async function rollDC(rollable) {
if (!rollable.dc) return { _total: 0 }; // If no DC, return fake total (it doesn't matter...)
const rdc = new Roll(rollable.dc);
await rdc.toMessage(
{},
{
rollMode: game.settings.get("downtime-ethck", "dcRollMode"),
create: true,
}
);
return rdc;
}
/*
For each given roll, determine the type (check, save, formula, tool, skill)
then construct our roll, roll it, then roll the DC. res is [rollTotal, dcTotal]
and used to calculate the overall results. This is wrapped in a promise mainly for
enabling awaits of Dice So Nice animations (see diceSoNiceRollComplete hook).
return: [rollTotal, dcTotal]
*/
async function rollRollable(actor, activity, rollable) {
return new Promise(async (resolve, reject) => {
let res = [];
let r = null;
let br = game.settings.get("downtime-ethck", "betterRollsCompat") && game.modules.get("betterrolls5e").active;
if (rollable.type === "ABILITY_CHECK") {
// roll an ability check, then dc
// rollAbilityTest assumes that the argument
// is in short form, i.e. "str", "con"
if (br) {
r = await BetterRolls.rollCheck(actor, rollable.roll, {});
} else {
r = await actor.rollAbilityTest(rollable.roll);
}
} else if (rollable.type === "SAVING_THROW") {
// roll a save
// rollAbilitySave has the same assumption
if (br) {
r = await BetterRolls.rollSave(actor, rollable.roll, {});
} else {
if (game.system.id === "dnd5e") {
r = await actor.rollAbilitySave(rollable.roll);
} else if (game.system.id === "pf1") {
r = await actor.rollSavingThrow(rollable.roll);
}
}
} else if (rollable.type === "TOOL_CHECK") {
let actorTool;
// instead of giving the user the 20+ instruments to select
// from, we instead have one option for each group
// When selected, this option will then find every tool of that type
// in the actor's inventory and provide the ability to choose
// between them.
// D&D5e 3.0 changed tool types
// now have Tool, Musical Instrument, Gaming Set, Artisan's Tools
// "", "music", "game", "art" are the ids
let actorTools;
if (game.system.id === "dnd5e") {
let toolType = rollable.roll;
if (toolType === "tool") {
toolType = "";
}
actorTools = actor.items.filter((item) => item.type === "tool" && item.system.type.value === toolType);
}
let toolChoices = {
rollableGroups: actorTools.map((tool) => {
return {
roll: tool.name,
dc: rollable.dc,
group: rollable.roll,
};
}),
};
const choice = await chooseRollDialog(toolChoices, rollable.roll);
actorTool = actorTools[choice[0]];
if (actorTool !== null) {
if (br) {
r = await BetterRolls.rollItem(actorTool).toMessage();
} else {
r = await actorTool.rollToolCheck();
}
} else {
// No tool of that name found.
ui.notifications.error(
"Tool with name " + rollable[0] + " not found. Please ensure the name is correct."
);
res = [];
}
} else if (rollable.type === "CUSTOM") {
// call our helper after separating all terms
r = await formulaRoll(rollable.roll.split(" + "), actor);
} else {
// skills...
// The Skill Custimization 5e module patches actor.rollSkill and makes it NOT be a promise
// so we have to handle it differently.
let skillCust = game.modules.get("skill-customization-5e");
if (skillCust && skillCust.active) {
r = await _skillCustHandler(rollable.roll, actor);
} else {
if (br) {
r = await BetterRolls.rollSkill(actor, rollable.roll, {});
} else {
r = await actor.rollSkill(rollable.roll);
}
}
}
// pf1 return object has an extra layer on top that we don't need
// so we strip it here, but our custom roll does not
if (game.system.id === "pf1" && rollable.type !== "CUSTOM") {
r = r._roll;
}
if (br) {
if (r._total === undefined) {
// For some reason Tool Checks still have this BetterRollsCardBinding
// so we alias some expected properties.
if (r.entries === undefined) {
r.entries = r.BetterRollsCardBinding.roll.entries;
r.params = r.BetterRollsCardBinding.roll.params;
}
const brEntries = r.entries
.find((part) => part.type === "multiroll")
.entries.map((entry) => entry.total);
let result = r.params.rollState === "lowest" ? Math.min(...brEntries) : Math.max(...brEntries);
r._total = result;
}
}
const dc = await rollDC(rollable);
res = [r._total, dc._total];
// For some reason, we don't have a roll or a dc roll...
if (res.length === 0) {
throw "Ethck's Downtime Tracking | Error on rolling.";
reject();
}
resolve(res);
});
}
async function compileDowntimeTab(CRASH_COMPAT, ethckDowntimeTabHtml, sheet) {
return new Promise((resolve, reject) => {
// Add our HTML nicely...
if (CRASH_COMPAT === true && game.settings.get("downtime-ethck", "crashCompat")) {
Hooks.on(`CrashTrainingTabReady`, async (app2, html2, data2) => {
ethckDowntimeTabHtml = ethckDowntimeTabHtml.find(".inventory-list").unwrap();
let crash5eTrainingHtml = html2.find(".crash-training");
crash5eTrainingHtml.find(".ethck-downtime, .items-list section").remove(); // Remove Old
crash5eTrainingHtml.find(".items-list").append(ethckDowntimeTabHtml); // Add New
crash5eTrainingHtml.find(".ethck-downtime").wrap("<section style='margin-top: 5%;'></section>");
const crashExtraDesc = game.settings.get("downtime-ethck", "crashCompatMessage");
if (crashExtraDesc) {
crash5eTrainingHtml
.find(".ethck-downtime")
.parent()
.prepend(
"<label>Above this is Crash's Tracking and Training. Below is Ethck's Downtime</label>"
);
}
resolve(crash5eTrainingHtml);
});
} else {
sheet.append(ethckDowntimeTabHtml);
resolve(sheet);
}
});
}
async function materialsPrompt(activity) {
return new Promise((resolve, reject) => {
if (!("ask_for_materials" in activity.options) || !activity.options.ask_for_materials) {
resolve("");
} else {
new Dialog({
title: `Enter Material Costs`,
content: `<input type="text" placeholder="20gp" id="materials"/>`,
buttons: {
submit: {
icon: "<i class='fas fa-check'></i>",
label: "Submit",
},
},
default: "submit",
close: async (html) => {
resolve(html.find("#materials").val());
},
}).render(true);
}
});
}
// Roll our custom formula
async function formulaRoll(formula, actor) {
return new Promise(async (resolve, reject) => {
// dRoll is the type (adv., norm, disadv.)
// dForm is the HTML of the dialog.
let [dRoll, dForm] = await _formulaDialog(formula);
// get our bonus
let bonus = $(dForm).find('input[name="bonus"]').val();
if (bonus) {
// destructure our array
formula.push(...bonus.split(" + "));
}
// only supports 1dX rolls by making them 2dX
if (game.system.id === "dnd5e") {
if (parseInt(formula[0].split("d")[0]) === 1) {
let mods = "";
if (dRoll !== 0) {
if (dRoll === 1) {
mods += "kh"; // Advantage
} else {
mods += "kl"; // Disadvantage
}
let firstTerms = formula[0].split("d");
let newFirst = "2d" + firstTerms[1];
formula[0] = newFirst + mods;
}
}
} else if (game.system.id === "pf1") {
// Pathfinder has "Normal", "Take 10", and "Take 20"
// The "Take X" sets the result to "X"
// Order is
// 1 = Normal
// 0 = Take 10
// -1 = Take 20
if (dRoll === 0) {
formula = [10];
} else if (dRoll === -1) {
formula = [20];
}
}
let context = rollContext(actor);
let myRoll = new Roll(formula.join(" + "), context);
await myRoll.toMessage();
// we're done!
resolve(myRoll);
});
}
function rollContext(actor) {
// Organize additional properties for use in the context
// This finds the value of hit dice for any class in the actor
let hdVals = [];
if (game.system.id === "dnd5e") {
hdVals = actor.items
.filter((item) => item.type === "class")
.map((hd) => parseInt(hd.system.hitDice.split("d")[1]));
} else if (game.system.id === "pf1") {
hdVals = actor.items.filter((item) => item.type === "class").map((hd) => parseInt(hd.data.hd));
}
// Find the min and the max
// These must be roll values, so add 1d to start.
let hd = {
min: "1d" + Math.min.apply(null, hdVals),
max: "1d" + Math.max.apply(null, hdVals),
};
// return custom context + og context in the same object
return mergeObject({ actor: actor, hd: hd }, actor.getRollData());
}
// slightly reworked _d20RollDialog from the d&d5e system
// formula is an array of parts
async function _formulaDialog(formula) {
let pf = game.system.id === "pf1";
return new Promise(async (resolve, reject) => {
let rollTemplate = await renderTemplate("modules/downtime-ethck/templates/custom-rolls.hbs", {
formula: formula.join(" + "),
rollModes: CONFIG.Dice.rollModes,
system: game.system.id,
});
new Dialog({
title: "Custom Formula Roll",
content: rollTemplate,
buttons: {
advantage: {
label: pf ? "Normal" : "Advantage",
callback: (event) => resolve([1, event]),
},
normal: {
label: pf ? "Take 10" : "Normal",
callback: (event) => resolve([0, event]),
},
disadvantage: {
label: pf ? "Take 20" : "Disadvantage",
callback: (event) => resolve([-1, event]),
},
},
default: "normal",
close: () => resolve(null),
}).render(true);
});
}
async function chooseRollDialog(groups, type = "") {
const dialogContent = await renderTemplate("modules/downtime-ethck/templates/chooseRoll.html", {
groups: groups,
type: type,
});
return new Promise(async (resolve, reject) => {
const dlg = new Dialog({
title: "Choose Roll",
content: dialogContent,
buttons: {
submit: {
icon: '<i class="fas fa-dice"></i>',
label: "Submit",
callback: (html) => {
let chosen = [];
let fields = html.find("form > fieldset > div > input:checked");
if (fields.length === Object.keys(groups).length) {
fields.each((i, check) => {
const c = parseInt($(check).val());
chosen.push(c);
});
resolve(chosen);
} else {
// This seems ugly, but it works.
throw new Error("Ethck's Downtime | Choice prompt not filled.");
}
},
},
},
close: reject,
});
dlg.render(true);
});
}
async function _skillCustHandler(skillAcr, actor) {
let br = game.settings.get("downtime-ethck", "betterRollsCompat") && game.modules.get("betterrolls5e").active;
return new Promise(async (resolve, reject) => {
actor.rollSkill(skillAcr, {}); // call the patched function
// only way to know it's done is by the final chat message, so listen for it
Hooks.on("createChatMessage", async (message, options, id) => {
// discard if not a roll
if (message.isRoll) {
// make sure it's our expected Skill Check
let skiname = CONFIG.DND5E.skills[skillAcr];
if (
(getProperty(message, "data.flavor") &&
getProperty(message, "data.flavor").includes(skiname + " Skill Check")) ||
(br && $(message.content).find("header h3").text() == skiname)
) {
// return the roll
if (br) {
resolve({ _total: parseInt($(message.content).find(".dice-total span").text()) });
} else {
resolve(message._roll);
}
}
}
});
});
}
// Determines whether or not the sheet should have its width adjusted.
// If the setting for extra width is set, and if the sheet is of a type for which
// we have training enabled, this returns true.
function adjustSheetWidth(app) {
let settingEnabled = !!game.settings.get("downtime-ethck", "extraSheetWidth");
let sheetHasTab =
(app.object.type === "npc" && game.settings.get("downtime-ethck", "enableTrainingNpc")) ||
(app.object.type === "character" && game.settings.get("downtime-ethck", "enableTraining"));
let currentWidth = app.position.width;
let defaultWidth = app.options.width;
let sheetIsSmaller = currentWidth < defaultWidth + game.settings.get("downtime-ethck", "extraSheetWidth");
return settingEnabled && sheetHasTab && sheetIsSmaller;
}
async function _downtimeMigrate() {
if (!game.user.isGM) return;
//await game.settings.set("downtime-ethck", "migrated", false);
const NEEDS_MIGRATION_VERSION = "0.7.6";
// Updating from old install -> Migrated
// Fresh install -> No migration CHECK
// Skipped multiple versions and upgrading
// X round of migrations (bound to happen again, right?)
let migrated = game.settings.get("downtime-ethck", "migrated");
// If we have migrated before
if (migrated.status) {
// If our version is newer than the NEEDS_MIGRATION_VERSION
if (isNewerVersion(game.modules.get("downtime-ethck").version, NEEDS_MIGRATION_VERSION)) return;
// If we are on the same version, but have migrated.
if (migrated.version === NEEDS_MIGRATION_VERSION) return;
}
// Save a backup of the old data
ui.notifications.info("Ethck's Downtime | Backing up World Downtimes");
const oldActivities = game.settings.get("downtime-ethck", "activities");
const jsonData = JSON.stringify(oldActivities, null, 2);
saveDataToFile(jsonData, "application/json", "downtime-ethck-world-activities-OLD.json");
ui.notifications.info("Ethck's Downtime | Saved Activity Data.");
ui.notifications.notify("Ethck's 5e Downtime Tracking | Beginning Migration to updated schema.");
// Update Actor Flags
game.actors.forEach(async (actor) => {
// If it doesn't have our flags, idc
let downtimes = actor.getFlag("downtime-ethck", "trainingItems");
if (!downtimes) return;
try {
let changed = false;
[downtimes, changed] = await _updateDowntimes(downtimes);
if (changed) {
let update = {
id: actor.id,
"flags.downtime-ethck": { trainingItems: downtimes },
};
await actor.update(update, { enforceTypes: false });
}
} catch (e) {
console.error(e);
ui.notifications.warning(
"Ethck's Downtime | Something went wrong while migrating. Please open bug report with your backed-up copy of your downtimes."
);
}
});
let worldDowntimes = game.settings.get("downtime-ethck", "activities");
if (worldDowntimes) {
let changed = false;
[worldDowntimes, changed] = await _updateDowntimes(worldDowntimes);
await game.settings.set("downtime-ethck", "activities", worldDowntimes);
}
ui.notifications.notify("Ethck's 5e Downtime Tracking | Migration Complete.");
await game.settings.set("downtime-ethck", "migrated", { status: true, version: NEEDS_MIGRATION_VERSION });
}
export async function _updateDowntimes(downtimes) {
let changed = false;
downtimes.forEach((downtime, i) => {
// Handle old private
// 12/3/2020 v0.3.3
if ("private" in downtime) {
// If previously updated, the "new" value might be here
if (!("actPrivate" in downtime)) {
downtime.actPrivate = downtime.private;
}
delete downtime.private;
changed = true;
}
// Update tables, might not be present?
// 12/3/2020 v0.3.3
if ("complication" in downtime) {
if ("table" in downtime.complication) {
// Old format where table was the string id of the table
if (typeof downtime.complication.table === "string" || downtime.complication.table instanceof String) {
let tid = "";
if (downtime.complication.table !== "") {
let table = game.tables.getName(downtime.complication.table);
if (!table) table = game.tables.get(downtime.complication.table);
tid = table.id;
}
downtime.complication.table = { id: tid };
changed = true;
}
}
}
// 12/23/2020 v0.4.0 transfer to new roll model
if ("rollableGroups" in downtime) {
let newRolls = downtime.rollableGroups.flatMap((group) => {
if (group.rolls.length === 0) return;
let g = group.group || "";
let rolls = group.rolls.map((roll) => {
// new format is an object
if (!Array.isArray(roll)) return;
let typeRoll = determineOldType(roll); // Determine type
let dc = roll[1] || null; // Use old DC, or default to null
let rollVal = roll[0];
// ensure our DC is a number
if (typeof dc === "number") {
dc = dc.toString();
}
if (typeRoll === "CUSTOM") {
rollVal = rollVal.split("Formula: ")[1];
} else if (typeRoll === "SKILL_CHECK") {
let skills = CONFIG.DND5E.skills;
// returns shorthand of skill
rollVal = Object.keys(skills).find((key) => skills[key] === rollVal);
} else if (typeRoll === "TOOL_CHECK") {
} else {
//abiCheck, save
if (typeRoll === "ABILITY_CHECK") {
rollVal = rollVal.split(" Check")[0];
} else {
rollVal = rollVal.split(" Saving Throw")[0];
}
let abilities = CONFIG.DND5E.abilities;
// Returns shorthand of ability
rollVal = Object.keys(abilities)
.find((key) => abilities[key] === rollVal)
.toLowerCase();
}
changed = true;
return { type: typeRoll, roll: rollVal, group: g, dc: dc };
});
return rolls;
});
newRolls = newRolls.filter(Boolean);
downtime.roll = newRolls;
}
// 12/23/2020 v0.4.0 transfer to new result model
if ("results" in downtime) {
// downtime.results[0] old format is an array
// new format is object
if (Array.isArray(downtime.results[0])) {
let res = duplicate(downtime.results);
let newRes = res.map((result) => {
return {
min: result[0], // lower bound
max: result[1], // high bound
details: result[2], // description
triggerComplication: false, // trigger complication if result occurs
};
});
downtime.result = newRes;
changed = true;
}
}
// 12/23/20 v0.4.0 transfer to new activity model
if ("rollableGroups" in downtime && "rollableEvents" in downtime) {
if (downtime?.type === "succFail") {
downtime.type = "SUCCESS_COUNT";
} else if (downtime?.type === "categories") {
downtime.type = "ROLL_TOTAL";
} else {
downtime.type = "NO_ROLL";
}
// Load new model.
downtimes[i] = {
name: downtime.name || "New Downtime Activity",
description: downtime.description || "My awesome downtime activity",
chat_icon: downtime.img || "icons/svg/d20.svg",
sheet_icon: downtime.rollIcon || "icons/svg/d20.svg",
type: downtime.type, //* ACTIVITY_TYPES
roll: downtime.roll, //* ACTIVITY_ROLL_MODEL
result: downtime.result, //* ACTIVITY_RESULT_MODEL
id: downtime.id.toString() || randomID(),
complication: {
chance: downtime.complication.chance || 0,
roll_table: downtime.complication.table.id || "",
},
options: {
rolls_are_private: downtime.actPrivate || false,
complications_are_private: downtime.compPrivate || false,
ask_for_materials: downtime.useMaterials || false,
days_used: downtime.timeTaken || "",
hidden: false,
},
};
changed = true;
}
// 3/20/2021 v0.4.3 added activity visibility
if (!("hidden" in downtime.options)) {
downtime.options.hidden = false;
changed = true;
}
// 3/12/2024 v0.7.7 converted tools to use D&D5e tool types
if (game.system.id === "dnd5e") {
if (downtime.roll.some((r) => r.type === "TOOL_CHECK")) {
downtime.roll.forEach((r, i) => {
if (r.type !== "TOOL_CHECK") return;
if (r.roll === "Kit") {
downtime.roll[i].roll = "tool";
} else if (r.roll === "Instrument") {
downtime.roll[i].roll = "music";
} else if (r.roll === "Set") {
downtime.roll[i].roll = "game";
} else if (r.roll === "Supplies") {
downtime.roll[i].roll = "art";
} else if (r.roll === "Tool") {
downtime.roll[i].roll = "tool";
} else if (r.roll === "Utensil") {
downtime.roll[i].roll = "art";
}
});
}
}
});
return [downtimes, changed];
}
function determineOldType(roll) {