65 lines
3.1 KiB
JavaScript
65 lines
3.1 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 statuses = document.querySelectorAll('.build-status');
|
|
const downloads = document.querySelectorAll('.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);
|
|
for (const status of statuses) {
|
|
status.textContent = `Актуальная версия — ${build.version}`;
|
|
}
|
|
for (const download of downloads) {
|
|
download.href = build.url;
|
|
download.textContent = `Скачать сборку ${build.version}`;
|
|
download.hidden = false;
|
|
}
|
|
} catch {
|
|
for (const status of statuses) {
|
|
status.textContent = 'Не удалось определить актуальную версию. Кнопка откроет репозиторий — выбери актуальную сборку в README.';
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|