-
Notifications
You must be signed in to change notification settings - Fork 9
/
spiredb.js
314 lines (283 loc) · 8.75 KB
/
spiredb.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
var traps = "_FZPLSCK";
var core_tiers = ["common", "uncommon", "rare", "epic", "legendary", "magnificent", "ethereal"];
var core_mods = {"F":"fire", "P":"poison", "L":"lightning", "S":"strength", "C":"condenser", "R":"runestones"};
var number_suffices = ["", "k", "M", "B", "T", "Qa", "Qi", "Sx", "Sp", "Oc"];
function format_number(num)
{
var magnitude = 0;
while(num>1000)
{
magnitude += 1;
num /= 1000;
}
if(magnitude>0)
return num.toFixed(2)+number_suffices[magnitude];
else
return num;
}
function SpireClient()
{
this.spire = document.getElementById("spire");
this.layout_display = document.getElementById("layout-string");
this.stats = document.getElementById("stats");
this.upgrades = document.getElementById("upgrades");
this.core = document.getElementById("core");
this.ui = document.getElementById("ui");
this.query_form = this.ui.querySelector("form");
this.error_display = this.ui.querySelector(".error");
this.pending_submit = null;
this.create_spire = function create_spire(floors, traps)
{
this.spire.innerText = "";
for(var i=floors-1; i>=0; --i)
{
var row = document.createElement("tr");
this.spire.appendChild(row);
for(var j=0; j<5; ++j)
{
var cell = document.createElement("td");
var trap = traps[i*5+j];
if(trap)
{
cell.innerText = trap;
cell.classList.add(trap);
}
row.appendChild(cell);
}
}
}
this.calculate_cost = function calculate_cost(traps)
{
var fire_cost = 100;
var frost_cost = 100;
var poison_cost = 500;
var lightning_cost = 1000;
var strength_cost = 3000;
var condenser_cost = 6000;
var knowledge_cost = 9000;
var cost = 0;
for(var i=0; i<traps.length; ++i)
{
var t = traps[i];
if(t=='F')
{
cost += fire_cost;
fire_cost = fire_cost*3/2;
}
else if(t=='Z')
{
cost += frost_cost;
frost_cost *= 5;
}
else if(t=='P')
{
cost += poison_cost;
poison_cost = poison_cost*7/4;
}
else if(t=='L')
{
cost += lightning_cost;
lightning_cost *= 3;
}
else if(t=='S')
{
cost += strength_cost;
strength_cost *= 100;
}
else if(t=='C')
{
cost += condenser_cost;
condenser_cost *= 100;
}
else if(t=='K')
{
cost += knowledge_cost;
knowledge_cost *= 100;
}
}
return cost;
}
this.describe_upgrade = function describe_upgrade(level)
{
level = Number(level);
var charCode = (level<10 ? 48+level : 55+level);
return String.fromCharCode(charCode);
}
this.describe_upgrades = function decsribe_upgrades()
{
var fe = this.query_form.elements;
return this.describe_upgrade(fe.fire.value)+this.describe_upgrade(fe.frost.value)+
this.describe_upgrade(fe.poison.value)+this.describe_upgrade(fe.lightning.value);
}
this.describe_core = function describe_core()
{
var fe = this.query_form.elements;
var core = fe.core_tier.value;
if(core=="0")
return "";
if(fe.core_fire.value>0)
core += "/F:"+fe.core_fire.value;
if(fe.core_poison.value>0)
core += "/P:"+fe.core_poison.value;
if(fe.core_lightning.value>0)
core += "/L:"+fe.core_lightning.value;
if(fe.core_strength.value>0)
core += "/S:"+fe.core_strength.value;
if(fe.core_condenser.value>0)
core += "/C:"+fe.core_condenser.value;
if(fe.core_runestones.value>0)
core += "/R:"+fe.core_runestones.value;
return core;
}
this.query_spire = function query_spire()
{
if(this.pending_submit)
{
var xhr = new XMLHttpRequest();
xhr.open("POST", "/submit");
xhr.send(this.pending_submit);
this.pending_submit = null;
}
var fe = this.query_form.elements;
var query_type = fe.query_type.value;
var upgrades = this.describe_upgrades();
var floors = fe.floors.value;
var budget = fe.budget.value;
var query = "upg="+upgrades+" f="+floors+" rs="+budget+" "+query_type;
var core = this.describe_core();
if(core)
query += " core="+core;
var xhr = new XMLHttpRequest();
xhr.open("POST", "/query");
var _this = this;
xhr.addEventListener("load", function(){ _this.query_complete(xhr); });
xhr.send(query);
}
this.query_complete = function query_complete(xhr)
{
var space = xhr.response.indexOf(" ");
var result = xhr.response.substring(0, space);
if(result=="ok")
{
this.error_display.innerText = "";
var fields = {};
var start = 3;
while(start<xhr.response.length)
{
space = xhr.response.indexOf(" ", start);
if(space<0)
space = xhr.response.length;
var equal = xhr.response.indexOf("=", start);
if(equal<space)
fields[xhr.response.substring(start, equal)] = xhr.response.substring(equal+1, space);
start = space+1;
}
this.create_spire(fields.t.length/5, fields.t);
var layout_str = "";
for(var i=0; i<fields.t.length; ++i)
layout_str += traps.indexOf(fields.t[i]);
layout_str += "+"+fields.upg+"+"+fields.t.length/5;
this.layout_display.innerText = layout_str;
this.stats.style.display = "block";
this.stats.querySelector("#cost").innerText = format_number(fields.rs);
this.stats.querySelector("#damage").innerText = format_number(fields.damage);
this.stats.querySelector("#income_sec").innerText = format_number(fields.income);
this.stats.querySelector("#income_hr").innerText = format_number(fields.income*3600);
this.stats.querySelector("#income_enemy").innerText = format_number(fields.enemy_worth);
this.stats.querySelector("#threat").innerText = fields.threat;
this.upgrades.style.display = "block";
this.upgrades.querySelector("span").innerText = fields.upg;
if(fields.core)
{
var span = this.core.querySelector("span");
var core = {};
var parts = fields.core.split("/");
span.innerText = core_tiers[parts[0]-1];
for(var i=1; i<parts.length; ++i)
{
span.appendChild(document.createTextNode(" "));
var mod = document.createElement("span");
mod.classList.add(parts[i][0]);
mod.innerText = core_mods[parts[i][0]]+" "+parts[i].substring(2)+"%";
span.appendChild(mod);
}
this.core.style.display = "block";
}
else
this.core.style.display = "none";
}
else
this.error_display.innerText = xhr.response;
}
this.save_pasted = function save_pasted(event)
{
var save_data = event.clipboardData.getData("text");
var save_json = JSON.parse(LZString.decompressFromBase64(save_data));
var layout = "";
for(var i=0; i<save_json.playerSpire.main.layout.length; ++i)
{
var trap = save_json.playerSpire.main.layout[i].trap;
if(!trap || !trap.name)
layout += "_";
else if(trap.name=="Frost")
layout += "Z";
else
layout += trap.name[0];
}
var save_traps = save_json.playerSpire.traps
var fe = this.query_form.elements;
fe.floors.value = save_json.playerSpire.main.rowsAllowed;
fe.budget.value = Math.floor(this.calculate_cost(layout)+save_json.playerSpire.main.runestones);
fe.fire.value = save_traps.Fire.level;
fe.frost.value = save_traps.Frost.level;
fe.poison.value = save_traps.Poison.locked ? 0 : save_traps.Poison.level;
fe.lightning.value = save_traps.Lightning.locked ? 0 : save_traps.Lightning.level;
fe.core_tier.value = "0";
fe.core_fire.value = "";
fe.core_poison.value = "";
fe.core_lightning.value = "";
fe.core_strength.value = "";
fe.core_condenser.value = "";
fe.core_runestones.value = "";
if("rarity" in save_json.global.CoreEquipped)
{
fe.core_tier.value = save_json.global.CoreEquipped.rarity+1;
var core_mods = save_json.global.CoreEquipped.mods;
for(var i=0; i<core_mods.length; ++i)
{
var mod = core_mods[i];
if(mod[0]=="fireTrap")
fe.core_fire.value = mod[1];
else if(mod[0]=="poisonTrap")
fe.core_poison.value = mod[1];
else if(mod[0]=="lightningTrap")
fe.core_lightning.value = mod[1];
else if(mod[0]=="strengthEffect")
fe.core_strength.value = mod[1];
else if(mod[0]=="condenserEffect")
fe.core_condenser.value = mod[1];
else if(mod[0]=="runestones")
fe.core_runestones.value = mod[1];
}
}
this.create_spire(save_json.playerSpire.main.rowsAllowed, layout);
var upgrades = this.describe_upgrades();
this.pending_submit = "upg="+upgrades+" t="+layout;
var core = this.describe_core();
if(core)
this.pending_submit += " core="+core;
this.layout_display.innerText = "";
this.core.style.display = "none";
this.stats.style.display = "none";
this.upgrades.style.display = "none";
fe.save.value = "Data loaded!";
setTimeout(function(){ fe.save.value = ""; }, 3000);
}
var _this = this;
this.query_form.addEventListener("submit", function(event){ _this.query_spire(); event.preventDefault(); });
var save_input = this.query_form.elements.save;
save_input.addEventListener("paste", function(event){ _this.save_pasted(event); event.preventDefault(); });
this.create_spire(5, "");
}
var client = null;
document.addEventListener("DOMContentLoaded", function(){ client = new SpireClient(); });