-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
909 lines (831 loc) · 27.4 KB
/
options.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/* Global variables
*/
var optEncountersToGraphSavedNonRaid = false;
var optEncountersToGraphSavedRaid = false;
var optCustomMinIvPercentSaved = false;
var optCustomRateModifierSaved = false;
var encountersToGraphDefaultNonRaid;
var encountersToGraphDefaultRaid;
/* PageOption objects
*
* PageOptionBool, PageOptionFloat, PageOptionFloatOrAny, PageOptionInt, PageOptionIntOrAny, and PageOptionString
* all inherit from the base PageOption. Each object provides a value property that can be set by .setValue(),
* written to the page with .writeToPage(), or read from the page with .getFromPage(). The previous value held
* is saved in the previousValue property.
*
* The pageElement is the html id of the input on the page, and must be one of 'select', 'textbox', or 'checkbox'.
* The pageContainerElement is the html id of the container div for the input.
*
* PageOption itself should not be used directly.
*/
//Base PageOption prototype
function PageOption(name,optionType,pageElement,pageElementType,pageContainerElement)
{
this.name = name;
if (optionType === "bool" || optionType === "int" || optionType === "float" || optionType === "string")
{
this.optionType = optionType;
}
else
{
throw new TypeError("PageOption constructor -- Invalid optionType: " + optionType);
}
this.pageElement = pageElement;
if (pageElementType === "select" || pageElementType === "textbox" || pageElementType === "checkbox")
{
this.pageElementType = pageElementType;
}
else
{
throw new TypeError("PageOption constructor -- Invalid pageElementType: " + pageElementType);
}
this.pageContainerElement = pageContainerElement;
}
PageOption.prototype.setValue = function(v)
{
this.previousValue = this.value;
this.value = v;
}
PageOption.prototype.writeToPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
document.getElementById(this.pageElement).value = this.value;
}
else if (this.pageElementType === "checkbox")
{
if (this.optionType === "bool")
{
document.getElementById(this.pageElement).checked = this.value;
}
else
{
throw new TypeError("PageOption writeToPage -- trying to write invalid optionType " + this.optionType + " to checkbox.");
}
}
}
//PageOptionString prototype -- inherits from PageOption
function PageOptionString(name,pageElement,pageElementType,pageContainerElement,defaultVal,validVals)
{
PageOption.call(this,name,"string",pageElement,pageElementType,pageContainerElement);
if (Array.isArray(validVals))
{
this.validVals = validVals;
}
else
{
throw new TypeError("PageOptionString constructor -- validVals must be an array.");
}
if (validVals.includes(defaultVal))
{
this.defaultVal = defaultVal;
this.value = defaultVal;
}
else
{
throw new RangeError("PageOptionString constructor -- invalid defaultVal.");
}
}
PageOptionString.prototype = Object.create(PageOption.prototype);
PageOptionString.prototype.setValue = function(v)
{
if (this.validVals.includes(v))
{
this.previousValue = this.value;
this.value = v.toString();
}
else
{
throw new RangeError(this.name + " has an invalid value: " + v + ".");
}
}
PageOptionString.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
this.setValue(document.getElementById(this.pageElement).value);
}
else if (this.pageElementType === "checkbox")
{
this.setValue(document.getElementById(this.pageElement).checked);
}
}
//PageOptionBool prototype -- inherits from PageObject
function PageOptionBool(name,pageElement,pageElementType,defaultVal)
{
PageOption.call(this,name,"bool",pageElement,pageElementType);
this.defaultVal = Boolean(defaultVal);
this.value = Boolean(defaultVal);
}
PageOptionBool.prototype = Object.create(PageOption.prototype);
PageOptionBool.prototype.setValue = function(v)
{
this.previousValue = this.value;
this.value = Boolean(v);
}
PageOptionBool.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
this.setValue(document.getElementById(this.pageElement).value === 'true');
}
else if (this.pageElementType === "checkbox")
{
this.setValue(document.getElementById(this.pageElement).checked);
}
}
//PageOptionFloat prototype -- inherits from PageOption
function PageOptionFloat(name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax)
{
PageOption.call(this,name,"float",pageElement,pageElementType,pageContainerElement);
if (Number.isFinite(defaultVal))
{
this.defaultVal = defaultVal;
this.value = defaultVal;
}
else
{
throw new TypeError("PageOptionFloat constructor -- invalid defaultVal.");
}
if (validMinType === "nomin" || validMinType === "inclusivemin" || validMinType === "exclusivemin")
{
this.validMinType = validMinType;
}
else
{
throw new TypeError("PageOptionFloat constructor -- invalid validMinType.");
}
if (Number.isFinite(validMin))
{
this.validMin = validMin;
}
else
{
throw new TypeError("PageOptionFloat constructor -- invalid validMin.");
}
if (validMaxType === "nomax" || validMaxType === "inclusivemax" || validMaxType === "exclusivemax")
{
this.validMaxType = validMaxType;
}
else
{
throw new TypeError("PageOptionFloat constructor -- invalid validMaxType.");
}
if (Number.isFinite(validMax))
{
this.validMax = validMax;
}
else
{
throw new TypeError("PageOptionFloat constructor -- invalid validMax.");
}
}
PageOptionFloat.prototype = Object.create(PageOption.prototype);
PageOptionFloat.prototype.setValue = function(v)
{
if (isNaN(v))
{
throw new TypeError(this.name + " must be a number.");
}
else
{
v = parseFloat(v);
}
if (!Number.isFinite(v))
{
throw new TypeError(this.name + " is not an valid number.");
}
else
{
if ((this.validMinType === "inclusivemin") && (v < this.validMin))
{
throw new RangeError(this.name + " must be at least " + this.validMin + ".");
}
else if ((this.validMinType === "exclusivemin") && (v <= this.validMin))
{
throw new RangeError(this.name + " must be greater than " + this.validMin + ".");
}
else if ((this.validMaxType === "inclusivemax") && (v > this.validMax))
{
throw new RangeError(this.name + " can't be greater than " + this.validMax + ".");
}
else if ((this.validMaxType === "exclusivemax") && (v >= this.validMax))
{
throw new RangeError(this.name + "must be less than " + this.validMax + ".");
}
else
{
this.previousValue = this.value;
this.value = v;
}
}
}
PageOptionFloat.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
this.setValue(document.getElementById(this.pageElement).value);
}
else if (this.pageElementType === "checkbox")
{
throw new TypeError("PageOptionFloat getFromPage -- Can't get float from checkbox on page");
}
}
//PageOptionFloatOrAny prototype -- inherits from PageOptionFloat
function PageOptionFloatOrAny(name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax)
{
if (defaultVal === "any")
{
PageOptionFloat.call(this,name,pageElement,pageElementType,pageContainerElement,0,validMinType,validMin,validMaxType,validMax);
this.defaultVal = "any";
this.value = "any";
}
else
{
PageOptionFloat.call(this,name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax);
}
}
PageOptionFloatOrAny.prototype = Object.create(PageOptionFloat.prototype);
PageOptionFloatOrAny.prototype.setValue = function(v)
{
if (v === "any")
{
this.previousValue = this.value;
this.value = "any";
}
else
{
PageOptionFloat.prototype.setValue.call(this, v);
}
}
PageOptionFloatOrAny.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
if (document.getElementById(this.pageElement).value === 'any')
{
this.setValue('any');
}
else
{
this.setValue(document.getElementById(this.pageElement).value);
}
}
else if (this.pageElementType === "checkbox")
{
throw new TypeError("PageOptionFloat getFromPage -- Can't get float from checkbox on page");
}
}
//PageOptionInt prototype -- inherits from PageOption
function PageOptionInt(name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax)
{
PageOption.call(this,name,"int",pageElement,pageElementType,pageContainerElement);
if (Number.isInteger(defaultVal))
{
this.defaultVal = defaultVal;
this.value = defaultVal;
}
else
{
throw new TypeError("PageOptionInt constructor -- invalid defaultVal.");
}
if (validMinType === "nomin" || validMinType === "inclusivemin" || validMinType === "exclusivemin")
{
this.validMinType = validMinType;
}
else
{
throw new TypeError("PageOptionInt constructor -- invalid validMinType.");
}
if (Number.isInteger(validMin))
{
this.validMin = validMin;
}
else
{
throw new TypeError("PageOptionInt constructor -- invalid validMin.");
}
if (validMaxType === "nomax" || validMaxType === "inclusivemax" || validMaxType === "exclusivemax")
{
this.validMaxType = validMaxType;
}
else
{
throw new TypeError("PageOptionInt constructor -- invalid validMaxType.");
}
if (Number.isInteger(validMax))
{
this.validMax = validMax;
}
else
{
throw new TypeError("PageOptionInt constructor -- invalid validMax.");
}
}
PageOptionInt.prototype = Object.create(PageOption.prototype);
PageOptionInt.prototype.setValue = function (v)
{
if (isNaN(v))
{
throw new TypeError(this.name + " is not a valid integer.");
}
else
{
v = parseFloat(v);
}
if (!Number.isInteger(v))
{
throw new TypeError(this.name + " must be an integer.");
}
else
{
if ((this.validMinType === "inclusivemin") && (v < this.validMin))
{
throw new RangeError(this.name + " must be at least " + this.validMin + ".");
}
else if ((this.validMinType === "exclusivemin") && (v <= this.validMin))
{
throw new RangeError(this.name + " must be greater than " + this.validMin + ".");
}
else if ((this.validMaxType === "inclusivemax") && (v > this.validMax))
{
throw new RangeError(this.name + " can't be greater than " + this.validMax + ".");
}
else if ((this.validMaxType === "exclusivemax") && (v >= this.validMax))
{
throw new RangeError(this.name + "must be less than " + this.validMax + ".");
}
else
{
this.previousValue = this.value;
this.value = v;
}
}
}
PageOptionInt.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
this.setValue(document.getElementById(this.pageElement).value);
}
else if (this.pageElementType === "checkbox")
{
throw new TypeError("PageOptionFloat getFromPage -- Can't get int from checkbox on page");
}
}
//PageOptionIntOrAny prototype -- inherits from PageOptionInt
function PageOptionIntOrAny(name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax)
{
if (defaultVal === "any")
{
PageOptionInt.call(this,name,pageElement,pageElementType,pageContainerElement,0,validMinType,validMin,validMaxType,validMax);
this.defaultVal = "any";
this.value = "any";
}
else
{
PageOptionFloat.call(this,name,pageElement,pageElementType,pageContainerElement,defaultVal,validMinType,validMin,validMaxType,validMax);
}
}
PageOptionIntOrAny.prototype = Object.create(PageOptionInt.prototype);
PageOptionIntOrAny.prototype.setValue = function(v)
{
if (v === "any")
{
this.previousValue = this.value;
this.value = "any";
}
else
{
PageOptionInt.prototype.setValue.call(this, v);
}
}
PageOptionIntOrAny.prototype.getFromPage = function()
{
if (this.pageElementType === "select" || this.pageElementType === "textbox")
{
if (document.getElementById(this.pageElement).value === 'any')
{
this.setValue('any');
}
else
{
this.setValue(document.getElementById(this.pageElement).value);
}
}
else if (this.pageElementType === "checkbox")
{
throw new TypeError("PageOptionFloat getFromPage -- Can't get int from checkbox on page");
}
}
/* pageOpts global variable -- contains various PageOption objects that can be set */
var pageOpts = { appraisal: new PageOptionString("Appraisal","appraisal","select","opt_minivpercent","best",["best","good","aboveaverage","any","other"]),
minivpercent: new PageOptionFloat("Minimum IV Percentage","min_iv_percent","textbox","opt_minivpercent",82.2,"inclusivemin",0,"inclusivemax",100),
minattackiv: new PageOptionIntOrAny("Minimum Attack IV","min_attack_iv","select","opt_minattackiv","any","inclusivemin",0,"inclusivemax",15),
mindefenseiv: new PageOptionIntOrAny("Minimum Defense IV","min_defense_iv","select","opt_mindefenseiv","any","inclusivemin",0,"inclusivemax",15),
minstaminaiv: new PageOptionIntOrAny("Minimum Stamina IV","min_stamina_iv","select","opt_minstaminaiv","any","inclusivemin",0,"inclusivemax",15),
encountertype: new PageOptionString("Encounter Type","encounter_type","select","opt_encountertype","normal",["normal","raid","boosted"]),
minlevel: new PageOptionIntOrAny("Minimum Pokemon Level","min_level","select","opt_minlevel","any","inclusivemin",1,"inclusivemax",35),
trainerlevel: new PageOptionInt("Trainer Level","trainer_level","select","opt_trainerlevel",30,"inclusivemin",1,"inclusivemax",30),
ratemodifierselect: new PageOptionString("Rate Modifier Selection","ratemodifierselect","select","opt_ratemodifier","1",["1","450","75","60","50","45","35","19","24.5","custom"]), //TODO: Update pageElement
ratemodifier: new PageOptionFloat("Rate Modifier","rate_modifier","textbox","opt_ratemodifier",1,"exclusivemin",0,"inclusivemax",1),
ratemodifierinv: new PageOptionFloat("Rate Modifier (inverse)","rate_modifier_inv","textbox","opt_ratemodifier",1,"exclusivemin",0,"nomax",0),
pokemontoget: new PageOptionInt("Number of Pokemon Needed","pokemon_to_get","textbox","opt_pokemontoget",1,"inclusivemin",1,"nomax",0),
chartmode: new PageOptionString("Chart Mode","chart_mode","select","opt_chartmode","single",["single","area","pmf","cdf","normalpdf","normalcdf"]),
encounterstograph: new PageOptionInt("Encounters to Graph","encounters_to_graph","textbox","opt_encounterstograph",200,"inclusivemin",1,"nomax",0), //TODO: Set max
autoencounterstograph: new PageOptionBool("Auto Encounters to Graph","auto_encounters_to_graph","checkbox","opt_encounterstograph",true)
};
/* resetOptionDefaults()
* Reset all pageOpts to their defaults
*/
function resetOptionDefaults()
{
optEncountersToGraphSavedNonRaid = false;
optEncountersToGraphSavedRaid = false;
optCustomMinIvPercentSaved = false;
optCustomRateModifierSaved = false;
encountersToGraphDefaultNonRaid = 300;
encountersToGraphDefaultRaid = 30;
for (var k in pageOpts)
{
pageOpts[k].setValue(pageOpts[k].defaultVal);
}
}
/* getPageOptions()
* Get all pageOpts from the values on the page form
*/
function getPageOptions()
{
clearErrors();
var err = 0;
for (var k in pageOpts)
{
try
{
pageOpts[k].getFromPage();
}
catch (e)
{
alert(e.message);
pageOpts[k].setValue(pageOpts[k].previousValue);
pageOpts[k].writeToPage();
}
}
return;
}
/* setPageOptions()
* Write all pageOpts to the page form
*/
function setPageOptions()
{
for (var k in pageOpts)
{
pageOpts[k].writeToPage();
}
}
/* processOptions()
* Process various selection and input boxes on the page
* Print errors if necessary
*
* Called from onchange on all <select> and <input>
*/
function processOptions()
{
clearErrors();
var pageOptsError = getPageOptions();
/* Process appraisal selection
* Change min_iv_percent to match selected value and disable/enable the text box as needed
*/
if(pageOpts.appraisal.value === "best")
{
pageOpts.minivpercent.setValue(82.2);
document.getElementById("min_iv_percent").disabled = true;
}
else if(pageOpts.appraisal.value === "good")
{
pageOpts.minivpercent.setValue(66.7);
document.getElementById("min_iv_percent").disabled = true;
}
else if(pageOpts.appraisal.value === "aboveaverage")
{
pageOpts.minivpercent.setValue(51.1);
document.getElementById("min_iv_percent").disabled = true;
}
else if(pageOpts.appraisal.value === "any")
{
pageOpts.minivpercent.setValue(0);
document.getElementById("min_iv_percent").disabled = true;
}
else if(pageOpts.appraisal.value === "other")
{
document.getElementById("min_iv_percent").disabled = false;
//If the Minimum IV Percent box was just changed, save the value.
if (pageOpts.minivpercent.previousValue != pageOpts.minivpercent.value)
{
optCustomMinIvPercentSaved = pageOpts.minivpercent.value;
}
//Otherwise, restore the saved value.
else if (optCustomMinIvPercentSaved)
{
pageOpts.minivpercent.setValue(optCustomMinIvPercentSaved);
}
}
/* Process encounter type selection
* Set the encounters to graph text box to either the default or the saved value for the selected encounter type
* Enable or disable the minimum level and trainer level selections
* Enable or disable the different Pokemon levels that become available in normal or boosted mode
*/
if(pageOpts.encountertype.value === "normal")
{
document.getElementById("opt_minlevel").disabled = false;
document.getElementById("opt_trainerlevel").disabled = false;
changePokeLvlOption(31,35,"disabled");
changePokeLvlOption(1,5,"enabled");
document.getElementById("opt_minlevel").style.display = "block"; //Display the minimum Pokemon level / Trainer level div
document.getElementById("opt_trainerlevel").style.display = "block";
}
else if(pageOpts.encountertype.value === "boosted")
{
document.getElementById("opt_minlevel").disabled = false;
document.getElementById("opt_trainerlevel").disabled = false;
changePokeLvlOption(31,35,"enabled");
changePokeLvlOption(1,5,"disabled");
document.getElementById("opt_minlevel").style.display = "block";
document.getElementById("opt_minlevel").style.display = "block";
}
else if(pageOpts.encountertype.value === "raid")
{
document.getElementById("opt_minlevel").disabled = true;
document.getElementById("opt_minlevel").disabled = true;
document.getElementById("opt_minlevel").style.display = "none"; //Hide the minimum Pokemon level / Trainer level div
document.getElementById("opt_minlevel").style.display = "none";
}
/* Process minimum Pokemon level selection
* Disable the trainer level selection if the minimum Pokemon level is set to any.
*/
if(pageOpts.minlevel.value === "any")
{
document.getElementById("trainer_level").disabled = true;
}
else
{
document.getElementById("trainer_level").disabled = false;
}
/* Process the rate modifier select box
* If the rate is set to custom, enable the text box to set a custom rate.
* Otherwise, disable the text box and fill it with the selected value.
*/
if (pageOpts.ratemodifierselect.value === "custom")
{
document.getElementById("rate_modifier").disabled = false;
document.getElementById("rate_modifier_inv").disabled = false;
//If the rate modifier box was just changed, save the value and update the inverse.
if (pageOpts.ratemodifier.previousValue != pageOpts.ratemodifier.value)
{
optCustomRateModifierSaved = pageOpts.ratemodifier.value;
pageOpts.ratemodifierinv.setValue(parseFloat((1/pageOpts.ratemodifier.value).toFixed(4)));
}
//If the inverse rate modifier box was just changed, calculate the rate modifier, update it, and save the value.
else if (pageOpts.ratemodifierinv.previousValue != pageOpts.ratemodifierinv.value)
{
pageOpts.ratemodifier.setValue(parseFloat((1/pageOpts.ratemodifierinv.value).toFixed(8)));
optCustomRateModifierSaved = pageOpts.ratemodifier.value;
}
//Otherwise, set both boxes to the saved value.
else if (optCustomRateModifierSaved)
{
pageOpts.ratemodifier.setValue(optCustomRateModifierSaved);
pageOpts.ratemodifierinv.setValue(parseFloat((1/optCustomRateModifierSaved).toFixed(4)));
}
}
else
{
document.getElementById("rate_modifier").disabled = true;
document.getElementById("rate_modifier_inv").disabled = true;
pageOpts.ratemodifierinv.setValue(parseFloat(pageOpts.ratemodifierselect.value));
pageOpts.ratemodifier.setValue(parseFloat((1/pageOpts.ratemodifierselect.value).toFixed(8)));
}
/* Process Chart Mode select
* Enable the autoencounterstograph checkbox for single and area chart modes
*/
if (pageOpts.chartmode.value === "single" || pageOpts.chartmode.value === "area")
{
document.getElementById("autoencounterstograph").style.display = "inline";
}
else
{
document.getElementById("autoencounterstograph").style.display = "none";
}
/* Process Encounters to Graph box
* If the value was manually updated, turn off automatic encounters to graph
*/
if (pageOpts.encounterstograph.value != pageOpts.encounterstograph.previousValue)
{
pageOpts.autoencounterstograph.setValue(false);
}
if (!validateOptions()) //Calculate auto encounters and write everything to the page if there were no validation errors
{
if ((pageOpts.autoencounterstograph.value === true) && (pageOpts.chartmode.value === "single" || pageOpts.chartmode.value === "area"))
{
var autoEncountersToGraphValue = calculateAutoEncountersToGraph(); //Can't do this in a single line like pageOpts.encounterstograph.setValue(calculateAutoEncountersToGraph())
pageOpts.encounterstograph.setValue(autoEncountersToGraphValue); //...because pageOpts is reset deep within calculateAutoEncountersToGraph().
}
setPageOptions();
}
}
/* validateOptions()
* Validates the values in pageOpts to ensure the combination is valid.
* Adds any errors and calls processErrors() to print them.
*
* Returns 0 if no errors were found.
*/
function validateOptions(optionsSource)
{
//clearErrors();
//Checks if value is contained in validvals
function isValid(value, validvals)
{
valid = false;
for (var i = 0; i < validvals.length; i++)
{
if (value === validvals[i])
{
valid = true;
}
}
return valid;
}
// Validate combination of Minimum IV Percentage and Appraisal
if (pageOpts.appraisal.value !== "other")
{
if (((pageOpts.appraisal.value === "best") && (pageOpts.minivpercent.value != 82.2)) ||
((pageOpts.appraisal.value === "good") && (pageOpts.minivpercent.value != 66.7)) ||
((pageOpts.appraisal.value === "aboveaverage") && (pageOpts.minivpercent.value != 51.1)) ||
((pageOpts.appraisal.value === "any") && (pageOpts.minivpercent.value != 0)))
{
addError("Minimum IV Percent doesn't match Appraisal selection.",pageOpts.appraisal);
}
}
// Validate combination of trainer level and pokemon level
if (pageOpts.minlevel.value !== "any")
{
if (pageOpts.encountertype.value === "normal")
{
if (pageOpts.minlevel.value > pageOpts.trainerlevel.value)
{
addError("Minimum Pokemon level can't be greater than Trainer Level.",pageOpts.minlevel);
}
if (pageOpts.minlevel.value > 30)
{
addError("Minimum Pokemon level can't be greater than 30 unless weather boosted.",pageOpts.minlevel);
}
}
else if (pageOpts.encountertype.value === "boosted")
{
if (pageOpts.minlevel.value > pageOpts.trainerlevel.value + 5)
{
addError("Minimum Pokemon level can't be greater than Trainer Level + 5 when weather boosted.",pageOpts.minlevel);
}
if (pageOpts.minlevel.value < 6)
{
addError("Minimum Pokemon level can't be less than 6 when weather boosted.",pageOpts.minlevel);
}
}
}
// Validate combination of Rate Modifier and Rate Modifier Inverse
if ((pageOpts.ratemodifier.value > (1.001 / pageOpts.ratemodifierinv.value)) ||
(pageOpts.ratemodifier.value < (0.999 / pageOpts.ratemodifierinv.value)))
{
//The two values need to be inverses of each other, but allow some .1% leeway to account for float rounding
addError("Rate modifier and inverse do not match.",pageOpts.ratemodifier);
}
// Validate combination of chart mode and number of Pokemon needed */
if (pageOpts.chartmode.value === "single")
{
if (pageOpts.pokemontoget.value > 32)
{
addError("Simple chart only supports a maximum of 32 Pokemon needed.",pageOpts.pokemontoget);
}
}
else if (pageOpts.chartmode.value === "area")
{
if (pageOpts.pokemontoget.value > 10)
{
addError("Stacked area chart only supports a maximum of 10 Pokemon needed.",pageOpts.pokemontoget);
}
else if(pageOpts.pokemontoget.value === 1)
{
addError("The stacked area chart must have more than 1 matching Pokemon needed.",pageOpts.pokemontoget);
}
}
if (pageOpts.encounterstograph.value > Number.MAX_SAFE_INTEGER)
{
addError("Number of encounters to graph can't be bigger than " + Number.MAX_SAFE_INTEGER + ".",pageOpts.encounterstograph);
}
else if (pageOpts.encounterstograph.value < pageOpts.pokemontoget.value)
{
addError("Number of encounters to graph can't be smaller than the number of Pokemon needed.",pageOpts.encounterstograph);
}
processErrors(optionsSource);
return numErrors;
}
var errorText;
var numErrors = 0;
function addError(err,invalidOption)
{
errorText += `<li>${err}</li>`;
if (invalidOption)
{
document.getElementById(invalidOption.pageContainerElement).className += " invalidoptionentry";
}
numErrors++;
}
function clearErrors()
{
errorText = "";
numErrors = 0;
optionEntryDivs = document.getElementsByClassName("optionentry");
var i;
for (i=0; i < optionEntryDivs.length; i++)
{
optionEntryDivs[i].className = "optionentry";
}
}
function processErrors(optionsSource)
{
// Display any error text
if (numErrors > 0)
{
document.getElementById("calcbutton").disabled = true;
if (optionsSource == "url")
{
document.getElementById("urlerror").style.display = "block";
document.getElementById("urlerror").innerHTML = "<div class='errortext'><div class='errorheading'>" +
"You got to this page via a direct URL that tried to preset the options and draw a finished chart for you.<br><br>" +
"Unfortunately, something went wrong, and the options encoded in the URL were invalid.<br><br>" +
"Make sure you copied the full URL properly, or contact the person that provided it to you.<br><br>" +
"The errors were:" +
"</div><ul>" + errorText + "</ul></div>";
}
else
{
document.getElementById("optionerror").style.display = "block";
document.getElementById("optionerror").innerHTML = "<div class='errortext'><div class='errorheading'>Invalid Options:</div><ul>" + errorText + "</ul></div>";
}
}
else
{
document.getElementById("optionerror").innerHTML = "";
document.getElementById("optionerror").style.display = "none";
document.getElementById("urlerror").innerHTML = "";
document.getElementById("urlerror").style.display = "none";
document.getElementById("calcbutton").disabled = false;
clearErrors();
}
}
/* changePokeLvlOption()
* Enable or Disable any option elements between the low and high in the Pokemon level selection
*/
function changePokeLvlOption(optionnumlow,optionnumhigh,state)
{
var i,disabled;
if (state === "disabled")
{
disabled = true;
}
else if (state === "enabled")
{
disabled = false;
}
else
{
disabled = false;
}
for (i = optionnumlow; i <= optionnumhigh; i++)
{
document.getElementById("pokelvl" + i).disabled = disabled;
}
}
/* changeTrainerLvlOption()
* Enable or Disable any option elements between the low and high in the trainer level selection
*/
function changeTrainerLvlOption(optionnumlow,optionnumhigh,state)
{
var i,disabled;
if (state === "disabled")
{
disabled = true;
}
else if (state === "enabled")
{
disabled = false;
}
else
{
disabled = false;
}
for (i = optionnumlow; i <= optionnumhigh; i++)
{
document.getElementById("trainerlvl" + i).disabled = disabled;
}
}