-
Notifications
You must be signed in to change notification settings - Fork 12
/
script-universal_function.js
2012 lines (1910 loc) · 67.8 KB
/
script-universal_function.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
//仿GM_xmlhttpRequest函数v1.5
const GM_xmlhttpRequest = function(GM_param) {
const xhr = new XMLHttpRequest(); //创建XMLHttpRequest对象
xhr.open(GM_param.method, GM_param.url, true);
if (GM_param.responseType) xhr.responseType = GM_param.responseType;
if (GM_param.overrideMimeType) xhr.overrideMimeType(GM_param.overrideMimeType);
xhr.onreadystatechange = function(e) //设置回调函数
{
const _xhr = e.target;
if (_xhr.readyState === _xhr.DONE) { //请求完成时
console.debug("http状态码:", _xhr.status);
if ((_xhr.status === 200) && GM_param.onload) //正确加载时
{
GM_param.onload(_xhr);
} else if (_xhr.status !== 200 && GM_param.onerror) //发生错误时
{
GM_param.onerror(_xhr);
}
}
};
if (GM_param.onprogress)
xhr.upload.onprogress = function(e) { GM_param.onprogress(e.target) };
//添加header
for (let header in GM_param.headers) {
xhr.setRequestHeader(header, GM_param.headers[header]);
}
//发送数据
xhr.send(GM_param.data ? GM_param.data : null);
};
//获取URL参数
function getQueryString(name, inputURL = document.location) {
const url = new URL(inputURL);
if (!Array.isArray(name)) name = [name];
//可以以数组形式传递 name 的多个别名,比如 getQueryString(["l","lang"])
let value;
for (let index = 0; index < name.length; index++) {
value = url.searchParams.get(name[index]);
if (value) break;
}
return value;
}
const localStorage_getBoolean = function(name, defaultValue = false) {
const value = localStorage.getItem(name);
if (value === null) return defaultValue;
else if (typeof value === 'string' && /^\s*true\s*$/i.test(value)) return true;
else return Boolean(Number(value));
}
/* 写了,但是暂时不用
// 将字符串转为二进制字符串
String.prototype.toUTF8Blob = function() {
return new Blob([this.valueOf()], {
type: 'text/plain'
});
}
Blob.prototype.toBase64 = function() {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = (event) => {
resolve(event.target?.result);
};
// readAsDataURL
fileReader.readAsDataURL(this.valueOf());
fileReader.onerror = () => {
reject(new Error('blobToBase64 error'));
};
});
}
const Base64 = {
strToBase64: function(str) {
const encoder = new TextEncoder()
const view = encoder.encode(str);
const base64 = Base64.encodeFromUint8Array(view);
return base64;
},
base64ToStr: function(base64) {
const decoder = new TextDecoder()
const view = Base64.decodeToUint8Array(base64);
const str = decoder.decode(view);
return str;
},
//Base64还原成Uint8Array
decodeToUint8Array: function base64DecToArr(sBase64, nBlocksSize) {
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91
? nChr - 65
: nChr > 96 && nChr < 123
? nChr - 71
: nChr > 47 && nChr < 58
? nChr + 4
: nChr === 43
? 62
: nChr === 47
? 63
: 0;
}
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, ""); // Remove any non-base64 characters, such as trailing "=", whitespace, and more.
const nInLen = sB64Enc.length;
const nOutLen = nBlocksSize
? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize
: (nInLen * 3 + 1) >> 2;
const taBytes = new Uint8Array(nOutLen);
let nMod3;
let nMod4;
let nUint24 = 0;
let nOutIdx = 0;
for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4));
if (nMod4 === 3 || nInLen - nInIdx === 1) {
nMod3 = 0;
while (nMod3 < 3 && nOutIdx < nOutLen) {
taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
nMod3++;
nOutIdx++;
}
nUint24 = 0;
}
}
return taBytes;
},
//Uint8Array编码成Base64
encodeFromUint8Array: function base64EncArr(aBytes) {
function uint6ToB64(nUint6) {
return nUint6 < 26
? nUint6 + 65
: nUint6 < 52
? nUint6 + 71
: nUint6 < 62
? nUint6 - 4
: nUint6 === 62
? 43
: nUint6 === 63
? 47
: 65;
}
let nMod3 = 2;
let sB64Enc = "";
const nLen = aBytes.length;
let nUint24 = 0;
for (let nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
// To break your base64 into several 80-character lines, add:
// if (nIdx > 0 && ((nIdx * 4) / 3) % 76 === 0) {
// sB64Enc += "\r\n";
// }
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(
uint6ToB64((nUint24 >>> 18) & 63),
uint6ToB64((nUint24 >>> 12) & 63),
uint6ToB64((nUint24 >>> 6) & 63),
uint6ToB64(nUint24 & 63)
);
nUint24 = 0;
}
}
return (
sB64Enc.substring(0, sB64Enc.length - 2 + nMod3) +
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "==")
);
}
}
//Buffer转16进制字符串
Uint8Array.prototype.toHex = function() {
return [...this].map(n=>n.toString(16).padStart(2,'0')).join('');
}
*/
/**
* 大数字以数字量级分隔符形式输出
* @param {Array} separators 数字量级分隔符数组,从低到高排列
* @param {number} splitDigits 分隔位数
* @returns {function} 数字量级分隔符形式输出数字的函数
*/
function BigNumberToStringLocalise(separators, splitDigits = 3 ) {
if (!Array.isArray(separators)) throw new TypeError('分隔符需要使用数组列出数字量级');
if (!Number.isInteger(splitDigits)) throw new TypeError('数字分隔位数必须为整数');
if (splitDigits < 1) throw new RangeError('数字分隔位数至少是1位');
const grouping = 10 ** splitDigits;
separators = separators.map(String); //确保所有的分割符都是字符串
return function(options = {}){
const thisValue = this.valueOf();
if (thisValue === 0 ||
thisValue === Infinity ||
thisValue === -Infinity
) {
return this.toLocaleString();
}
if (Number.isNaN(thisValue)) return 0..bigNumberToString();
const numLevels = [];
let numTemp = Math.abs(thisValue);
do {
numLevels.push(numTemp % grouping); //这一段数量级的数字
numTemp = Math.floor(numTemp / grouping);
} while (numTemp > 0 && numLevels.length < (separators.length - 1))
if (numTemp > 0) {
numLevels.push(numTemp);
}
const { sub: useSubElement } = options;
const outArr = [];
if (thisValue < 0) outArr.push('-'); //小于0的添加负号
for (let i = numLevels.length - 1; i >= 0; i--) {
if (numLevels[i] == 0) continue; //如果这一段是0,直接不显示
let numberStr = numLevels[i].toString(10);
if (i == numLevels.length - 1) numberStr = numberStr.padStart(i == numLevels.length - 1 ? 1 : splitDigits, "0");
outArr.push(numberStr);
if (useSubElement) {
const separator = document.createElement("sub");
separator.textContent = separators[i];
outArr.push(separator);
} else {
outArr.push(separators[i]);
}
}
return useSubElement ? outArr.nodeJoin() : outArr.join('').trim();
}
}
Number.prototype.bigNumberToString = BigNumberToStringLocalise(['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q'], 3);
//最多保留N位小数,不留0
Number.prototype.keepCounts = function(decimalDigits = 2, plusSign = false) {
let newNumber = Number(this.toFixed(decimalDigits));
return (plusSign && this > 0 ? '+' : '') + newNumber.bigNumberToString();
}
//Bitwise
Number.prototype.notNeighbour = function() {
const num = this.valueOf();
return ~num & (num << 1 | num >> 1);
}
//数组删除自己尾部的空元素
Array.prototype.deleteLatter = function(item = null) {
let index = this.length - 1;
for (; index >= 0; index--) {
if (this[index] !== item) {
break;
}
}
this.splice(index + 1);
return this;
}
//数组去重,改变自身
Array.prototype.distinct = function() {
const _set = new Set(this);
this.length = 0;
this.push(..._set);
return this.valueOf();
}
//数组交换元素
Array.prototype.switch = function(i1, i2) {
if (Math.max(i1, i2) >= this.length) return false;
let temp = this[i1];
this[i1] = this[i2];
this[i2] = temp;
return true;
}
//数组随机排序
Array.prototype.shuffle = function() {
let length = this.length;
while (length) {
randomIndex = Math.floor(Math.random() * length--);
this.switch(length, randomIndex);
}
return this;
}
//数组随机移除元素
Array.prototype.randomShift = function() {
return this.splice(Math.random() * this.length, 1)?.[0];
}
//数组分组函数,用法:array.groupBy((a,b)=>a.type === b.type)
Array.prototype.groupBy = function(func) {
const groups = this.reduce((pre,cur)=>{
const grp = pre.find(grp=>grp?.[0] && func(grp?.[0], cur));
if (grp)
grp.push(cur);
else
pre.push([cur]);
return pre;
}, []);
return groups;
}
//将内容添加到代码片段
DocumentFragment.prototype.ap = function(...args)
{
const items = args.flat(Infinity).filter(item=>item !== null && item !== void 0);
this.append(...items);
return this;
}
/**
* 将数组和分隔符添加到一个代码片段,类似join
* @param {(string | Node)} separator 每个对象之间合并的分割符或代码片段
* @returns {DocumentFragment} 一个文档片段
*/
Array.prototype.nodeJoin = function(separator)
{
const frg = document.createDocumentFragment();
this.forEach((item, idx, arr)=>{
frg.ap(item);
if (idx < (arr.length - 1) && separator !== null && separator !== void 0) {
frg.ap(separator instanceof Node ? separator.cloneNode(true) : separator);
}
});
return frg;
}
//数组随机选择一个元素
Array.prototype.randomItem = function(){
return this[Math.randomInteger(this.length-1)];
};
Math.randomInteger = function(max, min = 0) {
let _max = Math.max(max, min),
_min = Math.min(max, min);
return this.floor(this.random()*(_max-_min+1)+_min);
}
Math.isPowerOfTwo = function(n) {
if (Number.isInteger(n) && n > 0)
return (n & (n - 1)) === 0;
else
return false;
}
class Bin extends Set {
static #typeError_Constructor = "传入的不是 number 和 bigint 类型或这个两个类型的数组";
static #typeError_FlagsNum = "传入的不是 number 和 bigint 类型";
static #typeError_FlagsArray = "传入的不是 number 类型的数组";
static #typeError_NotInteger = "传入的不是 整数";
static #rangeError_NotSafe = "传入的 number 大于 53 位";
/**
* 构建函数
* @param {(number | bigint | number[])} arg 传入参数
*/
constructor (arg) {
if (typeof arg === "number" || typeof arg === "bigint") {
super(Bin.unflags(arg));
} else if (Array.isArray(arg) &&
arg.every(item=>typeof item === "number" && Number.isSafeInteger(item))
){
super(arg);
} else {
throw new TypeError(Bin.#typeError_Constructor);
}
}
/**
* 将数字或大整形flag转换为数组
* @param {(number | bigint)} number 输入的数字
* @returns {number[]} 输出数组
*/
static unflags(number) {
const arr = [];
if (!number) return arr;
const inputType = typeof number;
if (inputType === "number" || inputType === "bigint"){
if (inputType === "number" && number > Number.MAX_SAFE_INTEGER) {
throw new RangeError(Bin.#rangeError_NotSafe);
}
const isBigint = inputType === "bigint";
for (let i = 0, flag = isBigint ? 1n : 1; flag <= number; i++, flag = (isBigint ? 2n : 2) ** (isBigint ? BigInt(i) : i)) {
if (number & flag) {
arr.push(i);
}
}
return arr;
} else {
throw new TypeError(Bin.#typeError_FlagsNum + " " + number);
}
}
/**
* 将数字序号转换为数字
* @param {number[]} indexArr 输入的序号数组
* @returns {(number | bigint)} 输出的数字
*/
static enflags(indexArr) {
if (Array.isArray(indexArr) &&
indexArr.every(item=>typeof item === "number" && Number.isSafeInteger(item))
){
let result = 0, isBigint = false, baseNum = 2;
for (let i = 0; i < indexArr.length; i++) {
let value = indexArr[i];
//当数值大于52位,即需要换BigInt
if (value > 52 && !isBigint) {
isBigint = true;
result = BigInt(result);
baseNum = BigInt(baseNum);
}
isBigint && (value = BigInt(value)); //一旦需要BigInt了,就转换
result = result + baseNum ** value; //用乘方相加而不是位移的优点是可以得到 0xFFFFFFFF 而不是 -1
}
return result;
} else {
throw new TypeError(Bin.#typeError_FlagsArray);
}
}
get int() {
return Bin.enflags(Array.from(this.values()));
}
add(index) {
if (typeof index === "number" && Number.isSafeInteger(index)){
super.add(index);
} else {
throw new TypeError(Bin.#typeError_NotInteger);
}
}
}
//带标签的模板字符串
function tp(stringsArr, ...keys) {
return ((...values)=>{
const dict = values[values.length - 1] || {};
const fragment = document.createDocumentFragment();
for (let i = 0; i < keys.length; i++) {
fragment.append(stringsArr[i]);
const key = keys[i];
const value = Number.isInteger(key) ? values[key] : dict[key];
if (value !== null && value !== void 0) {
try {
fragment.append((value instanceof Node && keys.lastIndexOf(key) !== i) ? value.cloneNode(true) : value); //如果是不最后一个匹配的标签,就插入克隆的DOM,否则可以插入原始的DOM(保留行为)
}
catch(e) {
console.error("模板字符串错误: %o,", e, values, keys, value);
}
}
}
//补上最后一个字符串
fragment.append(stringsArr[keys.length]);
return fragment;
});
}
//来自于 https://www.cnblogs.com/zhenguo-chen/p/14672332.html
function deepMerge(obj1, obj2) {
let key;
for (key in obj2) {
// 如果target(也就是obj1[key])存在,且是对象的话再去调用deepMerge,否则就是obj1[key]里面没这个对象,需要与obj2[key]合并
// 如果obj2[key]没有值或者值不是对象,此时直接替换obj1[key]
obj1[key] =
obj1[key] &&
obj1[key].toString() === "[object Object]" &&
(obj2[key] && obj2[key].toString() === "[object Object]")
? deepMerge(obj1[key], obj2[key])
: (obj1[key] = obj2[key]);
}
return obj1;
}
//▼ADPCM播放相关,来自 https://github.com/jy4340132/aaa
const pcmMemory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
const pcmImportObj = {
env: {
abortStackOverflow: () => { throw new Error("overflow"); },
table: new WebAssembly.Table({ initial: 0, maximum: 0, element: "anyfunc" }),
tableBase: 0,
memory: pcmMemory,
memoryBase: 102400,
STACKTOP: 0,
STACK_MAX: pcmMemory.buffer.byteLength,
}
};
let pcmPlayer = null;
let adpcm_wasm = null;
async function decodeAudio(fileName, decodeCallback) {
if (pcmPlayer != null) {
pcmPlayer.close();
}
pcmPlayer = new PCMPlayer(1, 44100);
let request = await fetch(fileName);
let buffer = await request.arrayBuffer();
let audioData = new Uint8Array(buffer);
if (audioData[16] == 0x10) { //普通WAV,建立一个audio来播放
console.debug('当前 WAV 为 普通 WAV');
const audioCtx = new AudioContext();
const decodedData = await audioCtx.decodeAudioData(buffer);
const source = new AudioBufferSourceNode(audioCtx);
source.buffer = decodedData;
source.connect(audioCtx.destination);
source.start(0);
} else { //audioData[16] == 0x14
console.debug('当前 WAV 为 PCM WAV');
let step = 160;
for (let i = 0; i < audioData.byteLength; i += step) {
let pcm16BitData = decodeCallback(audioData.slice(i, i + step));
let pcmFloat32Data = Std.shortToFloatData(pcm16BitData);
pcmPlayer.feed(pcmFloat32Data);
}
}
}
fetch("library/jy4340132-aaa/adpcm.wasm").then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, pcmImportObj))
.then((wasm) => {
adpcm_wasm = wasm;
/*addButton("adpcm").onclick = function () {
let decoder = new Adpcm(wasm, pcmImportObj);
decoder.resetDecodeState(new Adpcm.State(0, 0));
decodeAudio("demo.adpcm", decoder.decode.bind(decoder));
}*/
});
function playVoiceById(id) { //点击label才播放语音
if (!Number.isInteger(id)) {
throw new TypeError("传入的音频 ID 不是整数");
}
const sndURL = `sound/voice/${currentDataSource.code}/padv${id.toString().padStart(3,'0')}.wav`;
const decoder = new Adpcm(adpcm_wasm, pcmImportObj);
decoder.resetDecodeState(new Adpcm.State(0, 0));
decodeAudio(sndURL, decoder.decode.bind(decoder));
}
//▲ADPCM播放相关
// 加载 image
function loadImage(url) {
return new Promise(function(resolve, reject) {
const image = new Image();
image.src = url;
image.type = "svg"
image.crossOrigin = 'Anonymous';
image.onload = function() {
resolve(this);
};
image.onerror = function(err) {
reject(err);
};
});
}
//代码来自 https://segmentfault.com/a/1190000004451095
function fileReader (file, options = {}) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.onload = function () {
resolve(reader);
};
reader.onerror = reject;
if (options.accept && !new RegExp(options.accept).test(file.type)) {
reject({
code: 1,
msg: 'wrong file type'
});
}
if (!file.type || /^text\//i.test(file.type) || options.readType == "text") {
reader.readAsText(file);
} else {
reader.readAsDataURL(file);
}
});
}
function dbReadKey (db, tableName, keys) {
return new Promise(function (resolve, reject) {
const transaction = db.transaction([tableName]);
const objectStore = transaction.objectStore(tableName);
const request = objectStore.get(keys);
request.onsuccess = function(event) {
resolve(request.result);
};
request.onerror = reject;
});
}
function dbCount (db, tableName, key) {
return new Promise(function (resolve, reject) {
const transaction = db.transaction([tableName]);
const objectStore = transaction.objectStore(tableName);
const request = objectStore.count(key);
request.onsuccess = function() {
resolve(request.result);
}
request.onerror = reject;
});
}
function dbReadAll (db, tableName) {
return new Promise(async function (resolve, reject) {
const datas = [];
const transaction = db.transaction([tableName]);
const objectStore = transaction.objectStore(tableName);
const request = objectStore.openCursor();
request.onsuccess = function(event) {
const cursor = event.target.result;
if (cursor) {
// cursor.value 包含正在被遍历的当前记录
// 这里你可以对 result 做些什么
datas.push(cursor.value);
cursor.continue();
} else {
// 没有更多 results
resolve(datas);
}
};
request.onerror = reject;
});
}
function dbWrite (db, tableName, data, keys) {
return new Promise(function (resolve, reject) {
const transaction = db.transaction([tableName], "readwrite");
const objectStore = transaction.objectStore(tableName);
const request = objectStore.put(data, keys);
request.onsuccess = function(event) {
resolve(event);
};
request.onerror = reject;
});
}
function dbDelete (db, tableName, keys) {
return new Promise(function (resolve, reject) {
const transaction = db.transaction([tableName], "readwrite");
const objectStore = transaction.objectStore(tableName);
const request = objectStore.delete(keys);
request.onsuccess = function(event) {
resolve(event);
};
request.onerror = reject;
});
}
//1个潜觉需要用多少格子
function latentUseHole(latentId) {
switch (latentId) {
case 12: case 16: case 17: case 18: case 19:
case 20: case 21: case 22: case 23: case 24:
case 25: case 26: case 27: case 28: case 29:
case 30: case 31: case 32: case 33: case 34:
case 35: case 36: case 43: case 44: case 45:
{
return 2;
}
case 13: case 14: case 15: case 37: case 38:
case 39: case 40: case 41: case 42: case 46:
case 47: case 48: case 49:
{
return 6;
}
case 1: case 2: case 3: case 4: case 5:
case 6: case 7: case 8: case 9: case 10:
case 11: case 12:
default:
{
return 1;
}
}
}
//获取最大潜觉数量
function getMaxLatentCount(id) { //转生2和超转生3为8个格子
const card = Cards[id];
return card && card.is8Latent ? 8 : 6;
}
//计算用了多少潜觉格子
function usedHole(latents) {
return latents.reduce((usedHole, latentId) => usedHole + latentUseHole(latentId), 0);
}
//计算所有队伍中有多少个该觉醒
function awokenCountInFormation(formationTeams, awokenIndex, solo, teamsCount) {
const formationAwokenCount = formationTeams.reduce(function(previous, team) {
return previous + awokenCountInTeam(team, awokenIndex, solo, teamsCount);
}, 0);
return formationAwokenCount;
}
//计算单个队伍中有多少个该觉醒
function awokenCountInTeam(team, awokenIndex, solo, teamsCount) {
const [memberArray, assistArray] = team;
const teamAwokenCount = memberArray.reduce(function(previous, mon, idx) {
if (mon.id <= 0) { //如果是delay和null
return previous;
}
const card = Cards[mon.id];
if (!card || !card.enabled) { //如果卡片未启用
return previous;
}
const assist = assistArray[idx];
const assistCard = Cards[assist.id];
//启用的觉醒数组片段
let enableAwoken = card.awakenings.slice(0, mon.awoken);
//单人、3人时,大于等于100级且297时增加超觉醒
if ((solo || teamsCount === 3) && mon.sawoken > 0 &&
(mon.level >= 100 && mon.plus.every(p=>p>=99) ||
mon.sawoken === card.syncAwakening)
) {
enableAwoken.push(mon.sawoken);
}
if (assistCard && assistCard.enabled && assistCard.awakenings.includes(49)) { //如果卡片未启用
enableAwoken.push(...assistCard.awakenings.slice(0, assist.awoken));
}
//相同的觉醒数
const hasAwoken = enableAwoken.filter(ak => { return ak == awokenIndex; }).length;
return previous + hasAwoken;
}, 0);
return teamAwokenCount;
}
//返回可用的怪物名称
function returnMonsterNameArr(card, lsList = currentLanguage.searchlist, defaultCode = currentDataSource.code) {
const monNameArr = lsList.map(lc => { //取出每种语言
if (lc == defaultCode)
return card.name;
else if (card.otLangName)
return card.otLangName[lc];
}).filter(ln => //去掉空值和问号
typeof(ln) == "string" && ln.length > 0 && !new RegExp("^(?:초월\\s*)?[\\?\\*]+", "i").test(ln)
);
if (monNameArr.length < 1) //如果本来的列表里没有名字
{
monNameArr.push(card.name); //只添加默认名字
}
return monNameArr;
}
//Code From pad-rikuu
function valueAt(level, maxLevel, curve) {
const f = (maxLevel === 1 || level >= maxLevel) ? 1 : ((level - 1) / (maxLevel - 1));
return curve.min + (curve.max - curve.min) * f ** curve.scale;
}
//Code From pad-rikuu
function curve(c, level, maxLevel, limitBreakIncr, limitBreakIncr120) {
let value = valueAt(level, maxLevel, {
min: c.min,
max: c.max !== void 0 ? c.max : (c.min * maxLevel),
scale: c.scale || 1
});
if (level > maxLevel) {
const exceed99 = Math.min(level - maxLevel, 11);
const exceed110 = Math.max(0, level - 110);
value += c.max !== void 0 ?
((c.max * (limitBreakIncr / 100) * (exceed99 / 11)) + (c.max * (limitBreakIncr120 / 100) * (exceed110 / 10))) :
(c.min * exceed99 + c.min * exceed110);
}
return value;
}
//计算怪物的经验值
function calculateExp(member) {
if (!member) return null;
const memberCard = Cards[member.id];
if (!memberCard || memberCard.id == 0 || !memberCard.enabled) return null;
const expArray = [
Math.round(valueAt(member.level, 99, memberCard.exp)) //99级以内的经验
];
if (member.level > 99)
expArray.push(Math.max(0, Math.min(member.level, 110) - 100) * 5000000);
if (member.level > 110)
expArray.push(Math.max(0, Math.min(member.level, 120) - 110) * 20000000);
return expArray;
}
//计算怪物的能力
function calculateAbility(member, assist = null, solo = true, teamsCount = 1) {
if (!member) return null;
const memberCard = Cards[member.id];
const assistCard = assist ? Cards[assist.id] : null;
if (!memberCard || memberCard.id == 0 || !memberCard.enabled) return null;
const bonusScale = [0.1, 0.05, 0.15]; //辅助宠物附加的属性倍率
const plusAdd = [10, 5, 3]; //加值的增加值
const limitBreakIncr120 = [10, 5, 5]; //120三维增加百分比例
const awokenAdd = [ //对应加三维觉醒的序号与增加值
[{ index: 1, value: 500 }, { index: 65, value: -2500 }], //HP
[{ index: 2, value: 100 }, { index: 66, value: -1000 }], //ATK
[{ index: 3, value: 200 }, { index: 67, value: -2000 }] //RCV
];
const previousAwokenScale = [ //在297之前,对应比例加三维觉醒的序号与倍率值,就是 63 语音觉醒
[{ index: 63, scale: 1.1 }], //HP
[{ index: 63, scale: 1.1 }], //ATK
[{ index: 63, scale: 1.1 }] //RCV
];
const latterAwokenScale = [ //在297之后,对应比例加三维觉醒的序号与倍率值,30 协力觉醒、127 三维觉醒
[{ index: 127, scale: 1.5 }], //HP
[{ index: 127, scale: 1.5 }], //ATK
[{ index: 127, scale: 1.5 }] //RCV
];
if (!solo) { //协力时计算协力觉醒
latterAwokenScale.forEach(ab => {
ab.push({ index: 30, scale: 1.5 });
});
}
const latentScale = [ //对应加三维潜在觉醒的序号与增加比例
[{ index: 1, scale: 0.015 }, { index: 12, scale: 0.03 }, { index: 28, scale: 0.045 }, { index: 43, scale: 0.10 }], //HP
[{ index: 2, scale: 0.01 }, { index: 12, scale: 0.02 }, { index: 29, scale: 0.03 }, { index: 44, scale: 0.08 }], //ATK
[{ index: 3, scale: 0.1 }, { index: 12, scale: 0.2 }, { index: 30, scale: 0.3 }, { index: 45, scale: 0.35 }] //RCV
];
const memberCurves = [memberCard?.hp, memberCard?.atk, memberCard?.rcv];
const assistCurves = assistCard?.canAssist && [assistCard.hp, assistCard.atk, assistCard.rcv];
//储存点亮的觉醒
let awokenList = memberCard.awakenings.slice(0, member.awoken);
//单人、3人时,大于等于100级且297时增加超觉醒
if ((solo || teamsCount === 3) && member.sawoken > 0 &&
(member.level >= 100 && member.plus.every(p=>p>=99) ||
member.sawoken === memberCard.syncAwakening)
) {
awokenList.push(member.sawoken)
}
//如果有武器还要计算武器的觉醒
let enableBouns = false;
if (assistCard?.id > 0 && assistCard.enabled) {
const assistAwokenList = assistCard.awakenings.slice(0, assist.awoken); //储存武器点亮的觉醒
if (assistAwokenList.includes(49)) { //49是武器觉醒,确认已经点亮了武器觉醒
awokenList.push(...assistAwokenList);
}
enableBouns = memberCard.attrs[0] === assistCard.attrs[0] || memberCard.attrs[0] == 6 || assistCard.attrs[0] == 6;
}
//地下城强化
const dge = formation.dungeonEnchance;
const dgeRate = [dge.rate.hp, dge.rate.atk, dge.rate.rcv];
const isDge = (()=>{
const memberAttrsTypesWithWeapon = typeof member.getAttrsTypesWithWeapon === "function" ? member.getAttrsTypesWithWeapon(assist) : memberCard;
const baseBool = dge.rarities.includes(memberCard.rarity) //符合星级
|| dge?.collabs?.includes(memberCard.collabId) //符合合作
|| dge?.gachas?.some(n=>memberCard.gachaIds.includes(n)); //符合抽蛋桶
return {
awoken: baseBool //计算武器觉醒
|| memberAttrsTypesWithWeapon.attrs.some(attr=>dge.attrs.includes(attr)) //符合属性
|| memberAttrsTypesWithWeapon.types.some(type=>dge.types.includes(type)) //符合类型
,
noAwoken: baseBool //不计算武器觉醒
|| memberCard.attrs.some(attr=>dge.attrs.includes(attr)) //符合属性
|| memberCard.types.some(type=>dge.types.includes(type)) //符合类型
,
};
})();
//地下城阴阳加护强化
if (dge.benefit) { //当存在加护
const benefitAwokens = [128 , 129]; //0b1是阳,0b10是阴,可以两者都强化
Bin.unflags(dge.benefit).forEach(idx=>{
const akId = benefitAwokens[idx]; //得到加护觉醒编号
latterAwokenScale[0].push({ index: akId, scale: 1.2 }); //HP
latterAwokenScale[1].push({ index: akId, scale: 5 }); //ATK
latterAwokenScale[2].push({ index: akId, scale: 1.2 }); //RCV
});
}
if (dge.stage > 1) { //当存在地下城层数
let scale = 1;
if (dge.stage>=10) scale = 2;
else if (dge.stage>=5) scale = 1.5;
const akId = 130; //130号熟成觉醒
latterAwokenScale.forEach(ab => {
ab.push({ index: akId, scale: scale });
});
}
const abilitys = memberCurves.map((ab, idx) => {
const n_base = Math.round(curve(ab, member.level, memberCard.maxLevel, memberCard.limitBreakIncr, limitBreakIncr120[idx])); //等级基础三维
const n_plus = member.plus[idx] * plusAdd[idx]; //加值增加量
let n_assist_base = 0,
n_assist_plus = 0; //辅助的bonus
//计算辅助的额外血量
if (assistCurves && enableBouns) {
n_assist_base = Math.round(curve(assistCurves[idx], assist.level, assistCard.maxLevel, assistCard.limitBreakIncr, limitBreakIncr120[idx])); //辅助等级基础三维
n_assist_plus = assist.plus[idx] * plusAdd[idx]; //辅助加值增加量
}
//用来计算倍率觉醒的最终倍率是多少,reduce用
function calculateAwokenScale(previous, aw) {
const awokenCount = awokenList.filter(ak => ak == aw.index).length; //每个倍率觉醒的数量
return previous * aw.scale ** awokenCount;
}
//倍率类觉醒的比例,直接从1开始乘
const n_previousAwokenScale = previousAwokenScale[idx].reduce(calculateAwokenScale, 1);
//觉醒增加的数值
const n_awoken = awokenList.length > 0 ?
Math.round(awokenAdd[idx].reduce((previous, aw) => {
const awokenCount = awokenList.filter(ak => ak == aw.index).length; //每个觉醒的数量
if (awokenCount > 0)
return previous + aw.value * awokenCount;
else
return previous;
}, 0)) :
0;
//潜觉增加的倍率,从0开始,增加比例小于1,是加法不是乘法
const n_latentScale = (member.latent && member.latent.length > 0) ?
latentScale[idx].reduce((previous, la) => {
const latentCount = member.latent.filter(l => l === la.index).length; //每个潜觉的数量
return previous + la.scale * latentCount;
}, 0) :
0;
const dgeScale = { //地下城强化比例
awoken: isDge.awoken && dgeRate[idx] !== 1 ? dgeRate[idx] : 1,
noAwoken: isDge.noAwoken && dgeRate[idx] !== 1 ? dgeRate[idx] : 1,
};
if (idx === 1 && dgeScale.awoken < 1 && awokenList.includes(106)) {
//觉醒有浮游,比例乘以20
dgeScale.awoken = Math.min(1, dgeScale.awoken * 20);
}
let reValue = Math.round(n_base * n_previousAwokenScale) + n_plus +
Math.round(n_base * n_latentScale) + n_awoken +
Math.round((n_assist_base + n_assist_plus) * bonusScale[idx]);
//觉醒生效时的协力、1.5三维、阴阳、熟成等的倍率
reValue = Math.floor(reValue * latterAwokenScale[idx].reduce(calculateAwokenScale, 1) * dgeScale.awoken);
//因为语音觉醒觉醒无效也生效,所以这里需要计算
let reValueNoAwoken = Math.round(n_base * n_previousAwokenScale) + n_plus +
Math.round((n_assist_base + n_assist_plus) * bonusScale[idx]);
reValueNoAwoken = Math.floor(reValueNoAwoken * dgeScale.noAwoken)
if (idx < 2) //idx顺序为HP、ATK、RCV
{ //HP和ATK最低为1
reValue = Math.max(reValue, 1);
reValueNoAwoken = Math.max(reValueNoAwoken, 1);
}
return [reValue, reValueNoAwoken];
});
return abilitys;
}
function calculateAbility_max(id, solo, teamsCount, maxLevel = 110) {
const card = Cards[id];
const tempMon = {
id: id,
level: card.limitBreakIncr ? maxLevel : card.maxLevel,
plus: (card.stackable || card.types[0] == 15 && card.types[1] == -1) ? [0, 0, 0] : [99, 99, 99], //当可以叠加时,不能打297
awoken: card.awakenings.length,
sawoken: 0
};
/*强制计算超觉醒
if (card.superAwakenings.includes(127)) {
tempMon.sawoken = 127;
}*/
const abilities = calculateAbility(tempMon, null, solo, teamsCount);
if (abilities) {
const [[hp,hpNA], [atk,atkNA], [rcv,rcvNA]] = abilities;
return {
withAwoken: {
hp: hp,
atk: atk,
rcv: rcv,
},
noAwoken: {
hp: hpNA,
atk: atkNA,
rcv: rcvNA,
},
};
} else {
return null;
}
}
//搜索卡片用
function searchCards(cards, {attrs: sAttrs, fixMainColor, types, typeAndOr, rares, awokens, sawokens, equalAk, incSawoken, canAssist, canLv110, is8Latent, notWeapon}) {
let cardsRange = [...cards]; //这里需要复制一份原来的数组,不然若无筛选,后面的排序会改变初始Cards
if (canAssist) cardsRange = cardsRange.filter(card=>card.canAssist);
if (canLv110) cardsRange = cardsRange.filter(card=>card.limitBreakIncr>0);
if (is8Latent) cardsRange = cardsRange.filter(card=>card.is8Latent);
if (notWeapon) cardsRange = cardsRange.filter(card=>!card.awakenings.includes(49) && //不是武器
!card.stackable); //不可堆叠
//属性
const anyAttrsFlag = 0b101_1111; //所有颜色的查找,注意右边才是最低位
sAttrs = sAttrs.map(attr=>attr || anyAttrsFlag); //如果传入搜索为0,提高到任意色
if (sAttrs.some(attr=>(attr & anyAttrsFlag) !== anyAttrsFlag)) { //当任一属性不为任意颜色时才需要筛选属性,否则跳过属性筛选
//如果固定顺序就直接使用当前颜色顺序;否则不考虑顺序时,去除任意色
// const attrNums = sAttrs.filter(attr=>fixMainColor || attr > 0 && (attr & anyAttrsFlag) !== anyAttrsFlag)
// .map(attr=>{
// const attrNum = Bin.unflags(attr);
// if (attrNum.includes(6)) attrNum.push(undefined,-1); //如果是包含6的,就添加-1和undefined的值
// return attrNum;
// });
if (fixMainColor) {//如果固定了顺序
//只有第一属性有搜索内容时才搜索无主属性
const isSearchNoMainAttr = (sAttrs[0] ^ 0b100_0000) > 0 && sAttrs.slice(1).every(attr=>(attr & anyAttrsFlag) === anyAttrsFlag);
cardsRange = cardsRange.filter(({attrs:cAttrs}) => {
//默认逻辑为,只要不是any,就判断这个颜色是否包含了对应的颜色
//不能用怪物颜色来查找,因为怪物只有一个颜色就会提前退出循环,导致不搜索副属性
return sAttrs.every((sAttr, idx)=>{
if (idx === 0 && isSearchNoMainAttr && //第一属性搜索,需要搜索无主属性时
//只选第一属性的时候,且第一属性为无主属性的时候,也显示副属性等于主属性的
cAttrs[0] === 6 && //角色第一属性为无主属性