forked from cyruzzo/AboveVTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombatTracker.js
360 lines (296 loc) · 9.65 KB
/
CombatTracker.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
function init_combat_tracker(){
window.ROUND_NUMBER =1;
ct=$("<div id='combat_tracker'/>");
ct.css("height","20px"); // IMPORTANT
toggle=$("<button id='combat_button' class='hideable'><u>C</u>OMBAT</button>");
toggle.click(function(){
if($("#combat_tracker_inside").is(":visible")){
$("#combat_tracker_inside").attr('style', 'display: none;');
$("#combat_tracker").css("height","20px"); // IMPORTANT
}
else{
$("#combat_tracker_inside").attr('style', 'display: block;');
$("#combat_tracker").css("height","450px"); // IMPORTANT
}
});
ct.append(toggle);
ct_inside=$("<div id='combat_tracker_inside'/>");
ct_inside.hide();
ct.append(ct_inside);
ct_area=$("<table id='combat_area'/>");
const ct_list_wrapper = $(`<div class="tracker-list"></div>`);
ct_list_wrapper.append(ct_area);
ct_inside.append(ct_list_wrapper);
rn = $(`<div id='round_number_label'><strong>ROUND:</strong><input class="roundNum" style="font-size: 11px; width: 42px; appearance: none;" type='number' id='round_number' value=${window.ROUND_NUMBER}></div>`)
reset_rounds=$("<button style='font-size: 10px;'>RESET</button>");
reset_rounds.click(function (){
window.ROUND_NUMBER = 1;
document.getElementById('round_number').value = window.ROUND_NUMBER;
$("#combat_area tr").first().attr('data-current','1');
next.removeAttr('data-current');
next.css('background','');
ct_persist();
});
rn.find("#round_number").change(function (data) {
if( !isNaN(data.currentTarget.value)){
window.ROUND_NUMBER = Math.round(data.currentTarget.value);
ct_persist();
}
document.getElementById('round_number').value = window.ROUND_NUMBER;
});
ct_inside.append(rn);
if(window.DM)
{
rn.append(reset_rounds);
}
else
{
rn.find("#round_number").prop("readonly", true);
}
buttons=$("<div id='combat_footer'/>");
reroll=$("<button>REROLL</button>");
reroll.click(function(){
$("#combat_area tr[data-target]").each(function(){
$(this).removeAttr('data-current');
});
$("#combat_area tr[data-monster]").each(function(idx){
let element=$(this);
window.StatHandler.rollInit($(this).attr('data-monster'),function(value){
element.find(".init").val(value);
ct_reorder(false);
});
setTimeout(ct_persist,5000); // quick hack to save and resync only one time
});
$("#combat_area tr").first().attr('data-current','1');
});
clear=$("<button>CLEAR</button>");
clear.click(function(){
$("#combat_area").empty();
window.ROUND_NUMBER = 1;
document.getElementById('round_number').value = window.ROUND_NUMBER;
ct_persist();
});
next=$("<button id='combat_next_button'><u>N</u>EXT</button>");
next.click(function(){
if($("#combat_area tr").length==0)
return;
current=$("#combat_area tr[data-current=1]");
if(current.length==0){
console.log('nessuno selezionato');
$("#combat_area tr").first().attr('data-current','1');
}
else{
current.removeAttr('data-current');
current.css('background','');
next=current.next();
if(next.length==0){
window.ROUND_NUMBER++;
document.getElementById('round_number').value = window.ROUND_NUMBER;
next=$("#combat_area tr").first()
}
next.attr('data-current','1');
}
ct_persist();
//var target=$("#combat_area tr[data-current=1]").attr('data-target');
});
roll=$("<button>ROLLINIT</button>");
roll.click(function(){
$("#combat_area tr[data-monster]").each(function(idx){
let element=$(this);
if(element.find(".init").val()!=0) // DON'T ROLL AGAIN'
return;
window.StatHandler.rollInit($(this).attr('data-monster'),function(value){
element.find(".init").val(value);
ct_reorder(false);
});
setTimeout(ct_persist,5000); // quick hack to save and resync only one time
});
});
if(window.DM){
buttons.append(roll);
buttons.append(clear);
buttons.append(reroll);
buttons.append(next);
buttons.css('font-size','10px');
ct_inside.append(buttons);
}
if(window.DM) {
ct.addClass('tracker-dm');
} else {
ct.addClass('tracker-player');
}
$("#site").append(ct);
}
function ct_reorder(persist=true) {
var items = $("#combat_area tbody").children().sort(
function(a, b) {
var vA = parseInt($(".init", a).val());
var vB = parseInt($(".init", b).val());
return (vA > vB) ? -1 : (vA < vB) ? 1 : 0;
});
$("#combat_area tbody").append(items);
if(persist)
ct_persist();
}
function ct_add_token(token,persist=true,disablerolling=false){
// TODO: check if the token is already in the tracker..
token.options.combat = true;
//token.sync();
if (token.persist != null) token.persist();
selector="#combat_area tr[data-target='"+token.options.id+"']";
if($(selector).length>0)
return;
entry=$("<tr/>");
entry.css("height","30px");
entry.attr("data-target",token.options.id);
entry.addClass("CTToken");
if ((token.options.name) && (window.DM || !token.options.monster || token.options.revealname)) {
entry.attr("data-name", token.options.name);
entry.addClass("hasTooltip");
}
if(token.options.monster > 0)
entry.attr('data-monster',token.options.monster);
img=$("<img width=35 height=35 class='Avatar_AvatarPortrait__2dP8u'>");
img.attr('src',token.options.imgsrc);
img.css('border','3px solid '+token.options.color);
entry.append($("<td/>").append(img));
let init=$("<input class='init' maxlength=2 style='font-size:12px;'>");
init.css('width','20px');
init.css('-webkit-appearance','none');
if(window.DM){
init.val(0);
init.change(ct_reorder);
}
else{
init.attr("disabled","disabled");
}
entry.append($("<td/>").append(init));
// auto roll initiative for monsters
if(window.DM && (token.options.monster > 0) && (!disablerolling)){
window.StatHandler.rollInit(token.options.monster,function(value){
init.val(value);
setTimeout(ct_reorder,1000);
});
}
hp=$("<div class='hp'/>");
hp.text(token.options.hp);
hp.css('font-size','11px');
//hp.css('width','20px');
if(window.DM || !(token.options.monster > 0) )
entry.append($("<td/>").append(hp));
else
entry.append($("<td/>"))
max_hp=$("<div/>");
max_hp.text("/"+token.options.max_hp);
max_hp.css('font-size','11px');
//max_hp.css('width','20px');
if(window.DM || !(token.options.monster > 0) )
entry.append($("<td/>").append(max_hp));
else
entry.append($("<td/>"));
var buttons=$("<td/>");
find=$("<button style='font-size:10px;'>FIND</button>");
find.click(function(){
var target=$(this).parent().parent().attr('data-target');
if(target in window.TOKEN_OBJECTS){
window.TOKEN_OBJECTS[target].highlight();
}
});
buttons.append(find);
del=$("<button style='font-size:10px;'>DEL</button>");
del.click(
function(){
if($(this).parent().parent().attr("data-current")=="1"){
$("#combat_next_button").click();
}
$(this).parent().parent().remove();
ct_persist();
}
);
if(window.DM)
buttons.append(del);
if(token.options.monster > 0){
stat=$("<button style='font-size:10px;'>STAT</button>");
stat.click(function(){
iframe_id="#iframe-monster-"+token.options.monster;
if($(iframe_id).is(":visible"))
$(iframe_id).hide();
else{
$(".monster_frame").hide();
load_monster_stat(token.options.monster, token.options.id);
}
});
if(window.DM)
buttons.append(stat);
}
else{
stat=$("<button style='font-size:10px;'>STAT</button>");
stat.click(function(){
open_player_sheet(token.options.id);
});
if(window.DM)
buttons.append(stat);
}
entry.append(buttons);
$("#combat_area").append(entry);
$("#combat_area td").css("vertical-align","middle");
if(persist){
ct_persist();
}
}
function ct_persist(){
var data= [];
$('#combat_area tr').each( function () {
data.push( {
'data-target': $(this).attr("data-target"),
'init': $(this).find(".init").val(),
'current': ($(this).attr("data-current")=="1")
});
});
data.push({'data-target': 'round',
'round_number':window.ROUND_NUMBER});
var itemkey="CombatTracker"+$("#message-broker-client").attr("data-gameId");
localStorage.setItem(itemkey,JSON.stringify(data));
window.MB.sendMessage("custom/myVTT/CT",data);
}
function ct_load(data=null){
if(data==null){
var itemkey="CombatTracker"+$("#message-broker-client").attr("data-gameId");
data=$.parseJSON(localStorage.getItem(itemkey));
}
if(data){
for(i=0;i<data.length;i++){
if( (data[i]['data-target'] in window.TOKEN_OBJECTS)){
ct_add_token(window.TOKEN_OBJECTS[data[i]['data-target']] ,false,true);
$("#combat_area tr[data-target='"+data[i]['data-target']+"']").find(".init").val(data[i]['init']);
if(data[i]['current']){
$("#combat_area tr[data-target='"+data[i]['data-target']+"']").attr("data-current","1");
}
}
else if (data[i]['data-target'] === 'round')
{
window.ROUND_NUMBER = data[i]['round_number'];
document.getElementById('round_number').value = window.ROUND_NUMBER;
}
}
}
if(window.DM)
ct_persist();
}
function ct_remove_token(token,persist=true) {
if (persist == true) {
token.options.combat = false;
token.sync();
if (token.persist != null) token.persist();
}
let id = token.options.id;
if ($("#combat_area tr[data-target='" + id + "']").length > 0) {
if ($("#combat_area tr[data-target='" + id + "']").attr('data-current') == "1") {
$("#combat_next_button").click();
}
$("#combat_area tr[data-target='" + id + "']").remove(); // delete token from the combat tracker if it's there
}
if (persist) {
ct_persist();
}
}