-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
159 lines (136 loc) · 6.03 KB
/
main.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
// Import JavaScript modules
import { registerSettings } from "./module/settings.js";
import { preloadTemplates } from "./module/templates.js";
import {
GroupSkillCheck,
GroupAbilityCheck
} from "./module/apps.js";
import { avgD20roll, debug } from "./module/lib.js";
/* ------------------------------------ */
/* Initialize module */
/* ------------------------------------ */
Hooks.once('init', async function () {
debug.log(true, 'Initializing');
// Test whether game system is supported by the module
CONFIG._grouproll_systemSupported = game.system.id === "dnd5e";
// Assign custom classes and constants
CONFIG._grouproll_module_advantageStatus = {
"-1": {
"label": "Disadvantage",
"icon": '<i class="fas fa-minus"></i>'
},
"0": {
"label": "Normal",
"icon": '<i class="far fa-circle"></i>'
},
"1": {
"label": "Advantage",
"icon": '<i class="fas fa-plus"></i>'
}
};
// Register custom module settings
registerSettings();
// Preload Handlebars templates
await preloadTemplates();
});
/* ------------------------------------ */
/* Setup module */
/* ------------------------------------ */
Hooks.once('setup', function () {
debug.log(true, 'Setup');
});
/* ------------------------------------ */
/* When ready */
/* ------------------------------------ */
Hooks.once('ready', function () {
debug.log(true, 'Ready');
// Enable module if game system is supported
if (CONFIG._grouproll_systemSupported) {
// Flag whether it is safe to insert code to average 1d20 rolls
// - Only use with D&D 5e system
// - Do not use if BetterRolls5e module is enabled
const averagingOK = game.system.id === "dnd5e" && !game.modules.get("betterrolls5e")?.active;
// Only insert averaging code if roll averaging is enabled in grouproll module
if (averagingOK && game.settings.get("grouproll", "averageRolls") !== "n") {
debug.log(false, "Averaging enabled for d20 rolls");
// Regex for matching roll types
CONFIG._grouproll_module_matchrolls = new RegExp(
"(" + game.i18n.localize("GRTYPE.Death") + ")|(" +
game.i18n.localize("GRTYPE.Check") + "|" +
game.i18n.localize("GRTYPE.Save") + "|" +
game.i18n.localize("GRTYPE.Skill") + "|" +
game.i18n.localize("GRTYPE.Tool") + ")|(" +
game.i18n.localize("GRTYPE.Attack") + ")");
// Insert custom d20 roll averaging function into dnd5e system
CONFIG.Dice.D20Roll.prototype.grouprollAverage = avgD20roll;
CONFIG.Dice.D20Roll.prototype.grouprollEvaluate = Roll.prototype.evaluate;
CONFIG.Dice.D20Roll.prototype.evaluate = async function ({ minimize = false, maximize = false, allowStrings = false, allowInteractive = true, ...options} = {}) {
await this.grouprollEvaluate({ minimize, maximize, allowStrings, allowInteractive });
// Do not use averaging for manual rolls, and only for normal 1d20 rolls
if (!this.terms[0].options.isManualRoll && this.options.advantageMode === 0 && this.terms[0].faces === 20 && this.terms[0].number === 1) {
// Use the flavor to identify the roll type
const rollType = this.options.flavor.match(CONFIG._grouproll_module_matchrolls);
// Debug detection of roll types when adding new languages
if (debug.enabled) {
if (rollType[1]) debug.log(true, rollType[1] + " = Death Saving Throw");
else if (rollType[2]) debug.log(true, rollType[2] + " = Check or Save");
else if (rollType[3]) debug.log(true, rollType[3] + " = Attack Roll");
}
// Average normal d20 rolls only for selected roll types
switch (game.settings.get("grouproll", "averageRolls")) {
case "c":
if (rollType[2]) await this.grouprollAverage(this);
break;
case "a":
if (rollType[2] || rollType[3]) await this.grouprollAverage(this);
break;
default:
};
};
// Return adjusted roll
return this;
};
};
// Copy name to label; fix for DFreds Convenient Effects
CONFIG.statusEffects = CONFIG.statusEffects.map( x => {
x.label = x.label || x.name;
return x;
});
} else {
debug.log(true, "Current game system not supported")
};
});
/* ------------------------------------ */
/* Additional Hooks */
/* ------------------------------------ */
Hooks.on('getSceneControlButtons', controls => {
// Enable module if game system is supported
if (CONFIG._grouproll_systemSupported) {
// Add Group Roll buttons to UI
let tokenButton = controls.find(b => b.name == "token");
if (tokenButton) {
tokenButton.tools.push(
{
name: "skill",
title: "Group Skill Check",
icon: "fas fa-user-check",
visible: game.user.isGM,
onClick: () => {
ui.controls.initialize({tool: "select"});
return new GroupSkillCheck().render(true);
}
},
{
name: "ability",
title: "Group Saving Throw",
icon: "fas fa-user-shield",
visible: game.user.isGM,
onClick: () => {
ui.controls.initialize({tool: "select"});
return new GroupAbilityCheck().render(true);
}
}
);
};
};
});