forked from tonifisler/foundry-group-initiative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup-initiative.js
260 lines (225 loc) · 7.52 KB
/
group-initiative.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
// @ts-check
const MODULE_NAME = 'group-initiative';
const SETTING_NAME = 'rollGroupInitiative';
// Default setting
let CONFIG_GROUPINITIATIVE = false;
let CONFIG_SKIPGROUPED = false;
/**
* Shortcut to localize.
*
* @param key
* @returns {string}
*/
const i18n = key => game.i18n.localize(key);
/**
* Sets the settings or returns the current value.
*
* @param key
* @param setting
* @returns {*}
*/
const initSetting = (key, setting) => {
let config;
try {
config = game.settings.get(MODULE_NAME, key);
} catch (e) {
if (e.message !== 'This is not a registered game setting') {
throw e;
}
game.settings.register(MODULE_NAME, key, setting);
config = game.settings.get(MODULE_NAME, key);
}
return config;
};
/**
* Override the RollNPC method.
*
* @returns {Promise<void>}
*/
async function rollNPC() {
const npcs = this.turns.filter(
t => (!t.actor || !t.players.length) && !t.initiative
);
if (!npcs.length) return;
await rollGroupInitiative.call(this, npcs);
}
/**
* Override the RollAll method.
*
* @returns {Promise<void>}
*/
async function rollAll() {
const unrolled = this.turns.filter(t => !t.initiative);
if (!unrolled.length) return;
await rollGroupInitiative.call(this, unrolled);
}
/**
* Roll the group initiative
*/
async function rollGroupInitiative(creatures) {
console.log('group-initiative | Rolling initiative!');
// Split the combatants in groups based on actor id.
const groups = creatures.reduce(
(g, combatant) => ({
...g,
[combatant.actor.id]: (g[combatant.actor.id] || []).concat(combatant.data._id),
}),
{}
);
// Get first Combatant id for each group
const ids = Object.keys(groups).map(key => groups[key][0]);
const messageOptions = {
flavor: i18n('COMBAT.groupRollsInitiative'),
};
// Roll initiative for the group leaders only.
await this.rollInitiative(ids, {messageOptions});
// Prepare the others in the group.
const updates = creatures.reduce((updates, {id, initiative, actor}) => {
const group = groups[actor.data._id];
if (group.length <= 1 || initiative) return updates;
// Get initiative from leader of group.
initiative = this.getCombatant(group[0]).initiative;
updates.push({_id: id, initiative});
return updates;
}, []);
// Batch update all other combatants.
this.updateEmbeddedEntity('Combatant', updates);
}
/**
* Add the setting option in the combat tracker config.
*/
Hooks.on('renderCombatTrackerConfig', async (ctc, html) => {
const data = {
rollGroupInitiative: CONFIG_GROUPINITIATIVE,
skipGrouped: CONFIG_SKIPGROUPED,
};
const newOption = await renderTemplate(
'modules/group-initiative/templates/combat-config.html',
data
);
html.css({height: 'auto'}).find('button[name=submit]').before(newOption);
});
/**
* Save the setting when closing the combat tracker config.
*/
Hooks.on('closeCombatTrackerConfig', async ({form}) => {
CONFIG_GROUPINITIATIVE = form.querySelector('#rollGroupInitiative').checked;
CONFIG_SKIPGROUPED = form.querySelector('#skipGrouped').checked;
// Save the setting when closing the combat tracker setting.
await game.settings.set(MODULE_NAME, SETTING_NAME, CONFIG_GROUPINITIATIVE);
await game.settings.set(MODULE_NAME, "skipGrouped", CONFIG_SKIPGROUPED);
});
/**
* Override the roll methods from combat tracker.
*/
Hooks.on('renderCombatTracker', ( app, html, options ) => {
let combat = options.combat ;
if (!combat) return;
if (!combat.originalRollNPC) {
combat.originalRollNPC = combat.rollNPC;
}
if (!combat.originalRollAll) {
combat.originalRollAll = combat.rollAll;
}
if (CONFIG_GROUPINITIATIVE) {
combat.rollNPC = rollNPC.bind(combat);
combat.rollAll = rollAll.bind(combat);
} else {
// Reset the methods.
if (combat.originalRollNPC) {
combat.rollNPC = combat.originalRollNPC;
}
if (combat.originalRollAll) {
combat.rollAll = combat.originalRollAll;
}
}
});
/**
* Init the settings.
*/
Hooks.once('init', () => {
CONFIG_GROUPINITIATIVE = initSetting(SETTING_NAME, {
name: i18n('COMBAT.RollGroupInitiative'),
hint: i18n('COMBAT.RollGroupInitiativeHint'),
default: CONFIG_GROUPINITIATIVE,
type: Boolean,
scope: 'world',
config: false,
});
CONFIG_SKIPGROUPED = initSetting("skipGrouped", {
name: "Skip grouped combatants", // LOCALIZE
hint: "Skip combatants following the first in a group.", // LOCALIZE
default: CONFIG_SKIPGROUPED,
type: Boolean,
scope: 'world',
config: false,
})
});
Hooks.on("renderCombatTracker", async (app, html, data) => {
// if not using grouped initiative, return
if (!CONFIG_GROUPINITIATIVE) return;
const combat = app.combat;
// if no combat, return
if (!combat) return;
let combatants = combat.turns;
// create initiative groups; array of arrays
let groups = [];
let groupsIndex = 0;
groups[groupsIndex] = [combatants[0]];
for (let i = 1; i < combatants.length; i++) {
// if current combatant has a different actor than previous combatant, create a new initiative group
if (combatants[i].actor.id !== combatants[i - 1].actor.id) {
groupsIndex++;
groups[groupsIndex] = [];
}
groups[groupsIndex].push(combatants[i]);
}
// if only 1 initiative group, return
if (groups.length < 2) return;
// filter out initiative groups with only a single combatant (no need to collapse)
groups = groups.filter(g => g.length > 1);
// for each group, use the first combatant as a "header"
const headerCombatants = groups.map(g => g[0]);
// get the list item HTML element corresponding to the header combatants
const lis = html.find("li");
const headerCombatantLIs = headerCombatants.map(c => {
for (let li of lis) if ($(li).data("combatantId") === c._id) return li;
});
// create initiative groups of list item elements
const initiativeGroups = []
for (let i = 0; i < headerCombatantLIs.length; i++) {
const current = headerCombatantLIs[i];
const currentGroup = [current];
$(current).nextAll().splice(0, groups[i].length - 1).forEach(e => currentGroup.push(e));
initiativeGroups.push(currentGroup);
}
// for each list item element initiative group, wrap group in a HTML details element to collapse them
for (let i = 0; i < initiativeGroups.length; i++) {
$(initiativeGroups[i]).wrapAll(`<details id="${headerCombatants[i]._id}" />`);
$(initiativeGroups[i]).css("padding-left", "30px");
// create a summary element for each details element, based on header combatant
const headerCombatant = headerCombatants[i];
const currentGroup = $(headerCombatantLIs[i]).prop("parentElement");
const headerHTML = `
<summary>
<li class="combatant actor directory-item flexrow">
<img class="token-image" src=${headerCombatant.img} title=${headerCombatant.name}>
<div class="token-name flexcol">
<h4>${headerCombatant.name}</h4>
</div>
<div class="token-initiative">
<span class="initiative">${headerCombatant.initiative || ""}</span>
</div>
</li>
</summary>
`;
$(headerHTML).prependTo(currentGroup)
}
// if current combatant is in a collapsed group, open the group
const activeCombatantLI = html.find("li.active");
const details = $(activeCombatantLI).prop("parentElement");
if ($(details).prop("nodeName") === "DETAILS") {
if (CONFIG_SKIPGROUPED && $(activeCombatantLI).prev().prop("nodeName") === "LI") return game.combat.nextTurn();
$(details).prop("open", true);
}
});