-
Notifications
You must be signed in to change notification settings - Fork 19
/
renderer.js
3993 lines (3806 loc) · 176 KB
/
renderer.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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jshint node: true */
/* jshint jquery: true */
/* jshint esversion: 6 */
/* jshint browser: true */
"use strict";
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
// Requirements
var fs = require( "fs" );
var async = require( "async" );
var marked = require( "marked" );
var open = require( "open" );
const {app} = require( "electron" ).remote;
const path = require( "path" );
var ipcRenderer = require('electron').ipcRenderer;
var config = require( app.getPath( "userData" ) + path.sep + "config.json" );
if ( Object.keys( config ).length === 0 ) {
console.log( "Fallback" );
config = require( __dirname + "/config.json" );
}
// Connect to server
var io = require( "socket.io-client" )
var socket;
const pako = require('pako');
var itemTypes = require( "./itemTypes.json" );
var Item = require( "./item.js" );
var Misc = require( "./misc.js" );
var Filter = require( "./filter.js" );
var Filters = require( "./filters.js" );
var Group = require( "./filter-group.js" );
var FilterGroups = require( "./filter-groups.js" );
var Currency = require( "./currency.js" );
var Chunk = require( "./chunk.js" );
var BlackList = require( "./blacklist.js" );
// Current leagues
var leagues = config.leagues;
var groups = new FilterGroups([]);
var filters = new Filters([]);
var mu = require( 'mu2' );
mu.root = __dirname + '/templates';
const eNotify = require('electron-notify');
eNotify.setConfig({
// appIcon: path.join(__dirname, 'logo.png'),
defaultStyleText: {
color: 'grey',
// fontWeight: 'bold',
},
defaultStyleContainer: {
color: 'white',
overflow: 'hidden',
padding: 8,
fontFamily: 'Arial',
fontSize: 12,
position: 'relative',
lineHeight: '15px'
},
});
// Get signal to close all notifications from main
ipcRenderer.on( 'close-notifications', function () {
eNotify.closeAll();
socket.disconnect();
});
var ncp = require( "copy-paste" );
var editingFilter = ""; // Are we editing filters at the moment
var editingGroup = ""; // Are we editing a group at the moment
var downloading = false; // Is the tool downloading chunks at the moment
var results = {};
var resultsId = {};
var entryLookup = {};
var itemInStash = {};
// Price regexp
var priceReg = /(?:([0-9\.]+)|([0-9]+)\/([0-9]+)) ([a-z]+)/g;
var currencyRates = {};
var itemRates = {};
var delayQueue = []; // Stores filtered items
var displayingNotification = false;
var lastItem = null;
var prices = {};
var loadedAffix = false;
var editingAffix = "";
var interrupt = false;
var sold = 0;
var audio = new Audio( __dirname + '/' + config.sound );
var itemBlackList;
var playerBlackList;
var modGroups = {};
var modGroupsId = {};
// Elements to enable/disable when group editing
var elementList = {
"fields": [
"#item", "#item-type", "#armor", "#es", "#evasion", "#affixes",
"#affix-min", "#affix-max", "#sockets-total", "#sockets-red",
"#sockets-green", "#sockets-blue", "#sockets-white", "#level", "#quality",
"#experience", "#tier", "#open-prefixes", "#open-suffixes", "#map-quantity",
"#map-rarity", "#map-pack-size", "#dps", "#pdps", "#edps"
],
"selects": [
"#links", "#corrupted", "#crafted", "#enchanted",
"#identified", "#rarity"
]
};
var editedGroupId;
var editedGroupType;
var originalQueueInterval = config.NOTIFICATION_QUEUE_INTERVAL;
const CURRENCY_RATES_REFRESH_INTERVAL = 30 * 60 * 1000;
const ITEM_RATES_REFRESH_INTERVAL = 60 * 60 * 1000;
$( document).ready( function() {
// From https://stackoverflow.com/questions/40544394/stoppropagation-in-scroll-method-wont-stop-outer-div-from-scrolling
// Prevent parent scrolling while scrolling child
$.fn.dontScrollParent = function()
{
this.bind('mousewheel DOMMouseScroll',function(e)
{
var delta = e.originalEvent.wheelDelta || -e.originalEvent.detail;
if (delta > 0 && $(this).scrollTop() <= 0)
return false;
if (delta < 0 && $(this).scrollTop() >= this.scrollHeight - $(this).height())
return false;
return true;
});
}
$( "#conditions" ).dontScrollParent();
// Interface - actions binding
// ------------------------------------------------------------------------
// Cancel editing when 'Cancel filter' is clicked
$( "#cancel-filter" ).click( function() {
cancelEditAction();
});
// When clicking on 'Add group', add group
$( "#add-group" ).click( function() {
addGroupAction();
});
// When clicking on 'Add filter', add filter
$( "#add-filter" ).click( function() {
addFilterAction();
});
// When typing in the filter filter input text field
$( "#filter-filter" ).keyup( function() {
filterFilterListAction();
});
// When typing in the item filter input text field
$( "#item-filter" ).keyup( function() {
filterResultListAction();
});
// Fold/unfold filter list when clicking on the arrow
$( "#fold-filters" ).click( function() {
foldFilters();
});
// Actions
// ------------------------------------------------------------------------
// Fold/unfold filter list action
var foldFilters = function() {
$( "#fold-filters" ).toggleClass( "folded" );
// If filter was unfolded
if ( $( "#fold-filters" ).hasClass( "folded" )) {
// Fold content
$( "#filters" ).slideUp();
// Disable filtering
$( "#filter-filter" ).prop( "disabled", true );
// Otherwise
} else {
// Unfold content
$( "#filters" ).slideDown();
// Enable filtering
$( "#filter-filter" ).prop( "disabled", false );
}
};
// View setup when dismissing a new update
var dismissUpdate = function() {
// Unblur everything
$( ".filter-form" ).removeClass( "blurred" );
$( ".filter-interaction" ).removeClass( "blurred" );
$( ".filter-list" ).removeClass( "blurred" );
$( ".filter-results" ).removeClass( "blurred" );
$( ".progress" ).removeClass( "blurred" );
$( ".cover" ).fadeOut();
$( ".new-update" ).fadeOut( "fast" );
};
// Action when the download button is pressed
var downloadUpdate = function( version ) {
open( "https://github.com/licoffe/POE-sniper/releases/" + version );
};
// Animate scroll to the top of the page
var scrollToTopAction = function() {
// scroll body to 0px on click
$('body,html').animate({
scrollTop: 0
}, config.SCROLL_BACK_TOP_SPEED );
return false;
};
// Action when the user is typing in the filter items text field
var filterResultListAction = function() {
// TODO: See if OK performance wise
$( ".results .collection-item" ).show();
var text = $( "#item-filter" ).val().toLowerCase();
$( ".results .collection-item" ).each( function() {
if ( text !== "" ) {
var itemName = $( this ).find( ".item" ).text();
var leagueName = $( this ).find( ".item-league" ).text();
var typeLine = $( this ).find( ".item-typeLine" ).text();
// If the item name nor league match the text typed, hide the item
if ( itemName.toLowerCase().indexOf( text ) === -1 &&
leagueName.toLowerCase().indexOf( text ) === -1 &&
typeLine.toLowerCase().indexOf( text ) === -1 ) {
$( this ).hide();
}
}
});
$( "#results-amount" ).text( $( ".entry:visible" ).length );
};
// Action when the user is typing in the filter filters text field
var filterFilterListAction = function() {
var text = $( "#filter-filter" ).val().toLowerCase();
$( "#filters .collection-item" ).each( function() {
if ( text === "" ) {
$( this ).show();
} else {
var itemName = $( this ).find( ".item" ).text();
if ( itemName.toLowerCase().indexOf( text ) === -1 ) {
$( this ).hide();
}
}
});
// Update filter amount
updateFilterAmount();
};
var resetFilters = function() {
$( "#item" ).val( "" );
$( "#price" ).val( "" );
$( "#currency" ).val( "chaos" );
$( "#currency").material_select();
$( "#links" ).val( "any" );
$( "#links").material_select();
$( "#sockets-total" ).val( "" );
$( "#sockets-red" ).val( "" );
$( "#sockets-green" ).val( "" );
$( "#sockets-blue" ).val( "" );
$( "#sockets-white" ).val( "" );
$( "#corrupted" ).val( "any" );
$( "#corrupted").material_select();
$( "#crafted" ).val( "any" );
$( "#crafted").material_select();
$( "#enchanted" ).val( "any" );
$( "#enchanted").material_select();
$( "#identified" ).val( "any" );
$( "#identified").material_select();
$( "#level" ).val( "" );
$( "#tier" ).val( "" );
$( "#experience" ).val( "" );
$( "#quality" ).val( "" );
$( "#rarity" ).val( "any" );
$( "#rarity").material_select();
$( "#armor" ).val( "" );
$( "#es" ).val( "" );
$( "#evasion" ).val( "" );
$( "#dps" ).val( "" );
$( "#pdps" ).val( "" );
$( "#edps" ).val( "" );
$( "#price-bo" ).prop( "checked", true );
$( "#clipboard" ).prop( "checked", false );
$( "#convert-currency" ).prop( "checked", true );
// $( "#affixes-list" ).empty();
$( "#conditions" ).empty();
modGroups = {};
modGroupsId = {};
$( "#affix-weight" ).val( "" );
$( "#mod-group-min" ).val( "" );
$( "#mod-group-max" ).val( "" );
$( "#condition-selector" ).val( "and" );
$( "#condition-selector" ).material_select();
$( "#item-type" ).val( "" );
$( "#affix-min" ).val( "" );
$( "#affix-max" ).val( "" );
$( "#affixes" ).val( "" );
$( "#open-prefixes" ).val( "" );
$( "#open-suffixes" ).val( "" );
$( "#map-quantity" ).val( "" );
$( "#map-rarity" ).val( "" );
$( "#map-pack-size" ).val( "" );
Materialize.updateTextFields();
loadedAffix = false;
editingAffix = "";
$( "#add-affix" ).addClass( "disabled" );
$( "#cancel-affix" ).addClass( "disabled" );
$( "#add-affix" ).text( "Add" );
$( "#affixes" ).prop( "disabled", false );
resetConditionSelector();
$( ".form-affix-weight" ).hide();
$( ".form-affix-value" ).show();
};
// When selecting mod group type, toggle input fields
$( "#condition-selector" ).change( function() {
var val = $( this ).val();
// Empty group min/max fields
$( "#mod-group-min" ).val( "" );
$( "#mod-group-max" ).val( "" );
Materialize.updateTextFields();
// Enable/disable group min/max fields
if ( val.indexOf( "not" ) !== -1 || val.indexOf( "if" ) !== -1 || val.indexOf( "and" ) !== -1 ) {
$( "#mod-group-min" ).attr( "disabled", true );
$( "#mod-group-max" ).attr( "disabled", true );
} else {
$( "#mod-group-min" ).attr( "disabled", false );
$( "#mod-group-max" ).attr( "disabled", false );
}
if ( val.indexOf( "weight" ) !== -1 ) {
$( ".form-affix-weight" ).show();
$( ".form-affix-value" ).hide();
} else {
$( ".form-affix-weight" ).hide();
$( ".form-affix-value" ).show();
}
// Fill the group min max using values from modGroups
// Find the mod with the right id in modGroups
async.eachLimit( Object.keys( modGroups ), 1, function( group, cbGroup ) {
if ( group === val ) {
console.log( modGroups[group].min );
$( "#mod-group-min" ).val( modGroups[group].min );
$( "#mod-group-max" ).val( modGroups[group].max );
Materialize.updateTextFields();
}
cbGroup();
});
});
// When clicking on 'Clear Filter'
var cancelEditAction = function() {
resetFilters();
// If we are editing and the button is 'Clear Filter'
if ( $( this ).text() !== "Clear filter" ) {
$( "#add-filter" ).html( "<i class=\"material-icons\">playlist_add</i><span>Add filter</span>" );
$( "#cancel-filter" ).html( "<i class=\"material-icons\">delete</i><span>Clear filter</span>" );
$( "#cancel-filter" ).removeClass( "red" );
$( "#add-filter" ).removeClass( "green" );
editingFilter = "";
}
editingGroup = "";
enableElements( elementList );
};
var renderAffixes = function( filter, cb ) {
// Format mods
var index = 0;
async.eachLimit( filter.modGroups, 1, function( group, cbGroup ) {
// Only display non empty groups
if ( Object.keys( group.mods ).length > 0 ) {
var generatedModGroup = "";
var obj = {};
if ( group.type === "SUM" || group.type === "COUNT" || group.type === "WEIGHT" ) {
index++;
var groupMinDisp = group.min !== "" ? group.min : "…";
var groupMaxDisp = group.max !== "" ? group.max : "…";
var parameter = "( " + groupMinDisp + " - " + groupMaxDisp + " )";
obj = {
"id": group.id,
"type": group.type,
"index": "[" + index + "]",
"parameter": parameter
};
} else {
obj = {
"id": group.id,
"type": group.type,
"index": "",
"parameter": ""
};
}
$( "#filter-detail-" + filter.id + " .affix-filter-list" ).append(
"<span class=\"badge badge-" + group.type + "\" data-badge-caption=\"" + group.type + "\"></span>" +
"<span class=\"condition-parameter\">" + obj.parameter + "</span><br>"
);
async.eachLimit( Object.keys( group.mods ), 1, function( mod, cbMod ) {
var weight = "";
if ( group.type === "WEIGHT" ) {
weight = "Weight: " + group.mods[mod].weight;
}
var generated = "";
// Find out the mod type (explicit, implicit, etc.)
var extractReg = /^\(([a-zA-Z ]+)\)\s*/;
var sharpReg = /#/;
var match = extractReg.exec( mod );
var matched;
if ( !match ) {
matched = "Explicit";
} else {
matched = match[1];
}
var displayMin = "<span class='value'>" + group.mods[mod].min + "</span>";
var displayMax = "<span class='value'>" + group.mods[mod].max + "</span>";
var title = mod;
var count = ( title.match( /#/g ) || []).length;
if ( count > 1 ) {
title = title.replace( "#", displayMin );
title = title.replace( "#", displayMax );
} else {
title = mod.replace(
"#", "( " + displayMin + " - " +
displayMax + " )" );
}
// title = title.replace( sharpReg, "( " + displayMin + " - " + displayMax + " )" );
var obj = {
"title": title.replace( /^\([a-zA-Z ]+\)\s*/, "" ),
"id": Misc.guidGenerator(),
"typeLC": matched.toLowerCase(),
"type": matched,
"weight": weight
};
mu.compileAndRender( "affix-filter.html", obj )
.on( "data", function ( data ) {
generated += data.toString();
})
.on( "end", function () {
// console.log( "Modified: " + generated );
// $( "#condition-container-" + group.id ).append( generated );
// $( "#" + obj.id ).data( "data-item", obj );
// // When clicking on remove affix
// $( ".remove-affix" ).click( function() {
// $( this ).parent().parent().remove();
// });
$( "#filter-detail-" + filter.id + " .affix-filter-list" ).append( generated );
cbMod();
});
}, function() {
cbGroup();
});
} else {
cbGroup();
}
}, function() {
if ( filter.modGroups.length === 0 ) {
$( "#filter-detail-" + filter.id + " .affix-filter-list" ).hide();
}
cb();
});
};
// When adding a new group
var addGroupAction = function() {
var group = {
name: "new-group#" + groups.groupList.length,
id: Misc.guidGenerator(),
checked: true,
color: "#22ff93"
};
group = new Group( group );
groups.add( group );
groups.save();
appendAndBind( group, function() {});
};
var formatTitle = function( formData, str ) {
var min = 0;
var max = 9999999;
var step = 0.0001;
var title = "";
if ( formData[str] !== "" ) {
if ( formData[str + "Min"] !== min && formData[str + "Max"] !== max ) {
title += "<span class=\"filter-property\">" + str + " [" + formData[str + "Min"] +
" - " + formData[str + "Max"] + "]</span>";
} else if ( formData[str + "Min"] !== min ) {
if ( formData[str + "Min"] > Math.floor( formData[str + "Min"])) {
title += "<span class=\"filter-property\">" + str + " >" + Math.floor(formData[str + "Min"]) + "</span>";
} else {
title += "<span class=\"filter-property\">" + str + " ≥" + formData[str + "Min"] + "</span>";
}
} else {
if ( formData[str + "Max"] < Math.ceil( formData[str + "Max"] )) {
title += "<span class=\"filter-property\">" + str + " <" + Math.ceil(formData[str + "Max"]) + "</span>";
} else {
title += "<span class=\"filter-property\">" + str + " ≤" + formData[str + "Max"] + "</span>";
}
}
}
return title;
};
var formatFilter = function( formData, callback ) {
console.log( modGroups );
async.eachLimit( Object.keys( modGroups ), 1, function( group, cbGroup ) {
async.eachLimit( Object.keys( modGroups[group].mods ), 1, function( mod, cbMod ) {
var cleanedAffix =
mod.replace( /(<span class=\'value\'>[^<>]+<\/span>)/g, "#" )
.replace( "( # - # )", "#" )
.replace( "Unique explicit", "Explicit" )
.replace( "Essence", "Explicit" )
.replace( "Talisman implicit", "Implicit" );
formData.affixes[cleanedAffix] = {
"min": modGroups[group].mods[mod].min,
"max": modGroups[group].mods[mod].max,
"type": modGroups[group].type
};
var count = ( mod.match( /#/g ) || []).length;
var affix = "";
if ( count > 1 ) {
affix = mod.replace( "#", formData.affixes[cleanedAffix].min );
affix = affix.replace( "#", formData.affixes[cleanedAffix].max );
} else {
affix = mod.replace(
"#", "( " + formData.affixes[cleanedAffix].min + " - " +
formData.affixes[cleanedAffix].max + " )" );
}
cbMod();
}, function() {
cbGroup();
});
}, function() {
formData.modGroups = modGroups;
});
// If filter has a budget, make sure the budget has <= 2
// trailing digits and save it
if ( formData.budget ) {
formData.budget = Math.round( formData.budget * 100 ) / 100;
formData.displayPrice = formData.budget + " " + formData.currency;
// Otherwise save "Any price" as the display price
} else {
formData.displayPrice = "Any price";
}
formData.currency = Currency.shortToLongLookupTable[formData.currency];
// Format the filter title
var title = "";
if ( formData.corrupted === "true" ) {
title += "<span class=\"filter-corrupted\">Corrupted</span>";
}
if ( formData.enchanted === "true" ) {
title += "<span class=\"filter-enchanted\">Enchanted</span>";
}
if ( formData.crafted === "true" ) {
title += "<span class=\"filter-crafted\">Crafted</span>";
}
if ( formData.itemType !== "any" && formData.itemType !== "" ) {
title += "<span style=\"padding-right: 10px;\">" + formData.item + "(any " + formData.itemType + ")</span>";
} else {
title += "<span style=\"padding-right: 10px;\">" + formData.item + "</span>";
}
if ( formData.links !== "0" && formData.links !== "45" && formData.links !== "any" ) {
title += "<span class=\"filter-links\">" + formData.links + "L</span>";
} else if ( formData.links === "0" ) {
title += "<span class=\"filter-links\">< 5L</span>";
} else if ( formData.links === "45" ) {
title += "<span class=\"filter-links\">< 6L</span>";
}
var total;
if ( formData.socketsTotal === "" ) {
total = 0;
} else {
total = formData.socketsTotal;
}
var string = "";
var detailSocket = "";
if ( formData.socketsRed !== "" && formData.socketsRed !== 0 ) {
if ( formData.socketsTotal === "" ) {
total += parseInt( formData.socketsRed );
}
detailSocket += formData.socketsRed + "R ";
}
if ( formData.socketsGreen !== "" && formData.socketsGreen !== 0 ) {
if ( formData.socketsTotal === "" ) {
total += parseInt( formData.socketsGreen );
}
detailSocket += formData.socketsGreen + "G ";
}
if ( formData.socketsBlue !== "" && formData.socketsBlue !== 0 ) {
if ( formData.socketsTotal === "" ) {
total += parseInt( formData.socketsBlue );
}
detailSocket += formData.socketsBlue + "B ";
}
if ( formData.socketsWhite !== "" && formData.socketsWhite !== 0 ) {
if ( formData.socketsTotal === "" ) {
total += parseInt( formData.socketsWhite );
}
detailSocket += formData.socketsWhite + "W ";
}
if ( detailSocket !== "" ) {
string = total + "S ( " + detailSocket + ")";
} else {
string = total + "S";
}
if ( total > 0 ) {
title += "<span class=\"filter-sockets\">" + string + "</span>";
}
title += formatTitle( formData, "armor" );
title += formatTitle( formData, "es" );
title += formatTitle( formData, "evasion" );
title += formatTitle( formData, "dps" );
title += formatTitle( formData, "pdps" );
title += formatTitle( formData, "edps" );
title += formatTitle( formData, "quality" );
title += formatTitle( formData, "level" );
title += formatTitle( formData, "tier" );
title += formatTitle( formData, "experience" );
title += formatTitle( formData, "mapPackSize" );
title += formatTitle( formData, "mapQuantity" );
title += formatTitle( formData, "mapRarity" );
title += formatTitle( formData, "openPrefixes" );
title += formatTitle( formData, "openSuffixes" );
// If we're editing an existing filter, keep the current filter id
// otherwise generate a new one
var filterId = editingFilter !== "" ? editingFilter : Misc.guidGenerator();
formData.id = filterId;
formData.title = title;
formData.active = true;
callback( formData );
};
// When adding a new filter
var addFilterAction = function() {
if ( editingGroup ) {
applyGroupEdition( editingGroup );
} else {
// Get all filter data from the form
fetchFormData( function( formData ) {
console.log( formData );
// var re = /([0-9.]+)/g;
formatFilter( formData, function( formData ) {
var filter;
// If editing the filter, search for its group
if ( editingFilter !== "" ) {
async.each( filters.filterList, function( filter, cbFilter ) {
if ( filter.id === editingFilter ) {
formData.group = filter.group;
}
cbFilter();
}, function() {
filter = new Filter( formData );
});
// Otherwise, assign an empty group value
} else {
formData.group = "";
filter = new Filter( formData );
}
// console.log( formData );
// Add new filter
if ( $( "#add-filter" ).text() === "playlist_addAdd filter" ) {
filters.add( filter );
console.log( filter );
filters.findFilterIndex( filter, function( res ) {
console.log( res );
filters.save();
filter.render( function( generated ) {
postRender( filter, generated, res.index );
});
});
// Update existing filter
} else {
filters.update( filter, function() {
console.log( filters );
filter.render( function( generated ) {
filters.findFilterIndex( filter, function( res ) {
postRender( filter, generated, res.index );
});
});
});
filters.save();
}
});
});
}
};
// Helpers
/**
* Setup autocompletion for both name and affixes
*
* @params Nothing
* @return Nothing
*/
var setupAutocomplete = function() {
// Setup name completion
var data = require( "./autocomplete.json" );
var autocompleteContent = {};
async.each( data, function( entry, cb ) {
if ( entry.name === '' ) {
autocompleteContent[entry.typeLine] = entry.icon;
} else {
autocompleteContent[entry.name] = entry.icon;
}
cb();
}, function( err ) {
if ( err ) {
console.log( err );
}
$( '#item' ).autocomplete({
data: autocompleteContent,
limit: 20
});
// Close on Escape
$( '#item' ).keydown( function( e ) {
if ( e.which == 27 ) {
$( ".autocomplete-content" ).empty();
}
});
// Setup affix completion
var affixCompletion = require( "./affix-completion.json" );
// console.log( affixCompletion );
$( '#affixes' ).autocomplete({
data: affixCompletion,
limit: 100
});
// Close on Escape
$( '#affixes' ).keydown( function( e ) {
if ( e.which == 27 ) {
$( ".autocomplete-content" ).empty();
}
});
$( '#affixes' ).keyup( function( e ) {
var code = e.which;
// If the key is a letter, a number, a space or backspace
if ( code >= 65 && code <= 90 || code >= 48 && code <= 57 || code === 32 || code === 8 ) {
$( ".autocomplete-content li span:visible" ).each( function() {
var text = $( this ).text();
var reg = /\[(.*)\]/;
var match = reg.exec( text );
if ( match && match[1]) {
text = text.replace( "[" + match[1] + "]", "" ).trim();
var affixType = "affix-" + match[1].toLowerCase();
$( this ).html(
"<span class='badge " + affixType + "' data-badge-caption='" + match[1] +
"'></span>" + text );
} else {
// $( this ).html( "<span class='badge red' data-badge-caption='Unknown'></span>" + text );
}
// When selecting an item in the completion menu
$( this ).click( function () {
var affixType = $( this ).find( ".badge" ).data( "badge-caption" );
var affixName = $( this ).text();
setTimeout( function() { $( "#affixes" ).val( "(" + affixType + ") " + affixName ); }, 1 );
});
});
}
});
// Setup type completion
var typeCompletion = require( "./type-completion.json" );
$( '#item-type' ).autocomplete({
data: typeCompletion,
limit: 20
});
// Close on Escape
$( '#item-type' ).keydown( function( e ) {
if ( e.which == 27 ) {
$( ".autocomplete-content" ).empty();
}
});
// Setup blacklist reasons completion
$( '#blacklist-reason' ).autocomplete({
data: {
"Scammer": null,
"Bot": null,
"Price fixing": null,
"Low balling": null,
"Ignoring messages": null,
"No intent to sell": null
},
limit: 20
});
// Close on Escape
$( '#blacklist-reason' ).keydown( function( e ) {
if ( e.which == 27 ) {
$( ".autocomplete-content" ).empty();
}
});
});
};
// Return min and max values for a given filter dimension, depending on the
// expression
var parseField = function( value, str ) {
var data = {};
var step = 0.0001;
var max = 9999999;
var min = 0;
// Match mathematic operators <, <=, >, >=
var symbolReg = /(\<|\<=|\>|\>=)\s*([0-9.]+)/;
var rangeReg = /([0-9.]+)\s*\-\s*([0-9]+)/; // Match ranges
var match = symbolReg.exec( value );
// If we have an expression with <, <=, > or >=
if ( match ) {
switch ( match[1]) {
case "<":
data[str + "Min"] = min;
data[str + "Max"] = parseFloat( match[2]) - step;
break;
case "<=":
data[str + "Min"] = min;
data[str + "Max"] = parseFloat( match[2]);
break;
case ">":
data[str + "Min"] = parseFloat( match[2]) + step;
data[str + "Max"] = max;
break;
case ">=":
data[str + "Min"] = parseFloat( match[2]);
data[str + "Max"] = max;
break;
}
return data;
} else {
// If we have a range, extract min and max values
match = rangeReg.exec( value );
if ( match ) {
data[str + "Min"] = parseFloat( match[1]);
data[str + "Max"] = parseFloat( match[2]);
return data;
// By default, only specify min value
} else {
data[str + "Min"] = parseFloat( value );
data[str + "Max"] = max;
return data;
}
}
};
/**
* Fetch information from filled form data
*
* @params callback
* @return return collected data through callback
*/
var fetchFormData = function( callback ) {
var data = {};
data.league = $( "#league" ).val();
data.item = $( "#item" ).val();
data.budget = $( "#price" ).val();
data.currency = $( "#currency" ).val();
data.links = $( "#links" ).val();
data.socketsTotal = $( "#sockets-total" ).val();
data.socketsRed = $( "#sockets-red" ).val();
data.socketsGreen = $( "#sockets-green" ).val();
data.socketsBlue = $( "#sockets-blue" ).val();
data.socketsWhite = $( "#sockets-white" ).val();
data.corrupted = $( "#corrupted" ).val();
data.crafted = $( "#crafted" ).val();
data.enchanted = $( "#enchanted" ).val();
data.identified = $( "#identified" ).val();
data.level = $( "#level" ).val();
var levelData = parseField( data.level, "level" );
Object.assign( data, levelData );
data.tier = $( "#tier" ).val();
var tierData = parseField( data.tier, "tier" );
Object.assign( data, tierData );
data.experience = $( "#experience" ).val();
var experienceData= parseField( data.experience, "experience" );
Object.assign( data, experienceData );
data.quality = $( "#quality" ).val();
var qualityData = parseField( data.quality, "quality" );
Object.assign( data, qualityData );
data.rarity = $( "#rarity" ).val();
data.armor = $( "#armor" ).val();
var armorData = parseField( data.armor, "armor" );
Object.assign( data, armorData );
data.es = $( "#es" ).val();
var esData = parseField( data.es, "es" );
Object.assign( data, esData );
data.evasion = $( "#evasion" ).val();
var evasionData = parseField( data.evasion, "evasion" );
Object.assign( data, evasionData );
data.dps = $( "#dps" ).val();
var dpsData = parseField( data.dps, "dps" );
Object.assign( data, dpsData );
data.pdps = $( "#pdps" ).val();
var pdpsData = parseField( data.pdps, "pdps" );
Object.assign( data, pdpsData );
data.edps = $( "#edps" ).val();
var edpsData = parseField( data.edps, "edps" );
Object.assign( data, edpsData );
data.buyout = $( "#price-bo" ).is(":checked");
data.clipboard = $( "#clipboard" ).is(":checked");
data.convert = $( "#convert-currency" ).is(":checked");
data.itemType = $( "#item-type" ).val();
data.openPrefixes = $( "#open-prefixes" ).val();
var openPrefixesData = parseField( data.openPrefixes, "openPrefixes" );
Object.assign( data, openPrefixesData );
data.openSuffixes = $( "#open-suffixes" ).val();
var openSuffixesData = parseField( data.openSuffixes, "openSuffixes" );
Object.assign( data, openSuffixesData );
data.mapQuantity = $( "#map-quantity" ).val();
var mapQuantityData = parseField( data.mapQuantity, "mapQuantity" );
Object.assign( data, mapQuantityData );
data.mapRarity = $( "#map-rarity" ).val();
var mapRarityData = parseField( data.mapRarity, "mapRarity" );
Object.assign( data, mapRarityData );
data.mapPackSize = $( "#map-pack-size" ).val();
var mapPackSizeData = parseField( data.mapPackSize, "mapPackSize" );
Object.assign( data, mapPackSizeData );
data.affixesDis = [];
data.affixes = {};
callback( data );
};
/**
* Color item name depending on rarity
*
* @params filter
* @return nothing
*/
var colorRarity = function( filter ) {
if ( filter.rarity === "1" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "magic" );
} else if ( filter.rarity === "2" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "rare" );
} else if ( filter.rarity === "3" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "unique" );
} else if ( filter.rarity === "4" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "gem" );
} else if ( filter.rarity === "5" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "currency" );
} else if ( filter.rarity === "6" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "divination" );
} else if ( filter.rarity === "8" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "prophecy" );
} else if ( filter.rarity === "9" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "legacy" );
} else if ( filter.rarity === "not-unique" ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "not-unique" );
}
};
var updateFilterAmount = function( id ) {
// If filters are folded, count the amount of filters in the array
if ( $( "#fold-filters" ).hasClass( "folded" )) {
$( "#filters-amount" ).text( filters.length );
// Otherwise count the visible filters
} else {
$( "#filters-amount" ).text( $( "#filters .collection-item:visible" ).length );
}
bindRemoveFilter( id );
};
// Hide/show filter processing time
var displayTimes = function() {
if ( config.showFilterProcessingTime ) {
$( ".performance-info" ).show();
} else {
$( ".performance-info" ).hide();
}
};
// Hide/show search engines (poe.trade, poe-rates, etc.)
var displaySearchEngines = function() {
if ( config.showPoeTradeLink ) {
$( ".search-engines" ).show();
$( ".poe-trade-link" ).show();
} else {
$( ".poe-trade-link" ).hide();
}
if ( config.showPoeNinjaLink ) {
$( ".search-engines" ).show();
$( ".poe-ninja-link" ).show();
} else {
$( ".poe-ninja-link" ).hide();
}