-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlants.js
324 lines (280 loc) · 8.6 KB
/
Plants.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
var plantsJson, rarityJson, locationJson;
$.getJSON("https://jeremyhouser.github.io/PlantGathering/JSON/plants.json", function(data)
{
plantsJson = data;
populateScrollable();
});
$.getJSON("https://jeremyhouser.github.io/PlantGathering/JSON/rarities.json", function(data)
{
rarityJson = data;
$.each(rarityJson, function(i, field)
{
$('#raritySelect').append("<option class='rarityOption'>" + field.Name + "</option>");
});
});
$.getJSON("https://jeremyhouser.github.io/PlantGathering/JSON/locations.json", function(data)
{
locationJson = data;
$.each(locationJson, function(i, field)
{
$('#environmentSelect').append("<option class='environmentOption'>" + field.Name + "</option>");
});
});
function FindPlantInRarity(plantName, jsonToSearch)
{
var plantRar;
$.each(jsonToSearch, function(i, field)
{
$.each(field.Plants, function(j, item)
{
if(item == plantName)
{
plantRar = field.Name;
return false;
}
});
});
return plantRar;
}
function FindPlantInLocation(plantName)
{
var location = "", location1 = "", location2 = "", location3 = "";
var locCount = 0;
$.each(locationJson, function(i, field)
{
$.each(field.Plants, function(j, item)
{
if(item == plantName)
{
++locCount;
if(locCount === 1)
{
location = field.Name;
location1 = field.Name;
}
if(locCount === 2)
{
location2 = field.Name;
location = location1 + " and " + location2
}
if(locCount === 3)
{
location3 = field.Name;
location = location1 + ", " + location2 + ", and " + location3;
}
}
});
});
return location;
}
//only works for plantsJson
//populates all plants into .scrollable
function populateScrollable()
{
$(".plantsClickable").remove();
$.each(plantsJson, function(i, field)
{
$('.scrollable').append("<li class='plantsClickable'>" + field.Name + "</li>");
});
}
function falseSubmit()
{
return false;
}
function NoResults()
{
if(!($('.plantsClickable').is(':visible')))
{
if($('.noResults').length < 1)
{
$('.scrollable').append("<li class='noResults'>No Results!</li>");
}
}
else if($('.noResults').length > 0)
{
$('.noResults').each(function()
{
$('.noResults').remove();
});
}
}
//this grabs anything matching the filters selected //////////////////////////////////////////???
function populateScrollableSelect(filterList)
{
var currentlyIn = [];
var exclNum = 0;
$.each(filterList, function(i, v)
{
if ($("input:radio:checked").val() == "include")
{
//rarity filter
//list of plants matching selected rarity
$.each(rarityJson, function(i, field)
{
$.each(field.Plants, function(j, value)
{
if (field.Name == v)
{
if ($.inArray(value, currentlyIn) == -1) {
currentlyIn.push(value);
}
}
});
});
//location filter
//list of plants matching selected location
$.each(locationJson, function(i, field)
{
$.each(field.Plants, function(j, value)
{
if (field.Name == v)
{
if ($.inArray(value, currentlyIn) == -1) {
currentlyIn.push(value);
}
}
});
});
}
//exclusive
else
{
if (exclNum == 0)
{
$.each(rarityJson, function(i, field)
{
if (field.Name == v)
{
currentlyIn = field.Plants;
}
});
$.each(locationJson, function(i, field)
{
if (field.Name == v)
{
currentlyIn = field.Plants;
}
});
}
else
{
var temp = [];
var crossCheck = [];
$.each(rarityJson, function(i, field)
{
if (field.Name == v)
{
crossCheck = field.Plants;
}
});
$.each(locationJson, function(i, field)
{
if (field.Name == v)
{
crossCheck = field.Plants;
}
});
$.each(currentlyIn, function(ind, val) {
if ($.inArray(val, crossCheck) > -1) {
temp.push(val);
}});
currentlyIn = temp;
}
exclNum++;
}
});
currentlyIn.sort();
//remove all elements for restock
$('.plantsClickable').remove();
if (currentlyIn.length == 0)
{
NoResults();
}
//restock fresh produce
$.each(currentlyIn, function(k, pV)
{
$('.scrollable').append("<li class='plantsClickable'>" + pV + "</li>");
NoResults();
});
}
//events
$(document).ready(function() {
$('#plantSearchBox').val("");
$('input:checkbox').prop('checked', false);
$(document).on('click', '.plantsClickable', function(event)
{
var desc;
for (var i = 0, len = plantsJson.length; i < len; i++)
{
if (plantsJson[i].Name === $(this).text())
{
$('#plantName').text(plantsJson[i].Name);
$('#plantLocation').text(FindPlantInLocation(plantsJson[i].Name) + " • ");
$('#plantRarity').text(FindPlantInRarity(plantsJson[i].Name, rarityJson));
$('#plantInfo').text(plantsJson[i].Description);
break;
}
}
if($('#plantLocation').text() == " • ")
{
$('#plantLocation').text("??? • ");
}
});
//this is the search function, it searches only the li in .scrollable, completely isolating it from any bugs that exist in the populate methods
$('#plantSearchBox').keyup(function() {
var g = $(this).val().toLowerCase();
if(g == ""){
$('.plantsClickable').show();
}
else
{
$('li.plantsClickable').each(function(){
var text = $(this).text().toLowerCase();
(text.indexOf(g) >= 0) ? $(this).show() : $(this).hide();
});
}
//ensures that "No Results" only shows up once
NoResults();
});
// click event that grabs name of clicked checkbox and calls populateScrollable or populateScrollableSelect depending on if any checkbox is checked or not
$(document).find("input:checkbox").click(function() {
if ($("input:checkbox:checked").length)
{
var filterList = [];
//this needs refining to cross reference
$("input:checkbox:checked").each(function () {
filterList.push($(this).val());
});
populateScrollableSelect(filterList);
}
else
{
populateScrollable();
}
});
$("input:radio").change(function() {
$("input:checkbox:checked").prop('checked', false);
populateScrollable();
});
//! everything past this point makes the roll tables work
$("#roll20").click( function()
{
var resultd20 = getRandomInt(1, 20);
$("#resultd20").text(resultd20);
});
$("#roll100").click( function()
{
var resultd100 = getRandomInt(1, 100);
$("#resultd100").text(resultd100);
});
$('.item').mousemove( function(e) {
$(this).children('p').show().css({'top': e.pageY + 10, 'left': e.pageX + 10});
});
$('.item').mouseleave( function() {
$(this).children('p').hide();
});
});
var dieNum;
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}