site.lyte.dev/content/ourcraft-status.md
2022-02-15 13:29:19 -06:00

96 lines
4.2 KiB
Markdown

---
title: Ourcraft Server Status Checker
---
<p id="server-status-loading">
Checking <code>ourcraft.lyte.dev</code> server status...
</p>
<div id="server-status-check-failed">
<p>
Failed to retrieve <code>ourcraft.lyte.dev</code> server status. Unfortunately, that usually means we're <code><span style="color: var(--syntax-mag);">OFFLINE</span></code>.
</p>
<p>
Please yell at <code>@lytedev</code> in the Ourcraft Discord to fix it!
</p>
<p><button id="server-status-check-button">Try Again</button></p>
</div>
<div id="server-status-online" style="display: none;">
<p>Server <code>ourcraft.lyte.dev</code> is <code><span style="color: var(--syntax-name);">ONLINE</span></code></p>
<p id="server-status-player-info" style="display: none;">There are currently <span id="server-status-current-num-players">0</span>/<span id="server-status-max-players">20</span> players online.</p>
<h3 id="server-status-player-sampling-header" style="display: none;">Online Players</h3>
<ul id="server-status-player-sampling" style="display: none;">
<li>Player</li>
</ul>
<p id="server-status-player-sampling-insufficient" style="display: none">... and more!</p>
</div>
<p id="server-status-offline" style="display: none;">
Server is <code><span style="color: var(--syntax-mag);">OFFLINE</span></code>
</p>
<script src="https://mcapi.us/scripts/minecraft.min.js"></script>
<script>
function showServerStatus(key) {
for (const name of ['server-status-check-failed', 'server-status-online', 'server-status-offline', 'server-status-loading']) {
if (name == key) {
document.getElementById(name).style.display = 'block'
} else {
document.getElementById(name).style.display = 'none'
}
}
}
function checkServerStatus() {
showServerStatus('server-status-loading')
MinecraftAPI.getServerStatus('ourcraft.lyte.dev', {port: 25565}, function (err, status) {
console.debug("Status Errors:", err || false)
if (err || !('online' in status)) {
showServerStatus('server-status-check-failed')
} else {
handleOnlineStatus(status)
}
});
}
function handleOnlineStatus(status) {
console.debug("Status Data:", status)
if (status.online === true) {
showServerStatus('server-status-online')
const info = document.getElementById('server-status-player-info')
const samplingHeader = document.getElementById('server-status-player-sampling-header')
const samplingList = document.getElementById('server-status-player-sampling')
const insufficient = document.getElementById('server-status-player-sampling-insufficient')
info.style.display = 'none'
samplingHeader.style.display = 'none'
samplingList.style.display = 'none'
insufficient.style.display = 'none'
if ('players' in status) {
if ('max' in status.players && 'now' in status.players) {
info.style.display = 'block'
document.getElementById('server-status-current-num-players').textContent = status.players.now
document.getElementById('server-status-max-players').textContent = status.players.max
if ('sample' in status.players && status.players.now > 0) {
samplingHeader.style.display = 'block'
samplingList.style.display = 'block'
samplingList.textContent = ''
for (const player of status.players.sample) {
const newListItem = document.createElement('li')
newListItem.textContent = player.name
newListItem.title = player.id
samplingList.appendChild(newListItem)
}
if (status.players.sample.length != status.players.now) {
document.getElementById('server-status-player-sampling-insufficient').style.display = 'block'
}
}
}
}
} else {
showServerStatus('server-status-offline')
}
}
document.getElementById("server-status-check-button").addEventListener("click", checkServerStatus)
checkServerStatus()
</script>