-
Notifications
You must be signed in to change notification settings - Fork 0
/
esearch.js
280 lines (280 loc) · 8.46 KB
/
esearch.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
var used_variables;
var cats;
const filter_expr = document.getElementById('filter-expr');
const sort_expr = document.getElementById('sort-expr');
const search_result = document.getElementById('search-result');
const tbody = document.getElementById('tbody');
const pages_a = document.getElementById('pages-a');
var hide_seach = false;
const tables = document.getElementById('tables');
const toggle_s = document.getElementById('toggle-s');
const trait_s = document.getElementById('trait-s');
const atk_s = document.getElementById('atk-s');
const ab_s = document.getElementById('ab-s');
const kind_s = document.getElementById('kind-s');
const atkBtn = atk_s.firstElementChild.firstElementChild;
const traitBtn = trait_s.firstElementChild.firstElementChild;
const abBtn = ab_s.firstElementChild.nextElementSibling.firstElementChild;
const name_search = document.getElementById('name-search');
var last_forms;
function rerender(event) {
event.preventDefault();
const id = event.currentTarget.id;
pages_a.textContent = '';
if (id >= 9) {
let i = 0;
let maxPage = Math.min(Math.ceil(last_forms.length / 10), id + 7);
for (let c = id - 2;c <= maxPage;++c) {
const td = document.createElement('td');
td.innerText = c.toString();
td.onclick = rerender;
td.id = c;
if (c == id) {
td.classList.add('N');
td.onclick = null;
}
pages_a.appendChild(td);
if (++i >= 10) break;
}
}
renderTable(last_forms, id);
}
function renderTable(forms, page = 1) {
let H = page * 10;
let display_forms = forms.slice(H - 10, H);
tbody.textContent = '';
search_result.innerText = `顯示第${H - 9}到第${Math.min(forms.length, H)}個結果,共有${forms.length}個結果`;
last_forms = forms;
if (forms.length == 0) {
tbody.innerHTML =
'<tr><td colSpan="13">沒有符合調件的敵人!</td></tr>';
return;
}
if (!pages_a.children.length) {
let c = 1;
for (let i = 0;i < forms.length;i += 10) {
const td = document.createElement('td');
td.innerText = c.toString();
td.onclick = rerender;
td.id = c;
if (page == c) {
td.classList.add('N');
td.onclick = null;
}
pages_a.appendChild(td);
if (c++ >= 10) break;
}
}
for (let i = 0;i < display_forms.length;++i) {
const tr = document.createElement('tr');
const theForm = display_forms[i][1];
const texts = [theForm.id - 2, '', theForm.name ? theForm.name : (theForm.jp_name ? theForm.jp_name : '?'), ~~theForm.gethp(), ~~theForm.getatk(),
~~theForm.getdps(), theForm.kb, theForm.range, numStrT(theForm.attackF).replace('秒', '秒/下'), theForm.speed, theForm.earn, numStr(display_forms[i][0])];
for (let j = 0;j < texts.length;++j) {
const e = document.createElement('td');
e.innerText = texts[j].toString();
tr.appendChild(e);
}
const a = document.createElement('a');
const img = new Image();
const ss = t3str(theForm.id - 2);
img.src = '/data/enemy/' + ss + '/enemy_icon_' + ss + '.png';
img.onerror = function(event) {
event.currentTarget.src = '/data/enemy/' + ss + '/edi_' + ss + '.png';
}
a.href = './enemy.html?id=' + (theForm.id - 2).toString();
a.appendChild(img);
tr.children[1].appendChild(a);
tbody.appendChild(tr);
}
}
function simplify(code) {
return code.replaceAll('\n', '').replaceAll(' ', '').replaceAll('\r', '').replaceAll('\t', '');
}
function calculate(code = '') {
pages_a.textContent = '';
const sortCode = simplify(sort_expr.value);
const url = new URL(location.pathname, location.href);
if (!code.length) {
const codes = [];
const traits = Array.from(trait_s.querySelectorAll('.o-selected'));
if (traits.length) {
let M = traits.map(x => x.getAttribute('data-expr'));
url.searchParams.set('traits', M.join(' '));
if (traitBtn.value == 'OR') {
codes.push(M.join('||'));
} else {
codes.push(M.join('&&'));
}
}
const kinds = Array.from(kind_s.querySelectorAll('.o-selected'));
if (kinds.length) {
let M = kinds.map(x => x.getAttribute('data-expr'));
url.searchParams.set('kinds', M.join(' '));
codes.push(M.join('||'));
}
const atks = Array.from(atk_s.querySelectorAll('.o-selected'));
if (atks.length) {
let M = atks.map(x => x.getAttribute('data-expr'));
url.searchParams.set('atks', M.join(' '));
if (atkBtn.value == 'OR') {
codes.push(M.join('||'));
} else {
codes.push(M.join('&&'));
}
}
const abs = Array.from(ab_s.querySelectorAll('.o-selected'));
if (abs.length) {
let M = abs.map(x => x.getAttribute('data-expr'));
url.searchParams.set('abs', M.join(' '));
if (abBtn.value == 'OR') {
codes.push(M.join('||'));
} else {
codes.push(M.join('&&'));
}
}
if (codes.length) {
code = (filter_expr.value = codes.map(x => `(${x})`).join('&&'));
} else
code = '1';
} else {
if (!code.length)
return renderTable([]);
url.searchParams.set('filter', code);
}
var pcode;
try {
pcode = pegjs.parse(code);
} catch (e) {
alert(e.toString());
}
let f = eval(`form => (${pcode})`);
results = cats.filter(f);
try {
pcode = pegjs.parse(sortCode || '1');
} catch (e) {
alert(e.toString());
}
let fn = eval(`form => (${pcode})`);
results = results.map((form, i) => {
const x = fn(form);
return [isFinite(x) ? x : 0, form];
}).sort((a, b) => b[0] - a[0]);
renderTable(results);
if (sortCode.length)
url.searchParams.set('sort', sortCode);
const a = atkBtn.value == 'OR' ? '1' : '0';
const b = traitBtn.value == 'OR' ? '1' : '0';
const c = abBtn.value == 'OR' ? '1' : '0';
url.searchParams.set('ao', a+b+c);
history.pushState({}, "", url);
}
function addBtns(parent, s) {
if (!s) return;
const n = s.split(' ');
for (let c of parent.querySelectorAll('button')) {
if (s.includes(c.parentNode.getAttribute('data-expr'))) {
c.parentNode.classList.add('o-selected');
}
}
}
loadAllEnemies()
.then(_cats => {
cats = _cats;
document.getElementById('loader').style.display = 'none';
loader_text.style.display = 'none';
document.getElementById('main').style.display = 'block';
const params = new URLSearchParams(location.search);
const filter = params.get('filter');
const sort = params.get('sort');
if (filter)
filter_expr.value = filter;
if (sort)
sort_expr.value = sort;
const ao = params.get('ao');
if (ao) {
atkBtn.value = ao[0] == '1' ? 'OR' : 'AND';
traitBtn.value = ao[1] == '1' ? 'OR' : 'AND';
abBtn.value = ao[2] == '1' ? 'OR' : 'AND';
}
addBtns(atk_s, params.get('atks'));
addBtns(ab_s, params.get('abs'));
addBtns(trait_s, params.get('traits'));
addBtns(kind_s, params.get('kinds'));
calculate(filter ? filter : '');
});
document.querySelectorAll('button').forEach(elem => {
elem.state = '0';
elem.addEventListener("click", function (event) {
const t = event.currentTarget;
if (t.state == '0') {
t.parentNode.classList.add('o-selected');
t.state = '1';
} else {
t.parentNode.classList.remove('o-selected');
t.state = '0';
}
calculate();
});
});
document.querySelectorAll('.or-and').forEach(e => {
e.onclick = function(event) {
const t = event.currentTarget;
if (t.value == 'OR')
t.value = 'AND';
else
t.value = 'OR';
calculate();
};
});
document.getElementById('filter-go').onclick = function() {
calculate(simplify(filter_expr.value));
}
document.getElementById('filter-clear').onclick = function() {
function fn(x) { x.classList.remove('o-selected'); };
trait_s.querySelectorAll('.o-selected').forEach(fn);
atk_s.querySelectorAll('.o-selected').forEach(fn);
ab_s.querySelectorAll('.o-selected').forEach(fn);
kind_s.querySelectorAll('.o-selected').forEach(fn);
filter_expr.value = '';
sort_expr.value = '';
calculate();
}
toggle_s.onclick = function() {
if (hide_seach) {
tables.style.left = '390px';
tables.style.width = 'calc(100% - 400px)';
document.documentElement.style.setProperty('--mhide', 'block');
toggle_s.innerText = '隱藏搜尋器';
} else {
document.documentElement.style.setProperty('--mhide', 'none');
tables.style.left = '0px';
tables.style.width = '100%';
toggle_s.innerText = '顯示搜尋器';
}
hide_seach = !hide_seach;
}
name_search.oninput = function() {
let search = name_search.value.trim();
if (!search)
return;
let digit = search.length >= 1;
for (let c of search) {
const x = c.codePointAt(0);
if (x < 48 || x > 57)
digit = false;
}
var s = cats.slice(2);
const results = [];
if (digit) {
let x = parseInt(search);
if (x < cats.length) {
results.push([1, s[x]]);
s.splice(x, 1);
}
}
for (let C of s)
if ((C.name && C.name.includes(search) || (C.jp_name && C.jp_name.includes(search))))
results.push([1, C]);
renderTable(results);
}