-
Notifications
You must be signed in to change notification settings - Fork 0
/
findBonus.js
330 lines (313 loc) · 15.5 KB
/
findBonus.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
javascript:
var villages = [];
var woodBonus = [];
var stoneBonus = [];
var ironBonus = [];
var barracksBonus = [];
var warehouseBonus = [];
var workshopBonus = [];
var farmBonus = [];
var allResBonus = [];
var stableBonus = [];
var backgroundColor = "#36393f";
var borderColor = "#3e4147";
var baseColor = "#e3d5b3";
var titleColor = "#ffffdf";
var headerColor = "#202225";
var currentTime = Date.parse(new Date());
if (localStorage.getItem("mapVillageTime") != null) {
mapVillageTime = localStorage.getItem("mapVillageTime");
if (parseInt(currentTime) >= parseInt(mapVillageTime) + 60 * 60 * 24 * 1000) {
//hour has passed
console.log("Hour has passed, recollecting the village data");
$.get("map/village.txt", function (data) {
villages = CSVToArray(data);
localStorage.setItem("mapVillageTime",Date.parse(new Date()));
localStorage.setItem("mapVillageTxt",data);
})
.done(function () {
findBonus();
});
}
else {
// within same hour
console.log("Hour not over yet, waiting to recollect, using old data");
data = localStorage.getItem("mapVillageTxt");
villages = CSVToArray(data);
findBonus();
}
}
else {
//get villageTxt for first time
console.log("Grabbing village.txt for the first time");
$.get("map/village.txt", function (data) {
villages = CSVToArray(data);
localStorage.setItem("mapVillageTime",Date.parse(new Date()));
localStorage.setItem("mapVillageTxt",data);
})
.done(function () {
findBonus();
});
}
function findBonus() {
for (i = 0; i < villages.length; i++) {
if (villages[i][6] != "0" && villages[i][4] == "0") {
switch (villages[i][6]) {
case "1":
woodBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "2":
stoneBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "3":
ironBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "4":
farmBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "5":
barracksBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "6":
stableBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "7":
workshopBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "8":
allResBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
case "9":
warehouseBonus.push([villages[i][2] + "|" + villages[i][3], villages[i][0]]);
break;
default:
break;
}
}
}
html = `
<div id="main" class="ui-widget-content" style="background-color:${backgroundColor}">
<table id="bonusVillages" class="vis" border="1" style="width: 100%;background-color:${backgroundColor};border-color:${borderColor}">
<tr style="background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/wood.png" style="width:48px;height:48px;" onClick="displayArray(0)"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/stone.png" style="width:48px;height:48px;" onClick="displayArray(1)"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/iron.png" style="width:48px;height:48px;" onclick="displayArray(2)"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/farm.png" style="width:48px;height:48px;" onclick="displayArray(3)"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/barracks.png" style="width:48px;height:48px;" onclick="displayArray(4)"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/stable.png" style="width:48px;height:48px;" onclick="displayArray(5)" role="button"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/garage.png" style="width:48px;height:48px;" onclick="displayArray(6)" role="button"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/all.png" style="width:48px;height:48px;" onclick="displayArray(7)" role="button"/>
</td>
<td style="text-align:center; width:auto; background-color:${baseColor}"><img src="graphic/bonus/storage.png" style="width:48px;height:48px;" onclick="displayArray(8)" role="button"/>
</td>
</tr>
<tr style="background-color:${backgroundColor}">
<td id="villageList" colspan="9" style="background-color:${backgroundColor}">
</td>
</tr>
</table>
</div>`
Dialog.show("Content", html);
}
function CSVToArray(strData, strDelimiter) {
strDelimiter = (strDelimiter || ",");
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
var arrData = [[]];
var arrMatches = null;
while (arrMatches = objPattern.exec(strData)) {
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[1];
if (
//check if its string
strMatchedDelimiter.length &&
strMatchedDelimiter !== strDelimiter
) {
//create new row
arrData.push([]);
}
var strMatchedValue;
if (arrMatches[2]) {
//get rid of quotes
strMatchedValue = arrMatches[2].replace(
new RegExp("\"\"", "g"),
"\""
);
} else {
// no quotes
strMatchedValue = arrMatches[3];
}
//add to array
arrData[arrData.length - 1].push(strMatchedValue);
}
// Return the parsed data.
return (arrData);
}
function displayArray(x) {
console.log(x);
tempHTML = `<table id="bonusWood" class="vis" border="1" align="center" style="background-color:${backgroundColor};border-color:${borderColor};">
<tr style="background-color:${backgroundColor}"><td style="text-align:center;background-color:${headerColor}"><font color="white">Coordinate</font></td><td style="text-align:center;background-color:${headerColor}"><font color="white">Distance</font></td></tr>`;
if (x == 0) {
for (var i = 0; i < woodBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${woodBonus[i][1]}"><font color="white">${woodBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(woodBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 1) {
for (var i = 0; i < stoneBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${stoneBonus[i][1]}"><font color="white">${stoneBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(stoneBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 2) {
for (var i = 0; i < ironBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${ironBonus[i][1]}"><font color="white">${ironBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(ironBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 3) {
for (var i = 0; i < farmBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${farmBonus[i][1]}"><font color="white">${farmBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(farmBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 4) {
for (var i = 0; i < barracksBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${barracksBonus[i][1]}"><font color="white">${barracksBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(barracksBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 5) {
for (var i = 0; i < stableBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${stableBonus[i][1]}"><font color="white">${stableBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(stableBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 6) {
for (var i = 0; i < workshopBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${workshopBonus[i][1]}"><font color="white">${workshopBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(workshopBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 7) {
for (var i = 0; i < allResBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${allResBonus[i][1]}"><font color="white">${allResBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(allResBonus[i][0])}</font></span></td>
</tr>`
}
}
if (x == 8) {
for (var i = 0; i < warehouseBonus.length; i++) {
tempHTML += `
<tr style="text-align:center; width:auto; background-color:${backgroundColor}">
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><a href="/game.php?&screen=info_village&id=${warehouseBonus[i][1]}"><font color="white">${warehouseBonus[i][0]}</font></a></td>
<td style="text-align:center; width:auto; background-color:${backgroundColor}"><span><font color="white">${calcDistance(warehouseBonus[i][0])}</font></span></td>
</tr>`
}
}
tempHTML += "</table>";
$("#villageList").eq(0).empty();
$("#villageList").eq(0).append(tempHTML);
sortTableTest(1)
}
function calcDistance(village) {
var x1 = game_data.village.x, y1 = game_data.village.y, x2 = village.match(/(\d*)\|(\d*)/)[1], y2 = village.match(/(\d*)\|(\d*)/)[2];
//calculate distance from current village
var a = x1 - x2;
var b = y1 - y2;
var distance = Math.round(Math.hypot(a, b));
return distance;
}
function sortTableTest(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("bonusWood");
switching = true;
// Set the sorting direction to ascending:
dir = "asc";
/* Make a loop that will continue until
no switching has been done: */
while (switching) {
// Start by saying: no switching is done:
switching = false;
rows = table.rows;
/* Loop through all table rows (except the
first, which contains table headers): */
for (i = 1; i < (rows.length - 1); i++) {
// Start by saying there should be no switching:
shouldSwitch = false;
/* Get the two elements you want to compare,
one from current row and one from the next: */
x = rows[i].getElementsByTagName("td")[n];
y = rows[i + 1].getElementsByTagName("td")[n];
/* Check if the two rows should switch place,
based on the direction, asc or desc: */
if (dir == "asc") {
if (Number(x.innerText.replace(/\./g, '')) > Number(y.innerText.replace(/\./g, ''))) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
if (Number(x.innerText.replace(/\./g, '')) < Number(y.innerText.replace(/\./g, ''))) {
// If so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/* If a switch has been marked, make the switch
and mark that a switch has been done: */
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
// Each time a switch is done, increase this count by 1:
switchcount++;
} else {
/* If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again. */
if (switchcount == 0 && dir == "desc") {
dir = "asc";
switching = true;
}
}
}
}