forked from Mapaler/PADDashFormation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
3616 lines (3336 loc) · 131 KB
/
script.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
let Cards = []; //怪物数据
let Skills = []; //技能数据
let currentLanguage; //当前语言
let currentDataSource; //当前数据
const teamBigBoxs = []; //储存全部teamBigBox
const allMembers = []; //储存所有成员,包含辅助
let interchangeSvg; //储存划线的SVG
let controlBox; //储存整个controlBox
let statusLine; //储存状态栏
let formationBox; //储存整个formationBox
let editBox; //储存整个editBox
let showSearch; //整个程序都可以用的显示搜索函数
const dataStructure = 3; //阵型输出数据的结构版本
const className_displayNone = "display-none";
const dataAttrName = "data-value"; //用于储存默认数据的属性名
const isGuideMod = Boolean(Number(getQueryString("guide"))); //是否以图鉴模式启动
if (location.search.includes('&')) {
location.search = location.search.replace(/&/ig, '&');
}
//一开始就加载当前语言
if (currentLanguage == undefined)
{
const parameter_i18n = getQueryString("l") || getQueryString("lang"); //获取参数指定的语言
const browser_i18n = (navigator.language || navigator.userLanguage); //获取浏览器语言
currentLanguage = languageList.find(lang => { //筛选出符合的语言
if (parameter_i18n) //如果已指定就用指定的语言
return parameter_i18n.includes(lang.i18n);
else //否则筛选浏览器默认语言
return browser_i18n.includes(lang.i18n);
}) ||
languageList[0]; //没有找到指定语言的情况下,自动用第一个语言(英语)
//因为Script在Head里面,所以可以这里head已经加载好可以使用
document.head.querySelector("#language-css").href = `languages/${currentLanguage.i18n}.css`;
}
//一开始就加载当前数据
if (currentDataSource == undefined)
{
const parameter_dsCode = getQueryString("s"); //获取参数指定的数据来源
currentDataSource = parameter_dsCode ?
(dataSourceList.find(ds => ds.code == parameter_dsCode) || dataSourceList[0]) : //筛选出符合的数据源
dataSourceList[0]; //没有指定,直接使用日服
}
const dbName = "PADDF";
var db = null;
const DBOpenRequest = indexedDB.open(dbName,2);
DBOpenRequest.onsuccess = function(event) {
db = event.target.result; //DBOpenRequest.result;
console.log("PADDF:数据库已可使用");
loadData();
};
DBOpenRequest.onerror = function(event) {
// 错误处理
console.log("PADDF:数据库无法启用,删除可能存在的异常数据库。",event);
indexedDB.deleteDatabase(dbName); //直接把整个数据库删掉
console.log("也可能是隐私模式导致无法启用数据库,于是尝试不保存的情况下读取数据。");
loadData();
//alert('Some errors have occurred, please refresh the page.');
//history.go(); //直接强制刷新
};
DBOpenRequest.onupgradeneeded = function(event) {
let db = event.target.result;
let store;
// 建立一个对象仓库来存储用户的相关信息,我们选择 id 作为键路径(key path)
// 因为 id 可以保证是不重复的
store = db.createObjectStore("cards");
store = db.createObjectStore("skills");
// 使用事务的 oncomplete 事件确保在插入数据前对象仓库已经创建完毕
store.transaction.oncomplete = function(event) {
console.log("PADDF:数据库建立完毕");
};
};
/*class Member2
{
constructor(oldMenber = null,isAssist = false)
{
if (oldMenber)
{ //Copy一个
this.id = oldMenber.id;
this.level = oldMenber.level;
this.plus = [...oldMenber.plus];
this.awoken = oldMenber.awoken;
this.sAwoken = oldMenber.sAwoken;
this.latent = [...oldMenber.latent];
this.skilllevel = oldMenber.sAwoken;
this.assist = oldMenber.assist;
}else
{ //全新的
this.id = 0;
this.level = 1;
this.plus = [0,0,0];
this.awoken = 0;
this.sAwoken = null;
this.latent = [];
this.skilllevel = null;
this.assist = null;
}
this.isAssist = isAssist;
}
calculateAbility(solo,teamCount){
const card = Cards[this.id];
let bonus = null;
if (!this.isAssist &&
this.assist &&
this.assist.id>0 &&
Cards[this.assist.id].attrs[0] === card.attrs[0]
){
bonus = this.assist.calculateAbility(solo,teamCount);
}
}
toJSON(){
}
}*/
//队员基本的留空
var Member = function() {
this.id = 0;
this.ability = [0, 0, 0];
this.abilityNoAwoken = [0, 0, 0];
};
Member.prototype.outObj = function() {
const m = this;
if (m.id == 0) return null;
let obj = [m.id];
if (m.level != undefined) obj[1] = m.level;
if (m.awoken != undefined) obj[2] = m.awoken;
if (m.plus != undefined && m.plus instanceof Array && m.plus.length >= 3 && (m.plus[0] + m.plus[1] + m.plus[2]) != 0) {
if (m.plus[0] === m.plus[1] && m.plus[0] === m.plus[2]) { //当3个加值一样时只生成第一个减少长度
obj[3] = m.plus[0];
} else {
obj[3] = m.plus;
}
}
if (m.latent != undefined && m.latent instanceof Array && m.latent.length >= 1) obj[4] = m.latent;
if (m.sawoken != undefined && m.sawoken >= 0) obj[5] = m.sawoken;
const card = Cards[m.id] || Cards[0]; //怪物固定数据
const skill = Skills[card.activeSkillId];
//有技能等级,并且技能等级低于最大等级时才记录技能
if (m.skilllevel != undefined && m.skilllevel < skill.maxLevel) obj[6] = m.skilllevel;
return obj;
};
Member.prototype.loadObj = function(m, dataVersion) {
if (m == undefined) //如果没有提供数据,直接返回默认
{
this.id = 0;
return;
}
if (dataVersion == undefined) dataVersion = 1;
this.id = dataVersion > 1 ? m[0] : m.id;
this.level = dataVersion > 1 ? m[1] : m.level;
this.awoken = dataVersion > 1 ? m[2] : m.awoken;
if (dataVersion > 1) {
if (isNaN(m[3]) || m[3] == null) {
this.plus = m[3];
} else {
const singlePlus = parseInt(m[3], 10); //如果只有一个数字时,复制3份
this.plus = [singlePlus, singlePlus, singlePlus];
}
} else {
this.plus = m.plus;
}
if (!(this.plus instanceof Array)) this.plus = [0, 0, 0]; //如果加值不是数组,则改变
this.latent = dataVersion > 1 ? m[4] : m.latent;
if (this.latent instanceof Array && dataVersion <= 2) this.latent = this.latent.map(l => l >= 13 ? l + 3 : l); //修复以前自己编的潜觉编号为官方编号
if (!(this.latent instanceof Array)) this.latent = []; //如果潜觉不是数组,则改变
this.sawoken = dataVersion > 1 ? m[5] : m.sawoken;
this.skilllevel = m[6] || null;
};
Member.prototype.loadFromMember = function(m) {
if (m == undefined) //如果没有提供数据,直接返回默认
{
return;
}
this.id = m.id;
};
//只用来防坐的任何队员
var MemberDelay = function() {
this.id = -1;
};
MemberDelay.prototype = Object.create(Member.prototype);
MemberDelay.prototype.constructor = MemberDelay;
//辅助队员
var MemberAssist = function() {
this.level = 0;
this.awoken = 0;
this.plus = [0, 0, 0];
Member.call(this);
};
MemberAssist.prototype = Object.create(Member.prototype);
MemberAssist.prototype.constructor = MemberAssist;
MemberAssist.prototype.loadFromMember = function(m) {
if (m == undefined) //如果没有提供数据,直接返回默认
{
return;
}
this.id = m.id;
if (m.level != undefined) this.level = m.level;
if (m.awoken != undefined) this.awoken = m.awoken;
if (m.plus != undefined && m.plus instanceof Array && m.plus.length >= 3 && (m.plus[0] + m.plus[1] + m.plus[2]) > 0) this.plus = JSON.parse(JSON.stringify(m.plus));
if (m.skilllevel != undefined) this.skilllevel = m.skilllevel;
};
//正式队伍
var MemberTeam = function() {
this.latent = [];
MemberAssist.call(this);
//sawoken作为可选项目,默认不在内
};
MemberTeam.prototype = Object.create(MemberAssist.prototype);
MemberTeam.prototype.constructor = MemberTeam;
MemberTeam.prototype.loadFromMember = function(m) {
if (m == undefined) //如果没有提供数据,直接返回默认
{
return;
}
this.id = m.id;
if (m.level != undefined) this.level = m.level;
if (m.awoken != undefined) this.awoken = m.awoken;
if (m.plus != undefined && m.plus instanceof Array && m.plus.length >= 3 && (m.plus[0] + m.plus[1] + m.plus[2]) > 0) this.plus = JSON.parse(JSON.stringify(m.plus));
if (m.latent != undefined && m.latent instanceof Array && m.latent.length >= 1) this.latent = JSON.parse(JSON.stringify(m.latent));
if (m.sawoken != undefined) this.sawoken = m.sawoken;
if (m.ability != undefined && m.ability instanceof Array && m.plus.length >= 3) this.ability = JSON.parse(JSON.stringify(m.ability));
if (m.abilityNoAwoken != undefined && m.abilityNoAwoken instanceof Array && m.plus.length >= 3) this.abilityNoAwoken = JSON.parse(JSON.stringify(m.abilityNoAwoken));
if (m.skilllevel != undefined) this.skilllevel = m.skilllevel;
};
var Formation = function(teamCount, memberCount) {
this.title = "";
this.detail = "";
this.teams = [];
for (let ti = 0; ti < teamCount; ti++) {
const team = [
[], //队员
[], //辅助
0, //徽章
0, //队长更换序号
];
for (let mi = 0; mi < memberCount; mi++) {
team[0].push(new MemberTeam());
team[1].push(new MemberAssist());
}
this.teams.push(team);
}
};
Formation.prototype.outObj = function() {
const obj = {};
if (this.title != undefined && this.title.length > 0) obj.t = this.title;
if (this.detail != undefined && this.detail.length > 0) obj.d = this.detail;
obj.f = this.teams.map(t => {
const teamArr = [];
teamArr[0] = t[0].map(m =>
m.outObj()
).DeleteLatter();
teamArr[1] = t[1].map(m =>
m.outObj()
).DeleteLatter();
if (t[2]) teamArr[2] = t[2];
if (t[3]) teamArr[3] = t[3];
return teamArr;
});
obj.v = dataStructure;
/*if (obj.f.every(team=>team[0].length == 0 && team[1].length == 0 && team[2] == undefined) &&
!obj.t &&
!obj.d)
return null;*/
return obj;
};
Formation.prototype.loadObj = function(f) {
if (f == undefined) //如果没有提供数据,要返回空的
{
this.title = "";
this.detail = "";
this.teams.forEach(function(t, ti) {
t[0].forEach(function(m, mi) {
m.loadObj(null);
});
t[1].forEach(function(m, mi) {
m.loadObj(null);
});
t[2] = 0;
t[3] = 0;
});
return;
}
const dataVeision = f.v ? f.v : (f.f ? 2 : 1); //是第几版格式
this.title = dataVeision > 1 ? f.t : f.title;
this.detail = dataVeision > 1 ? f.d : f.detail;
const loadTeamArr = dataVeision > 1 ? f.f : f.team;
this.teams.forEach(function(t, ti) {
const tf = loadTeamArr[ti];
if (tf) {
t[0].forEach(function(m, mi) {
const fm = tf[0][mi];
m.loadObj(fm, dataVeision);
});
t[1].forEach(function(m, mi) {
const fm = tf[1][mi];
m.loadObj(fm, dataVeision);
});
t[2] = tf[2] || 0; //徽章
t[3] = tf[3] || 0; //队长
}
});
if (f.b)
this.teams[0][2] = f.b; //原来模式的徽章
};
//进化树
class EvoTree
{
constructor(mid, parent = null)
{
const _this = this;
this.parent = parent;
if (parent == null)
{
//mid = Cards[mid].evoRootId;
function returnRootId(mid)
{
mid = Cards[mid].evoRootId;
const m = Cards[mid];
if (m.henshinFrom && m.henshinFrom < m.id)
{ //只有变身来源小于目前id的,才继续找base
mid = returnRootId(m.henshinFrom);
}
return mid;
}
mid = returnRootId(mid);
}
const card = Cards[mid];
this.id = mid;
this.idArr = parent ? parent.idArr : [];
this.card = card;
this.children = [];
this.evoType = null;
if (parent == null)
{
this.evoType = "Base";
}
else if (card.henshinFrom == parent.id)
{
this.evoType = "Henshin";
}
else
{
if (card.evoMaterials.includes(3826)) //像素进化
{
this.evoType = "Pixel Evo";
}else if (card.awakenings.includes(49)) //武器
{
this.evoType = "Assist Evo";
}else if (card.isUltEvo) //究进
{
if (parent.card.isUltEvo) //超究进
{
this.evoType = "Super Ult Evo";
}else
{
this.evoType = "Ult Evo";
}
}else
{
if (card.henshinFrom == parent.id)
{
this.evoType = "Henshin";
}else if (parent.card.isUltEvo) //转生
{
this.evoType = "Reincarnation";
}else if(parent.evoType == "Reincarnation")
{
this.evoType = "Super Reincarnation";
}else
{
this.evoType = "Evolution";
}
}
}
if (this.idArr.includes(mid))
{
if (card.henshinFrom == parent.id)
{
this.evoType = "Henshin Loop";
}
return this;
}else
{
this.idArr.push(mid);
}
if (card.henshinTo)
this.children.push(new EvoTree(card.henshinTo,_this));
if (this.evoType != "Henshin")
this.children.push(...Cards.filter(scard=>scard.evoBaseId == mid && scard.id != mid).map(scard=>new EvoTree(scard.id,_this)));
//this.children = (card.henshinTo && card.henshinTo != card.evoRootId ? [new EvoTree(card.henshinTo,_this)] : []).concat(Cards.filter(scard=>scard.evoBaseId == mid && scard.id != mid).map(scard=>new EvoTree(scard.id,_this)));
};
toListNode()
{
const createCardHead = editBox.createCardHead;
const tBox = document.createElement("div");
tBox.className = "evo-box";
const evoPanel = tBox.appendChild(document.createElement("div"));
evoPanel.className = "evo-panel " + this.evoType.toLowerCase().replace(/\s/g,"-");
const evotPanel_L = evoPanel.appendChild(document.createElement("div"));
evotPanel_L.className = "evo-panel-left";
const evotPanel_R = evoPanel.appendChild(document.createElement("div"));
evotPanel_R.className = "evo-panel-right";
const evoTypeDiv = evotPanel_L.appendChild(document.createElement("div"));
evoTypeDiv.className = "evo-type-div";
const evoType = evoTypeDiv.appendChild(document.createElement("span"));
evoType.className = "evo-type";
const monHead = evotPanel_L.appendChild(createCardHead(this.id));
monHead.className = "monster-head";
const monName = evotPanel_R.appendChild(document.createElement("div"));
monName.className = "monster-name";
monName.textContent = returnMonsterNameArr(this.card, currentLanguage.searchlist, currentDataSource.code)[0];
const evotMaterials = evotPanel_R.appendChild(document.createElement("ul"));
evotMaterials.className = "evo-materials";
this.card.evoMaterials.forEach(mid=>{
//const li = evotMaterials.appendChild(document.createElement("li"));
evotMaterials.appendChild(createCardHead(mid));
});
const evoSubEvo = tBox.appendChild(document.createElement("ul"));
evoSubEvo.className = "evo-subevo";
this.children.forEach(subEvo=>{
const li = evoSubEvo.appendChild(document.createElement("li"));
li.appendChild(subEvo.toListNode());
});
return tBox;
};
}
//切换通用的切换className显示的函数
function toggleDomClassName(checkBox, className, checkedAdd = true, dom = document.body) {
if (!checkBox) return;
const checked = checkBox.checked;
if (checked && checkedAdd || !checked && !checkedAdd) {
dom.classList.add(className);
return true;
} else {
dom.classList.remove(className);
return false;
}
}
//清除数据
function clearData()
{
const locationURL = new URL(location);
locationURL.searchParams.delete('d'); //删除数据
locationURL.searchParams.delete('l'); //删除语言
location = locationURL.toString();
}
//轮换ABC队伍
function swapABCteam()
{
if (formation.teams.length > 1) {
formation.teams.push(formation.teams.splice(0, 1)[0]); //将队伍1移动到最后
creatNewUrl();
refreshAll(formation);
}
}
function henshinStep(step)
{
if (step == 0) return;
function gotoHenshin(card, nstep)
{
if (nstep > 0 && card.henshinTo)
{ //是变身的则返回
return gotoHenshin(Cards[card.henshinTo], --nstep);
}
else if (nstep < 0 && card.henshinFrom)
{
return gotoHenshin(Cards[card.henshinFrom], ++nstep);
}
else
{
return card;
}
}
formation.teams.forEach(team=>{
team[0].forEach(member=>{
const mid = member.id;
const card = Cards[mid];
if (step > 0 ? card.henshinTo : (card.henshinFrom && member.level <= 99))
{ //要变身前的才进行操作
const _card = gotoHenshin(card, step);
member.id = _card.id;
member.awoken = _card.awakenings.length;
}
});
});
creatNewUrl();
refreshAll(formation);
}
//在单人和多人之间转移数据
function turnPage(toPage, e = null) {
let pagename = null;
switch (toPage) {
case 1:
if (formation.teams[0][0].length < 6) {
//把第二支队伍的队长添加到最后方
formation.teams[0][0].push(formation.teams[1][0][0]);
formation.teams[0][1].push(formation.teams[1][1][0]);
}
//删掉第2支开始的队伍
formation.teams.splice(1);
pagename = "solo.html";
break;
case 2:
if (formation.teams.length < 2) { //从1人到2人
formation.teams[1] = [
[],
[]
];
//把右边的队长加到第二支队伍最后面
formation.teams[1][0].splice(0, 0, formation.teams[0][0].splice(5, 1)[0]);
formation.teams[1][1].splice(0, 0, formation.teams[0][1].splice(5, 1)[0]);
} else { //从3人到2人,直接删除后面两个队伍
//删掉第3支开始的队伍
formation.teams.splice(2);
//删掉前面两支队伍的战友
formation.teams[0][0].splice(5);
formation.teams[0][1].splice(5);
formation.teams[1][0].splice(5);
formation.teams[1][1].splice(5);
}
formation.badge = 0;
pagename = "multi.html";
break;
case 3:
if (formation.teams.length < 2) { //从1人到3人
} else { //从2人到3人
formation.teams[0][0].push(formation.teams[1][0][0]);
formation.teams[0][1].push(formation.teams[1][1][0]);
formation.teams[1][0].push(formation.teams[0][0][0]);
formation.teams[1][1].push(formation.teams[0][1][0]);
}
formation.badge = 0;
pagename = "triple.html";
break;
}
const newURL = creatNewUrl({ url: pagename, notPushState: true });
if (e && e.ctrlKey) {
window.open(newURL);
} else {
location.href = newURL;
}
}
window.onload = function(event) {
if (!Array.prototype.flat)
{
alert("请更新您的浏览器。\nPlease update your browser.");
}
controlBox = document.body.querySelector(".control-box");
statusLine = controlBox.querySelector(".status"); //显示当前状态的
formationBox = document.body.querySelector(".formation-box");
editBox = document.body.querySelector(".edit-box");
if (isGuideMod) {
console.info('现在是 怪物图鉴 模式');
document.body.classList.add('guide-mod');
}
const helpLink = controlBox.querySelector(".help-link");
if (location.hostname.includes("gitee")) { helpLink.hostname = "gitee.com"; }
//▼添加语言列表开始
const langSelectDom = controlBox.querySelector(".languages");
languageList.forEach(lang =>
langSelectDom.options.add(new Option(lang.name, lang.i18n))
);
const langOptionArray = Array.from(langSelectDom.options);
langOptionArray.find(langOpt => langOpt.value == currentLanguage.i18n).selected = true;
//▲添加语言列表结束
//▼添加数据来源列表开始
const dataSelectDom = controlBox.querySelector(".datasource");
dataSourceList.forEach(ds =>
dataSelectDom.options.add(new Option(ds.source, ds.code))
);
const dataSourceOptionArray = Array.from(dataSelectDom.options);
dataSourceOptionArray.find(dataOpt => dataOpt.value == currentDataSource.code).selected = true;
//添加数据class
document.body.classList.add("ds-" + currentDataSource.code);
//▲添加数据来源列表结束
//设定初始的显示设置
toggleDomClassName(controlBox.querySelector("#show-mon-id"), 'not-show-mon-id', false);
//记录显示CD开关的状态
const showMonSkillCd_id = "show-mon-skill-cd";
const btnShowMonSkillCd = controlBox.querySelector(`#btn-${showMonSkillCd_id}`);
btnShowMonSkillCd.checked = Boolean(Number(localStorage.getItem("PADDF-" + showMonSkillCd_id)));
btnShowMonSkillCd.onclick = function(){
toggleDomClassName(this, showMonSkillCd_id);
localStorage.setItem("PADDF-" + showMonSkillCd_id, Number(this.checked));
};
btnShowMonSkillCd.onclick();
//记录显示觉醒开关的状态
const showMonAwoken_id = "show-mon-awoken";
const btnShowMonAwoken = controlBox.querySelector(`#btn-${showMonAwoken_id}`);
btnShowMonAwoken.checked = Boolean(Number(localStorage.getItem("PADDF-" + showMonAwoken_id)));
btnShowMonAwoken.onclick = function(){
toggleDomClassName(this, showMonAwoken_id);
localStorage.setItem("PADDF-" + showMonAwoken_id, Number(this.checked));
};
btnShowMonAwoken.onclick();
toggleDomClassName(controlBox.querySelector("#btn-show-awoken-count"), 'not-show-awoken-count', false);
initialize(); //界面初始化
};
function loadData(force = false)
{
if (force)
console.info('强制更新数据。');
const _time = new Date().getTime();
//开始读取解析怪物数据
const sourceDataFolder = "monsters-info";
if (statusLine) statusLine.classList.add("loading-check-version");
GM_xmlhttpRequest({
method: "GET",
url: `${sourceDataFolder}/ckey.json${force?`?t=${_time}`:''}`, //版本文件
onload: function(response) {
dealCkeyData(response.response);
},
onerror: function(response) {
console.error("新的 Ckey JSON 数据获取失败。", response);
return;
}
});
//处理返回的数据
function dealCkeyData(responseText)
{ //处理数据版本
let newCkeys; //当前的Ckey们
let lastCkeys; //以前Ckey们
let currentCkey; //获取当前语言的ckey
let lastCurrentCkey; //以前的当前语言的ckey
try {
newCkeys = JSON.parse(responseText);
} catch (e) {
console.error("新的 Ckey 数据 JSON 解码出错。", e);
return;
}
console.debug("目前使用的数据区服是 %s。", currentDataSource.code);
currentCkey = newCkeys.find(ckey => ckey.code == currentDataSource.code); //获取当前语言的ckey
lastCkeys = localStorage.getItem("PADDF-ckey"); //读取本地储存的原来的ckey
try {
lastCkeys = JSON.parse(lastCkeys);
if (lastCkeys == null || !(lastCkeys instanceof Array))
lastCkeys = [];
} catch (e) {
console.error("旧的 Ckey 数据 JSON 解码出错。", e);
return;
}
lastCurrentCkey = lastCkeys.find(ckey => ckey.code == currentDataSource.code);
if (!lastCurrentCkey) { //如果未找到上个ckey,则添加个新的
lastCurrentCkey = {
code: currentDataSource.code,
ckey: {},
updateTime: null
};
lastCkeys.push(lastCurrentCkey);
}
if (statusLine) statusLine.classList.remove("loading-check-version");
if (statusLine) statusLine.classList.add("loading-mon-info");
if (!force && db && currentCkey.ckey.card == lastCurrentCkey.ckey.card) {
console.debug("Cards ckey相等,直接读取已有的数据。");
const transaction = db.transaction([`cards`]);
const objectStore = transaction.objectStore(`cards`);
const request = objectStore.get(currentDataSource.code);
request.onerror = function(event) {
console.error("Cards 数据库内容读取失败。");
};
request.onsuccess = function(event) {
if (request.result instanceof Array)
{
Cards = request.result;
dealCardsData(Cards);
}else
{
console.info("Cards 数据库内容不存在,需重新下载。");
downloadCardsData();
}
};
} else {
console.log("Cards 需重新下载。");
downloadCardsData();
}
function downloadCardsData()
{
GM_xmlhttpRequest({
method: "GET",
url: `${sourceDataFolder}/mon_${currentDataSource.code}.json?t=${_time}`, //Cards数据文件
onload: function(response) {
try {
Cards = JSON.parse(response.response);
} catch (e) {
console.error("Cards 数据 JSON 解码出错。", e);
return;
}
if (db)
{
const transaction = db.transaction([`cards`], "readwrite");
transaction.oncomplete = function(event) {
console.log("Cards 数据库写入完毕。");
lastCurrentCkey.ckey.card = currentCkey.ckey.card;
lastCurrentCkey.updateTime = currentCkey.updateTime;
localStorage.setItem("PADDF-ckey", JSON.stringify(lastCkeys)); //储存新的ckey
dealCardsData(Cards);
};
const objectStore = transaction.objectStore(`cards`);
objectStore.put(Cards,currentDataSource.code);
}else //隐私模式无法启动数据库
{
dealCardsData(Cards);
}
},
onerror: function(response) {
console.error("Cards JSON 数据获取失败。", response);
}
});
}
function dealCardsData()
{
if (editBox)
{
const monstersList = editBox.querySelector("#monsters-name-list");
let fragment = document.createDocumentFragment();
Cards.forEach(function(m) { //添加下拉框候选
const opt = fragment.appendChild(document.createElement("option"));
opt.value = m.id;
opt.label = m.id + " - " + returnMonsterNameArr(m, currentLanguage.searchlist, currentDataSource.code).join(" | ");
/*const linkRes = new RegExp("link:(\\d+)", "ig").exec(m.specialAttribute);
if (linkRes) { //每个有链接的符卡,把它们被链接的符卡的进化根修改到链接前的
const toId = parseInt(linkRes[1], 10);
const _m = Cards[toId];
//if (_m.evoBaseId == 0)
// _m.evoRootId = m.evoRootId;
m.henshinTo = toId;
_m.henshinFrom = m.id;
}*/
});
monstersList.appendChild(fragment);
}
if (statusLine) statusLine.classList.remove("loading-mon-info");
if (statusLine) statusLine.classList.add("loading-skill-info");
if (!force && db && currentCkey.ckey.skill == lastCurrentCkey.ckey.skill) {
console.debug("Skills ckey相等,直接读取已有的数据。");
const transaction = db.transaction([`skills`]);
const objectStore = transaction.objectStore(`skills`);
const request = objectStore.get(currentDataSource.code);
request.onerror = function(event) {
console.error("Skills 数据库内容读取失败。");
};
request.onsuccess = function(event) {
if (request.result instanceof Array)
{
Skills = request.result;
dealSkillData(Skills);
}else
{
console.info("Skills 数据库内容不存在,需重新下载。");
downloadSkillData();
}
};
} else {
console.log("Skills 需重新下载。");
downloadSkillData();
}
function downloadSkillData()
{
GM_xmlhttpRequest({
method: "GET",
url: `${sourceDataFolder}/skill_${currentDataSource.code}.json?t=${_time}`, //Skills数据文件
onload: function(response) {
try {
Skills = JSON.parse(response.response);
} catch (e) {
console.log("Skills 数据 JSON 解码出错", e);
return;
}
if (db)
{
const transaction = db.transaction([`skills`], "readwrite");
transaction.oncomplete = function(event) {
console.log("Skills 数据库写入完毕。");
lastCurrentCkey.ckey.skill = currentCkey.ckey.skill;
lastCurrentCkey.updateTime = currentCkey.updateTime;
localStorage.setItem("PADDF-ckey", JSON.stringify(lastCkeys)); //储存新的ckey
dealSkillData(Skills);
};
const objectStore = transaction.objectStore(`skills`);
objectStore.put(Skills,currentDataSource.code);
}else //隐私模式无法启动数据库
{
dealSkillData(Skills);
}
},
onerror: function(response) {
console.error("Skills JSON 数据获取失败", response);
}
});
}
function dealSkillData()
{
//显示数据更新时间
let controlBoxHook = setInterval(checkControlBox, 500); //循环检测controlBox
checkControlBox();
function checkControlBox()
{
if (controlBox)
{
const updateTime = controlBox.querySelector(".datasource-updatetime");
updateTime.textContent = new Date(currentCkey.updateTime).toLocaleString(undefined, { hour12: false });
clearInterval(controlBoxHook);
}
}
//initialize(); //初始化
if (statusLine) statusLine.classList.remove("loading-skill-info");
//如果通过的话就载入URL中的怪物数据
let formationBoxHook = setInterval(checkFormationBox, 500); //循环检测formationBox
checkFormationBox();
function checkFormationBox()
{
if (formationBox)
{
reloadFormationData();
clearInterval(formationBoxHook);
}
}
}
}
}
}
//重新读取URL中的Data数据并刷新页面
function reloadFormationData(event) {
let formationData;
if (event && event.state && event.state.outForm)
{
//直接使用现有数据
formationData = event.state.outForm;
//console.log("直接读取",formationData);
}else
{
try {
const parameterDataString = getQueryString("d") || getQueryString("data");
formationData = JSON.parse(parameterDataString);
//console.log("从URL读取",formationData);
} catch (e) {
console.error("URL中队伍数据JSON解码出错", e);
return;
}
}
formation.loadObj(formationData);
refreshAll(formation);
if (isGuideMod)
{
let mid;
if (event && event.state && event.state.mid)
{
mid = event.state.mid;
}else
{
mid = parseInt(getQueryString("id"),10);
}
if (!isNaN(mid))
{
editBox.mid = mid;
editBoxChangeMonId(mid);
}
if (event && event.state && event.state.searchArr)
{
showSearch(event.state.searchArr.map(id=>Cards[id]));
}
}
}
window.addEventListener('popstate',reloadFormationData); //前进后退时修改页面
//创建新的分享地址
function creatNewUrl(arg) {
if (arg == undefined) arg = {};
if (!!(window.history && history.pushState)) { // 支持History API
const language_i18n = arg.language || getQueryString("l") || getQueryString("lang"); //获取参数指定的语言
const datasource = arg.datasource || getQueryString("s");
const outObj = formation.outObj();
const newSearch = new URLSearchParams();
if (language_i18n) newSearch.set("l", language_i18n);
if (datasource && datasource != "ja") newSearch.set("s", datasource);
if (getQueryString("guide")) newSearch.set("guide", getQueryString("guide"));
if (getQueryString("id")) newSearch.set("id", getQueryString("id"));
if (outObj)
{
const dataJsonStr = JSON.stringify(outObj); //数据部分的字符串
newSearch.set("d", dataJsonStr);
}
const newUrl = (arg.url || "") + (newSearch.toString().length > 0 ? '?' + newSearch.toString() : "");
if (!arg.notPushState) {
history.pushState({outForm: outObj}, null, newUrl.length > 0 ? newUrl : location.pathname);
} else {
return newUrl;
}
}
}
//截图
function capture() {
statusLine.classList.add("prepare-cauture");
const titleBox = formationBox.querySelector(".title-box");
const detailBox = formationBox.querySelector(".detail-box");
const txtTitle = titleBox.querySelector(".title");
const txtDetail = detailBox.querySelector(".detail");
//去掉可能的空白文字的编辑状态
titleBox.classList.remove("edit");
detailBox.classList.remove("edit");
const downLink = controlBox.querySelector(".down-capture");
html2canvas(formationBox).then(canvas => {
canvas.toBlob(function(blob) {
window.URL.revokeObjectURL(downLink.href);
downLink.href = URL.createObjectURL(blob);
downLink.download = `${teamsCount}P formation cauture.png`;
downLink.click();
statusLine.classList.remove("prepare-cauture");
//如果是空白文字,加回编辑状态
if (txtTitle.value.length == 0)
titleBox.classList.add("edit");
if (txtDetail.value.length == 0)
detailBox.classList.add("edit");
});
//document.body.appendChild(canvas);
});
}
//初始化
function initialize() {
//触屏使用的切换显示的线条
interchangeSVG = document.body.querySelector("#interchange-line");
interchangeSVG.line = interchangeSVG.querySelector("g line");
interchangeSVG.changePoint = function(p1, p2) {
const line = this.line;
if (p1 && p1.x != undefined)
line.setAttribute("x1", p1.x);
if (p1 && p1.y != undefined)
line.setAttribute("y1", p1.y);
if (p2 && p2.x != undefined)
line.setAttribute("x2", p2.x);
if (p2 && p2.y != undefined)
line.setAttribute("y2", p2.y);
};
//标题和介绍文本框
const titleBox = formationBox.querySelector(".title-box");
const detailBox = formationBox.querySelector(".detail-box");
const txtTitle = titleBox.querySelector(".title");
const txtDetail = detailBox.querySelector(".detail");
const txtTitleDisplay = titleBox.querySelector(".title-display");
const txtDetailDisplay = detailBox.querySelector(".detail-display");