-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbz.js
244 lines (222 loc) · 7.29 KB
/
tbz.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
'use strict';
let db = {
'body': ['Unarmed Combat', 'Wormcharm'],
'agility': ['Movement', 'Melee Weapons', 'Evasion', 'Stealth', 'Ninjutsu', 'Criminal Arts'],
'senses': ['First aid', 'Notice', 'Marksman', 'Pursuit/Hunt', 'Forgery'],
'knowledge': ['Information', 'Onmyoujutsu'],
'spirit': ['Willpower', 'Resonance', 'Interface', 'Buddhist Magic'],
'empathy': ['Persuation', 'Pillow Arts', 'Perform'],
'station': ['Strategy', 'Etiquette', 'Shinto', 'Art of Rule'],
}
let ranks = {
'Movement': 1,
'Melee Weapons': 1,
'Evasion': 1,
'Stealth': 1,
'Unarmed Combat': 1,
'First aid': 1,
'Notice': 1,
'Marksman': 1,
'Pursuit/Hunt': 1,
'Information': 1,
'Willpower': 1,
'Persuation': 1,
'Pillow Arts': 1,
};
document.querySelectorAll('th.label').forEach(function(elm, index) {
let name = elm.textContent.toLowerCase();
let tbody = elm.parentNode.parentNode
if (name in db) {
let dbi = db[name];
for (let item in dbi) {
let tr = document.createElement("tr")
tr.innerHTML = '<td class="label">' + dbi[item] + '</td><td class="ranks"></td>'
tr.querySelector('.ranks').setAttribute("data-skill", dbi[item])
tbody.appendChild(tr)
}
elm.parentNode.classList.add(name);
tbody.parentNode.classList.add(name);
}
});
function resetRanks() {
const div = this.parentNode
const skill = div.getAttribute('data-skill')
if (skill in ranks) {
div.setAttribute('data-value', ranks[skill])
}
const value = div.getAttribute('data-value')
let divs = div.children
for (let i = 0, len = divs.length; i < len; i++) {
let elm = divs[i];
const rank = elm.getAttribute('data-rank')
if (rank <= value) {
elm.classList.add('rank-filled')
} else {
elm.classList.remove('rank-filled')
}
}
}
function hoverRanks(evt) {
const elm = this
const siblings = [].slice.call(elm.parentNode.children)
const i = siblings.indexOf(elm) + 1
for (const div of siblings.slice(0, i)) {
div.classList.add("rank-filled")
}
for (const div of siblings.slice(i)) {
div.classList.remove("rank-filled")
}
}
function setRanks() {
const div = this
const skill = div.parentNode.getAttribute("data-skill")
let rank = div.getAttribute('data-rank')
if (ranks[skill] == rank) {
rank = rank - 1
}
ranks[skill] = rank
resetRanks.call(div)
}
document.getElementById('attribute-tables')
.querySelectorAll('input')
.forEach(function(elm) {
elm.classList.add('unfilled');
elm.addEventListener('input', function() {
elm.classList.remove('unfilled');
if (!(elm.value)) elm.classList.add('unfilled');
})
})
document.querySelectorAll('.ranks')
.forEach(function(elm) {
let newVal = 0
let skill = elm.getAttribute('data-skill');
if (skill && ranks[skill]) {
newVal = ranks[skill]
}
elm.setAttribute('data-value', newVal);
for (let i = 0; i < 5; i++) {
let div = document.createElement("div")
div.classList.add("rank")
div.setAttribute('data-rank', i + 1)
div.addEventListener("mouseenter", hoverRanks)
div.addEventListener("mouseleave", resetRanks);
div.addEventListener("click", setRanks);
elm.appendChild(div);
resetRanks.call(div);
}
})
function setWounds() {
const span = this
const siblings = [].slice.call(span.parentNode.children)
const i = siblings.indexOf(span)
if (span.classList.contains("wound-level-max")) {
span.classList.remove("wound-level-max")
span.classList.remove("i-wound-filled")
span.classList.add("i-wound")
const prev = siblings[i - 1]
prev && prev.classList.add("wound-level-max");
} else {
siblings.forEach(function(elm) { elm.classList.remove("wound-level-max") })
span.classList.add("wound-level-max");
span.classList.add("i-wound-filled");
for (const div of siblings.slice(0, i + 1)) {
div.classList.add("i-wound-filled")
}
for (const div of siblings.slice(i + 1)) {
div.classList.remove("i-wound-filled")
}
}
}
function makeSpan() {
const span = document.createElement("span")
span.classList.add("i-wound")
span.addEventListener("click", setWounds)
return span
}
function fillWithSpans(elm, num) {
elm.innerHTML = ""
for (let i = 0; i < num; i++) {
elm.appendChild(makeSpan())
}
}
fillWithSpans(document.querySelector('#light div'), 10)
fillWithSpans(document.querySelector('#heavy div'), 5)
fillWithSpans(document.querySelector('#critical div'), 3)
fillWithSpans(document.querySelector('#dead div'), 1)
// choose
// name
// description
// image
// archetypes
// -- add together karma, attribute penalty, weapons, special abilities, skills etc
//
// Saving and loading
let saved_ids = ["name", "body", "agility", "spirit", "station", "knowledge", "empathy", "senses", "notes", "concept",
"description", "archetypes", "soul", "vitality",
];
function saveCharacterToLocal() {
'use strict';
console.log("Saving.");
let characterObject = {};
for (let i = 0, len = saved_ids.length; i < len; i++) {
characterObject[saved_ids[i]] = document.getElementById(saved_ids[i]).value;
}
characterObject.skills = {};
for (let rank in ranks) {
characterObject.skills[rank] = ranks[rank];
}
let json_char = JSON.stringify(characterObject);
localStorage.setItem("saved-tenra-character", json_char);
}
function loadCharacterFromLocal() {
console.log("Loading.");
let characterString = localStorage.getItem("saved-tenra-character");
if (characterString) {
console.log(characterString);
let characterObject = JSON.parse(characterString);
console.log(characterObject);
let attr, skill;
for (skill in characterObject.skills) {
if (characterObject.skills.hasOwnProperty(skill)) {
ranks[skill] = characterObject.skills[skill];
}
}
for (attr in characterObject) {
if (characterObject.hasOwnProperty(attr)) {
let elem = document.getElementById(attr);
if (elem) {
elem.value = characterObject[attr];
if (characterObject[attr]) elem.classList.remove("unfilled");
}
}
}
}
console.log("Loading complete. Updating.");
document.querySelectorAll('.ranks div').forEach(function(elm) {
resetRanks.call(elm)
})
}
function calcSecondaryAttributes() {
let body = document.getElementById("body").value;
let spirit = document.getElementById("spirit").value;
let knowledge = document.getElementById("knowledge").value;
if (body && spirit) {
let vitality = parseInt(body) + parseInt(spirit);
document.getElementById("vitality").value = vitality;
}
if (knowledge && spirit) {
let soul = (parseInt(spirit) + parseInt(knowledge)) * 2;
document.getElementById("soul").value = soul;
}
if (body) {
let rounddiv = function(val, div) { return Math.max(Math.ceil(parseInt(body) / div, 10), 1); }
document.getElementById("light-wounds").value = rounddiv(body, 1);
document.getElementById("heavy-wounds").value = rounddiv(body, 2);
document.getElementById("critical-wounds").value = rounddiv(body, 4);
document.getElementById("dead-wounds").value = 1;
}
}
console.log("Binding save/load handlers.");
document.getElementById('save').addEventListener("click", saveCharacterToLocal);
document.getElementById('load').addEventListener("click", loadCharacterFromLocal);
document.getElementById('calc').addEventListener("click", calcSecondaryAttributes);