forked from licoffe/POE-sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
1701 lines (1601 loc) · 70.1 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
// 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 config = require( app.getPath( "userData" ) + path.sep + "config.json" );
if ( Object.keys( config ).length === 0 ) {
console.log( "Fallback" );
config = require( __dirname + "/config.json" );
}
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 Currency = require( "./currency.js" );
var Chunk = require( "./chunk.js" );
// Current leagues
var leagues = config.leagues;
var filters = new Filters([]);
var mu = require( 'mu2' );
mu.root = __dirname + '/templates';
const notifier = require('node-notifier');
notifier.on( "timeout", function () {
// displayingNotification = false;
});
notifier.on( "click", function () {
// displayingNotification = false;
Misc.formatMessage( lastItem, function( str ) {
ncp.copy( str, function() {
notifier.notify({
'title': 'Message copied to clipboard',
'message': str,
});
});
});
});
var ncp = require( "copy-paste" );
var editingFilter = ""; // Are we editing filters 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 writeFilterStats = function( filterStats ) {
// fs.appendFile( __dirname + "/stats_filters.csv", filterStats, function( err ) {
// if ( err ) {
// return console.log( err );
// }
// console.log( "The file was saved!" );
// });
// };
$( document).ready( function() {
// Interface - actions binding
// ------------------------------------------------------------------------
// Cancel editing when 'Cancel filter' is clicked
$( "#cancel-filter" ).click( function() {
cancelEditAction();
});
// 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 ( $( "#fold-filters" ).hasClass( "folded" )) {
$( "#filters" ).slideUp();
} else {
$( "#filters" ).slideDown();
}
};
// 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();
}
}
});
$( "#filters-amount" ).text( $( "#filters .collection-item:visible" ).length );
};
var resetFilters = function() {
$( "#league" ).val( config.leagues[config.defaultLeagueIndex]);
$( "#league").material_select();
$( "#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( "" );
$( "#price-bo" ).prop( "checked", true );
$( "#clipboard" ).prop( "checked", false );
$( "#affixes-list" ).empty();
$( "#item-type" ).val( "any" );
$( "#item-type").material_select();
Materialize.updateTextFields();
};
// 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 = "";
}
};
// When adding a new filter
var addFilterAction = function() {
// console.log( "Adding filter" );
fetchFormData( function( formData ) {
// console.log( formData );
// var re = /([0-9.]+)/g;
$( ".affix-item" ).each( function() {
data = $( this ).data( "data-item" );
formData.affixes[data.title] = [data.min, data.max];
var count = ( data.title.match( /#/g ) || []).length;
// console.log( data.title );
// console.log( count );
var affix = "";
if ( count > 1 ) {
affix = data.title.replace( "#", data.min );
affix = affix.replace( "#", data.max );
} else {
affix = data.title.replace( "#", "( " + data.min + " - " + data.max + " )" );
}
formData.affixesDis.push( affix );
});
// Convert price to exa if higher than exa rate
// if ( formData.budget > currencyRates[formData.league].exa && formData.currency === "chaos" ) {
// formData.budget /= currencyRates[formData.league].exa;
// formData.currency = "exa";
// }
if ( formData.budget ) {
formData.budget = Math.round( formData.budget * 100 ) / 100;
formData.displayPrice = formData.budget + " " + formData.currency;
} else {
formData.displayPrice = "Any price";
}
if ( formData.currency === "chaos" ) {
formData.currency = "Chaos Orb";
} else if ( formData.currency === "exa" ) {
formData.currency = "Exalted Orb";
}
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" ) {
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 !== "any" ) {
title += "<span class=\"filter-links\">" + formData.links + "L</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>";
}
if ( formData.armor !== "" ) {
title += "<span class=\"filter-property\">Armor>=" + formData.armor + "</span>";
}
if ( formData.es !== "" ) {
title += "<span class=\"filter-property\">ES>=" + formData.es + "</span>";
}
if ( formData.evasion !== "" ) {
title += "<span class=\"filter-property\">Evasion>=" + formData.evasion + "</span>";
}
if ( formData.dps !== "" ) {
title += "<span class=\"filter-property\">DPS>=" + formData.dps + "</span>";
}
if ( formData.pdps !== "" ) {
title += "<span class=\"filter-property\">PDPS>=" + formData.pdps + "</span>";
}
if ( formData.quality !== "" ) {
title += "<span class=\"filter-property\">quality>=" + formData.quality + "%</span>";
}
if ( formData.level !== "" ) {
title += "<span class=\"filter-property\">level>=" + formData.level + "</span>";
}
if ( formData.tier !== "" ) {
title += "<span class=\"filter-property\">Tier>=" + formData.tier + "</span>";
}
if ( formData.experience !== "" ) {
title += "<span class=\"filter-property\">Experience>=" + formData.experience + "%</span>";
}
if ( formData.affixesDis.length > 0 ) {
title += "<span class=\"filter-affix\">" + formData.affixesDis.join( ", " ) + "</span>";
}
var filterId = editingFilter !== "" ? editingFilter : Misc.guidGenerator();
formData.id = filterId;
formData.title = title;
formData.active = true;
var filter = new Filter( formData );
console.log( filter );
console.log( formData );
if ( $( "#add-filter" ).text() === "playlist_addAdd filter" ) {
// console.log( filters );
filters.add( filter );
filters.findFilterIndex( filter, function( res ) {
filters.save();
filter.render( function( generated ) {
postRender( filter, generated , res.index );
});
});
} else {
filters.update( filter, function() {
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: 20
});
// Close on Escape
$( '#affixes' ).keydown( function( e ) {
if ( e.which == 27 ) {
$( ".autocomplete-content" ).empty();
}
});
});
};
/**
* 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();
data.tier = $( "#tier" ).val();
data.experience = $( "#experience" ).val();
data.quality = $( "#quality" ).val();
data.rarity = $( "#rarity" ).val();
data.armor = $( "#armor" ).val();
data.es = $( "#es" ).val();
data.evasion = $( "#evasion" ).val();
data.dps = $( "#dps" ).val();
data.pdps = $( "#pdps" ).val();
data.buyout = $( "#price-bo" ).is(":checked");
data.clipboard = $( "#clipboard" ).is(":checked");
data.itemType = $( "#item-type" ).val();
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.id ).parent().parent().find( ".item" ).addClass( "magic" );
} else if ( filter.rarity === "2" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "rare" );
} else if ( filter.rarity === "3" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "unique" );
} else if ( filter.rarity === "4" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "gem" );
} else if ( filter.rarity === "5" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "currency" );
} else if ( filter.rarity === "6" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "divination" );
} else if ( filter.rarity === "8" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "prophecy" );
} else if ( filter.rarity === "9" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "legacy" );
} else if ( filter.rarity === "not-unique" ) {
$( "#" + filter.id ).parent().parent().find( ".item" ).addClass( "not-unique" );
}
};
var updateFilterAmount = function( id ) {
// $( "#filters-amount" ).text( filters.length );
$( "#filters-amount" ).text( $( "#filters .collection-item:visible" ).length );
bindRemoveFilter( id );
};
// When clicking on the minus sign, remove filter
var bindRemoveFilter = function( id ) {
$( "#" + id + ".remove-filter" ).click( function() {
// Remove this entry
$( this ).parent().parent().parent().parent().remove();
var newFilters = [];
var id = $( this ).attr( "id" );
// console.log( id );
// Search for filter index and remove it from the array
async.each( filters.filterList, function( filter, cb ) {
if ( filter.id !== id ) {
newFilters.push( filter );
}
cb();
}, function( err ) {
if ( err ) {
console.log( err );
}
filters = new Filters( newFilters );
// console.log( filters );
updateFilterAmount( id );
filters.save();
});
});
};
// Load filters
var loadFilters = function() {
// Load filters file in memory
// If filters exist in app data, use them, otherwise copy
// the default file to app data folder
var filterData = {};
if ( !fs.existsSync( app.getPath( "userData" ) + path.sep + "filters.json" )) {
console.log( "Filters file does not exist, creating it" );
var readStream = fs.createReadStream( __dirname + path.sep + "filters.json" );
var writeStream = fs.createWriteStream( app.getPath( "userData" ) + path.sep + "filters.json" );
writeStream.on( "close", function() {
filterData = require( app.getPath( "userData" ) + path.sep + "filters.json" );
});
readStream.pipe( writeStream );
} else {
console.log( "Loading filters from " + app.getPath( "userData" ) + path.sep + "filters.json" );
filterData = require( app.getPath( "userData" ) + path.sep + "filters.json" );
}
// For each filter, generate using the 'filter' template
// and add them to the filters array
async.each( filterData, function( filter, cbFilter ) {
if ( !filter.displayPrice && filter.budget && filter.budget > 0 ) {
filter.displayPrice = filter.budget + " " + filter.currency;
} else if ( !filter.displayPrice && ( !filter.budget || filter.budget === 0 )) {
filter.displayPrice = "Any price";
}
// Fix for change of currency from long to short form
if ( filter.currency === "Chaos Orb" ) {
filter.currency = "chaos";
} else if ( filter.currency === "Exalted Orb" ) {
filter.currency = "exa";
}
filter = new Filter( filter );
filters.add( filter );
cbFilter();
}, function( err ) {
if ( err ) {
console.log( err );
}
async.each( filters.filterList, function( filter, cbSorted ) {
filter.render( function( generated ) {
$( "#filters ul" ).append( generated );
// Color item name depending on rarity
colorRarity( filter );
if ( filter.buyout ) {
$( "#" + filter.id ).parent().parent().find( ".buyout" ).hide();
}
if ( !filter.clipboard ) {
$( "#" + filter.id ).parent().parent().find( ".clipboard" ).hide();
}
colorFilter( filter );
bindFilterToggleState( filter.id );
bindFilterEdit( filter.id );
updateFilterAmount( filter.id );
cbSorted();
});
}, function( err ) {
if ( err ) {
console.log( err );
}
filters.save();
poeTradeStats( filters );
});
});
};
/**
* Color filter based on item name to help visually differentiate
*
* @params Filter
* @return Nothing
*/
var colorFilter = function( filter ) {
if ( itemTypes["divination-card"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "divination" );
} else if ( itemTypes["prophecy"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "prophecy" );
} else if ( itemTypes["unique"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "unique" );
} else if ( itemTypes["currency"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "currency" );
} else if ( itemTypes["gem"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "gem" );
} else if ( itemTypes["map"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "map" );
} else if ( itemTypes["fragment"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "fragment" );
} else if ( itemTypes["essence"].types.indexOf( filter.item ) !== -1 ) {
$( "#filter-detail-" + filter.id + " .item" ).addClass( "essence" );
}
};
/**
* View update after creating a new filter
*
* @params Filter object, generated HTML code and position
* @return Nothing
*/
var postRender = function( filter, generated, position ) {
var last = false;
// If new item, is the last in the list
if (( $( "#filters ul li" ).length > 0 && position + 1 > $( "#filters ul li" ).length - 1 ) || ( position + 1 > $( "#filters ul li" ).length )) {
last = true;
// console.log( "Last in list" );
}
if ( $( "#add-filter" ).text() === "playlist_addAdd filter" ) {
if ( last ) {
$( "#filters ul" ).append( generated );
} else {
$( generated ).insertBefore( $( "#filters ul li" )[position] );
}
} else {
$( "#filters ul li" ).has( "#" + editingFilter ).remove();
if ( last ) {
$( "#filters ul" ).append( generated );
} else {
$( generated ).insertBefore( $( "#filters ul li" )[position] );
}
editingFilter = "";
$( "#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" );
}
// Color item name depending on rarity
colorRarity( filter );
// console.log( filter.buyout );
if ( filter.buyout ) {
$( "#" + filter.id ).parent().parent().find( ".buyout" ).hide();
}
if ( !filter.clipboard ) {
$( "#" + filter.id ).parent().parent().find( ".clipboard" ).hide();
}
colorFilter( filter );
bindFilterToggleState( filter.id );
bindFilterEdit( filter.id );
updateFilterAmount( filter.id );
poeTradeStats( filters );
};
var render = function( filter ) {
var generated = "";
mu.compileAndRender( "filter.html", filter )
.on( "data", function ( data ) {
generated += data.toString();
})
.on( "end", function () {
});
filters.save();
};
var bindFilterToggleState = function( id ) {
$( "#enable-filter-" + id ).click( function() {
filters.toggle( id, function() {
filters.save();
});
});
};
var bindFilterEdit = function( id ) {
$( ".filter-detail#filter-detail-" + id ).click( function() {
scrollToTopAction(); // Scroll back to top
editingFilter = id;
// Search for filter with the corresponding id
async.each( filters.filterList, function( filter, cbFilter ) {
// if filter matches, load all filter information in the fields
if ( filter.id === id ) {
$( "#league" ).val( filter.league );
$( "#league").material_select();
$( "#item" ).val( filter.item );
$( "#price" ).val( filter.budget );
if ( filter.currency === "Chaos Orb" ) {
$( "#currency" ).val( "chaos" );
} else {
$( "#currency" ).val( "exa" );
}
$( "#currency").material_select();
$( "#links" ).val( filter.links );
$( "#links").material_select();
$( "#sockets-total" ).val( filter.socketsTotal );
$( "#sockets-red" ).val( filter.socketsRed );
$( "#sockets-green" ).val( filter.socketsGreen );
$( "#sockets-blue" ).val( filter.socketsBlue );
$( "#sockets-white" ).val( filter.socketsWhite );
$( "#corrupted" ).val( filter.corrupted );
$( "#corrupted").material_select();
$( "#crafted" ).val( filter.crafted );
$( "#crafted").material_select();
$( "#enchanted" ).val( filter.enchanted );
$( "#enchanted").material_select();
$( "#identified" ).val( filter.identified );
$( "#identified").material_select();
$( "#level" ).val( filter.level );
$( "#tier" ).val( filter.tier );
$( "#experience" ).val( filter.experience );
$( "#quality" ).val( filter.quality );
$( "#rarity" ).val( filter.rarity );
$( "#rarity").material_select();
$( "#armor" ).val( filter.armor );
$( "#es" ).val( filter.es );
$( "#evasion" ).val( filter.evasion );
$( "#dps" ).val( filter.dps );
$( "#pdps" ).val( filter.pdps );
$( "#price-bo" ).prop( "checked", filter.buyout );
$( "#clipboard" ).prop( "checked", filter.clipboard );
$( "#add-filter" ).html( "<i class=\"material-icons\">thumb_up</i><span>Update filter</span>" );
$( "#cancel-filter" ).html( "<i class=\"material-icons\">thumb_down</i><span>Cancel edit</span>" );
$( "#cancel-filter" ).addClass( "red" ).removeClass( "blue-grey" );
$( "#add-filter" ).addClass( "green" ).removeClass( "blue-grey" );
$( "#item-type" ).val( filter.itemType );
$( "#item-type" ).material_select();
$( "#affixes-list" ).empty();
Materialize.updateTextFields();
// For each affix
async.each( filter.affixesDis, function( affix, cbAffix ) {
var generated = "";
// Extract title, min and max
var reg = /([0-9\.]+)/g;
var match = reg.exec( affix );
var matches = [];
while ( match !== null ) {
matches.push( parseFloat( match[1]));
match = reg.exec( affix );
}
var title = affix.replace( reg, "#" );
var obj = {
title: title,
min: matches[0],
max: matches[1],
affix: affix,
id: Misc.guidGenerator()
};
mu.compileAndRender( "affix.html", obj )
.on( "data", function ( data ) {
generated += data.toString();
})
.on( "end", function () {
$( "#affixes-list" ).append( generated );
$( "#" + obj.id ).data( "data-item", obj );
// When clicking on remove affix
$( ".remove-affix" ).click( function() {
$( this ).parent().parent().remove();
});
});
cbAffix();
});
}
cbFilter();
}, function( err ) {
if ( err ) {
console.log( err );
}
});
});
};
var poeTradeStats = function( filters ) {
console.log( "Refreshing poe.trade stats" );
Misc.publishStatusMessage( "Fetching item stats from poe.trade" );
async.each( filters.filterList, function( filter, cbFilter ) {
var str = "";
$.post( "http://poe.trade/search", { name: filter.item, league: filter.league, online: "x", has_buyout: "1" }, function( data ) {
var wrapper = document.getElementById( "poe-trade-output" );
wrapper.innerHTML = data;
// $( "div.poe-trade-output" ).html( data );
$( "#poe-trade-output script" ).remove();
$( "#poe-trade-output link" ).remove();
var prices = [];
var priceCount = {};
var mostPopularCount = 0;
var mostPopularPrice = "";
var sellers = [];
$( "#poe-trade-output .item" ).each( function() {
var seller = $( this ).data( "seller" );
if ( sellers.indexOf( seller ) === -1 ) {
// console.log( $( this ).data( "buyout" ));
sellers.push( seller );
prices.push( $( this ).data( "buyout" ));
if ( !priceCount[$( this ).data( "buyout" )]) {
priceCount[$( this ).data( "buyout" )] = 0;
}
priceCount[$( this ).data( "buyout" )]++;
if ( priceCount[$( this ).data( "buyout" )] > mostPopularCount ) {
mostPopularPrice = $( this ).data( "buyout" );
mostPopularCount = priceCount[$( this ).data( "buyout" )];
}
}
});
str += "<span>Poe.trade stats based on <b>" + prices.length + "</b> items</span>";
for ( var p in priceCount ) {
if ( priceCount.hasOwnProperty( p ) && priceCount[p] > 1 ) {
str += "<span>" + p + ": <b>" + priceCount[p] + "</b></span>";
}
}
$( "#" + filter.id ).parent().parent().find( ".item-stats" ).html(
str
);
// console.log( "Stats over " + prices.length + " items" );
// console.log( "Most popular price: " + mostPopularPrice + " with " + mostPopularCount );
// console.log( "Median price: " + prices[parseInt( prices.length/2)]);
cbFilter();
});
});
};
loadFilters();
setInterval( poeTradeStats, config.POE_TRADE_STATS_INTERVAL, filters );
// When clicking on 'Snipe', download change_ids and filter them
$( "#snipe" ).click( function() {
if ( !downloading ) {
downloading = true;
interrupt = false;
$( ".progress" ).fadeIn();
$( "#snipe" ).html( "<i class=\"material-icons\">pause</i><span>Stop</span>" );
Chunk.getLastChangeId( function( entry ) {
downloadChunk( entry, downloadChunk );
});
} else {
downloading = false;
interrupt = true;
$( ".progress" ).fadeOut();
$( "#snipe" ).html( "<i class=\"material-icons\">play_arrow</i><span>Snipe</span>" );
}
});
var updateResultsAmount = function() {
$( "#results-amount" ).text( $( ".entry:visible" ).length );
};
// When clicking on share entries icon
// $( "#share-entries" ).click( function() {
// fs.writeFile( __dirname + "/export.json", JSON.stringify( results ), function( err ) {
// if ( err ) {
// console.log( err );
// }
// });
// });
// When clicking on clear entries icon
$( "#clear-entries" ).click( function() {
$( "#results ul" ).empty();
results = {};
resultsId = [];
updateResultsAmount();
});
// When clicking on an entry result, copy a whisper message to clipboard
var bindClickEntry = function( id ) {
$( "#" + id ).click( function() {
var data = $( this ).data( "item" );
// var str = "@" + data.accountName + " Hi " + data.accountName + ", I would like to buy your '" + data.name + "' in " + data.league + " for " + data.price + " in stash '" + data.stashName + "'. Is it still available? :)";
Misc.formatMessage( data, function( str ) {
ncp.copy( str, function() {
console.log( data );
notifier.notify({
'title': 'Message copied to clipboard',
'message': str,
});
});
});
});
};
// When clicking on 'add affix'
$( "#add-affix" ).click( function() {
var affix = $( "#affixes" ).val();
if ( affix !== "" ) {
var min = $( "#affix-min" ).val();
var max = $( "#affix-max" ).val();
var count = ( affix.match( /#/g ) || []).length;
if ( count > 1 ) {
affix = affix.replace( "#", min );
affix = affix.replace( "#", max );
} else {
affix = affix.replace( "#", "( " + min + " - " + max + " )" );
}
var obj = {
title: $( "#affixes" ).val(),
affix: affix,
min: min,
max: max,
id: Misc.guidGenerator()
};
var generated = "";
mu.compileAndRender( "affix.html", obj )
.on( "data", function ( data ) {
generated += data.toString();
})
.on( "end", function () {
$( "#affixes-list" ).append( generated );
$( "#" + obj.id ).data( "data-item", obj );
// When clicking on remove affix
$( ".remove-affix" ).click( function() {
$( this ).parent().parent().remove();
});
});
}
});
/**
* Send notification and format message
*
* @params Item
* @return Nothing
*/
var notifyNewItem = function( item ) {
displayingNotification = true;
var audio = new Audio( __dirname + '/' + config.sound );
audio.volume = config.volume;
audio.play();
var displayName = item.name;
if ( item.typeLine !== item.name && ( item.frameType > 0 && item.frameType < 4 )) {
displayName += " (" + item.typeLine + ")";
}
notifier.notify({
title: displayName,
message: "Price: " + item.displayPrice,
wait: true
}, function ( err ) {
if ( err ) {
// console.log( err );
}
displayingNotification = false;
});
// If copy to clipboard enabled, do it
if ( item.clipboard || $( "#global-clipboard" ).prop( "checked" )) {
Misc.formatMessage( item, function( str ) {
ncp.copy( str, function() {
});
});
}
};
var renderSockets = function( item ) {
var rsc = {
"D": "./media/socket_dex.png",
"S": "./media/socket_str.png",
"I": "./media/socket_int.png",
"G": "./media/socket_white.png"
};
var currentGroup = -1;
var lastGroup = -1;
var socketIndex = 0;
async.each( item.sockets, function( socket, cbSocket ) {
socketIndex++;
currentGroup = socket.group;
// Change image resource associated to socket type and reveal
$( "li#" + item.id + " .socket" + socketIndex ).attr( "src", rsc[socket.attr]).show();
// If we are still in the same group, draw a link
if ( currentGroup === lastGroup && socketIndex > 1 ) {
$( "li#" + item.id + " .link" + ( socketIndex - 1 )).show();
}