summaryrefslogtreecommitdiff
path: root/static/js/minecraft.js
blob: 757053c18110deb8a7b535ceedc3371b46780f66 (plain) (blame)
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
(function () {
  const el = document.getElementById("mc-status");
  if (!el) return;

  function escape(s) {
    const d = document.createElement("div");
    d.textContent = s;
    return d.innerHTML;
  }

  async function poll() {
    try {
      const r = await fetch("/minecraft", { headers: { Accept: "application/json" } });
      if (!r.ok) return;
      const s = await r.json();
      if (!s.online) {
        el.textContent = "server offline";
      } else if (s.count === 0) {
        el.textContent = `online — 0/${s.max} players`;
      } else {
        el.innerHTML = `online — ${s.count}/${s.max}: ` + s.players.map(escape).join(", ");
      }
    } catch (e) {
      /* keep the last render */
    }
  }

  poll();
  setInterval(poll, 10000);
})();