-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·1718 lines (1564 loc) · 65.5 KB
/
server.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
//for File management
var fs = require('fs');
var request = require('request');
var rp = require('request-promise');
var _ = require('lodash'); //for search functions
var translate = require('google-translate-api');
//for server setup
var compression = require('compression');
var bodyParser = require('body-parser');
var express = require('express'),
app = express();
//for command line
var argv = require('yargs')
.usage('Usage: $0 -p [integer] -i [string of IP address] -r -n -t')
.default("p", 8081)
.default("i", '127.0.0.1')
.alias('p', 'port')
.alias('i', 'ip').alias('i', 'ip-address')
.alias('r', 'reload').alias('r','refresh')
.alias('n','notranslate')
.alias('t', 'test_function')
.describe('p', 'Port to run server on')
.describe('i', 'IP Address to run server on')
.describe('r', 'Force a redownload of the database. Use this if you have issues with the JSON files.')
.describe('n', 'Disable translation (for testing)')
.describe('t', 'Run test function')
.help('h')
.alias('h', 'help')
.argv;
//source: http://stackoverflow.com/questions/7067966/how-to-allow-cors
//CORS middleware, required for cross-domain reqeusting
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
};
app.use(compression());
app.use(bodyParser.urlencoded({extended: false}));
app.use(allowCrossDomain);
//on-going database that is a combination of 3 other databases (GL,EU,JP)
var master_list = {
unit: {},
item: {},
es: {}
};
//statistics of the server
var stats = {
last_update: null,
gl: {
num_units: 0,
num_items: 0,
num_es: 0,
newest_units: [],
newest_items: [],
newest_es: []
},
jp: {
num_units: 0,
num_items: 0,
num_es: 0,
newest_units: [],
newest_items: [],
newest_es: []
},
eu: {
num_units: 0,
num_items: 0,
num_es: 0,
newest_units: [],
newest_items: [],
newest_es: []
}
};
//donwnload a single file
function json_download_promisified(url,local_name){
return new Promise(function(fulfill,reject){
console.log("DL: " + url + " > " + local_name);
try{
fs.mkdirSync(__dirname + '/json/');
}catch(err){/*do nothing as directory already exists*/}
var destination = fs.createWriteStream(__dirname + '/json/' + local_name);
console.log(destination.path);
request(url).pipe(destination).on('finish', function () {
fulfill(local_name);
});
});
}
//run an array against a function that returns a promise n times
//each function is expected to receive the object at an array index
function do_n_at_a_time(arr, n, promiseFn) {
function n_recursive(arr, n, acc, callbackFn) {
if (arr.length === 0) {
callbackFn(acc);
} else {
var max = (arr.length < n) ? arr.length : n;
var promises = [];
for (var i = 0; i < max; ++i) {
var curObject = arr.shift();
promises.push(promiseFn(curObject));
}
Promise.all(promises)
.then(function (results) {
for (var i = 0; i < results.length; ++i) {
acc.push(results[i]);
}
n_recursive(arr, n, acc, callbackFn);
});
}
}
var new_arr = arr.slice();
return new Promise(function (fulfill, reject) {
n_recursive(new_arr, n, [], fulfill);
});
}
function rename_file_promisified(cur_name,new_name){
return new Promise(function(fulfill,reject){
fs.readFile(__dirname + "/json/" + cur_name, 'utf8', function (err, data) {
if (err) {
console.log("Couldn't find " + cur_name + " to rename");
fulfill(); //file doesn't exist, but that's okay
}else{
fs.writeFile(__dirname + "/json/" + new_name, data, 'utf8',function(err){
if(err) console.log("Couldn't rename " + cur_name + " to " + new_name);
else console.log("Renamed " + cur_name + " to " + new_name);
fulfill(); //finished trying to rename file
});
}
});
});
}
//add in anything in db_sub that is not in db_main
function merge_databases(db_main, db_sub, server) {
function get_unit_home_server(id) {
if (id >= 10000 && id < 70000) {
return 'jp';
} else if (id < 800000 && id >= 700000) {
return 'eu';
} else if (id >= 800000 && id < 900000) {
return 'gl';
} else {
// console.log("Unkown root for " + id);
return 'unknown';
}
}
function get_server_id(unit_id, server){
var id = parseInt(unit_id);
//add special case for overlapping IDs
if (get_unit_home_server(id) == 'eu' && server == 'gl') {
id = "8" + id.toString();
console.log("Changing " + unit + " to " + id);
} else {
id = id.toString();
}
return id;
}
var local_obj = JSON.parse(JSON.stringify(db_main)); //casting
var previous_evos = [];
for (var unit in db_sub) { //iterate through everything in object
var id = get_server_id(unit, server);
if (local_obj[unit] !== undefined) { //exists, so just add date add time
if (local_obj[unit].server.indexOf(server) == -1) {
local_obj[unit].server.push(server);
// local_obj[unit]["db_add_time"].push(new Date().toUTCString());
}
//save evo mats
if (local_obj[id].evo_mats === undefined && db_sub[unit].evo_mats !== undefined){
var next_id = get_server_id(db_sub[unit].next, server);
local_obj[id].evo_mats = db_sub[unit].evo_mats;
local_obj[id].next = next_id.toString();
previous_evos.push({
id: next_id,
prev: id.toString()
});
// local_obj[next_id].prev = id;
}
} else { //doesn't exist, so add it and date add time
local_obj[id] = db_sub[unit];
local_obj[id].server = [server];
// local_obj[unit]["db_add_time"] = [new Date().toUTCString()];
}
}
//add previous evo data once all units are added
for(var i = 0; i < previous_evos.length; ++i){
local_obj[previous_evos[i].id].prev = previous_evos[i].prev.toString();
}
return local_obj;
}
//adds a section in in the sub database to the main database
function add_field_to_db(db_main, db_sub, func){
for(var unit in db_sub){
try{
func(db_main[unit], db_sub[unit], db_main, db_sub);
}catch(err){
continue;
}
}
}
//convert all IDs in recipes to names
function translate_recipes(items){
for(var i in items){
var curItem = items[i];
if(curItem.recipe !== undefined){
for(var m in curItem.recipe.materials){
curItem.recipe.materials[m].name = items[curItem.recipe.materials[m].id].name;
// console.log(curItem.recipe.materials[m].name);
}
}
}
}
//create usage field for all items
function get_item_usage(items){
//for every item
for(var i in items){
var curItem = items[i];
curItem.usage = [];
//for every other item with a recipe
for(var j in items){
if(items[j].recipe !== undefined && j !== i){
//for every material in the other item
for(var m in items[j].recipe.materials){
if(items[j].recipe.materials[m].id == curItem.id){
curItem.usage.push({
id: j,
name: items[j].name
});
}
}
}
}
}
}
function load_json_promisified(file, alternative_files){
return new Promise(function(fulfill,reject){
//try to load first file
fs.readFile(__dirname + "/json/" + file, 'utf8', function (err, data) {
if (err) {
//try another file if possible
if(alternative_files !== undefined && alternative_files.length > 0){
var new_file = alternative_files.pop();
console.log("Couldn't load " + file + ". Trying " + new_file);
load_json_promisified(new_file,alternative_files).then(fulfill).catch(reject);
return;
}else{
reject("Error: cannot open " + file + " or its alternatives");
return;
}
}
//return parsed data
var result;
try{
result = JSON.parse(data);
}catch(parseError){
//try another file if possible
if (alternative_files !== undefined && alternative_files.length > 0) {
var new_file = alternative_files.pop();
console.log(parseError,"Couldn't load " + file + ". Trying " + new_file);
load_json_promisified(new_file, alternative_files).then(fulfill).catch(reject);
return;
} else {
reject(`${parseError}` + "\nError: cannot open " + file + " or its alternatives");
return;
}
}
if(file.indexOf("-old.json") > -1){
// console.log(file.indexOf("-old.json"));
rename_file_promisified(file,file.replace("-old.json",".json"))
.then(function(){
console.log("Successfully loaded old file. Renamed old file to current file");
fulfill(result);
}).catch(reject);
}else{
fulfill(result);
}
});
});
}
//server: gl, jp, or eu
function single_server_db_load(server){
return new Promise(function(fulfill,reject){
var mini_db = {};
//load files asynchronously
var main_promise = load_json_promisified('info-' + server + '.json', ['info-' + server + '-old.json'])
.then(function(result){
mini_db.main = result;
}).catch(reject);
var sp_promise = load_json_promisified('feskills-' + server + '.json', ['feskills-' + server + '-old.json'])
.then(function (result) {
mini_db.sp = result;
}).catch(reject);
var evo_promise = load_json_promisified('evo_list-' + server + '.json', ['evo_list-' + server + '-old.json'])
.then(function (result) {
mini_db.evo = result;
}).catch(reject);
var es_promise = load_json_promisified('es-' + server + '.json', ['es-' + server + '-old.json'])
.then(function (result) {
mini_db.es = result;
}).catch(reject);
var item_promise = load_json_promisified('items-' + server + '.json', ['items-' + server + '-old.json'])
.then(function (result) {
mini_db.items = result;
}).catch(reject);
//process files once finished loading
Promise.all([main_promise, sp_promise, evo_promise, es_promise, item_promise])
.then(function(results){ //finished loading JSON files
console.log("Successfully opened files for " + server, "\nProceeding to load them now.");
//merge into one database object
add_field_to_db(mini_db.main,mini_db.evo,function(unit1,unit2, db_main, db_sub){
unit1.evo_mats = unit2.mats;
unit1.next = unit2.evo.id;
db_main[unit1.next].prev = unit1.id;
});
add_field_to_db(mini_db.main,mini_db.sp, function(unit1,unit2){
unit1.skills = unit2.skills;
});
var present_IDs = get_es_ids(mini_db.main);
// console.log(present_IDs);
for(let e in mini_db.es){
// console.log(e);
//remove ES already in in main unit DB
if(_.sortedIndexOf(present_IDs,parseInt(e)) > -1){
delete mini_db.es[e];
// console.log("Removing ES",e);
}
}
//return merged databases
fulfill({
unit: mini_db.main,
item: mini_db.items,
es: mini_db.es
});
}).catch(reject);
});
}
//make sure all known evo mats are in English
function translate_evo_mats(unit_db, item_db){
//for every unit with an evolution
for(var unit in unit_db){
var curUnit = unit_db[unit];
if(curUnit.evo_mats !== undefined){
//for every evo mat
for(var m = 0; m < curUnit.evo_mats.length; ++m){
var curMat = curUnit.evo_mats[m];
//use names currently available in database
if(curMat.type === "unit" && unit_db[curMat.id] !== undefined){
unit_db[unit].evo_mats[m].name = unit_db[curMat.id].name;
}else if(curMat.type === "item" && item_db[curMat.id] !== undefined){
unit_db[unit].evo_mats[m].name = item_db[curMat.id].name;
}
}
}
}
}
//load database from a file or files
function load_database(master){
return new Promise(function(fulfill,reject){
master.unit = {};
master.item = {};
master.es = {};
console.log("Loading individual databases");
var global = single_server_db_load('gl').catch(reject);
var japan = single_server_db_load('jp').catch(reject);
var europe = single_server_db_load('eu').catch(reject);
//wait for databases to finish loading
Promise.all([global, japan, europe])
.then(function(results){
console.log("Merging unit databases");
master.unit = merge_databases(master.unit, results[0].unit, 'gl');
master.unit = merge_databases(master.unit, results[2].unit, 'eu');
master.unit = merge_databases(master.unit, results[1].unit, 'jp');
console.log("Merging item databases...");
master.item = merge_databases(master.item, results[0].item, 'gl');
master.item = merge_databases(master.item, results[2].item, 'eu');
master.item = merge_databases(master.item, results[1].item, 'jp');
console.log("Merging ES databases...");
master.es = merge_databases(master.es, results[0].es, 'gl');
master.es = merge_databases(master.es, results[2].es, 'eu');
master.es = merge_databases(master.es, results[1].es, 'jp');
translate_recipes(master.item);
get_item_usage(master.item);
translate_evo_mats(master.unit, master.item);
console.log("Finished loading databases");
})
.then(function(){
update_statistics();
fulfill(); //finished loading and updating
})
.catch(reject);
});
}
//return everything that's in db_new and not in db_old
function get_db_diffs(db_old, db_new){
var diffs = [];
for(var elem in db_new){
if(db_old.indexOf(db_new[elem]) == -1){
diffs.push(db_new[elem]);
}
}
return diffs;
}
//update statistics for all servers
function update_statistics(){
function get_ids_from_server(server, db) {
var result = [];
for (let i in db) {
if (db[i].server.indexOf(server) > -1) {
result.push(i); //i is ID of current object
}
}
return result;
}
function update_statistics_per_server(server) {
//holds local file load/save functions
var updater = {
load: function (file, alternative_files) {
try {
return JSON.parse(fs.readFileSync(__dirname + "/json/" + file, 'utf8'));
} catch (err) {//error, try alternative files
if (alternative_files !== undefined && alternative_files.length > 0) {
var new_file = alternative_files.pop();
return updater.load(new_file, alternative_files);
} else {//return an error if none of the files work
return JSON.parse(fs.readFileSync(__dirname + "/json/" + file, 'utf8'));
}
}
},
save: function (file, data) {
fs.writeFile(__dirname + "/json/" + file, data, function (err) {
if (err) {
console.log(err);
}
console.log("Saved " + file);
return;
});
}
};
console.log("Updating " + server.toUpperCase() + " statistics...");
//gather basic statistics
var unit_id = get_ids_from_server(server,master_list.unit);
var item_id = get_ids_from_server(server,master_list.item);
var es_id = get_ids_from_server(server, master_list.es);
console.log(server,"current unit count", unit_id.length,"/ current item count", item_id.length, '/ current es count', es_id.length);
stats[server].num_units = unit_id.length;
stats[server].num_items = item_id.length;
stats[server].num_es = es_id.length;
//load previous data, if it exists
var unit_data;
try {
unit_data = updater.load('stats-unit-' + server + ".json");
//save differences, if any
if (unit_data.last_loaded.length !== stats[server].num_units || unit_data.last_loaded[0] !== unit_id[0]) {
if (unit_data.last_loaded.length !== stats[server].num_units) console.log(server,"unit last length", unit_data.last_loaded.length, "/ unit current length", stats[server].num_units);
else if (unit_data.last_loaded[0] !== unit_id[0]) console.log(server, "unit last loaded[0]", unit_data.last_loaded[0], "/ unit current loaded[0]", unit_id[0]);
unit_data.newest = get_db_diffs(unit_data.last_loaded, unit_id);
unit_data.last_loaded = unit_id;
updater.save('stats-unit-' + server + '.json', JSON.stringify(unit_data));
}
} catch (err) { //file doesn't exist
console.log("Creating new unit stats file for",server);
unit_data = {
newest: unit_id,
last_loaded: unit_id
};
updater.save('stats-unit-' + server + '.json', JSON.stringify(unit_data));
}
//load previous data, if it exists
var item_data;
try {
item_data = updater.load('stats-item-' + server + ".json");
//save differences, if any
if (item_data.last_loaded.length !== stats[server].num_items || item_data.last_loaded[0] !== item_id[0]) {
if (item_data.last_loaded.length !== stats[server].num_items) console.log(server,"item last length", item_data.last_loaded.length, "/ item cur length", stats[server].num_items);
else if (item_data.last_loaded[0] !== item_id[0]) console.log(server,"item last loaded[0]", item_data.last_loaded[0], "/ item current loaded[0]", item_id[0]);
item_data.newest = get_db_diffs(item_data.last_loaded, item_id);
item_data.last_loaded = item_id;
updater.save('stats-item-' + server + '.json', JSON.stringify(item_data));
}
} catch (err) { //file doesn't exist
console.log("Creating new item stats file for", server);
item_data = {
newest: item_id,
last_loaded: item_id
};
updater.save('stats-item-' + server + '.json', JSON.stringify(item_data));
}
var es_data;
try {
es_data = updater.load('stats-es-' + server + ".json");
//save differences, if any
if (es_data.last_loaded.length !== stats[server].num_es || es_data.last_loaded[0] !== es_id[0]) {
if (es_data.last_loaded.length !== stats[server].num_es) console.log(server, "es last length", es_data.last_loaded.length, "/ es cur length", stats[server].num_ess);
else if (es_data.last_loaded[0] !== es_id[0]) console.log(server, "es last loaded[0]", es_data.last_loaded[0], "/ es current loaded[0]", es_id[0]);
es_data.newest = get_db_diffs(es_data.last_loaded, es_id);
es_data.last_loaded = es_id;
updater.save('stats-es-' + server + '.json', JSON.stringify(es_data));
}
} catch (err) { //file doesn't exist
console.log("Creating new es stats file for", server);
es_data = {
newest: es_id,
last_loaded: es_id
};
updater.save('stats-es-' + server + '.json', JSON.stringify(es_data));
}
//keep track of newest on server
stats[server].newest_units = unit_data.newest;
stats[server].newest_items = item_data.newest;
stats[server].newest_es = es_data.newest;
}
console.log("Updating statistics...");
var servers = ['gl','jp','eu'];
stats.last_update = new Date().toUTCString();
for(var i = 0; i < servers.length; ++i){
update_statistics_per_server(servers[i]);
}
console.log("Finished updating statistics");
}
//reload database from remote
function reload_database(){
return new Promise(function(fulfill,reject){
console.log("Preparing to reload database...");
console.log("Saving old files");
var db_type = ['info','feskills','items','evo_list','es'];
var servers = ['gl','jp','eu'];
var promises = [];
//rename old files
for(var d = 0; d < db_type.length; ++d){
for(var s = 0; s < servers.length; ++s){
var filePrefix = db_type[d] + '-' + servers[s];
var p = rename_file_promisified(filePrefix + ".json", filePrefix + "-old.json");
promises.push(p);
}
}
Promise.all(promises)
.then(function(result){ //finished renaming files
//download files from remote servers and load database when finished
console.log("Downloading new files...");
var main_url = 'https://raw.githubusercontent.com/Deathmax/bravefrontier_data/master/';
var requests = [];
var info_requests = [];
// var sp_requests = [];
var other_requests = [];
var completed = 0;
//generate URLs to download from
for(var d = 0; d < db_type.length; ++d){
for(var s = 0; s < servers.length; ++s){
var url = main_url;
if(servers[s] !== "gl") url += servers[s] + "/";
url += db_type[d] + ".json";
var fileName = db_type[d] + "-" + servers[s] + ".json";
if(db_type[d] === 'info'){
info_requests.push({ url: url, fileName: fileName });
}else{
other_requests.push({ url: url, fileName: fileName });
}
}
}
var total_requests = info_requests.length + other_requests.length;
//DL info files one at a time
requests.push(do_n_at_a_time(info_requests,1,function(dl_request){
var url = dl_request.url;
var fileName = dl_request.fileName;
return json_download_promisified(url, fileName)
.then(function (name) {
console.log("Downloaded " + name + " (" + (++completed) + "/" + total_requests + ")");
});
}));
//DL other files 5 at a time
requests.push(do_n_at_a_time(other_requests, 5, function (dl_request) {
var url = dl_request.url;
var fileName = dl_request.fileName;
return json_download_promisified(url, fileName)
.then(function (name) {
console.log("Downloaded " + name + " (" + (++completed) + "/" + total_requests + ")");
});
}));
return Promise.all(requests);
})
.then(function(results){ //finished downloading files
return load_database(master_list);
})
.then(function(){
fulfill(); //done redownloading and reloading everything
})
.catch(reject);
});
}
app.get('/', function(request, response){
response.end("<h1>Hello World</h1>");
});
//show the statistics of the server
app.get('/status', function(request,response){
response.end(JSON.stringify(stats));
});
app.get('/reload', function(request,response){
reload_database().then(function(){
if (!argv.notranslate) {
var translations = [translate_jp_units(), translate_jp_items(), translate_jp_es()];
Promise.all(translations)
.then(function (result) {
send_updates().then(function (results) {
console.log("Sent update hooks");
});
console.log("Finished reloading database");
});
}
response.end(JSON.stringify(stats));
});
});
app.get('/unit/:id', function(request, response){
var unit = master_list.unit[request.params.id];
if(unit === undefined)
response.end(JSON.stringify({error: request.params.id + " is not found"}));
else
response.end(JSON.stringify(unit));
});
app.get('/item/:id', function(request,response){
var item = master_list.item[request.params.id];
if (item === undefined)
response.end(JSON.stringify({ error: request.params.id + " is not found" }));
else
response.end(JSON.stringify(item));
});
app.get('/es/:id', function(request,response){
var es = master_list.es[request.params.id];
if (es === undefined)
response.end(JSON.stringify({ error: request.params.id + " is not found" }));
else
response.end(JSON.stringify(es));
});
function safe_json_get(json_obj, fields_arr, default_return){
var curValue = json_obj;
// console.log(fields_arr);
try{
for(var f in fields_arr){
curValue = curValue[fields_arr[f]];
}
// console.log(curValue);
return JSON.stringify(curValue).toLowerCase();
}catch(err){
// console.log(err);
return (default_return !== undefined) ? default_return : "";
}
}
//get the corresponding unit value of a given query
function get_unit_query_value(queryField, unit){
try{
switch(queryField){
case 'unit_name_id':
return unit["guide_id"] + ": " + unit["name"].toLowerCase() + (unit.translated_name ? (" " + unit.translated_name.toLowerCase()) : "") + " (" + unit["id"]+")";
case 'rarity': return unit["rarity"].toString();
case 'element': return unit["element"].toLowerCase();
case 'gender': return unit["gender"].toLowerCase();
case 'move_speed': return unit["movement"]["skill"]["move speed type"].toLowerCase();
case 'ls_name': return (unit["leader skill"]["name"] + " - " + unit["leader skill"]["desc"]).toLowerCase();
case 'ls_effect': return JSON.stringify(unit["leader skill"]["effects"]);
case 'bb_name': return (unit["bb"]["name"] + " - " + unit["bb"]["desc"]).toLowerCase();
case 'bb_effect': return JSON.stringify(unit["bb"]["levels"][9]["effects"]);
case 'sbb_name': return (unit["sbb"]["name"] + " - " + unit["sbb"]["desc"]).toLowerCase();
case 'sbb_effect': return JSON.stringify(unit["sbb"]["levels"][9]["effects"]);
case 'ubb_name': return (unit["ubb"]["name"] + " - " + unit["ubb"]["desc"]).toLowerCase();
case 'ubb_effect': return JSON.stringify(unit["ubb"]["levels"][0]["effects"]);
case 'es_name': return (unit["extra skill"]["name"] + " - " + unit["extra skill"]["desc"]).toLowerCase();
case 'es_effect': return JSON.stringify(unit["extra skill"]["effects"]);
case 'sp_name':
var result = "";
for(sp in unit["skills"]){
result += unit["skills"][sp]["skill"]["desc"] + "\n";
}
return result;
case 'sp_effect':
var result = "";
for (sp in unit["skills"]) {
result += JSON.stringify(unit["skills"][sp]["skill"]["effects"]) + "\n";
}
return result;
case 'evo_mats': return JSON.stringify(unit["evo_mats"]);
case 'server': return unit["server"];
case 'all_desc': var msg = safe_json_get(unit, ["leader skill", "name"]) + " " + safe_json_get(unit, ["leader skill", "desc"]) + " ";
msg += safe_json_get(unit, ["extra skill", "name"]) + " " + safe_json_get(unit, ["extra skill", "desc"]) +" ";
msg += safe_json_get(unit, ["bb", "name"]) + " " + safe_json_get(unit, ["bb", "desc"]) + " ";
msg += safe_json_get(unit, ["sbb", "name"]) + " " + safe_json_get(unit, ["sbb", "desc"]) + " ";
msg += safe_json_get(unit, ["ubb", "name"]) + " " + safe_json_get(unit, ["ubb", "desc"]) + " ";
if (unit["skills"] !== undefined) {
for (sp in unit["skills"]) {
try{
msg += unit["skills"][sp]["skill"]["desc"] + " ";
}catch(err){
continue;
}
}
}
// console.log(msg);
return msg;
case 'all_effect': var msg = safe_json_get(unit, ["leader skill", "effects"]) + " ";
msg += safe_json_get(unit, ["extra skill", "effects"]) + " ";
msg += safe_json_get(unit, ["bb", "levels", 9, "effects"]) + " " + safe_json_get(unit, ["sbb", "levels", 9, "effects"]) + " " + safe_json_get(unit, ["ubb", "levels", 0, "effects"]);
if (unit["skills"] !== undefined) {
for (sp in unit["skills"]) {
try{
msg += JSON.stringify(unit["skills"][sp]["skill"]["effects"]) + "\n";
}catch(err){
continue;
}
}
}
// console.log(msg);
return msg;
default: return "";
}
}catch(err){
// console.log(err);
return "";
}
}
//returns true if all non-empty query values are in the given unit
function contains_unit_query(query, unit){
var ignored_fields = ['strict','translate','verbose'];
for(var q in query){
var curQuery = query[q].toLowerCase();
//wildcard queries
if (curQuery.length === 0 || (q == 'element' && curQuery == 'any') ||
(q == 'gender' && curQuery == 'any') ||
(q == 'server' && curQuery == 'any') || ignored_fields.indexOf(q) > -1) {
continue;
}
try{
var unitValue = get_unit_query_value(q, unit).toString();
if(unitValue.indexOf(curQuery) == -1){
// if(query.verbose == true || query.verbose == 'true') console.log("Failed on",unit.id,q,curQuery);
return false; //stop if any part of query is not in unit
}
}catch(err){ //only occurs if requested field is empty in unit
return false;
}
}
return true;
}
app.get('/search/unit', function (request, response) {
response.sendFile(__dirname + "/json/" + "search_unit.html");
});
function get_highest_rarity(category){
var final_id = (category + 1).toString();
for(var i = 8; i >= 0; --i){
var str = (category + i).toString();
if(master_list.unit[str] != undefined){
final_id = str;
break;
}
}
return parseInt(final_id);
}
//get the list of units linked together by evolution given a single unit
function get_evo_line(unit_id){
var evo = [];
var curUnit = master_list.unit[unit_id];
//go to lowest rarity unit
while(curUnit.prev !== undefined){
curUnit = master_list.unit[curUnit.prev];
}
//traverse to highest rarity unit
evo.push(curUnit.id);
while(curUnit.next !== undefined){
evo.push(curUnit.next);
curUnit = master_list.unit[curUnit.next];
}
return evo;
}
//shorten results to a single unit IFF only one type of unit exists in the list
//assumption: result_arr has at least one element in it
function shorten_results(result_arr, verbose){
var last_evo = get_evo_line(result_arr[0]);
var last_guide_id = master_list.unit[last_evo[0].toString()].guide_id;
if(verbose) console.log("last_evo", last_evo, "last_guide", last_guide_id);
//check for uniqueness, return original array if not unique
for(var u = 1; u < result_arr.length; ++u){
var cur_evo = get_evo_line(result_arr[u]);
var cur_guide_id = master_list.unit[cur_evo[0].toString()].guide_id;
if(verbose) console.log("cur_evo",u, cur_evo, "cur_guide", cur_guide_id);
if(cur_evo.length !== last_evo.length || cur_evo[0] !== last_evo[0] || cur_guide_id !== last_guide_id){
if(verbose) console.log("found first mismatch");
return result_arr;
}
}
//if this point is reached, then only one type of unit exists in the list
//return last unit in list as it's the highest rarity one
return [last_evo.pop()];
}
//shorten results to a single unit IFF only one type of unit exists in the list
function shorten_results_old(result_arr) {
// console.log("before shorten: " + JSON.stringify(result_arr));
var unique = [];
for (r in result_arr) {
var cur_id = result_arr[r];
var category = cur_id - (cur_id % 10);
if (unique.indexOf(category) == -1) {
unique.push(category);
}
}
// console.log(unique);
if (unique.length == 1) {
var result = result_arr[result_arr.length - 1]; //result is at the end of the list in special case
//check for special cases where ID rule doesn't apply due to shared IDs between
//two or more units
var special_case_arr = [860124,860125,750156];
var special_case = false;
for(c in special_case_arr){
if(result_arr.indexOf(special_case_arr[c]) > -1){
special_case = true;
}
}
//clear result array
while (result_arr.length != 0) {
result_arr.pop();
}
//push final result
if(!special_case)
result_arr.push(get_highest_rarity(unique[0]));
else
result_arr.push(result);
}
// console.log("after shorten: " + JSON.stringify(result_arr));
}
//given a series of search options, list units with those qualities
app.get('/search/unit/options', function(request,response){
var query = request.query;
if (query.verbose == true || query.verbose == 'true'){
console.log("Query",query);
}
var results = [];
for(u in master_list["unit"]){
var unit = master_list["unit"][u];
if(contains_unit_query(query, unit)){
// console.log("Found " + u);
results.push(u);
}
}
//if not using strict mode, try to shorten list
var notStrict = (query["strict"] == false || query["strict"] == 'false');
var noRarity = (query["rarity"] == undefined || query["rarity"] == "*" || query["rarity"].length == 0);
var notGuide = (query["unit_name_id"] == undefined || (!isNaN(query["unit_name_id"]) && parseInt(query["unit_name_id"]) >= 10011) || (isNaN(query["unit_name_id"]) && query["unit_name_id"].indexOf(":") == -1));
if (notStrict && noRarity && notGuide && results.length > 0) {
if(query.verbose == true || query.verbose == 'true'){
console.log("Results before shorten",results);
}
results = shorten_results(results, query.verbose == true || query.verbose == 'true');
}
if (query.verbose == true || query.verbose == 'true') console.log("Search results",results);
response.end(JSON.stringify(results));
});
function get_item_query_value(queryField, item){
try {
switch (queryField) {
case 'item_name_id':
return item["name"].toLowerCase() + + (item.translated_name ? (" " + item.translated_name.toLowerCase()) : "") + `(${item["id"]})`;
case 'item_desc': return item["desc"].toLowerCase();
case 'rarity': return item["rarity"].toString();
case 'type': return item["type"].toLowerCase();
case 'effect': return JSON.stringify(item["effect"]);
case 'sphere_type': return item["sphere type text"].toLowerCase();
case 'recipe': return JSON.stringify(item["recipe"]);
case 'server': return JSON.stringify(item["server"]);
default: return "";
}
} catch (err) {
// console.log(err);
return "";
}
}
//returns true if all non-empty query values are in the given item
function contains_item_query(query, item){
var ignored_fields = ['strict', 'translate', 'verbose'];
for (var q in query) {
var curQuery = query[q].toLowerCase();
//wildcard queries
if (curQuery == '' || (q == 'type' && curQuery == 'any') ||
(q == 'sphere_type' && curQuery == 'any') ||
(q == 'server' && curQuery == 'any') || ignored_fields.indexOf(q) > -1){
continue;
}
try{
var itemValue = get_item_query_value(q, item).toString();
if (itemValue.indexOf(curQuery) == -1) {
return false; //stop if any part of query is not in item
}
} catch (err) { //only occurs if requested field is empty in item
return false;
}
}
return true;
}
app.get('/search/item', function (request, response) {
response.sendFile(__dirname + "/" + "search_item.html");
});
//given a series of search options, list items with those qualities
app.get('/search/item/options', function (request, response) {
var query = request.query;
if (query.verbose == true || query.verbose == 'true')
console.log("Query", query);
// console.log(query);
var results = [];
for (i in master_list["item"]) {
var item = master_list["item"][i];
if (contains_item_query(query, item))
results.push(item["id"]);
}
if (query.verbose == true || query.verbose == 'true')
console.log("Search results", results);
response.end(JSON.stringify(results));
});
function get_es_query_value(queryField, es) {
try {
switch (queryField) {
case 'es_name_id':
return es["name"].toLowerCase() + + (es.translated_name ? (" " + es.translated_name.toLowerCase()) : "") + `(${es["id"]})`;
case 'es_desc': return es["desc"].toLowerCase();
case 'effects': return JSON.stringify(es["effects"]);
case 'server': return JSON.stringify(es["server"]);
default: return "";
}