-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (99 loc) · 2.95 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
var nodes = [];
var count = 0;
function AddItem(val, init){
var name;
var initiative;
if(val != null){
name = val;
initiative = document.getElementById(init).value
if(initiative == ""){
return;
}
}else{
name = document.getElementById('name').value;
initiative = document.getElementById('initiative').value;
if(name == "" || initiative == ""){
return;
}
}
if(nodes!=null){
nodes = nodes.filter(obj => {
return obj.name != name
})
}
var node = {
name: name,
initiative: initiative,
displayInitiative: initiative,
processed: false,
id: count,
modifier: initiative
}
nodes.push(node);
count++;
UpdateCache();
UpdateTable();
}
function UpdateTable(){
nodes.sort((a, b) => parseFloat(b.modifier) - parseFloat(a.modifier));
UpdateCache();
RemoveItems()
for (i = 0; i < nodes.length; i++) {
var tbodyRef = document.getElementById('table').getElementsByTagName('tbody')[0];
var currentId = nodes[i].id;
// Insert a row at the end of table
var newRow = tbodyRef.insertRow();
// Insert a cell at the end of the row
var nameCell = newRow.insertCell();
var initiativeCell = newRow.insertCell();
var endCell = newRow.insertCell();
var killCell = newRow.insertCell();
// Append a text node to the cell
var nameText = document.createTextNode(nodes[i].name);
var initiativeText = document.createTextNode(nodes[i].displayInitiative);
nameCell.appendChild(nameText);
initiativeCell.appendChild(initiativeText);
if(i == 0){
endCell.innerHTML += `<button id='btnEnd' onclick='EndTurn(${currentId})' button type="button"></button>`;
}else{
endCell.innerHTML += `<button id='btnEndHidden' onclick='EndTurn(${currentId})' button type="button"></button>`;
}
killCell.innerHTML += `<button id='btnKill' onclick='KillCharacter(${currentId})' button type="button"></button>`;
row = tbodyRef.insertRow();
}
}
function RemoveItemsAndEmptyNodes(){
EmptyNodes();
RemoveItems();
}
function EndTurn(id){
var result = nodes.filter(obj => {
return obj.id === id
})[0]
result.modifier -= 100;
UpdateTable();
}
function RemoveItems(){
$('#table tr:not(:first)').remove();
UpdateCache();
}
function EmptyNodes(){
nodes = [];
UpdateCache();
}
function KillCharacter(id){
nodes = nodes.filter(item => item.id !== id)
UpdateTable();
}
function UpdateCache(){
localStorage.setItem("nodes", JSON.stringify(nodes));
console.log(localStorage['nodes']);
}
function ParseCache(){
nodes = JSON.parse(localStorage.getItem("nodes") || "[]");
console.log(nodes);
}
window.addEventListener("load", function(){
ParseCache();
UpdateTable();
});