forked from Grae-Drake/hg-units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
292 lines (245 loc) · 10.1 KB
/
app.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
var unitLibrary = [];
var cityLibrary = [];
$(document).ready(function() {
// Get the data from Highgrounds git repo and populate results.
function getUnits() {
var indexURL = 'https://raw.githubusercontent.com/highgrounds' +
'/HighgroundsAssets/master/data/1stEdition.xml';
var hgData = $.get(indexURL, function() {
unitLibrary = extractUnitData(parseHighgroundsXml(hgData));
cityLibrary = extractCityData(parseHighgroundsXml(hgData));
populateCities(cityLibrary);
populateUnits(unitLibrary);
});
}
function parseHighgroundsXml(hgData) {
// Get the card data as XML, parse it, and convert it to friendly JSON.
var xmlString = hgData.responseText;
var parser = new DOMParser();
var xml = parser.parseFromString(xmlString, "text/xml");
return xmlToJson(xml);
}
function extractCityData(hgJson) {
// Build an array of objects from the JSON data holding all cities.
var cities = [];
rawCities = hgJson.data.CITYLIST.CITY;
// Omit the first "dummy" unit.
for (var i = 1 ; i < rawCities.length ; i++) {
var rawCity = rawCities[i];
var attributes = rawCity.attributes;
var types = [];
var gold = 0;
var crystal = 0;
var wood = 0;
var recrui = 0;
// Plug in resource production and recruit.
for (var j = 0 ; j < rawCity.ACTION.length ; j ++) {
var action = rawCity.ACTION[j];
switch (action.attributes.type) {
case "gold":
gold = action.attributes.value;
break;
case "crystal":
crystal = action.attributes.value;
break;
case "wood":
wood = action.attributes.value;
break;
case "recruit":
recruit = action.attributes.value;
break;
}
}
// Populate types array. Raw types can be an array or an object.
if ($.isArray(rawCity.TYPE)) {
for (var k = 0 ; k < rawCity.TYPE.length ; k ++) {
var type = rawCity.TYPE[k];
types.push(type["#text"]);
}
}
else if (typeof(rawCity.TYPE === "object")) {
types.push(rawCity.TYPE["#text"]);
}
cities.push({
"name": attributes.name,
"id": attributes.id,
"rarity": attributes.rarity,
"groundType": attributes.groundType,
"edition": attributes.edition,
"types": types,
"gold": gold,
"crystal": crystal,
"wood": wood,
"recruit": recruit
});
}
// sort cities
var cityOrder = ["Titan Ridge", "Dwila", "Sacred Woods", "The Helm",
"Crystal Camp", "Outfitter", "The Den", "The Grotto",
"Forest Village", "Shadow Pylon"];
var cityIndex = {};
for(var i = 0; i < cityOrder.length; i++){
cityIndex[cityOrder[i]] = i;
}
cities.sort(function compareCities(a, b){
return cityIndex[a] - cityIndex[b];
});
return cities;
}
function extractUnitData(hgJson) {
// Build an array of objects from the JSON data holding all units.
var rawCards = hgJson.data.CARDLIST.CARD;
var cards = [];
var rarities = {
0: "Common",
1: "Uncommon",
2: "Rare",
3: "Ultra Rare",
4: "Legendary"
};
// Iterate over the raw unit data. Omit the first "unknown" placeholder unit.
for (var i = 1 ; i < rawCards.length ; i++) {
var rawCard = rawCards[i];
var attributes = rawCard.attributes;
var homeActions = [];
var battleActions = [];
var types = [];
var searchText = "";
// Populate homeActions and battleActions arrays.
for (var j = 0 ; j < rawCard.ACTION.length ; j ++) {
var action = rawCard.ACTION[j].attributes;
var target = action.location === "home" ? homeActions : battleActions;
// Deal with weird action values like -9999 for "X" and
// -1000 for actions like frail and dormant.
var actionValue = action.value === "-9999" ? "X" :
action.value === "-1000" ? "" :
action.value;
// Deal with weird action types like Windfall and "duorainer" typo.
var actionType = action.type.indexOf("windfall") > -1 ? "windfall" :
action.type.indexOf("duorainer") > -1 ? "duoRainer" :
action.type;
target.push({"type": actionType, "value": actionValue});
searchText += "!action " + actionValue + " " + actionType + " ";
}
// Populate types array. Raw types can be an array or an object.
if ($.isArray(rawCard.TYPE)) {
for (var k = 0 ; k < rawCard.TYPE.length ; k++) {
types.push(rawCard.TYPE[k]["#text"]);
}
} else if (typeof(rawCard.TYPE) === "object") {
types.push(rawCard.TYPE["#text"]);
}
var card = {
"name": attributes.name,
"cost": {"gold": parseInt(attributes.g, 10),
"crystal": parseInt(attributes.c, 10),
"wood": parseInt(attributes.w, 10)},
"edition": attributes.edition,
"id": attributes.id,
"rarity": rarities[parseInt(attributes.rarity, 10)],
"homeActions": homeActions,
"battleActions": battleActions,
"types": types
};
searchText += ["!name", card.name,
card.types.join(" "),
card.cost.gold ? ["!cost",
card.cost.gold,
"gold"].join(" ") : "",
card.cost.crystal ? ["!cost",
card.cost.crystal,
"crystal"].join(" ") : "",
card.cost.wood ? ["!cost",
card.cost.wood,
"wood"].join(" ") : "",
"!rarity", card.rarity,
"!edition", card.edition,
].join(" ");
card.searchText = searchText;
cards.push(card);
}
return cards;
}
function getActiveCities() {
activeButtons = $('.button-primary');
return cityLibrary.filter(function(city) {
for (var i = 0 ; i < activeButtons.length ; i++) {
if ($(activeButtons[i]).data("cityId") === city.id) {
return true;
}
}
});
}
function filterUnits(unitLibrary, activeCities) {
return unitLibrary.filter(function(unit) {
// Always include free units.
if (unit.cost.gold === 0 && unit.cost.crystal === 0 && unit.cost.wood === 0 ) {
return true;
}
// Check whether the unit's cost matches each active city.
for (var i = 0 ; i < activeCities.length ; i ++) {
var city = activeCities[i];
if ((unit.cost.gold > 0) === (city.gold > 0) &&
(unit.cost.crystal > 0) === (city.crystal > 0) &&
(unit.cost.wood > 0) === (city.wood > 0)) {
return true;
}
}
});
}
// Templating.
var unitTemplateScript = $("#unit-card").html();
var unitTemplate = Handlebars.compile(unitTemplateScript);
var cityTemplateScript = $("#city-button").html();
var cityTemplate = Handlebars.compile(cityTemplateScript);
Handlebars.registerHelper('toUpperCase', function(str) {
return str.toUpperCase();
});
function populateUnits(units) {
for (var i = 0 ; i < units.length ; i++) {
var unit = units[i];
unit["cache"] = unit["cache"] || unitTemplate(unit);
$(".units").append(unit["cache"]);
}
$(".unit-sprite img").on("error abort", function() {
this.src="images/outline.png";
});
}
function populateCities(cities) {
for (var j = 0 ; j < cities.length ; j++) {
var city = cities[j];
city["cache"] = city["cache"] || cityTemplate(city);
$(".cities").append(city["cache"]);
}
$('.city').on('click', function() {
$(this).toggleClass('button-primary');
activeCities = getActiveCities();
$('.units').empty();
populateUnits(filterUnits(unitLibrary, activeCities));
searchFilter();
});
}
$("#search").on("keyup", searchFilter);
function searchFilter() {
var values = $("#search").val().toLowerCase().split(",");
console.log(values);
$(".units .unit").each(function() {
var matches = [];
var searchText = $(this).data("searchText").toLowerCase();
for (var i = 0 ; i < values.length ; i++) {
matches.push(searchText.search(values[i]) > -1);
}
if(matches.indexOf(false) > -1) {
$(this).hide();
}
else {
$(this).show();
}
});
}
$(".buttons i").on("click", function() {
$(".search-instructions").toggle();
});
// Go time.
getUnits();
});