-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus-effect-timer.js
126 lines (111 loc) · 3.61 KB
/
status-effect-timer.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
const applyConcToCaster = async function(caster, duration) {
let data = {
"flags": {
"core": {
"statusId": "concentrating"
}
},
"changes": [],
"duration": duration,
"icon": "modules/simple-5econs/icons/concentrating.svg",
"label": "Concentrating"
};
await caster.createEmbeddedEntity("ActiveEffect", data);
}
const setTimerX = async function(actor, effectTitle, relativeTo, concToCaster) {
let effect = await actor.effects.find(ef => ef.data.label === effectTitle);
if (!effect) {
ui.notifications.error("Something went wrong! Effect was not found on the token.");
return;
}
let currentRound = game.combat.current.round;
let currentTurn = game.combat.current.turn;
let duration = {
rounds: 10,
turns: 1,
startTurn: relativeTo,
startRound : relativeTo <= currentTurn ? currentRound : currentRound - 1
};
effect.update({duration : duration});
if (concToCaster){
applyConcToCaster(await game.combat.turns[relativeTo].actor, duration);
};
}
const setTimerEndOfTurn = async function(actor, effectTitle, relativeTo, concToCaster) {
let effect = await actor.effects.find(ef => ef.data.label === effectTitle);
if (!effect) {
ui.notifications.error("Something went wrong! Effect was not found on the token.");
return;
}
let currentRound = game.combat.current.round;
let currentTurn = game.combat.current.turn;
let duration = {
turns: game.combat.turns.length+1,
rounds: 0,
startTurn: relativeTo,
startRound : relativeTo <= currentTurn ? currentRound : currentRound - 1
};
effect.update({duration : duration});
if (concToCaster){
applyConcToCaster(await game.combat.turns[relativeTo].actor, duration);
};
}
const popDialog = function(event, actor){
if (!game.combat) {
ui.notifications.warn("Status effect timer module can only be used in combat. This is a subject to change.");
return;
}
let fighterOptions = game.combat.turns
.map((fighter, turn) => `<option value="${turn}"><img src=${fighter.img}> ${fighter.name} </option>`)
.join(``);
$(function() {
var defaultSelected = game.combat.turn;
$("#relativeToSelector").val(defaultSelected);
});
let cont = `Relative to: <select name="relativeToSelector" id="relativeToSelector">${fighterOptions}</select>
</br><input type="checkbox" name="applyConc" id="applyConc"> apply to ⇑them⇑ a concentration effect for the same duration.`;
new Dialog({
title: "Select duration",
content: cont,
buttons: {
a: {
label: "End of next turn",
callback: (html) => {
setTimerEndOfTurn(actor, event.currentTarget.title, document.getElementById("relativeToSelector").value, document.getElementById("applyConc").checked);
},
},
b: {
label: "10 rounds",
callback: (html) => {
setTimerX(actor, event.currentTarget.title, document.getElementById("relativeToSelector").value, document.getElementById("applyConc").checked);
},
}
},
default: "a",
}).render(true)
}
Hooks.on("ready", function() {
let originalToggle = TokenHUD.prototype._onToggleEffect;
TokenHUD.prototype._onToggleEffect = (function(event, overlay) {
originalToggle.bind(this);
if (event.shiftKey) {
popDialog(event,this.object.actor);
}
return originalToggle.bind(this)(event, overlay);
});
});
const removeFinishedEffects = async function() {
game.combat.turns.forEach(
fighter => fighter.actor.effects
.filter(e => e.duration.remaining != null && e.duration.remaining <= 0)
.forEach(async (e) => {e.delete()})
);
}
let lastTurnProcessed = -1;
Hooks.on("getCombatTrackerEntryContext", () => {
if (lastTurnProcessed != game.combat.turn) {
lastTurnProcessed = game.combat.turn;
removeFinishedEffects();
}
}
);