-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.js
1083 lines (1024 loc) · 42.5 KB
/
main.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
"use strict";
try {
require.resolve('electron');
} catch (e) {
console.error(`Electron is not installed. Make sure 'npm install' has been successfully run.`);
process.exit(1);
}
const { app, BrowserWindow, dialog, Menu, ipcMain } = require('electron');
const path = require('path').join,
fs = require('fs'),
url = require('url'),
prompt = require('electron-prompt'),
{ config, checknewestversion } = require('./lib/config.js');
const { analyze, paipugamedata, paipugamedata0, analyzeGameRecord, getUserID, setUserID, AnalyzeInit, Protobuf2Object, reporterror } = require('./lib/majsoul/analyze');
let InMacOS = process.platform == 'darwin';
let activate_devtool = 0;
let tempUserID = null;
let nowgamedata = null;
let liqiJSONdone = false;
var paipuversion = undefined;
var appPath = app.getAppPath();
let dataPath = 'data/';
const paipu_bk_folder_name = 'old_paipu_backup';
const metadata_query_step = 100; // How many uuids to query once. In test, 850 is safe and 1500 will be banned.
const metadata_fetch_delay = 1000; // ms to delay after fetching metadata
if (InMacOS) {
// let cwd = app.getPath('exe').replace(/\/[^\/]+$/, '');
let cwd = path(process.env.HOME, "Library", "Application Support", "MajsoulPaipuAnalyzer");
console.log('current dir: ' + cwd);
dataPath = cwd + '/' + dataPath;
if (!fs.existsSync(dataPath)) fs.mkdirSync(dataPath, { recursive: true });
}
else {
app.setPath('userData', appPath + '/UserData');
}
//app.disableHardwareAcceleration();
const ready = () => {
var newWindow = new BrowserWindow({
width: 650,
height: 700,
title: `Simple Mahjong`,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
var browseWindow = new BrowserWindow({
width: 1280,
height: 770,
title: `browser`,
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
newWindow.on('closed', () => {
newWindow = null;
if (browseWindow)
browseWindow.close();
})
browseWindow.on('closed', () => {
browseWindow = null;
if (newWindow)
newWindow.close();
})
browseWindow.nowingamepage = true;
browseWindow.webContents.on('dom-ready', function (){
//browseinject();
if (!/^https:\/\/(?:(?:www\.)?majsoul|game.mah?jo?n?g?-?soul|mahjongsoul)/.test(browseWindow.webContents.getURL())){
if (browseWindow.nowingamepage)
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '第三方账户登录提示',
message: '您已离开游戏页面进行第三方账户登录。如果出现无法正常登录的情况,请点击网页-登录专用窗口进行登录,在登录完成后关闭专用窗口并刷新本页面。'
});
browseWindow.nowingamepage = false;
}
else browseWindow.nowingamepage = true;
});
browseWindow.webContents.on('did-navigate', function (){
if (browseWindow.injectfinish == 2){
//如果跳转页面时已注入,那么标记为未注入并调用browseinject
browseWindow.injectfinish = 0;
for (let i in paipugamedata)
delete paipugamedata[i];
paipuversion = undefined;
browseinject();
}
});
function browseinject_one(filepath) {
if (browseWindow.injectfinish != 0)
//1 2对应注入请求发送未执行;注入完成。均不需要尝试注入
return;
fs.readFile(filepath, function (error, browsedata) {
if (error) console.log('read browseinject.js error: ' + error);
else{
browseWindow.injectfinish = 1;
browseWindow.webContents.executeJavaScript(String(browsedata))
.catch((err) => {
console.log(err);
browseWindow.injectfinish = 0;
});
}
});
}
function browseinject() {
browseinject_one(path(__dirname, 'lib', 'majsoul', 'browseinject.js'));
browseinject_one(path(__dirname, 'lib', 'majsoul', 'majsoul2tenhou.js'));
}
function bwindowload(url){
console.log('inject', browseWindow.injectfinish);
if (browseWindow.injectfinish == 2 || browseWindow.injectfinish == undefined)
browseWindow.injectfinish = 0;
for (let i in paipugamedata)
delete paipugamedata[i];
paipuversion = undefined;
if (url == undefined)
browseWindow.reload();
else browseWindow.loadURL(url, {userAgent: 'Chrome'});
browseinject();
}
var cantgetIDstr = '获取信息错误!无法获取用户ID,请确认已经进入大厅。或者尝试刷新页面。';
function showcantgetIDmsg(){
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '错误',
message: cantgetIDstr
});
}
function showliqinotdonemsg(){
if (liqiJSONdone) return false;
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '错误',
message: '未完成liqi.json数据获取,请稍后重试。如果长时间未完成请尝试刷新页面,或向开发者报告问题。'
});
return true;
}
function isspecialrule(roomdata){
let isspecialrule = roomdata.fanfu > 1 //>1番缚
|| roomdata.guyi_mode //古役
|| roomdata.begin_open_mode //配牌明牌
|| roomdata.xuezhandaodi //血战到底
|| roomdata.huansanzhang //换三张
|| roomdata.jiuchao_mode //鸠巢
|| roomdata.reveal_discard //暗夜之战
// || roomdata.dora3_mode
// || roomdata.muyu_mode
// || roomdata.chuanma
;
const basicrule = {
"dora_count":3,
"shiduan":true,
"can_jifei":true,
"tianbian_value":0, // TODO what's mean of tianbian
"liqibang_value":1000,
"changbang_value":300,
"noting_fafu_1":1000,
"noting_fafu_2":1500,
"noting_fafu_3":3000,
"have_liujumanguan":true,
"have_qieshangmanguan":false,
"have_biao_dora":true,
"have_gang_biao_dora":true,
"ming_dora_immediately_open":false,
"have_li_dora":true,
"have_gang_li_dora":true,
"have_sifenglianda":true,
"have_sigangsanle":true,
"have_sijializhi":true,
"have_jiuzhongjiupai":true,
"have_sanjiahele":false,
"have_toutiao":false,
"have_helelianzhuang":true,
"have_helezhongju":true,
"have_tingpailianzhuang":true,
"have_tingpaizhongju":true,
"have_yifa":true,
"have_nanruxiru":true,
"disable_multi_yukaman":false,
"disable_leijiyiman":false,
"fanfu":1,
"have_zimosun":true,
"disable_double_yakuman":false,
"disable_composite_yakuman":false,
"disable_double_wind_four_fu":false,
"disable_angang_guoshi":false,
"enable_renhe":false,
"enable_baopai_extend_settings":false,
}
const basickey = [
"player", "round", "init_point", "fandian", "time_fixed",
"time_add", "room", "contest_id", "has_ai", "bianjietishi",
"ai_level", "jingsuanyuandian", "shunweima_2", "shunweima_3",
"shunweima_4", "open_hand"
]
let isbasicrule = true;
for (let i in basicrule){
if (!((roomdata[i] == undefined) || roomdata[i] == basicrule[i]))
console.log(i, roomdata[i], basicrule[i]);
isbasicrule = isbasicrule && ((roomdata[i] == undefined) || roomdata[i] == basicrule[i]);
}
for (let i in roomdata)
if (basicrule[i] == undefined && !basickey.includes(i) && roomdata[i]){
// 存在不是基本规则或是基本参数的内容,当做特殊规则。
console.log(i, roomdata[i]);
isspecialrule = true;
}
return isspecialrule || !isbasicrule;
}
function iserrorpaipu(gamedata){
// 空数据,无房间号,房间号>=100是活动场
return !gamedata //空数据
|| !gamedata.roomdata //空房间数据
|| gamedata.roomdata.room == undefined //无房间号
|| gamedata.roomdata.room > 100 //房间号>100是活动场
|| isspecialrule(gamedata.roomdata) //包含特殊规则
;
}
function checkpaipugamedata(){
let msgstr = '', root, paipu4 = 0, paipu3 = 0, downloaded = 0, converted = 0, userid = getUserID(), errordata = 0;
if (userid < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
root = path(dataPath, 'majsoul', userid.toString());
let rawdirdata = new Set(fs.readdirSync(path(root, 'raw')));
let paipusdirdata = new Set(fs.readdirSync(path(root, 'paipus')));
for (let id in nowgamedata){
let data = nowgamedata[id];
if (iserrorpaipu(data)) errordata ++ ;
else if (data.roomdata.player == 3) paipu3 ++ ;
else paipu4 ++ ;
if (rawdirdata.has(id)) downloaded ++ ;
if (paipusdirdata.has(id)) converted ++ ;
}
msgstr = '用户ID: ' + userid
+ '\n4人牌谱: ' + paipu4
+ '\n3人牌谱: ' + paipu3
+ '\n未识别(无法分析,活动规则)牌谱: ' + errordata
+ '\n已下载: ' + downloaded
+ '\n已转换: ' + converted
+ '\n\n提示:工具会尝试下载所有牌谱,但是只会转换规则和四人段位场相同的牌谱。\n规则不同(例如宝牌数、番缚、古役等)的牌谱为未识别牌谱,无法转换。';
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '牌谱情报',
message: msgstr
});
}
var downloadconvertlist = undefined, downloadconvertresult = undefined;
var downloadnumber = 0, convertnumber = 0, downloadcount = 0, convertcount = 0;
function downloadconvertpaipu(){
let userid = getUserID();
if (userid < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
let root = path(dataPath, 'majsoul', userid.toString());
let rawdirdata = new Set(fs.readdirSync(path(root, 'raw')));
let paipusdirdata = new Set(fs.readdirSync(path(root, 'paipus')));
if (downloadconvertlist != undefined){
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '错误',
message: '存在正在执行的下载转换任务!如果转换异常停止请重启工具。'
});
return;
}
downloadconvertlist = [];
downloadconvertresult = [0, 0, 0, 0, 0]; // convert-success, index, total, time
downloadnumber = convertnumber = downloadcount = convertcount = 0;
for (let id in nowgamedata){
let needpaipu = !iserrorpaipu(nowgamedata[id]) && nowgamedata[id].roomdata.player == 4;
if (!rawdirdata.has(id) || !paipusdirdata.has(id) && needpaipu){
let needconvert = needpaipu && !paipusdirdata.has(id);
// id, (false: only download, true: download and convert)
downloadconvertlist.push([id, needconvert]);
if (needconvert) convertnumber ++ ;
downloadconvertresult[2] ++ ;
}
if (!needpaipu && paipusdirdata.has(id)){
// no need for convert and has converted, remove
console.log('find no need convert paipu, delete it', id, needpaipu);
fs.unlinkSync(path(root, 'paipus', id));
}
}
downloadnumber = downloadconvertresult[2];
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '开始下载转换',
message: '共有 ' + downloadconvertlist.length + ' 个牌谱需要下载\n有 ' +
convertnumber + ' 个牌谱需要转换\n在模拟窗口左上角可以看到进度'
});
nextdownloadconvert();
}
function nextdownloadconvert(){
if (downloadconvertlist.length == 0){
//显示转换完成正在组合
newWindow.webContents.send('downloadconvert', {}, true, downloadconvertresult);
setTimeout(function () { finaldownloadconvert(); }, 100);
return;
}
let id = downloadconvertlist[0][0];
browseWindow.webContents.send('fetchpaipudata', nowgamedata[id].uuid);
//newWindow.webContents.send('downloadconvert', nowgamedata[id], downloadconvertresult);
}
function fetchpaipudatacallback(res){
if (!res) {
// network error? res is null or undefined.
res = { error: 'response null/undefined error' };
}
let error = res.error, bytearr = res.data, url = res.data_url;
if (error){
if (error) console.log('read browseinject.js error: ' + JSON.stringify(error));
downloadconvertlist.splice(0, 1);
nextdownloadconvert();
return;
}
let id = downloadconvertlist[0][0], isconvert = downloadconvertlist[0][1];
let gamedata = nowgamedata[id];
if (url){
gamedata.url = url;
newWindow.webContents.send('downloadconvert', gamedata, isconvert, downloadconvertresult);
}
else{
newWindow.webContents.send('downloadconvert', gamedata, isconvert, downloadconvertresult, bytearr);
}
}
ipcMain.on('fetchpaipudatacallback', (event, uuid, res) => {
fetchpaipudatacallback(res);
});
function fetchpaipudatasave(uuid,res){
let error = res.error, head = res.head, bytearr = res.data, url = res.data_url;
if (error){
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '下载失败',
message: '错误信息:' + error
});
return;
}
let path = dialog.showSaveDialogSync({
title: '存储位置',
defaultPath: uuid
});
if (!path) return;
if (url){
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '下载地址',
message: '获取到牌谱下载地址,已将地址保存于指定位置,请自行下载'
});
}
fs.writeFileSync(path, url ? url : bytearr);
fs.writeFileSync(path + '.header', JSON.stringify(head, null, 4));
let p2obj = Protobuf2Object(bytearr);
console.log(JSON.stringify(head, null, 4));
}
ipcMain.on('fetchpaipudatasave', (event, uuid, res) => {
fetchpaipudatasave(uuid, res);
});
function finaldownloadconvert(){
//把牌谱连成一个数组减少分析时磁盘读取次数
let paipus = [];
let userid = getUserID();
let paipudir = path(dataPath, 'majsoul', userid.toString(), 'paipus');
let paipulist = fs.readdirSync(paipudir);
for (let i in paipulist)
if (paipulist[i] != paipu_bk_folder_name)
paipus.push(JSON.parse(fs.readFileSync(path(paipudir, paipulist[i]))));
// used to check every paipu is valid or not. may be used in the future
// for (let i in paipulist)
// if (paipulist[i] != paipu_bk_folder_name) {
// let one_paipu = JSON.parse(fs.readFileSync(path(paipudir, paipulist[i])));
// if (!iserrorpaipu(one_paipu.gamedata))
// paipus.push(JSON.parse(fs.readFileSync(path(paipudir, paipulist[i]))));
// }
fs.writeFileSync(path(dataPath, 'majsoul', userid.toString(), 'paipus.txt'), JSON.stringify(paipus));
let d = new Date();
d.setTime(downloadconvertresult[3] * 1000);
let timestr = d.toString();
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '下载转换完成',
message: '完成 ' + downloadcount + '/' + downloadnumber + ' 个下载任务,完成 '
+ convertcount + '/' + convertnumber + ' 个转换任务。\n下载成功牌谱的最晚时间是' + timestr
+ '\n\n提示:工具会尝试下载所有牌谱,但是只会转换规则和四人段位场相同的牌谱。\n规则不同(例如宝牌数、番缚、古役等)的牌谱为未识别牌谱,无法转换。'
});
downloadconvertresult = undefined;
downloadconvertlist = undefined;
if (tempUserID){
setUserID(tempUserID);
tempUserID = null;
}
}
function downloadconvertcallback(data){
let userid = getUserID();
downloadconvertresult[1] ++ ;
let nowconvert = downloadconvertlist[0][0];
let rawp = path(dataPath, 'majsoul', userid.toString(), 'raw', nowconvert);
let paipup = path(dataPath, 'majsoul', userid.toString(), 'paipus', nowconvert);
downloadconvertlist.splice(0, 1);
let raw = data.raw, paipu = data.paipu;
if (raw != undefined){
fs.writeFileSync(rawp, raw);
downloadcount ++ ;
if (paipu){
downloadconvertresult[0] ++ ;
convertcount ++ ;
fs.writeFileSync(paipup, JSON.stringify(paipu));
if (paipu.gamedata.endtime > downloadconvertresult[3])
downloadconvertresult[3] = paipu.gamedata.endtime;
paipuversion[nowconvert] = config.get('Version');
let paipuversiontxt = path(dataPath, 'majsoul', userid.toString(), 'paipuversion.txt');
fs.writeFileSync(paipuversiontxt, JSON.stringify(paipuversion));
}
else{
// convert fail or only download, remove paipu to avoid error
if (fs.existsSync(paipup)){
fs.unlinkSync(paipup);
console.log('paipup', paipup);
}
}
}
nextdownloadconvert();
}
function gotonewpage(str){
config.set('DefaultURL', str);
bwindowload(str);
}
function collectpaipucallback(err, option, data){
if (err != undefined){
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '错误',
message: err.type == 'UserID' ? cantgetIDstr : 'uiscript未定义,请确认已经进入雀魂。'
});
return;
}
analyzeGameRecord(data);
savegamedata(getUserID(), nowgamedata);
if (option == 'checkpaipugamedata')
checkpaipugamedata();
else if (option == 'downloadconvertpaipu')
downloadconvertpaipu();
}
ipcMain.on('collectpaipucallback', (event, err, option, data) => {
collectpaipucallback(err, option, data);
});
ipcMain.on('collectallpaipucallback', (event) => {
savegamedata(getUserID(), nowgamedata);
});
function bwindowsendmessage(cmd, d1, d2, d3, d4){ //先放4个参数,不够再加
if (browseWindow.injectfinish != 2){
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '错误',
message: '还未完成脚本注入,请稍后重试。'
});
return;
}
browseWindow.webContents.send(cmd, d1, d2, d3, d4);
}
function convert2tenhou() {
let uuids = [];
let heads = {};
for (let uuid in nowgamedata)
if (!iserrorpaipu(nowgamedata[uuid]) && nowgamedata[uuid].roomdata.player == 4)
uuids.push(uuid);
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '转换牌谱到天凤格式',
message: `尝试转换 ${uuids.length} 个牌谱到天凤格式中。只有能被正常分析且已经下载的四人牌谱才会被尝试转换。\n转换代码来自 GitHub: Equim-chan,转换结果仅供参考,不保证正确性。`
});
function restore() {
if (tempUserID) {
setUserID(tempUserID);
tempUserID = null;
}
}
let task_count = 0;
let success_count = 0;
ipcMain.removeAllListeners('converttenhoulogcallback');
ipcMain.on('converttenhoulogcallback', function (event, uuid, result) {
let tenhoufolder = path(dataPath, 'majsoul', getUserID().toString(), 'tenhou');
if (!fs.existsSync(tenhoufolder))
fs.mkdirSync(tenhoufolder);
if (result != undefined) {
success_count ++ ;
fs.writeFileSync(path(tenhoufolder, uuid), JSON.stringify(result));
}
if ( -- task_count == 0) {
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '转换完成',
message: `转换了 ${success_count} 个牌谱。\n存储位置: ${path('data', 'majsoul', getUserID().toString(), 'tenhou')}`
});
restore();
}
});
function realconvert(heads) {
let rawfolder = path(dataPath, 'majsoul', getUserID().toString(), 'raw');
for (let uuid in heads)
if (fs.existsSync(path(rawfolder, uuid)))
task_count ++ ;
for (let uuid in heads)
if (fs.existsSync(path(rawfolder, uuid)))
browseWindow.webContents.send('converttenhoulog', heads[uuid], fs.readFileSync(path(rawfolder, uuid)));
if (!task_count)
restore();
}
function sendmetadataquery() {
let part_data = uuids.splice(0, metadata_query_step);
if (part_data.length)
browseWindow.webContents.send('collectmetadata', part_data);
}
ipcMain.removeAllListeners('collectmetadatacallback'); // remove last listener to avoid duplicate running
ipcMain.on('collectmetadatacallback', (event, data) => {
for (let d of data)
heads[d.uuid] = d;
if (uuids.length)
setTimeout(sendmetadataquery, metadata_fetch_delay);
else{
realconvert(heads);
}
});
sendmetadataquery();
}
var menutemplate = [{
label: '牌谱',
submenu: [{
label: '查看已有牌谱情报',
click: function () {
nowgamedata = paipugamedata;
bwindowsendmessage('collectpaipu', 'checkpaipugamedata');
}
}, {
label: '自动获取牌谱数据',
click: function () {
nowgamedata = paipugamedata;
bwindowsendmessage('collectallpaipu', 'collectallpaipucallback');
}
}, {
label: '下载&转换牌谱',
click: function () {
nowgamedata = paipugamedata;
bwindowsendmessage('collectpaipu', 'downloadconvertpaipu');
}
}, {
label: '转换天凤牌谱',
click: function () {
nowgamedata = paipugamedata;
convert2tenhou();
}
}]
}, {
label: '网页',
submenu: [{
label: '刷新',
click: function () {
bwindowload();
}
}, {
label: '进入中文服',
click: function () {
gotonewpage('https://game.maj-soul.com/1/');
}
}, {
label: '进入日服',
click: function () {
gotonewpage('https://game.mahjongsoul.com');
}
}, {
label: '进入国际服',
click: function () {
gotonewpage('https://mahjongsoul.game.yo-star.com');
/* dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '国际服提示',
message: '由于技术原因,使用国际服时请确保当前网络能够较为通畅的访问Google, FaceBook等,否则很可能无法正确获取牌谱数据。'
}); */
}
}, {
label: '进入中文服(备用链接)',
click: function () {
gotonewpage('https://game.maj-soul.net/1/');
}
}, {
label: '指定服务器地址',
click: function () {
prompt({
title: '输入服务器地址(包含https://)',
label: '服务器地址',
}, browseWindow).then((res) => {
if (!res) return;
if (res.slice(0, 7) != 'http://' && res.slice(0, 8) != 'https://') {
dialog.showMessageBox({
type: 'error',
noLink: true,
buttons: ['确定'],
title: '服务器地址错误',
message: `请确认地址以http://或https://开头`
});
return;
}
gotonewpage(res);
})
.catch((e) => {
console.log(e);
});
}
}, {
label: '登录专用窗口',
click: function () {
var loginWindow = new BrowserWindow({
title: `login`,
show: false,
webPreferences: {
nodeIntegration: false
}
});
let str = config.get('DefaultURL');
console.log(str);
loginWindow.loadURL(str, {userAgent: 'Chrome'});
loginWindow.show();
}
}]
}, {
label: '公共牌谱列表',
submenu: [{
label: '下载指定牌谱到公共列表',
click: function() {
if (getUserID() < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
let response = dialog.showMessageBoxSync(browseWindow, {
type: 'info',
title: '选择输入方式',
noLink: true,
buttons: ['取消', '文件输入', '单牌谱输入'],
message: `请选择输入牌谱的方式。文件输入时一行可包含一个牌谱记录或牌谱链接。`
});
function getrecord(data){
let uuidcount = 0;
let uuids = []
for (let i in data){
let uuid = /(?:\d{6}-)?[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/.exec(data[i]);
if (uuid){
uuidcount ++ ;
if (!(uuid in paipugamedata0))
uuids.push(uuid.toString());
}
}
dialog.showMessageBox({
type: 'info',
title: '下载牌谱元数据',
noLink: true,
buttons: ['确定'],
message: `共发现 ${uuidcount} 个牌谱号,其中 ${uuids.length} 个需要下载元数据。` + (uuids.length ? `将在后台下载牌谱元数据,期间请勿进行其他操作,防止出现无法预料的后果。` : '')
});
let successcounter = 0;
function sendmetadataquery() {
let part_data = uuids.splice(0, metadata_query_step);
if (part_data.length)
browseWindow.webContents.send('collectmetadata', part_data);
}
ipcMain.removeAllListeners('collectmetadatacallback'); // remove last listener to avoid duplicate running
ipcMain.on('collectmetadatacallback', (event, data) => {
successcounter += data.length;
tempUserID = getUserID();
setUserID(0); // temporary set userid to 0 to notify analyzer write gamedata to gamedata0
analyzeGameRecord(data);
setUserID(tempUserID);
tempUserID = null;
if (uuids.length)
setTimeout(sendmetadataquery, metadata_fetch_delay);
else{
dialog.showMessageBoxSync({
type: 'info',
title: '下载牌谱元数据',
noLink: true,
buttons: ['确定'],
message: `成功下载 ${successcounter} 个牌谱元数据。获取详细牌谱数据请点击 公共牌谱列表-查看已有牌谱情报 。`
});
savegamedata(0, paipugamedata0);
}
});
sendmetadataquery();
}
if (response == 1){
let path = dialog.showOpenDialogSync({
title: '牌谱号/牌谱链接文件',
});
if (!path)
return;
let data = fs.readFileSync(path[0]).toString().split('\n');
getrecord(data);
}
if (response == 2){
prompt({
title: '输入牌谱',
label: '牌谱号/牌谱链接:',
}, browseWindow).then((res) => {
getrecord([res]);
});
}
}
}, {
label: '查看已有牌谱情报',
click: function () {
if (getUserID() < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
nowgamedata = paipugamedata0;
tempUserID = getUserID();
setUserID(0);
checkpaipugamedata();
setUserID(tempUserID);
tempUserID = null;
}
}, {
label: '下载&转换牌谱',
click: function () {
if (getUserID() < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
nowgamedata = paipugamedata0;
tempUserID = getUserID();
setUserID(0);
downloadconvertpaipu();
}
}, {
label: '转换天凤牌谱',
click: function () {
if (getUserID() < 0){
showcantgetIDmsg();
return;
}
if (showliqinotdonemsg()) return;
nowgamedata = paipugamedata0;
tempUserID = getUserID();
setUserID(0);
convert2tenhou();
}
}]
}, {
label: '其他',
submenu: [{
label: '清除配置数据',
click: function() {
let response = dialog.showMessageBoxSync({
type: 'warning',
title: '警告',
message: '是否清除配置数据?\n错误上报、默认服务器、更新检查等相关配置会被清除,data文件夹和config.json不会改动。',
cancelId: 1,
noLink: true,
buttons: ['是', '否']
});
if (response == 0){
config.clear();
}
}
}, {
label: '打开开发者工具',
click: function() {
newWindow.openDevTools();
browseWindow.openDevTools();
}
}, {
label: '下载牌谱并保存',
click: function() {
if (activate_devtool != 3)
return;
prompt({
title: '输入牌谱号',
label: '牌谱号:',
}, browseWindow).then((res) => {
let uuid = /(?:\d{6}-)?[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/.exec(res).toString();
browseWindow.webContents.send('fetchpaipudata', uuid, 'fetchpaipudatasave');
});
}
}, {
label: '选择牌谱并保存元信息',
click: function() {
if (activate_devtool != 3)
return;
let path = dialog.showOpenDialogSync({
title: '牌谱文件',
multiSelections: true
});
if (!path)
return;
for (let i in path){
let data = fs.readFileSync(path[i]);
data = analyze(data);
fs.writeFileSync(path[i] + '.json', JSON.stringify(data.toJSON()));
}
}
}, {
label: '关于',
click: function() {
let response = dialog.showMessageBoxSync({
type: 'info',
title: '关于',
noLink: true,
buttons: ['确定', '取消'],
message: `MajsoulPaipuCrawler v${config.get('Version')}\nContact: [email protected]\nGithub: https://github.com/zyr17/MajsoulPaipuAnalyzer`
});
activate_devtool |= response + 1;
}
}]
}];
if (InMacOS) menutemplate.splice(0, 0, {label: 'MacOS', submenu: []});
var bmenu = Menu.buildFromTemplate(menutemplate);
Menu.setApplicationMenu(bmenu);
newWindow.loadURL(url.format({
protocol: 'file:',
slashes: true,
pathname: path(__dirname, 'SimpleMahjong', 'index.html')
}));
newWindow.show();
bwindowload(config.get('DefaultURL'));
browseWindow.show();
//browseWindow.maximize();
function readgamedata(userid, paipugamedata, callback){
let root = path(dataPath, 'majsoul', userid.toString());
if (!fs.existsSync(dataPath)) fs.mkdirSync(dataPath, { recursive: true });
if (!fs.existsSync(path(dataPath, 'majsoul'))) fs.mkdirSync(path(dataPath, 'majsoul'));
if (!fs.existsSync(root)) fs.mkdirSync(root);
let ppp = path(root, 'raw');
if (!fs.existsSync(ppp)) fs.mkdirSync(ppp);
ppp = path(root, 'paipus');
if (!fs.existsSync(ppp)) fs.mkdirSync(ppp);
let gamedatatxt = path(root, 'gamedata.txt');
let gamedatas = [], oldgamedatas = [];
if (fs.existsSync(gamedatatxt))
gamedatas = fs.readFileSync(gamedatatxt).toString().split('\n');
for (let i in gamedatas){
let gamedata = gamedatas[i];
if (gamedata.length < 2) continue;
let gdata = JSON.parse(gamedata);
if (gdata.version == undefined || config.versionconvert(gdata.version) < config.versionconvert(config.get('GamedataMinVersion'))){
oldgamedatas.push(gdata);
continue;
}
let id = gdata.uuid;
if (paipugamedata[id] == undefined)
paipugamedata[id] = gdata;
}
if (oldgamedatas.length > 0){
dialog.showMessageBox({
type: 'info',
noLink: true,
buttons: ['确定'],
title: '发现旧牌谱',
message: `发现 ${oldgamedatas.length} 个旧版本获取的牌谱数据,将在10秒后开始更新,在弹出更新完成提示前请勿操作以免出现意外。`
});
let maincallback = callback;
let uuids = [];
for (let i in oldgamedatas)
uuids.push(oldgamedatas[i].uuid);
let successcounter = 0;
function sendmetadataquery() {
let part_data = uuids.splice(0, metadata_query_step);
if (part_data.length)
browseWindow.webContents.send('collectmetadata', part_data);
}
ipcMain.removeAllListeners('collectmetadatacallback'); // remove last listener to avoid duplicate running
ipcMain.on('collectmetadatacallback', (event, data) => {
successcounter += data.length;
tempUserID = getUserID();
setUserID(userid); // temporary set userid to 0 to notify analyzer write gamedata to gamedata0
analyzeGameRecord(data);
setUserID(tempUserID);
tempUserID = null;
if (uuids.length)
setTimeout(sendmetadataquery, metadata_fetch_delay);
else{
dialog.showMessageBoxSync({
type: 'info',
title: '更新牌谱元数据',
noLink: true,
buttons: ['确定'],
message: `成功更新 ${successcounter} 个牌谱元数据。`
});
savegamedata(userid, paipugamedata);
maincallback();
}
});
setTimeout(sendmetadataquery, 10000);
callback = null;
}
paipuversion = {};
ppp = path(root, 'paipuversion.txt');
if (fs.existsSync(ppp))
paipuversion = JSON.parse(String(fs.readFileSync(ppp)));
let paipusdirdata = new Set(fs.readdirSync(path(root, 'paipus')));
let oldpaipus = [], dirite = paipusdirdata.keys();
for (;;){
let i = dirite.next();
if (i.done) break;
i = i.value;
if (i == paipu_bk_folder_name)
continue;
let add = false;
if (!paipuversion.hasOwnProperty(i)){
let paipudata = JSON.parse(String(fs.readFileSync(path(root, 'paipus', i))));
if (paipudata.gamedata && paipudata.gamedata.version)