Files
reproxy/web/minecraft/app.js
T
2026-10-17 14:13:24 +07:00

59 lines
2.9 KiB
JavaScript

const repository = 'https://git.3crabs.space/user-penguin/gmancraft';
function parseLatestBuild(readme) {
// shortcut: формат строки актуальной версии README, при его смене нужен явный manifest.
const line = readme.split('\n').find(line => line.startsWith('Актуальная версия'));
const match = line?.match(/^Актуальная версия\s*—\s*\*\*(\d+\.\d+\.\d+)\*\*:\s*\[Скачать \1\]\((dist\/minecraft-lab-neoforge-spike-\1\/minecraft-lab-neoforge-spike-\1\.mrpack)\)/);
if (!match) throw new Error('Не найдена актуальная сборка');
return { version: match[1], url: `${repository}/raw/branch/main/${match[2]}` };
}
async function loadLatestBuild() {
const status = document.getElementById('build-status');
const download = document.getElementById('build-download');
try {
const response = await fetch('/minecraft/api/readme', {
cache: 'no-store',
signal: AbortSignal.timeout(10000),
});
if (!response.ok) throw new Error('README недоступен');
const data = await response.json();
if (data.encoding !== 'base64' || typeof data.content !== 'string') {
throw new Error('Неизвестный формат README');
}
const readme = new TextDecoder().decode(Uint8Array.from(atob(data.content), char => char.charCodeAt(0)));
const build = parseLatestBuild(readme);
status.textContent = `Актуальная версия — ${build.version}`;
download.href = build.url;
download.textContent = `Скачать сборку ${build.version}`;
download.hidden = false;
} catch {
status.textContent = 'Не удалось определить актуальную версию. Открой репозиторий по ссылке внизу страницы.';
}
}
async function loadServerStatus() {
const status = document.getElementById('server-status');
try {
const response = await fetch('/minecraft/api/status', {
cache: 'no-store',
signal: AbortSignal.timeout(10000),
});
if (!response.ok) throw new Error('Статус недоступен');
const data = await response.json();
if (data?.online === false) {
status.textContent = 'Сервер недоступен';
} else if (data?.online === true && Number.isSafeInteger(data.players?.online) && data.players.online >= 0) {
status.textContent = `Игроков онлайн: ${data.players.online}`;
} else {
throw new Error('Неизвестный формат статуса');
}
} catch {
status.textContent = 'Не удалось получить онлайн';
}
}
loadLatestBuild();
loadServerStatus();
setInterval(loadServerStatus, 5 * 60 * 1000);