-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
380 lines (323 loc) · 11.7 KB
/
script.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
$('.stat').bind('input', function()
{
var inputName = $(this).attr('name')
var mod = parseInt($(this).val()) - 10
if (mod % 2 == 0)
mod = mod / 2
else
mod = (mod - 1) / 2
if (isNaN(mod))
mod = ""
else if (mod >= 0)
mod = "+" + mod
var scoreName = inputName.slice(0, inputName.indexOf("score"))
var modName = scoreName + "mod"
$("[name='" + modName + "']").val(mod)
})
$('.statmod').bind('change', function()
{
var name = $(this).attr('name')
name = "uses" + name.slice(0, name.indexOf('mod'))
})
$("[name='classlevel']").bind('input', function()
{
var classes = $(this).val()
var r = new RegExp(/\d+/g)
var total = 0
var result
while ((result = r.exec(classes)) != null)
{
var lvl = parseInt(result)
if (!isNaN(lvl))
total += lvl
}
var prof = 2
if (total > 0)
{
total -= 1
prof += Math.trunc(total/4)
prof = "+" + prof
}
else
{
prof = ""
}
$("[name='proficiencybonus']").val(prof)
})
function totalhd_clicked()
{
$("[name='remaininghd']").val($("[name='totalhd']").val())
}
// Row counts used for saving/loading characters
var rows_attacks = 2;
var rows_inventory = 2;
var rows_attunements = 3;
var rows_spells = 2;
function save_character()
{
console.log("Saving character...")
var filename = ".dnd";
if (document.getElementById('charname').value == "") {
filename = "CharacterSheet" + filename;
} else {
filename = document.getElementById('charname').value + filename;
}
// Prepare form data for JSON format
const formId = "charsheet";
var url = location.href;
const formIdentifier = `${url} ${formId}`;
let form = document.querySelector(`#${formId}`);
let formElements = form.elements;
let data = { [formIdentifier]: {} };
for (const element of formElements) {
if (element.name.length > 0) {
if (element.type == 'checkbox') {
var checked = ($("[name='" + element.name + "']").prop("checked") ? 'checked' : 'unchecked');
data[formIdentifier][element.name] = checked;
} else {
data[formIdentifier][element.name] = element.value;
}
}
}
data = JSON.stringify(data[formIdentifier], null, 2)
type = 'application/json'
// Save JSON to file
var file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
// Protective autosave feature
window.onbeforeunload = function(){
if ($("[name='autosave']").prop("checked") == true) {
save_character();
}
}
// Functions for reading character from disk
function load_character(e) {
// Autosave character
if ($("[name='autosave']").prop("checked") == true) {
save_character();
}
// Load character
var file = e.target.files[0];
if (!file) {
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
// Set size of dynamic tables
var savedData = JSON.parse(contents);
while (rows_attacks > parseInt(savedData.rows_attacks)) {
remove_last_row('attacktable');
}
while (rows_attacks < parseInt(savedData.rows_attacks)) {
add_attack();
}
while (rows_attunements > parseInt(savedData.rows_attunements)) {
remove_last_row('attunementtable');
}
while (rows_attunements < parseInt(savedData.rows_attunements)) {
add_attunement();
}
while (rows_inventory > parseInt(savedData.rows_inventory)) {
remove_last_row('inventorytable');
}
while (rows_inventory < parseInt(savedData.rows_inventory)) {
add_inventory();
}
while (rows_spells > parseInt(savedData.rows_spells)) {
remove_last_row('spelltable');
}
while (rows_spells < parseInt(savedData.rows_spells)) {
add_spell();
}
// Prepare form data for JSON format
const formId = "charsheet";
var url = location.href;
const formIdentifier = `${url} ${formId}`;
let form = document.querySelector(`#${formId}`);
let formElements = form.elements;
// Display file content
savedData = JSON.parse(contents); // get and parse the saved data from localStorage
for (const element of formElements) {
if (element.name in savedData) {
if (element.type == 'checkbox') {
var checked = (savedData[element.name] == 'checked');
$("[name='" + element.name + "']").prop("checked", checked)
} else {
element.value = savedData[element.name];
}
}
}
};
reader.readAsText(file);
}
document.getElementById('buttonload').addEventListener('change', load_character, false);
function long_rest()
{
console.log("Taking long rest...")
/*
* To do on a long rest:
*
* x Reset hit points to max HP
* x Reset hit dice to max hit dice
* x Reset all spell slots available to max
* x Reset all death saves
* x Remind player to reset temp HP and limited use features and items
*
*/
$("[name='currenthp']").val($("[name='maxhp']").val())
$("[name='remaininghd']").val($("[name='totalhd']").val())
$("[name='spellslots1']").val($("[name='spellslotsmax1']").val())
$("[name='spellslots2']").val($("[name='spellslotsmax2']").val())
$("[name='spellslots3']").val($("[name='spellslotsmax3']").val())
$("[name='spellslots4']").val($("[name='spellslotsmax4']").val())
$("[name='spellslots5']").val($("[name='spellslotsmax5']").val())
$("[name='spellslots6']").val($("[name='spellslotsmax6']").val())
$("[name='spellslots7']").val($("[name='spellslotsmax7']").val())
$("[name='spellslots8']").val($("[name='spellslotsmax8']").val())
$("[name='spellslots9']").val($("[name='spellslotsmax9']").val())
$("[name='pactslots1']").val($("[name='pactslotsmax1']").val())
$("[name='deathsuccess1']").prop("checked", false);
$("[name='deathsuccess2']").prop("checked", false);
$("[name='deathsuccess3']").prop("checked", false);
$("[name='deathfail1']").prop("checked", false);
$("[name='deathfail2']").prop("checked", false);
$("[name='deathfail3']").prop("checked", false);
alert("Hit points, hit dice, and spell slots have been refreshed.\n\nPlease remember to reset Limited Use abilities, temporary hit points, and other effects as needed.")
}
function add_attack()
{
var tableRef = document.getElementById('attacktable')
var row = tableRef.insertRow(tableRef.rows.length)
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
var cell3 = row.insertCell(3);
cell0.innerHTML = "<td><input name='atkname" + rows_attacks + "' type='text'/></td>";
cell1.innerHTML = "<td><input name='atkbonus" + rows_attacks + "' type='text'/></td>";
cell2.innerHTML = "<td><input name='atkdamage" + rows_attacks + "' type='text'/></td>";
cell3.innerHTML = "<td colspan='2'><input name='atknotes" + rows_attacks + "' type='text'/></td>";
rows_attacks += 1;
$("[name='rows_attacks']").val(rows_attacks);
}
function add_spell()
{
var tableRef = document.getElementById('spelltable')
var row = tableRef.insertRow(tableRef.rows.length)
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
var cell3 = row.insertCell(3);
var cell4 = row.insertCell(4);
var cell5 = row.insertCell(5);
var cell6 = row.insertCell(6);
var cell7 = row.insertCell(7);
var cell8 = row.insertCell(8);
var cell9 = row.insertCell(9);
cell0.innerHTML = "<td><input name='spellprep" + rows_spells + "' type='checkbox' /></td>";
cell1.innerHTML = "<td><input name='spellname" + rows_spells + "' type='text' /></td>";
cell2.innerHTML = "<td><input name='spelllevel" + rows_spells + "' type='text' /></td>";
cell3.innerHTML = "<td><input name='spellsource" + rows_spells + "' type='text' /></td>";
cell4.innerHTML = "<td><input name='spellattacksave" + rows_spells + "' type='text' /></td>";
cell5.innerHTML = "<td><input name='spelltime" + rows_spells + "' type='text' /></td>";
cell6.innerHTML = "<td><input name='spellrange" + rows_spells + "' type='text' /></td>";
cell7.innerHTML = "<td><input name='spellduration" + rows_spells + "' type='text' /></td>";
cell8.innerHTML = "<td><input name='spellcomponents" + rows_spells + "' type='text' /></td>";
cell9.innerHTML = "<td><input name='spellnotes" + rows_spells + "' type='text' /></td>";
rows_spells += 1;
$("[name='rows_spells']").val(rows_spells);
}
function add_inventory()
{
var tableRef = document.getElementById('inventorytable')
var row = tableRef.insertRow(tableRef.rows.length)
var cell0 = row.insertCell(0);
var cell1 = row.insertCell(1);
var cell2 = row.insertCell(2);
var cell3 = row.insertCell(3);
var cell4 = row.insertCell(4);
var cell5 = row.insertCell(5);
cell0.innerHTML = "<td><input name='itemequipped" + rows_inventory + "' type='checkbox' /></td>";
cell1.innerHTML = "<td><input name='itemname" + rows_inventory + "' type='text' /></td>";
cell2.innerHTML = "<td><input name='itemcount" + rows_inventory + "' type='text' onchange='calc_carry_weight()' /></td>";
cell3.innerHTML = "<td><input name='itemweight" + rows_inventory + "' type='text' onchange='calc_carry_weight()' /></td>";
cell4.innerHTML = "<td><input name='itemvalue" + rows_inventory + "' type='text' /></td>";
cell5.innerHTML = "<td><input name='itemnotes" + rows_inventory + "' type='text' /></td>";
rows_inventory += 1;
$("[name='rows_inventory']").val(rows_inventory);
}
function add_attunement()
{
var tableRef = document.getElementById('attunementtable')
var row = tableRef.insertRow(tableRef.rows.length)
var cell0 = row.insertCell(0);
cell0.innerHTML = "<td><input name='attunement" + rows_attunements + "' type='text' /></td>";
rows_attunements += 1;
$("[name='rows_attunements']").val(rows_attunements);
}
function remove_last_row(tableId)
{
var tableRef = document.getElementById(tableId);
var rowCount = tableRef.rows.length;
tableRef.deleteRow(rowCount - 1);
switch(tableId) {
case 'attacktable':
rows_attacks -= 1;
if (rows_attacks < 0) {
rows_attacks = 0;
}
break;
case 'attunementtable':
rows_attunements -= 1;
if (rows_attunements < 0) {
rows_attunements = 0;
}
break;
case 'inventorytable':
rows_inventory -= 1;
if (rows_inventory < 0) {
rows_inventory = 0;
}
break;
case 'spelltable':
rows_spells -= 1;
if (rows_spells < 0) {
rows_spells = 0;
}
break;
}
$("[name='rows_attacks']").val(rows_attacks);
$("[name='rows_attunements']").val(rows_attunements);
$("[name='rows_inventory']").val(rows_inventory);
$("[name='rows_spells']").val(rows_spells);
}
function calc_carry_weight()
{
var total = 0;
var table = document.getElementById("inventorytable");
var trs = table.getElementsByTagName('tr');
for (var i=0; i < trs.length; i++) {
var tds = trs[i].getElementsByTagName('td');
var count_str = tds[2].getElementsByTagName('input')[0].value;
var weight_str = tds[3].getElementsByTagName('input')[0].value;
var count = (isNaN(parseFloat(count_str)) ? 0 : parseFloat(count_str))
var weight = (isNaN(parseFloat(weight_str)) ? 0 : parseFloat(weight_str))
console.log(count + " * " + weight + " = " + (count * weight));
total += count * weight;
}
document.getElementById("weightcarried").value = parseInt(total + 0.5);
}