-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_all.js
171 lines (158 loc) · 4.61 KB
/
common_all.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
/* eslint-disable no-unused-vars */
import { AutocompleteInteraction } from "discord.js";
import { complete_name_table, create_choice, create_choice_prerelease, escape_regexp, official_name } from "./ygo-query.mjs";
import { tc_entries, ruby_entries } from './common-json-loader.js';
const MAX_CHOICE = 25;
const choices_tc = new Map(tc_entries);
const choice_table = Object.create(null);
for (const locale of Object.keys(official_name)) {
choice_table[locale] = create_choice(locale);
}
choice_table['zh-tw'] = choices_tc;
refresh_choice_table();
const jp_entries = half_width_entries(choice_table['ja']);
export { choice_table };
/**
* @param {string} str
* @returns
*/
function toHalfWidth(str) {
return str.replace(/[A-Za-z0-9]/g, (s) => String.fromCharCode(s.charCodeAt(0) - 0xFEE0));
}
/**
* @param {string} str
* @returns
*/
function toFullWidth(str) {
return str.replace(/[A-Za-z0-9]/g, (s) => String.fromCharCode(s.charCodeAt(0) + 0xFEE0));
}
/**
* Check if 2 strings are case-insensitive equal.
* @param {string} a
* @param {string} b
* @returns boolean result
*/
function is_equal(a, b) {
return toHalfWidth(a.toLowerCase()) === toHalfWidth(b.toLowerCase());
}
function half_width_entries(choices) {
const result = [];
for (const [name, cid] of choices) {
result.push([toHalfWidth(name), cid]);
}
return result;
}
export function refresh_choice_table() {
const choices_tc_full = new Map(choices_tc);
const choices_pre = create_choice_prerelease();
for (const [name, id] of choices_pre) {
if (choices_tc.has(name)) {
console.error('duplicate tc name', `${name}: ${id}`);
continue;
}
choices_tc_full.set(name, id);
}
delete choice_table['full'];
choice_table['full'] = choices_tc_full;
}
/**
* @param {AutocompleteInteraction} interaction
* @param {[string, number][]} entries
* @returns id list
*/
function filter_choice(interaction, entries) {
const focused = interaction.options.getFocused().trim();
const starts_with = [];
const other = [];
const keyword = escape_regexp(toHalfWidth(focused));
const start = new RegExp(`^${keyword}`);
const include = new RegExp(`${keyword}`);
for (const [choice, cid] of entries) {
if (start.test(choice))
starts_with.push(cid);
else if (include.test(choice))
other.push(cid);
if (starts_with.length >= MAX_CHOICE)
return starts_with;
}
const ret = starts_with.concat(other);
if (ret.length > MAX_CHOICE)
ret.length = MAX_CHOICE;
return ret;
}
/**
* The autocomplete handler for Japanese card names, which also searches ruby.
* @param {AutocompleteInteraction} interaction
*/
export async function autocomplete_jp(interaction) {
const focused = interaction.options.getFocused().trim();
if (!focused) {
await interaction.respond([]);
return;
}
const ret = filter_choice(interaction, jp_entries);
if (ret.length < MAX_CHOICE) {
const ruby_max_length = MAX_CHOICE - ret.length;
const starts_with = [];
const other = [];
const cid_set = new Set(ret);
const keyword = escape_regexp(focused);
const start = new RegExp(`^${keyword}`);
const include = new RegExp(`${keyword}`);
for (const [ruby, cid] of ruby_entries) {
if (cid_set.has(cid))
continue;
if (start.test(ruby))
starts_with.push(cid);
else if (include.test(ruby))
other.push(cid);
if (starts_with.length >= ruby_max_length)
break;
}
ret.push(...starts_with);
if (ret.length < MAX_CHOICE)
ret.push(...other);
if (ret.length > MAX_CHOICE)
ret.length = MAX_CHOICE;
}
const name_jp = complete_name_table['ja'];
await interaction.respond(
ret.map(cid => ({ name: name_jp.get(cid), value: name_jp.get(cid) }))
);
}
/**
* The default autocomplete handler.
* @param {AutocompleteInteraction} interaction
* @param {string} request_locale
* @returns
*/
export async function autocomplete_default(interaction, request_locale) {
const focused = interaction.options.getFocused().trim();
if (!focused || !choice_table[request_locale]) {
await interaction.respond([]);
return;
}
const starts_with = [];
const other = [];
const keyword = escape_regexp(focused);
const start = new RegExp(`^${keyword}`, 'i');
const include = new RegExp(`${keyword}`, 'i');
for (const [choice, cid] of choice_table[request_locale]) {
if (start.test(choice))
starts_with.push(choice);
else if (include.test(choice))
other.push(choice);
if (starts_with.length >= MAX_CHOICE)
break;
}
let ret;
if (starts_with.length >= MAX_CHOICE)
ret = starts_with;
else
ret = starts_with.concat(other);
if (ret.length > MAX_CHOICE)
ret.length = MAX_CHOICE;
await interaction.respond(
ret.map(choice => ({ name: choice, value: choice }))
);
}