-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathygo-query.mjs
More file actions
1140 lines (1084 loc) · 32.2 KB
/
ygo-query.mjs
File metadata and controls
1140 lines (1084 loc) · 32.2 KB
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
import { rename, rm, writeFile } from 'node:fs/promises';
import { ltable_ocg, ltable_tcg, ltable_md, pack_list, pre_release, genesys_point, setname_table, load_name_table, ruby_table, id_to_cid } from './ygo-json-loader.mjs';
import { lang, bls_postfix, official_name, game_name } from './ygo-json-loader.mjs';
import { cid_table, name_table, md_table, md_card_list } from './ygo-json-loader.mjs';
import { escape_regexp, escape_wildcard, zh_collator, zh_compare } from './ygo-utility.mjs';
import { db_url1, db_url2, fetch_db } from './ygo-fetch.mjs';
import { card_types, monster_types, link_markers, md_rarity, spell_colors, trap_colors, CID_BLACK_LUSTER_SOLDIER, spell_types, trap_types, marker_char } from "./ygo-constant.mjs";
import { arg_full, arg_seventh, effect_filter, full_columns, full_filter, full_tables, stmt_full_count, stmt_full_default, stmt_seventh } from './ygo-sqlite.mjs';
import { like_pattern, name_condition, list_condition, alter_db, merge_db, query_db_v2, setcode_condition, sqlite3_open } from './ygo-sqlite.mjs';
export const regexp_mention = `(?<=「)[^「」]*「?[^「」]*」?[^「」]*(?=」)`;
const RESULT_PER_PAGE = 50;
/**
* @type {import('node:sqlite').DatabaseSync}
*/
let db = null;
/**
* @typedef {object} Entry
* @property {number} id
* @property {number} ot
* @property {number} alias
* @property {number} rule_code
* @property {number[]} setcode
* @property {number} type
* @property {number} atk
* @property {number} def
* @property {number} level
* @property {bigint} race
* @property {number} attribute
* @property {number} scale
*
* @property {string} name
* @property {string} desc
*/
/**
* @typedef {object} CardText
* @property {string} desc
* @property {string} [db_desc]
*/
/**
* @typedef {object} Card
* @property {number} id
* @property {number} [cid]
* @property {number} [rule_code]
* @property {string} tw_name
* @property {string} [ae_name]
* @property {string} [en_name]
* @property {string} [jp_name]
* @property {string} [jp_ruby]
* @property {string} [kr_name]
* @property {string} [md_name_en]
* @property {string} [md_name_jp]
*
* @property {number} ot
* @property {number[]} setcode
* @property {number} type
* @property {number} atk
* @property {number} [def]
* @property {number} [marker]
* @property {number} level
* @property {bigint} race
* @property {number} attribute
* @property {number} [scale]
* @property {number} [md_rarity]
* @property {CardText} text
*
* @property {number} artid
* @property {number} color - Card color for sorting
* @property {number} [pack_index]
*/
/**
* @type {Map<number, Card[]>}
*/
const multimap_seventh = new Map();
const card_names = new Map();
//workaround
await init_query();
/**
* @param {Entry} cdata
* @returns {Card}
*/
function generate_card(cdata) {
let id = cdata.id;
let artid = 0;
if (cdata.alias) {
id = cdata.alias;
artid = cdata.id;
}
const card = Object.create(null);
card.id = id;
// table lookup for alternative art
if (id_to_cid.has(id))
card.cid = id_to_cid.get(id);
if (cdata.rule_code)
card.rule_code = cdata.rule_code;
card.tw_name = cdata.name;
if (card.cid) {
for (const [locale, prop] of Object.entries(official_name)) {
if (name_table[locale][card.cid])
card[prop] = name_table[locale][card.cid];
else if (md_table[locale] && md_table[locale][card.cid])
card[game_name[locale]] = md_table[locale][card.cid];
if (locale === 'ja' && ruby_table[card.cid])
card.jp_ruby = ruby_table[card.cid];
}
}
for (const column in cdata) {
switch (column) {
case "id":
case "cid":
case "alias":
case "rule_code":
case "name":
case "desc":
continue;
case "scale":
if (cdata.type & monster_types.TYPE_PENDULUM)
card.scale = cdata.scale;
break;
case "def":
if (cdata.type & monster_types.TYPE_LINK)
card.marker = cdata.def;
else
card.def = cdata.def;
break;
default:
card[column] = cdata[column];
break;
}
}
if (card.cid && md_card_list[card.cid])
card.md_rarity = md_card_list[card.cid];
card.text = Object.create(null);
card.text.desc = cdata.desc;
card.artid = artid;
// color
if (card.type & card_types.TYPE_MONSTER) {
if (!(card.type & monster_types.TYPES_EXTRA)) {
if (card.type & monster_types.TYPE_TOKEN)
card.color = 0;
else if (card.type & monster_types.TYPE_NORMAL)
card.color = 1;
else if (card.type & monster_types.TYPE_RITUAL)
card.color = 3;
else if (card.type & monster_types.TYPE_EFFECT)
card.color = 2;
else
card.color = -1;
}
else {
if (card.type & monster_types.TYPE_FUSION)
card.color = 4;
else if (card.type & monster_types.TYPE_SYNCHRO)
card.color = 5;
else if (card.type & monster_types.TYPE_XYZ)
card.color = 6;
else if (card.type & monster_types.TYPE_LINK)
card.color = 7;
else
card.color = -1;
}
}
else if (card.type & card_types.TYPE_SPELL) {
const extype = card.type & ~card_types.TYPE_SPELL;
if (spell_colors[extype])
card.color = spell_colors[extype];
else
card.color = -1;
}
else if (card.type & card_types.TYPE_TRAP) {
const extype = card.type & ~card_types.TYPE_TRAP;
if (trap_colors[extype])
card.color = trap_colors[extype];
else
card.color = -1;
}
else {
card.color = -1;
}
return card;
}
/**
* The sqlite condition of monsters related to No.101 ~ No.107.
* @returns {string}
*/
// eslint-disable-next-line no-unused-vars
function seventh_condition() {
let condition1 = '0';
for (let i = 1; i <= 13; i += 1) {
if (!multimap_seventh.has(i))
continue;
let attr_value = 0;
let race_value = 0n;
for (const card of multimap_seventh.get(i)) {
attr_value |= card.attribute;
race_value |= card.race;
}
condition1 += ` OR level = ${i} AND (attribute & ${attr_value} OR race & ${race_value})`;
}
const ret = ` AND type & $monster AND NOT type & $extra AND (${condition1})`;
return ret;
}
// query
function is_string(str) {
return typeof str === 'string' && str.length > 0;
}
/**
* Parse param into sqlite statement condition.
* @param {object} params
* @param {number[]} [id_list]
* @returns {[string, object]}
*/
export function generate_condition(params, id_list) {
let qstr = "";
const arg = {};
const key_condition = [];
// primary key
if (Number.isSafeInteger(params.id)) {
key_condition.push('id = $id');
arg.$id = params.id;
}
if (Number.isSafeInteger(params.cid)) {
key_condition.push('cid = $cid');
arg.$cid = params.cid;
}
if (key_condition.length) {
qstr = ` AND (${key_condition.join(' OR ')})`;
return [qstr, arg];
}
// number
if (Array.isArray(id_list) && id_list.length) {
qstr += ` AND ${list_condition('id', 'id', id_list, arg)}`;
}
if (Number.isSafeInteger(params.ot) && params.ot > 0) {
qstr += " AND ot & $ot_mask = $ot";
arg.$ot_mask = 0x3;
arg.$ot = params.ot;
}
if (Number.isSafeInteger(params.ot_exclude) && params.ot_exclude > 0) {
qstr += " AND ot & $ot_mask != $ot_exclude";
arg.$ot_mask = 0x3;
arg.$ot_exclude = params.ot_exclude;
}
if (Number.isSafeInteger(params.type) && params.type > 0) {
qstr += " AND type & $type";
arg.$type = params.type;
}
if (Number.isSafeInteger(params.monster_type) && params.monster_type > 0) {
qstr += " AND type & $monster";
arg.$monster = card_types.TYPE_MONSTER;
if (Number.isSafeInteger(params.monster_type_op) && params.monster_type_op)
qstr += " AND type & $monster_type = $monster_type";
else
qstr += " AND type & $monster_type";
arg.$monster_type = params.monster_type;
}
if (Number.isSafeInteger(params.excluded_type) && params.excluded_type > 0) {
qstr += " AND NOT type & $excluded_type";
arg.$excluded_type = params.excluded_type;
}
if (Number.isSafeInteger(params.spell_type) && params.spell_type > 0) {
let subtype = params.spell_type;
let subtype_condition = "type & $stype";
if (subtype & spell_types.TYPE_NORMAL) {
subtype_condition += " OR type = $spell";
subtype = (subtype & ~spell_types.TYPE_NORMAL) >>> 0;
}
qstr += ` AND type & $spell AND (${subtype_condition})`;
arg.$spell = card_types.TYPE_SPELL;
arg.$stype = subtype;
}
if (Number.isSafeInteger(params.trap_type) && params.trap_type > 0) {
let subtype = params.trap_type;
let subtype_condition = "type & $ttype";
if (subtype & trap_types.TYPE_NORMAL) {
subtype_condition += " OR type = $trap";
subtype = (subtype & ~trap_types.TYPE_NORMAL) >>> 0;
}
qstr += ` AND type & $trap AND (${subtype_condition})`;
arg.$trap = card_types.TYPE_TRAP;
arg.$ttype = subtype;
}
if (Number.isSafeInteger(params.mention)) {
const tw_name = card_names.get(params.mention);
if (tw_name) {
if (Object.hasOwn(setname_table, tw_name)) {
qstr += `${effect_filter} AND "desc" REGEXP $mention`;
arg.$mention = `「${escape_regexp(tw_name)}」(?!怪|魔|陷|卡|融合怪獸|同步怪獸|超量怪獸|連結怪獸|儀式怪獸|靈擺怪獸|通常|永續|裝備|速攻|儀式魔法|場地|反擊)`;
}
else {
qstr += `${effect_filter} AND "desc" LIKE $mention ESCAPE '$'`;
arg.$mention = `%「${escape_wildcard(tw_name)}」%`;
}
arg.$normal = monster_types.TYPE_NORMAL;
arg.$pendulum = monster_types.TYPE_PENDULUM;
}
}
if (Number.isSafeInteger(params.md_rarity)) {
qstr += " AND md_rarity = $md_rarity";
arg.$md_rarity = params.md_rarity;
}
if (typeof params.pack === 'string' && Object.hasOwn(pack_list, params.pack)) {
const pack = pack_list[params.pack].filter(x => Number.isSafeInteger(x) && x > 0);
qstr += ` AND ${list_condition('id', 'pack', pack, arg)}`;
}
else if (typeof params.pack === 'string' && Object.hasOwn(pre_release, params.pack)) {
qstr += " AND (id BETWEEN $pack_begin AND $pack_end)";
arg.$pack_begin = pre_release[params.pack];
arg.$pack_end = pre_release[params.pack] + 500;
}
else if (Number.isSafeInteger(params.limit) && params.limit > 0) {
arg.$limit = params.limit;
if (Number.isSafeInteger(params.offset) && params.offset >= 0) {
arg.$offset = params.offset;
}
else {
arg.$offset = 0;
}
}
// text
if (is_string(params.keyword)) {
qstr += ` AND (${name_condition(params.keyword, arg)} OR "desc" LIKE $name ESCAPE '$')`;
}
else {
if (is_string(params.name)) {
qstr += ` AND ${name_condition(params.name, arg)}`;
}
else if (Number.isSafeInteger(params.setcode) && params.setcode > 0) {
qstr += ` AND ${setcode_condition(params.setcode, arg)}`;
}
if (is_string(params.desc)) {
qstr += ` AND "desc" LIKE $desc ESCAPE '$'`;
arg.$desc = like_pattern(params.desc);
}
}
if (is_string(params.en_name)) {
qstr += ` AND en_name LIKE $en_name ESCAPE '$'`;
arg.$en_name = params.en_name;
}
if (is_string(params.jp_name)) {
qstr += ` AND jp_name LIKE $jp_name ESCAPE '$'`;
arg.$jp_name = params.jp_name;
}
if (is_string(params.jp_ruby)) {
qstr += ` AND jp_ruby LIKE $jp_ruby ESCAPE '$'`;
arg.$jp_ruby = params.jp_ruby;
}
const command_length = qstr.length;
if (Number.isSafeInteger(params.material)) {
const tw_name = card_names.get(params.material);
if (tw_name) {
const material = escape_wildcard(tw_name);
let material_condition = "0";
for (let i = 0; i < 4; i += 1) {
material_condition += ` OR "desc" LIKE $mat${i} ESCAPE '$'`;
}
qstr += ` AND (${material_condition})`;
arg.$mat0 = `「${material}」+%`;
arg.$mat1 = `「${material}」(%)+%`;
arg.$mat2 = `%+「${material}」%`;
arg.$mat3 = `%「${material}」×%`;
}
}
// atk
let atk_from = -10;
let atk_to = -10;
let atk_condition = "";
if (Number.isSafeInteger(params.atk_from) && params.atk_from >= -1)
atk_from = params.atk_from;
if (Number.isSafeInteger(params.atk_to) && params.atk_to >= -1)
atk_to = params.atk_to;
if (atk_from === -1 || atk_to === -1) {
atk_condition = "atk = $unknown";
arg.$unknown = -2;
}
else if (atk_to >= 0) {
if (atk_from < 0)
atk_from = 0;
atk_condition = "(atk BETWEEN $atk_from AND $atk_to)";
arg.$atk_from = atk_from;
arg.$atk_to = atk_to;
}
else if (atk_from >= 0) {
atk_condition = "atk >= $atk_from";
arg.$atk_from = atk_from;
}
// def, exclude link monsters
let has_def = false;
let def_from = -10;
let def_to = -10;
let def_condition = "";
if (Number.isSafeInteger(params.def_from) && params.def_from >= -2)
def_from = params.def_from;
if (Number.isSafeInteger(params.def_to) && params.def_to >= -1)
def_to = params.def_to;
if (def_from === -1 || def_to === -1) {
def_condition = "def = $unknown";
arg.$unknown = -2;
has_def = true;
}
else if (def_from === -2) {
def_condition = "def = atk AND def >= $zero";
arg.$zero = 0;
has_def = true;
}
else if (def_to >= 0) {
if (def_from < 0)
def_from = 0;
def_condition = "(def BETWEEN $def_from AND $def_to)";
arg.$def_from = def_from;
arg.$def_to = def_to;
has_def = true;
}
else if (def_from >= 0) {
def_condition = "def >= $def_from";
arg.$def_from = def_from;
has_def = true;
}
if (atk_condition && def_condition) {
qstr += ` AND (${atk_condition} AND ${def_condition})`;
}
else if (atk_condition) {
qstr += ` AND ${atk_condition}`;
}
else if (def_condition) {
qstr += ` AND ${def_condition}`;
}
if (Number.isSafeInteger(params.sum) && params.sum >= 0) {
qstr += " AND atk >= $zero AND def >= $zero AND atk + def = $sum";
arg.$zero = 0;
arg.$sum = params.sum;
has_def = true;
}
if (has_def) {
qstr += " AND NOT type & $link";
arg.$link = monster_types.TYPE_LINK;
}
// lv, rank, link
if (Array.isArray(params.level) && params.level.length) {
qstr += ` AND ${list_condition('level', 'level', params.level, arg)}`;
}
else {
let level_from = -10;
let level_to = -10;
if (Number.isSafeInteger(params.level_from) && params.level_from >= 0)
level_from = params.level_from;
if (Number.isSafeInteger(params.level_to) && params.level_to >= 0)
level_to = params.level_to;
if (level_from >= 0 && level_to >= 0) {
qstr += " AND (level BETWEEN $level_from AND $level_to)";
arg.$level_from = level_from;
arg.$level_to = level_to;
}
else if (level_from >= 0) {
qstr += " AND level >= $level_from";
arg.$level_from = level_from;
}
else if (level_to >= 0) {
qstr += " AND level <= $level_to";
arg.$level_to = level_to;
}
}
// scale, pendulum monster only
let has_scale = false;
if (Array.isArray(params.scale) && params.scale.length) {
qstr += ` AND ${list_condition('scale', 'scale', params.scale, arg)}`;
has_scale = true;
}
else {
let scale_from = -10;
let scale_to = -10;
if (Number.isSafeInteger(params.scale_from) && params.scale_from >= 0)
scale_from = params.scale_from;
if (Number.isSafeInteger(params.scale_to) && params.scale_to >= 0)
scale_to = params.scale_to;
if (scale_from >= 0 && scale_to >= 0) {
qstr += ` AND (scale BETWEEN $scale_from AND $scale_to)`;
arg.$scale_from = scale_from;
arg.$scale_to = scale_to;
has_scale = true;
}
else if (scale_from >= 0) {
qstr += ` AND scale >= $scale_from`;
arg.$scale_from = scale_from;
has_scale = true;
}
else if (scale_to >= 0) {
qstr += ` AND scale <= $scale_to`;
arg.$scale_to = scale_to;
has_scale = true;
}
}
if (has_scale) {
qstr += " AND type & $pendulum";
arg.$pendulum = monster_types.TYPE_PENDULUM;
}
// attribute, race
if (Number.isSafeInteger(params.attribute) && params.attribute > 0) {
qstr += " AND attribute & $attribute";
arg.$attribute = params.attribute;
}
if (typeof params.race === 'bigint' && params.race > 0) {
qstr += " AND race & $race";
arg.$race = BigInt.asUintN(64, params.race);
}
// marker
if (Number.isSafeInteger(params.marker) && params.marker > 0) {
qstr += " AND type & $link";
arg.$link = monster_types.TYPE_LINK;
if (Number.isSafeInteger(params.marker_op) && params.marker_op)
qstr += " AND def & $marker = $marker";
else
qstr += " AND def & $marker";
arg.$marker = params.marker;
}
if (qstr.length !== command_length && !arg.$monster) {
qstr += " AND type & $monster";
arg.$monster = card_types.TYPE_MONSTER;
}
return [qstr, arg];
}
/**
* @param {string[]?} files
*/
export async function init_query(files = null) {
if (files === null || files.length === 0) {
const current_path = `${import.meta.dirname}/db/query.cdb`;
const base = `${import.meta.dirname}/db/main.cdb`;
const ext1 = `${import.meta.dirname}/db/pre.cdb`;
try {
const task1 = fetch_db(db_url1).then(data => writeFile(base, data));
const task2 = fetch_db(db_url2).then(data => writeFile(ext1, data));
await Promise.all([task1, task2]);
}
catch (error) {
console.error(error);
return;
}
const full_db = merge_db(base, [ext1]);
if (!full_db) {
return;
}
alter_db(full_db);
load_name_table(full_db);
full_db.close();
db?.close();
await rm(current_path, { force: true });
await rename(base, current_path);
db = sqlite3_open(current_path);
await rm(ext1, { force: true });
}
else {
db?.close();
const full_db = merge_db(files[0], files.slice(1));
if (!full_db) {
return;
}
alter_db(full_db);
load_name_table(full_db);
full_db.close();
db = sqlite3_open(files[0]);
}
// refresh multimap of No.101 ~ No.107
multimap_seventh.clear();
const seventh_cards = query(stmt_seventh, arg_seventh);
seventh_cards.sort((c1, c2) => zh_collator.compare(c1.tw_name, c2.tw_name));
for (const card of seventh_cards) {
if (!multimap_seventh.has(card.level))
multimap_seventh.set(card.level, []);
multimap_seventh.get(card.level).push(card);
}
const stmt1 = `SELECT id, name FROM ${full_tables} WHERE 1 = 1${full_filter}`;
for (const entry of query_db_v2(db, stmt1, arg_full)) {
card_names.set(entry.id, entry.name);
}
}
/**
* Check if `card.setcode` contains `value`.
* @param {Card} card
* @param {number} value
* @returns {boolean}
*/
export function is_setcode(card, value) {
const settype = value & 0x0fff;
const setsubtype = value & 0xf000;
for (const x of card.setcode) {
if ((x & 0x0fff) === settype && (x & setsubtype) === setsubtype)
return true;
}
return false;
}
/**
* Query card from all databases with statement `qstr` and binding object `arg`.
* @param {string} qstr
* @param {object} arg
* @returns {Card[]}
*/
export function query(qstr = stmt_full_default, arg = arg_full) {
const ret = [];
for (const cdata of query_db_v2(db, qstr, arg)) {
ret.push(generate_card(cdata));
}
return ret;
}
/**
* Query card from all databases with JSON object `params`.
* @param {object} params
*/
export function query_card(params) {
const meta = {
total: 0,
limit: 0,
offset: 0,
};
const [condition, arg_condition] = generate_condition(params);
if (Object.keys(arg_condition).length === 0) {
return { result: [], meta };
}
if (Number.isSafeInteger(params.id) || Number.isSafeInteger(params.cid)) {
const stmt = `SELECT ${full_columns} FROM ${full_tables} WHERE NOT (type & $token) AND (cid IS NOT NULL OR alias != 0 OR id > $max_id)${condition}`;
const arg = {
...arg_full,
...arg_condition,
};
const result = query(stmt, arg);
meta.total = result.length;
return { result, meta };
}
const stmt1 = `${stmt_full_default}${condition}`;
const arg1 = {
...arg_full,
...arg_condition,
};
const result = query(stmt1, arg1);
meta.total = result.length;
if (result.length === 0) {
return { result, meta };
}
let is_sorted = false;
if (typeof params.pack === 'string' && Object.hasOwn(pack_list, params.pack)) {
const pack = pack_list[params.pack];
const index_table = new Map();
for (let i = 0; i < pack.length; i += 1) {
if (Number.isSafeInteger(pack[i]) && pack[i] > 0) {
index_table.set(pack[i], i);
}
}
for (const card of result) {
card.pack_index = index_table.get(card.id);
}
result.sort((a, b) => a.pack_index - b.pack_index);
is_sorted = true;
meta.pack = params.pack;
}
else if (typeof params.pack === 'string' && Object.hasOwn(pre_release, params.pack)) {
is_sorted = true;
meta.pack = params.pack;
}
else if (arg_condition.$limit) {
meta.limit = arg_condition.$limit;
meta.offset = arg_condition.$offset;
}
if (meta.limit > 0) {
const command = `${stmt_full_count}${condition};`;
const arg2 = { ...arg1 };
delete arg2.$limit;
delete arg2.$offset;
const st = db.prepare(command);
st.setReturnArrays(true);
const rows = st.all(arg2);
meta.total = rows[0]?.[0] ?? 0;
return { result, meta };
}
if (Number.isSafeInteger(params.page) && params.page > 0) {
if (!is_sorted) {
result.sort(compare_card);
}
const begin = (params.page - 1) * RESULT_PER_PAGE;
const section = result.slice(begin, begin + RESULT_PER_PAGE);
meta.total = result.length;
return { result: section, meta };
}
return { result, meta };
}
/**
* The compare function of Card.
* @param {Card} a
* @param {Card} b
* @returns {number}
*/
export function compare_card(a, b) {
if (a.color !== b.color) {
return a.color - b.color;
}
if (a.level !== b.level) {
return b.level - a.level;
}
return zh_collator.compare(a.tw_name, b.tw_name);
}
/**
* @param {string} target_name
* @param {string} locale
* @returns {(card: Card) => number}
*/
function get_match_function(target_name, locale) {
if (locale === 'en') {
return (card) => {
const en_name = card.en_name ?? card.md_name_en;
return en_name.toLowerCase() === target_name ? 1 : 0;
};
}
return card => card.tw_name.toLowerCase() === target_name ? 1 : 0;
}
/**
* @param {string?} name
* @param {string} locale
* @returns {(a: Card, b: Card) => number}
*/
// eslint-disable-next-line no-unused-vars
function get_compare_function(name, locale) {
const target_name = name?.toLowerCase();
if (!target_name) {
return compare_card;
}
const match = get_match_function(target_name, locale);
return (a, b) => {
const scoreA = match(a);
const scoreB = match(b);
if (scoreA !== scoreB) {
return scoreB - scoreA;
}
return compare_card(a, b);
}
}
/**
* Get a card with id from all databases.
* @param {number|string} id
* @returns {?Card}
*/
export function get_card(id) {
if (typeof id === 'string')
id = Number.parseInt(id, 10);
if (!Number.isSafeInteger(id))
return null;
const stmt_id = `${stmt_full_default} AND id = $id;`;
const arg_id = {
...arg_full,
$id: id,
};
const result = query(stmt_id, arg_id);
if (result.length === 0)
return null;
return result[0];
}
/**
* Get the request_locale of `card` in region `locale`.
* @param {Card} card
* @param {string} locale
* @returns {string}
*/
export function get_request_locale(card, locale) {
if (card[official_name[locale]]) {
return locale;
}
if (card.ot === 2) {
return 'en';
}
if (card.jp_name) {
return 'ja';
}
if (card.md_rarity) {
return 'md';
}
return 'ja';
}
/**
* Get No.101 ~ No.107 Xyz Monsters with the same race or attribute.
* @param {Card} card
* @returns {Card[]}
*/
export function get_seventh_xyz(card) {
if (!(card.type & card_types.TYPE_MONSTER))
return [];
if (card.type & monster_types.TYPES_EXTRA)
return [];
if (!multimap_seventh.has(card.level))
return [];
const result = [];
for (const seventh of multimap_seventh.get(card.level)) {
if ((seventh.race & card.race) || (seventh.attribute & card.attribute)) {
result.push(seventh);
}
}
return result;
}
// print
/**
* Print the ATK or DEF of a card.
* @param {number} x
* @returns {string}
*/
export function print_ad(x) {
if (x === -2)
return '?';
else
return `${x}`;
}
/**
* Return the formatted lines of `card` in language `locale`.
* @param {Card} card
* @param {string} locale
* @returns {string[]}
*/
export function print_data(card, locale) {
const strings = lang[locale];
const result = [];
if (card.type & card_types.TYPE_MONSTER) {
const mtype = strings.type_name[card_types.TYPE_MONSTER];
let subtype = '';
let lvstr = '\u2605';
if (card.type & monster_types.TYPE_RITUAL) {
subtype = `/${strings.type_name[monster_types.TYPE_RITUAL]}`;
}
else if (card.type & monster_types.TYPE_FUSION) {
subtype = `/${strings.type_name[monster_types.TYPE_FUSION]}`;
}
else if (card.type & monster_types.TYPE_SYNCHRO) {
subtype = `/${strings.type_name[monster_types.TYPE_SYNCHRO]}`;
}
else if (card.type & monster_types.TYPE_XYZ) {
subtype = `/${strings.type_name[monster_types.TYPE_XYZ]}`;
lvstr = `\u2606`;
}
else if (card.type & monster_types.TYPE_LINK) {
subtype = `/${strings.type_name[monster_types.TYPE_LINK]}`;
lvstr = `LINK-`;
}
else if (card.type & monster_types.TYPE_SPSUMMON) {
subtype = `/${strings.type_name[monster_types.TYPE_SPSUMMON]}`;
}
if (card.type & monster_types.TYPE_PENDULUM) {
subtype += `/${strings.type_name[monster_types.TYPE_PENDULUM]}`;
}
// extype
if (card.type & monster_types.TYPE_NORMAL)
subtype += `/${strings.type_name[monster_types.TYPE_NORMAL]}`;
if (card.type & monster_types.TYPE_SPIRIT)
subtype += `/${strings.type_name[monster_types.TYPE_SPIRIT]}`;
if (card.type & monster_types.TYPE_UNION)
subtype += `/${strings.type_name[monster_types.TYPE_UNION]}`;
if (card.type & monster_types.TYPE_DUAL)
subtype += `/${strings.type_name[monster_types.TYPE_DUAL]}`;
if (card.type & monster_types.TYPE_TUNER)
subtype += `/${strings.type_name[monster_types.TYPE_TUNER]}`;
if (card.type & monster_types.TYPE_FLIP)
subtype += `/${strings.type_name[monster_types.TYPE_FLIP]}`;
if (card.type & monster_types.TYPE_TOON)
subtype += `/${strings.type_name[monster_types.TYPE_TOON]}`;
if (card.type & monster_types.TYPE_EFFECT)
subtype += `/${strings.type_name[monster_types.TYPE_EFFECT]}`;
result.push(`[${mtype}${subtype}]`);
const level = `${lvstr}${card.level || '?'}`;
const attribute = `/${strings.attribute_name[card.attribute] ?? 'null'}`;
const race = `/${strings.race_name[card.race] ?? 'null'}`;
const attack = `/${strings.value_name['atk']}${print_ad(card.atk)}`;
const defense = !(card.type & monster_types.TYPE_LINK) ? `/${strings.value_name['def']}${print_ad(card.def)}` : '';
result.push(`${level}${attribute}${race}${attack}${defense}`);
if (card.type & monster_types.TYPE_PENDULUM) {
const scale_left = ':small_blue_diamond:';
const scale_right = ':small_orange_diamond:';
result.push(`${scale_left}${card.scale}/${card.scale}${scale_right}`);
}
if (card.type & monster_types.TYPE_LINK) {
let marker_text = '';
for (let marker = link_markers.LINK_MARKER_TOP_LEFT; marker <= link_markers.LINK_MARKER_TOP_RIGHT; marker <<= 1) {
if (card.marker & marker)
marker_text += marker_char[marker];
else
marker_text += marker_char['default'];
}
result.push(marker_text);
marker_text = '';
if (card.marker & link_markers.LINK_MARKER_LEFT)
marker_text += marker_char[link_markers.LINK_MARKER_LEFT];
else
marker_text += marker_char['default'];
marker_text += marker_char.center;
if (card.marker & link_markers.LINK_MARKER_RIGHT)
marker_text += marker_char[link_markers.LINK_MARKER_RIGHT];
else
marker_text += marker_char['default'];
result.push(marker_text);
marker_text = '';
for (let marker = link_markers.LINK_MARKER_BOTTOM_LEFT; marker <= link_markers.LINK_MARKER_BOTTOM_RIGHT; marker <<= 1) {
if (card.marker & marker)
marker_text += marker_char[marker];
else
marker_text += marker_char['default'];
}
result.push(marker_text);
}
}
else if (card.type & card_types.TYPE_SPELL) {
const extype = card.type & ~card_types.TYPE_SPELL;
const mtype = `${strings.type_name[card_types.TYPE_SPELL]}`;
const subtype = `/${strings.type_name[extype] ?? '???'}`;
result.push(`[${mtype}${subtype}]`);
}
else if (card.type & card_types.TYPE_TRAP) {
const extype = card.type & ~card_types.TYPE_TRAP;
const mtype = `${strings.type_name[card_types.TYPE_TRAP]}`;
const subtype = `/${strings.type_name[extype] ?? '???'}`;
result.push(`[${mtype}${subtype}]`);
}
return result;
}
/**
* Print `card` in language `locale`.
* @param {Card} card
* @param {string} locale
* @returns {string}
*/
export function print_card(card, locale) {
const strings = lang[locale];
let card_name = 'null';
let other_name = '';
let desc = '';
switch (locale) {
case 'zh-tw':
card_name = card.tw_name;
if (card.jp_name)
other_name += `${card.jp_name}\n`;
else if (card.md_name_jp)
other_name += `${card.md_name_jp} (MD)\n`;
if (card.en_name)
other_name += `${card.en_name}\n`;
else if (card.md_name_en)
other_name += `${card.md_name_en} (MD)\n`;
desc = `${card.text.desc}\n--`;
break;
case 'ae':
card_name = card.ae_name;
if (card.jp_name)
other_name = `${card.jp_name}\n`;
else if (card.md_name_jp)
other_name = `${card.md_name_jp} (MD)\n`;
desc = card.text.db_desc ?? '';
break;
case 'ja':
if (card.jp_name)
card_name = card.jp_name;
else if (card.md_name_jp)
card_name = `${card.md_name_jp} (MD)`;
if (card.en_name)
other_name = `${card.en_name}\n`;
else if (card.md_name_en)
other_name = `${card.md_name_en} (MD)\n`;