summaryrefslogtreecommitdiff
path: root/src/components/ServerStatus.vue
blob: d9d23b24f7a0f3c27acf77648a23352799ea672a (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<template>
	<aside>
		<a v-if="Online && Players" title="View List" aria-label="view list of online players" target="_blank" rel="noopener" href="https://server.themanaworld.org">Online: {{Players}} players</a>
		<a v-if="Online && !Players" title="View List" aria-label="view list of online players" target="_blank" rel="noopener" href="https://server.themanaworld.org">Online</a>
		<a v-if="!Online" class="offline" title="???" aria-label="open a YouTube video" target="_blank" rel="noopener" href="https://www.youtube-nocookie.com/embed/ILVfzx5Pe-A?autoplay=1&amp;modestbranding=1">Offline</a>
	</aside>
</template>

<style scoped>
aside :any-link {
	text-decoration: none;
	text-shadow: 0 0 1ch #e1d6cf;
	color: green;
	display: block;
	padding: 8px;

	&.offline {
		color: #d42424;
	}
}
</style>

<script lang="ts">
import { Options, Vue } from "vue-class-component";

interface StatusResponse {
	serverStatus: string;
	playersOnline?: number;
}

@Options({})
export default class ServerStatus extends Vue {
	Players = 0;
	Online = true;

	private async getStatus () {
		const req = new Request(`${process.env.VUE_APP_API}/tmwa/server`, {
			mode: "cors",
			referrer: "no-referrer",
		});

		try {
			const rawResponse = await fetch(req);
			const data: StatusResponse = await rawResponse.json();

			this.Online = data.serverStatus === "Online";
			this.Players = data.playersOnline || 0;

			if (Reflect.has(self, "localStorage")) {
				localStorage.setItem("onlinePlayers", `${this.Players}`);
				localStorage.setItem("serverOnline", this.Online ? "true": "false");
			}
		} catch (err) {
			// API unreachable (assume it's offline as you cannot register)
			this.Online = false;
		}

		setTimeout(this.getStatus, 8000);
	}

	mounted () {
		// use the last cached value to populate prior to first fetch:
		if (Reflect.has(self, "localStorage")) {
			this.Players = +(localStorage.getItem("onlinePlayers") || 99);
			this.Online = !!(localStorage.getItem("serverOnline") || true);
		} else {
			this.Players = 99;
		}

		this.getStatus();
	}
}
</script>