Skip to content

Commit

Permalink
change export: get_seventh_xyz
Browse files Browse the repository at this point in the history
  • Loading branch information
salix5 committed Jul 7, 2024
1 parent d49e7e9 commit d0d7f87
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 55 deletions.
22 changes: 8 additions & 14 deletions common_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function fetch_desc(card, request_locale) {

/**
* Create the reply of `card` in region `locale`.
* @param {Object} card
* @param {ygo.Card} card
* @param {string} locale
* @param {boolean} seventh
* @returns
Expand Down Expand Up @@ -85,23 +85,17 @@ export function create_reply(card, locale, seventh = false) {
components.push(row1);
}
if (seventh) {
const [number_attr, number_race] = ygo.get_seventh_number(card);
if (number_attr || number_race) {
const result = ygo.get_seventh_xyz(card);
if (result.length) {
const row_seventh = new ActionRowBuilder();
if (number_attr) {
for (const seventh of result) {
const db_text = ygo.href_table.has(seventh.cid) ? ygo.href_table.get(seventh.cid) : 'No.10X';
const button1 = new ButtonBuilder()
.setCustomId('seventh_attr')
.setLabel(`No.${number_attr}`)
.setStyle(ButtonStyle.Secondary);
.setStyle(ButtonStyle.Link)
.setLabel(db_text)
.setURL(ygo.print_db_link(seventh.cid, ygo.get_request_locale(seventh, locale)));
row_seventh.addComponents(button1);
}
if (number_race) {
const button2 = new ButtonBuilder()
.setCustomId('seventh_race')
.setLabel(`No.${number_race}`)
.setStyle(ButtonStyle.Secondary);
row_seventh.addComponents(button2);
}
components.push(row_seventh);
}
}
Expand Down
94 changes: 53 additions & 41 deletions ygo-query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -332,29 +332,41 @@ const extra_setcode = {
8512558: [0x8f, 0x54, 0x59, 0x82, 0x13a],
};

const seventh_xyz = [];
const seventh_attribute = new Map();
const seventh_race = new Map();
const stmt_seventh = `${stmt_default} AND type & $xyz AND name like $no`;
const zh_collator = new Intl.Collator(collator_locale['zh-tw']);
const over_hundred = '(name like $101 OR name like $102 OR name like $103 OR name like $104 OR name like $105 OR name like $106 OR name like $107)';
const stmt_seventh = `${stmt_default} AND type & $xyz AND ${over_hundred}`;
const arg_seventh = {};
arg_seventh.$xyz = TYPE_XYZ;
Object.assign(arg_seventh, arg_default);
arg_seventh.$xyz = TYPE_XYZ;
for (let i = 0; i < 7; ++i) {
arg_seventh.$no = `%No.${101 + i}%`;
seventh_xyz[i] = query(stmt_seventh, arg_seventh);
for (const card of seventh_xyz[i]) {
if (!seventh_attribute.has(card.level))
seventh_attribute.set(card.level, new Map());
if (!seventh_attribute.get(card.level).has(card.attribute))
seventh_attribute.get(card.level).set(card.attribute, 101 + i);
if (!seventh_race.has(card.level))
seventh_race.set(card.level, new Map());
if (!seventh_race.get(card.level).has(card.race))
seventh_race.get(card.level).set(card.race, 101 + i);
}
arg_seventh[`$${101 + i}`] = `%No.${101 + i}%`;
}
const seventh_xyz = query(stmt_seventh, arg_seventh);
seventh_xyz.sort((c1, c2) => zh_collator.compare(c1.tw_name, c2.tw_name));

const mmap_seventh = [];
const href_table = new Map();
const re_number = /\w?No.10[1-7]/;
for (const card of seventh_xyz) {
multimap_insert(mmap_seventh, card.level, card);
const match = card.tw_name.match(re_number);
if (card.cid && match)
href_table.set(card.cid, match[0]);
}
const seventh_condition = create_seventh_condition();
export { seventh_xyz, seventh_condition };
export { seventh_xyz, seventh_condition, href_table };


/**
* @param {Array[]} mmap
* @param {number} key
* @param {Card} value
*/
function multimap_insert(mmap, key, value) {
if (!mmap[key])
mmap[key] = [];
mmap[key].push(value);
}

/**
* Set `card.setcode` from int64.
Expand Down Expand Up @@ -497,21 +509,21 @@ function edit_card(card) {
}

/**
* The sqlite condition of Monsters related to No.101 ~ No.107.
* The sqlite condition of monsters related to No.101 ~ No.107.
* @returns
*/
function create_seventh_condition() {
let condition1 = 'FALSE';
for (const [level, map1] of seventh_attribute) {
let condition1 = '0';
for (let i = 1; i <= 13; ++i) {
if (!mmap_seventh[i])
continue;
let attr_value = 0;
for (const attribute of map1.keys()) {
attr_value |= attribute;
}
let race_value = 0;
for (const race of seventh_race.get(level).keys()) {
race_value |= race;
for (const card of mmap_seventh[i]) {
attr_value |= card.attribute;
race_value |= card.race;
}
condition1 += ` OR level == ${level} AND (attribute & ${attr_value} OR race & ${race_value})`;
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;
Expand Down Expand Up @@ -696,21 +708,22 @@ export function get_request_locale(card, locale) {
}

/**
* Get the 101 ~ 107 number by attribute and race.
* Get No.101 ~ No.107 Xyz Monsters with the same race or attribute.
* @param {Card} card
* @returns
* @returns {Card[]}
*/
export function get_seventh_number(card) {
if (!(card.type & TYPE_MONSTER) || card.type & TYPE_EXTRA)
return [0, 0];
if (!seventh_attribute.has(card.level))
return [0, 0];
let number_attr = 0, number_race = 0;
if (seventh_attribute.get(card.level).has(card.attribute))
number_attr = seventh_attribute.get(card.level).get(card.attribute);
if (seventh_race.get(card.level).has(card.race))
number_race = seventh_race.get(card.level).get(card.race);
return [number_attr, number_race];
export function get_seventh_xyz(card) {
const result = [];
if (!mmap_seventh[card.level])
return result;
for (const seventh of mmap_seventh[card.level]) {
if (!seventh.cid)
continue;
if ((seventh.race & card.race) || (seventh.attribute & card.attribute)) {
result.push(seventh);
}
}
return result;
}

/**
Expand Down Expand Up @@ -1037,7 +1050,6 @@ export function create_choice(request_locale) {
return result;
}

const zh_collator = new Intl.Collator(collator_locale['zh-tw']);
/**
* @param {[string, number]} a
* @param {[string, number]} b
Expand Down
5 changes: 5 additions & 0 deletions ygo-utility.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @param {number} cid
* @param {string} request_locale
* @returns
*/
export function print_db_link(cid, request_locale) {
return `https://www.db.yugioh-card.com/yugiohdb/card_search.action?ope=2&cid=${cid}&request_locale=${request_locale}`;
}
Expand Down

0 comments on commit d0d7f87

Please sign in to comment.