476 lines
15 KiB
JavaScript
476 lines
15 KiB
JavaScript
/* GcTools 网页复刻版 —— 与 Android 版共用同一套数据表、命令语法和 OpenCommand 协议 */
|
||
|
||
const TABS = [
|
||
{
|
||
id: 'item', name: '物品', asset: 'Item.txt',
|
||
fields: [
|
||
{ k: 'count', label: '数量', def: 1 },
|
||
{ k: 'level', label: '等级', def: 1 },
|
||
],
|
||
actions: [
|
||
{ label: '给予物品', primary: true, cmd: (e, f) => `/give ${e.id} x${f.count} lv${f.level}` },
|
||
{ label: '生成物品', cmd: (e, f) => `/spawn ${e.id} x${f.count} lv${f.level}` },
|
||
{ label: '掉落物品', cmd: (e, f) => `/drop ${e.id} ${f.count}` },
|
||
],
|
||
},
|
||
{
|
||
id: 'avatar', name: '角色', asset: 'Avatar.txt',
|
||
fields: [
|
||
{ k: 'level', label: '等级', def: 90 },
|
||
{ k: 'cons', label: '命座', def: 90 },
|
||
{ k: 'talent', label: '天赋', def: 90 },
|
||
],
|
||
actions: [
|
||
{ label: '给予角色', primary: true, cmd: (e, f) => `/give ${e.id} lv${f.level} c${f.cons} sl${f.talent}` },
|
||
{ label: '直接生成', cmd: (e, f) => `/givechar ${e.id} ${f.level}` },
|
||
],
|
||
},
|
||
{
|
||
id: 'weapon', name: '武器', asset: 'Weapon.txt',
|
||
fields: [
|
||
{ k: 'count', label: '数量', def: 1 },
|
||
{ k: 'level', label: '等级', def: 90 },
|
||
{ k: 'refine', label: '精炼', def: 1 },
|
||
],
|
||
actions: [
|
||
{ label: '给予武器', primary: true, cmd: (e, f) => `/give ${e.id} x${f.count} lv${f.level} r${f.refine}` },
|
||
],
|
||
},
|
||
{
|
||
id: 'artifact', name: '圣遗物', asset: 'Artifact.txt',
|
||
fields: [{ k: 'level', label: '等级', def: 20 }],
|
||
actions: [
|
||
{ label: '给予圣遗物', primary: true, cmd: (e, f) => `/give ${e.id} lv${f.level}` },
|
||
],
|
||
},
|
||
{
|
||
id: 'monster', name: '怪物', asset: 'Monsters.txt',
|
||
fields: [
|
||
{ k: 'count', label: '数量', def: 1 },
|
||
{ k: 'level', label: '等级', def: 1 },
|
||
],
|
||
// 原版怪物命令是 /spawn <id> <数量> <等级>,等级前不带 lv,保持兼容
|
||
actions: [
|
||
{ label: '生成怪物', primary: true, cmd: (e, f) => `/spawn ${e.id} ${f.count} ${f.level}` },
|
||
],
|
||
},
|
||
{ id: 'command', name: '命令' },
|
||
{ id: 'remote', name: '远程' },
|
||
{ id: 'settings', name: '设置' },
|
||
];
|
||
|
||
const store = {
|
||
get host() { return localStorage.getItem('gctools.host') || 'http://127.0.0.1:443'; },
|
||
set host(v) { localStorage.setItem('gctools.host', v.trim()); },
|
||
get token() { return localStorage.getItem('gctools.token') || ''; },
|
||
set token(v) { localStorage.setItem('gctools.token', v.trim()); },
|
||
get auto() { return localStorage.getItem('gctools.auto') === '1'; },
|
||
set auto(v) { localStorage.setItem('gctools.auto', v ? '1' : '0'); },
|
||
};
|
||
|
||
const cache = new Map();
|
||
const state = { tab: 'item', lists: {}, selected: {}, fields: {}, cmd: '' };
|
||
|
||
const $ = (s) => document.querySelector(s);
|
||
|
||
function toast(msg, bad = false) {
|
||
const el = $('#toast');
|
||
el.textContent = msg;
|
||
el.classList.toggle('bad', bad);
|
||
el.hidden = false;
|
||
clearTimeout(toast._t);
|
||
toast._t = setTimeout(() => { el.hidden = true; }, 1800);
|
||
}
|
||
|
||
async function loadAsset(asset) {
|
||
if (cache.has(asset)) return cache.get(asset);
|
||
const res = await fetch('assets/' + asset, { cache: 'force-cache' });
|
||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||
const text = await res.text();
|
||
const out = [];
|
||
for (const line of text.split('\n')) {
|
||
const s = line.replace(/^\uFEFF/, '').trim();
|
||
if (!s || s.startsWith('//')) continue;
|
||
const i = s.indexOf(':');
|
||
if (i <= 0) continue;
|
||
const id = Number(s.slice(0, i).trim());
|
||
if (!Number.isFinite(id)) continue;
|
||
const name = s.slice(i + 1).trim();
|
||
if (name) out.push({ id, name });
|
||
}
|
||
cache.set(asset, out);
|
||
return out;
|
||
}
|
||
|
||
function search(list, q, limit = 200) {
|
||
const needle = (q || '').trim().toLowerCase();
|
||
if (!needle) return list.slice(0, limit);
|
||
const out = [];
|
||
for (const e of list) {
|
||
if (String(e.id).startsWith(needle) || e.name.toLowerCase().includes(needle)) {
|
||
out.push(e);
|
||
if (out.length >= limit) break;
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
// ---------- 渲染 ----------
|
||
|
||
function renderTabs() {
|
||
const nav = $('#tabs');
|
||
nav.innerHTML = '';
|
||
for (const t of TABS) {
|
||
const b = document.createElement('button');
|
||
b.className = 'tab' + (t.id === state.tab ? ' active' : '');
|
||
b.textContent = t.name;
|
||
b.onclick = () => {
|
||
state.tab = t.id;
|
||
history.replaceState(null, '', '#' + t.id);
|
||
render();
|
||
};
|
||
nav.appendChild(b);
|
||
}
|
||
}
|
||
|
||
async function render() {
|
||
renderTabs();
|
||
const view = $('#view');
|
||
view.innerHTML = '';
|
||
const tab = TABS.find((t) => t.id === state.tab);
|
||
if (!tab) return;
|
||
if (tab.id === 'command') return renderCommand(view);
|
||
if (tab.id === 'remote') return renderRemote(view);
|
||
if (tab.id === 'settings') return renderSettings(view);
|
||
return renderList(view, tab);
|
||
}
|
||
|
||
async function renderList(view, tab) {
|
||
const panel = document.createElement('div');
|
||
panel.className = 'panel';
|
||
|
||
const input = document.createElement('input');
|
||
input.className = 'search';
|
||
input.placeholder = '搜 id 或名字,例如 1001 / 甜甜花';
|
||
input.value = state.q || '';
|
||
panel.appendChild(input);
|
||
|
||
const list = document.createElement('div');
|
||
list.className = 'list';
|
||
panel.appendChild(list);
|
||
|
||
const fields = document.createElement('div');
|
||
fields.className = 'fields';
|
||
const fstate = state.fields[tab.id] || (state.fields[tab.id] = {});
|
||
for (const f of tab.fields) {
|
||
if (fstate[f.k] === undefined) fstate[f.k] = f.def;
|
||
const box = document.createElement('div');
|
||
box.className = 'field';
|
||
const lab = document.createElement('label');
|
||
lab.textContent = f.label;
|
||
const inp = document.createElement('input');
|
||
inp.inputMode = 'numeric';
|
||
inp.value = String(fstate[f.k]);
|
||
inp.oninput = () => {
|
||
const n = parseInt(inp.value, 10);
|
||
fstate[f.k] = Number.isFinite(n) ? n : f.def;
|
||
};
|
||
box.append(lab, inp);
|
||
fields.appendChild(box);
|
||
}
|
||
if (tab.fields.length) panel.appendChild(fields);
|
||
|
||
const actions = document.createElement('div');
|
||
actions.className = 'actions';
|
||
for (const a of tab.actions) {
|
||
const b = document.createElement('button');
|
||
b.className = 'btn' + (a.primary ? ' primary' : '');
|
||
b.textContent = a.label;
|
||
b.onclick = () => {
|
||
const sel = state.selected[tab.id];
|
||
if (!sel) return toast('先在上面的列表里选一项');
|
||
const f = state.fields[tab.id];
|
||
setCommand(a.cmd(sel, f));
|
||
};
|
||
actions.appendChild(b);
|
||
}
|
||
if (tab.actions.length) panel.appendChild(actions);
|
||
|
||
const hint = document.createElement('div');
|
||
hint.className = 'hint';
|
||
hint.textContent = '数据来自 APK 内置表,条目数:加载中…';
|
||
panel.appendChild(hint);
|
||
|
||
view.appendChild(panel);
|
||
|
||
const all = await loadAsset(tab.asset);
|
||
hint.textContent = `数据来自 APK 内置表「${tab.asset}」,共 ${all.length} 条;搜索只显示前 200 条。`;
|
||
|
||
const paint = () => {
|
||
list.innerHTML = '';
|
||
const rows = search(all, input.value, 200);
|
||
if (!rows.length) {
|
||
list.innerHTML = '<div class="more">没找到,换个关键字试试</div>';
|
||
return;
|
||
}
|
||
const frag = document.createDocumentFragment();
|
||
for (const e of rows) {
|
||
const row = document.createElement('div');
|
||
row.className = 'row' + (state.selected[tab.id]?.id === e.id ? ' sel' : '');
|
||
row.innerHTML = `<span class="id">${e.id}</span><span class="name"></span>`;
|
||
row.querySelector('.name').textContent = e.name;
|
||
row.onclick = () => {
|
||
state.selected[tab.id] = e;
|
||
paint();
|
||
};
|
||
frag.appendChild(row);
|
||
}
|
||
list.appendChild(frag);
|
||
};
|
||
input.oninput = paint;
|
||
paint();
|
||
if (!state.selected[tab.id] && tab.actions.length) {
|
||
// 默认选中第一条,省一次点击
|
||
const first = search(all, '', 1)[0];
|
||
if (first) state.selected[tab.id] = first;
|
||
paint();
|
||
}
|
||
}
|
||
|
||
function renderCommand(view) {
|
||
const panel = document.createElement('div');
|
||
panel.className = 'panel';
|
||
panel.innerHTML = '<div class="hint" style="margin:0 0 8px">手动输入一条命令,生成后可以复制或直接发送。</div>';
|
||
|
||
const input = document.createElement('textarea');
|
||
input.className = 'search';
|
||
input.rows = 3;
|
||
input.placeholder = '/give 1001 x10';
|
||
input.style.fontFamily = 'var(--mono)';
|
||
input.style.resize = 'vertical';
|
||
input.value = state.cmd || '';
|
||
panel.appendChild(input);
|
||
|
||
const log = document.createElement('div');
|
||
log.className = 'log';
|
||
log.textContent = '(还没有记录)';
|
||
|
||
const actions = document.createElement('div');
|
||
actions.className = 'actions';
|
||
const mk = (label, fn, primary = false) => {
|
||
const b = document.createElement('button');
|
||
b.className = 'btn' + (primary ? ' primary' : '');
|
||
b.textContent = label;
|
||
b.onclick = fn;
|
||
actions.appendChild(b);
|
||
};
|
||
mk('设为当前命令', () => {
|
||
const v = input.value.trim();
|
||
if (!v) return toast('先输入命令');
|
||
setCommand(v, false);
|
||
}, true);
|
||
mk('直接发送', async () => {
|
||
const v = input.value.trim();
|
||
if (!v) return toast('先输入命令');
|
||
setCommand(v, false);
|
||
const line = `→ ${v}`;
|
||
log.textContent = log.textContent === '(还没有记录)' ? line : log.textContent + '\n' + line;
|
||
try {
|
||
const data = await call('command', { command: v });
|
||
log.textContent += `\n✓ ${data || 'ok'}`;
|
||
toast('已发送');
|
||
} catch (err) {
|
||
log.textContent += `\n✗ ${err.message}`;
|
||
toast('发送失败', true);
|
||
}
|
||
log.scrollTop = log.scrollHeight;
|
||
});
|
||
mk('清空记录', () => { log.textContent = '(还没有记录)'; toast('已清空'); }, false);
|
||
|
||
panel.appendChild(actions);
|
||
panel.appendChild(log);
|
||
view.appendChild(panel);
|
||
}
|
||
|
||
function renderRemote(view) {
|
||
const panel = document.createElement('div');
|
||
panel.className = 'panel';
|
||
panel.innerHTML = '<h2 style="margin:0 0 4px;font-size:15px">服务器</h2>'
|
||
+ '<div class="hint" style="margin:0 0 10px">Grasscutter 服务端需要装 OpenCommand 插件,并在控制台拿到令牌。</div>';
|
||
|
||
const mkField = (label, value, placeholder) => {
|
||
const box = document.createElement('div');
|
||
box.className = 'field';
|
||
box.style.marginBottom = '8px';
|
||
const lab = document.createElement('label');
|
||
lab.textContent = label;
|
||
const inp = document.createElement('input');
|
||
inp.value = value;
|
||
inp.placeholder = placeholder;
|
||
box.append(lab, inp);
|
||
panel.appendChild(box);
|
||
return inp;
|
||
};
|
||
const host = mkField('地址', store.host, 'http://127.0.0.1:443');
|
||
const token = mkField('令牌', store.token, 'OpenCommand 控制台令牌');
|
||
|
||
const log = document.createElement('div');
|
||
log.className = 'log';
|
||
log.textContent = '(还没有测试)';
|
||
|
||
const actions = document.createElement('div');
|
||
actions.className = 'actions';
|
||
const mk = (label, fn, primary = false) => {
|
||
const b = document.createElement('button');
|
||
b.className = 'btn' + (primary ? ' primary' : '');
|
||
b.textContent = label;
|
||
b.onclick = fn;
|
||
actions.appendChild(b);
|
||
};
|
||
mk('测试连接', async () => {
|
||
store.host = host.value;
|
||
store.token = token.value;
|
||
log.textContent = '连接中…';
|
||
try {
|
||
const data = await call('ping', null);
|
||
log.textContent = `✓ 已连接${data ? ' · ' + data : ''}`;
|
||
toast('已连接');
|
||
paintStatus(true);
|
||
} catch (err) {
|
||
log.textContent = `✗ 连接失败:${err.message}`;
|
||
toast('连接失败', true);
|
||
paintStatus(false);
|
||
}
|
||
}, true);
|
||
mk('保存', () => {
|
||
store.host = host.value;
|
||
store.token = token.value;
|
||
toast('已保存');
|
||
paintStatus();
|
||
});
|
||
mk('清空', () => {
|
||
localStorage.removeItem('gctools.host');
|
||
localStorage.removeItem('gctools.token');
|
||
host.value = 'http://127.0.0.1:443';
|
||
token.value = '';
|
||
toast('已清空');
|
||
paintStatus();
|
||
});
|
||
|
||
panel.appendChild(actions);
|
||
panel.appendChild(log);
|
||
view.appendChild(panel);
|
||
}
|
||
|
||
function renderSettings(view) {
|
||
const panel = document.createElement('div');
|
||
panel.className = 'panel';
|
||
const sw = document.createElement('label');
|
||
sw.className = 'switch';
|
||
sw.innerHTML = '<span>生成命令后自动发送</span>';
|
||
const cb = document.createElement('input');
|
||
cb.type = 'checkbox';
|
||
cb.checked = store.auto;
|
||
cb.onchange = () => { store.auto = cb.checked; toast(cb.checked ? '已开启自动发送' : '已关闭自动发送'); };
|
||
sw.appendChild(cb);
|
||
panel.appendChild(sw);
|
||
|
||
const about = document.createElement('div');
|
||
about.className = 'hint';
|
||
about.style.marginTop = '12px';
|
||
about.innerHTML = 'GcTools 网页复刻版 1.21.0<br>'
|
||
+ '根据原 APK 逆向重写:数据表、命令语法、OpenCommand 协议一致;纯静态页面,可离线打开。<br><br>'
|
||
+ '数据与命令规则版权归原项目(Grasscutter / GcTools)所有,仅供本地使用。';
|
||
panel.appendChild(about);
|
||
view.appendChild(panel);
|
||
}
|
||
|
||
// ---------- 命令与网络 ----------
|
||
|
||
function setCommand(cmd, auto = true) {
|
||
state.cmd = cmd;
|
||
const el = $('#cmd');
|
||
el.textContent = cmd;
|
||
el.classList.remove('empty');
|
||
if (store.auto && auto) sendCommand(cmd);
|
||
else toast('已生成,点「发送到服务器」执行');
|
||
}
|
||
|
||
function paintStatus(ok) {
|
||
const el = $('#statusPill');
|
||
el.classList.remove('ok', 'bad');
|
||
if (ok === true) {
|
||
el.classList.add('ok');
|
||
el.textContent = '已连接';
|
||
return;
|
||
}
|
||
if (ok === false) {
|
||
el.classList.add('bad');
|
||
el.textContent = '连接失败';
|
||
return;
|
||
}
|
||
el.textContent = store.host || '未连接';
|
||
}
|
||
|
||
async function call(action, data) {
|
||
const host = store.host.replace(/\/+$/, '');
|
||
const url = host + '/opencommand/api';
|
||
let res;
|
||
try {
|
||
res = await fetch(url, {
|
||
method: 'POST',
|
||
headers: { 'Content-Type': 'application/json; charset=utf-8' },
|
||
body: JSON.stringify({ token: store.token, action, data: data ?? null }),
|
||
signal: AbortSignal.timeout(10000),
|
||
});
|
||
} catch (err) {
|
||
throw new Error('连不上服务器(网络或 CORS):' + (err.message || err.name));
|
||
}
|
||
const text = await res.text();
|
||
if (!res.ok) throw new Error(`HTTP ${res.status}${text ? '\n' + text : ''}`);
|
||
let json;
|
||
try { json = JSON.parse(text); } catch { throw new Error('响应不是 JSON:' + text.slice(0, 120)); }
|
||
if (json.retcode !== 200) throw new Error(json.message || `retcode ${json.retcode}`);
|
||
return json.data == null ? '' : String(json.data);
|
||
}
|
||
|
||
async function sendCommand(cmd) {
|
||
const el = $('#statusPill');
|
||
el.classList.remove('ok', 'bad');
|
||
el.textContent = '发送中…';
|
||
try {
|
||
await call('command', { command: cmd });
|
||
toast('已发送');
|
||
paintStatus(true);
|
||
} catch (err) {
|
||
toast('发送失败:' + err.message.split('\n')[0], true);
|
||
paintStatus(false);
|
||
}
|
||
}
|
||
|
||
// ---------- 启动 ----------
|
||
|
||
$('#copyBtn').onclick = async () => {
|
||
if (!state.cmd) return toast('先在上面的列表里选一项');
|
||
try {
|
||
await navigator.clipboard.writeText(state.cmd);
|
||
toast('已复制');
|
||
} catch {
|
||
toast('复制失败,请长按命令手动复制', true);
|
||
}
|
||
};
|
||
$('#sendBtn').onclick = () => {
|
||
if (!state.cmd) return toast('先输入命令');
|
||
sendCommand(state.cmd);
|
||
};
|
||
$('#statusPill').onclick = () => {
|
||
state.tab = 'remote';
|
||
render();
|
||
};
|
||
|
||
const initial = location.hash.replace('#', '');
|
||
if (TABS.some((t) => t.id === initial)) state.tab = initial;
|
||
|
||
paintStatus();
|
||
render();
|