-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
287 lines (239 loc) · 8.49 KB
/
test.js
File metadata and controls
287 lines (239 loc) · 8.49 KB
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
const maxLevel = 136;
var statpoints = maxLevel*2;
var remaining_points = statpoints;
var stats = new Map([['vitality', 0], ['magic', 0], ['weapons', 0], ['strength', 0]]);
var sliders = document.getElementsByClassName('slider');
var basehp = 968+(2*stats.get('vitality'));
var magicData = null;
var magics = [];
var fstyleData = null;
var fstyles = [];
var buildData = null;
var tables = [];
class Table{
constructor(type){
this.type = type;
this.index = document.getElementsByClassName(type+"Table").length;
this.selection = null;
this.tableID = type+this.index;
}
buildTable(){
if (this.type === "magic"){
this.numRows = 4;
this.numCols = 5;
this.arr_type = magics;
}
else if (this.type === "fstyle"){
this.numRows = 2;
this.numCols = 3;
this.arr_type = fstyles;
}
let div = document.getElementById("tables");
let tableElement = document.createElement("table");
tableElement.setAttribute("class", this.type+"Table");
tableElement.setAttribute("id", this.tableID);
let tbody = document.createElement("tbody");
for (let i = 0; i < this.numRows+0; i++) {
// Create a table row element
let row = document.createElement("tr");
for (let j = 0; j < this.numCols; j++) {
let element = this.arr_type[(i*this.numCols)+j]; //refers to magics array
if (element == undefined){
continue
}
// Create a table cell element (td)
let cell = document.createElement("td");
cell.setAttribute("id", element.name+this.index);
cell.addEventListener('click', ()=>
{this.select(element.name, this.tableID)}
)
let image = document.createElement("img");
image.setAttribute("src", "images/"+this.type+"s/"+element.name+".png");
image.setAttribute("width", "48px");
cell.appendChild(image);
// Append the cell to the row
row.appendChild(cell);
}
// Append the row to the table body
tbody.appendChild(row);
}
tableElement.appendChild(tbody);
div.appendChild(tableElement);
}
select(element, table){
//unselect oldskill
if(this.selection != null){
let old_skill = document.getElementById(this.selection+this.index).style;
old_skill.backgroundColor = "#666869";
old_skill.cursor = "pointer";
}
//selecting new figure
let new_skill = document.getElementById(element+this.index).style;
new_skill.backgroundColor = "#16181a";
new_skill.cursor = "default";
this.selection = element;
console.log(tables.map(x => x.selection));
}
}
class Magic {
constructor(name){
this.name = name
//this.damage = magicData[this.name].damage
//this.speed = magicData[this.name].speed
//this.size = magicData[this.name].size
//this.effect = magicData[this.name].effect
}
toString() {
return this.name //+ " Damage: "+this.damage+" Speed: "+this.speed+" Size: "+this.size;
}
}
class Fstyle {
constructor(name){
this.name = name
//this.damage = fstyleData[this.name].damage
//this.speed = fstyleData[this.name].speed
//this.size = fstyleData[this.name].size
//this.effect = fstylData[this.name].effect
}
}
function limitCheck(map, limit){
var sum = 0;
map.forEach(element => {
sum+= element
});
remaining_points = statpoints-sum;
return (sum > limit);
}
async function loadData() {
if (magicData === null) {
magicData = await fetchJSONData("./magics.json");
let iter = Object.keys(magicData)
for(let i = 0; i < iter.length; i++){
magics.push(new Magic(iter[i]))
}
}
if (fstyleData === null) {
fstyleData = await fetchJSONData("./fstyles.json");
let iter = Object.keys(fstyleData)
for(let i = 0; i < iter.length; i++){
fstyles.push(new Fstyle(iter[i]))
}
}
if (buildData === null) {
buildData = await fetchJSONData("./builds.json");
}
return undefined;
}
async function fetchJSONData(url) {
try {
const response = await fetch(url);
const jsonData = await response.json();
return jsonData;
}
catch (error) {
console.error("Error fetching JSON:", error);
}
}
function sliderUpdate() {
var newStats = new Map(stats);
newStats.set(this.id, this.valueAsNumber);
if (!limitCheck(newStats, statpoints)){
stats = newStats;
document.getElementById("remaining").innerHTML = "Remaining: "+remaining_points
}
else
this.valueAsNumber = stats.get(this.id);
document.getElementById(this.id+'-text').value = this.value;
build_calc();
}
function textUpdate() {
const int = Math.abs(parseInt(this.value));
if (isNaN(int)){
this.value = 0;
return}
const id = this.id.replace('-text', '')
var newStats = new Map(stats);
newStats.set(id, int);
if (!limitCheck(newStats, statpoints)){
stats = newStats;
document.getElementById(id).value = int;
document.getElementById("remaining").innerHTML = "Remaining: "+remaining_points
}
else
this.value = String(stats.get(id));
build_calc();
}
function build_calc(){
if (stats.get('vitality') > (statpoints*0.6))
loadBuild("Warden");
else if (stats.get('magic') > (statpoints*0.6))
loadBuild("Mage");
else if (stats.get('weapons') > (statpoints*0.6))
loadBuild("Warrior");
else if (stats.get('strength') > (statpoints*0.6))
loadBuild("Bezerker");
else if (stats.get('vitality') >= (statpoints*0.4) && stats.get('magic') >= (statpoints*0.4))
loadBuild("Paladin")
else if (stats.get('vitality') >= (statpoints*0.4) && stats.get('weapons') >= (statpoints*0.4))
loadBuild("Knight");
else if (stats.get('vitality') >= (statpoints*0.4) && stats.get('strength') >= (statpoints*0.4))
loadBuild("Juggernaut");
else if (stats.get('magic') >= (statpoints*0.4) && stats.get('weapons') >= (statpoints*0.4))
loadBuild("Conjurer");
else if (stats.get('magic') >= (statpoints*0.4) && stats.get('strength') >= (statpoints*0.4))
loadBuild("Warlock");
else if (stats.get('weapons') >= (statpoints*0.4) && stats.get('strength') >= (statpoints*0.4))
loadBuild("Warlord");
else
loadBuild("Savant");
}
async function loadBuild(text){
build = buildData[text];
color = build.color;
document.getElementById('build').innerHTML = text;
document.getElementById('build').style.color = color;
deleteTabs();
for(let i = 0; i < build.tabs.length; i++){
if (build.tabs[i] == "magic" || build.tabs[i] == "fstyle"){
generateTable(build.tabs[i]);}
}
}
function deleteTabs(){
deletetables = document.getElementById("tables").innerHTML = "";
table_selection = {}; //reset table selection
}
async function generateTable(skill){
let tableClass = new Table(skill);
tableClass.buildTable();
tables.push(tableClass);
}
async function update(){
for( let i = 0; i < sliders.length; i++){
sliders[i].oninput = sliderUpdate;
document.getElementsByClassName('input-text')[i].oninput = textUpdate;
}
}
async function load(){
document.getElementById('remaining').innerHTML = "Remaining: "+String(remaining_points);
for( let i = 0; i < sliders.length; i++){
sliders[i].setAttribute('max', statpoints);
}
await update();
await loadData();
}
async function select(magic_fs, table){
//unselect oldskill
index=table.slice(-1); //gets index of table from the tableid from last character
if(table_selection[table] != undefined){
//resetting old element
old_skill = document.getElementById(table_selection[table]+index).style;
old_skill.backgroundColor = "#666869";
old_skill.cursor = "pointer";
}
//selecting new element
new_skill = document.getElementById(magic_fs+index).style;
new_skill.backgroundColor = "#16181a";
new_skill.cursor = "default";
table_selection[table] = magic_fs;
console.log(table_selection);
}