-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
184 lines (167 loc) · 6.58 KB
/
main.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
"use strict"
function setup() {
console.log("setup(): Running");
var client = new XMLHttpRequest();
client.open("GET", "db.json", true);
client.onreadystatechange = function () { //callback
if (client.readyState == 4) {
if (client.status == 200 || client.status == 0) {
loadDatabase(JSON.parse(client.responseText));
}
}
};
client.send();
console.log("getFileContents(): Request Sent");
}
function loadDatabase(input) {
console.log("loadDatabase(): Running");
var borderline = "*-----------------------------------------*\n";
var midline = "|------------------+----------------------|\n"
var qmoves = input.qmoves;
var cmoves = input.cmoves;
var mons = input.mons;
var output = "";
var i = 0;
for (i = 0; i < mons.length; i += 1) { //For every mon
var montable = borderline;
var mon = mons[i];
montable += "| " + mon.id + " " + pad(mon.name, 12) + "| | ";
montable += "<span class='" + mon.type1 + "'> </span>" + "<span class='" + mon.type2 + "'> </span>\n"
montable += midline;
var j = 0;
for (j = 0; j < mon.quick.length; j += 1) { //for every quick move
var qmovename = mon.quick[j].name;
var qmoveleg = mon.quick[j].legacy;
var qmoveobj = getQuick(qmoves, qmovename);
montable += "| " + pad(qmovename, 17) + "| ";
//calculate DPS with STAB, format the number with leading 0s and decimal places
var dps = qmoveobj.power / qmoveobj.duration;
if (qmoveobj.type === mon.type1 || qmoveobj.type === mon.type2) {
dps *= 1.25;
}
montable += formatDecimal(dps, 1) + " DPS ";
var eps = qmoveobj.energy / qmoveobj.duration;
montable += formatDecimal(eps, 1) + " EPS ";
if (qmoveleg) {
montable += "L |";
} else {
montable += " |";
}
//Add move type
montable += " <span class='" + qmoveobj.type + "'> " + qmoveobj.type + " </span>\n";
}
montable += midline;
j = 0;
for (j = 0; j < mon.charge.length; j += 1) { //for every charge move
var cmovename = mon.charge[j].name;
var cmoveleg = mon.charge[j].legacy;
var cmoveobj = getCharge(cmoves, cmovename);
montable += "| " + pad(cmovename, 17) + "| ";
//calculate DPS with STAB, format the number with leading 0s and decimal places
var dps = cmoveobj.power / cmoveobj.duration;
if (cmoveobj.type === mon.type1 || cmoveobj.type === mon.type2) {
dps *= 1.25;
}
montable += formatDecimal(dps, 1) + " DPS " + cmoveobj.bars + " BAR ";
if (cmoveleg) {
montable += "L |";
} else {
montable += " |";
}
//Add move type
montable += " <span class='" + cmoveobj.type + "'> " + cmoveobj.type + " </span>\n";
}
montable += borderline + "\n";
output += montable;
}
document.getElementById("main").innerHTML = output;
var moveoutput = "";
i = 0;
for (i = 0; i < qmoves.length; i += 1) {
moveoutput += "* " + pad(qmoves[i].name, 17);
moveoutput += "POWER: " + padInt(qmoves[i].power, 3) + " ";
moveoutput += "ENERGY:" + padInt(qmoves[i].energy, 3) + " ";
moveoutput += "DURATION:" + formatDecimal(qmoves[i].duration, 1) + "s \n";
}
document.getElementById("qmoves").innerHTML = moveoutput;
moveoutput = "";
i = 0;
for (i = 0; i < cmoves.length; i += 1) {
moveoutput += "* " + pad(cmoves[i].name, 17);
moveoutput += "POWER: " + padInt(cmoves[i].power, 3) + " ";
moveoutput += "DURATION:" + formatDecimal(cmoves[i].duration, 1) + "s ";
if (cmoves[i].bars === "1") {
moveoutput += "<span class='" + cmoves[i].type + "'> </span>\n";
} else if (cmoves[i].bars === "2") {
moveoutput += "<span class='" + cmoves[i].type + "'> </span> ";
moveoutput += "<span class='" + cmoves[i].type + "'> </span>\n";
} else if (cmoves[i].bars === "3") {
moveoutput += "<span class='" + cmoves[i].type + "'> </span> ";
moveoutput += "<span class='" + cmoves[i].type + "'> </span> ";
moveoutput += "<span class='" + cmoves[i].type + "'> </span>\n";
} else {
moveoutput += "<span class='" + cmoves[i].type + "'></span>\n";
}
}
document.getElementById("cmoves").innerHTML = moveoutput;
}
//Pad a string with spaces
function pad(input, length) {
var str = input;
var j = 0;
for (j = 0; j < (length - input.length); j += 1) {
str += " ";
}
return str;
}
//plugs spaces in front of an integer
function padInt(input, expectedlength) {
var str = "";
var i = 0;
for (i = input.length; i < expectedlength; i += 1) {
str += " ";
}
str += input;
return str;
}
//formats a dps value into the 00.0* format, where the number of * is the precision value
function formatDecimal(input, prec) {
var str = "";
if (input < 10) { //pad starting
str += " ";
}
str += roundPrec(input, prec).toString();
if (str.length == 2) {
str += ".0";
} else if (str.length == 3) { //NaN
str += " ";
}
return str;
}
//round with precision, from http://stackoverflow.com/questions/7342957/how-do-you-round-to-1-decimal-place-in-javascript
function roundPrec(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
//returns quick move object corresponding to given name
function getQuick(qmoves, qmovename) {
var k = 0;
for (k = 0; k < qmoves.length; k += 1) { //search quick move database
if (qmoves[k].name === qmovename) {
return qmoves[k];
}
}
console.log("Error: Move " + qmovename + " was not found in db.json");
return {"name": "???", "type": "UNK", "power": "0", "duration": "0"};
}
//returns charge move object corresponding to given name
function getCharge(cmoves, cmovename) {
var k = 0;
for (k = 0; k < cmoves.length; k += 1) { //search quick move database
if (cmoves[k].name === cmovename) {
return cmoves[k];
}
}
console.log("Error: Move " + cmovename + " was not found in db.json");
return {"name": "???", "type": "UNK", "power": "0", "duration": "0.0", "crit": "0.00", "bars": "?"};
}