-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.html
175 lines (175 loc) · 5.88 KB
/
peers.html
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TrollTower - Peers Monitor</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
</head>
<body>
<div id="wrapper">
<h5>Worlds</h5>
<div id="peers"></div>
<h5>Peers/hour</h5>
<canvas id="graphA" class="graph"></canvas>
<h5>Peers/day</h5>
<canvas id="graphB" class="graph"></canvas>
</div>
<script>
const dom = {
graphA: document.getElementById('graphA'),
graphB: document.getElementById('graphB'),
peers: document.getElementById('peers'),
};
const rooms = ['Menu', 'Brittle', 'City', 'Tower', 'Well'].map((room) => {
const div = document.createElement('div');
div.className = 'room';
const bar = document.createElement('div');
bar.className = 'bar';
div.appendChild(bar);
const id = document.createElement('div');
id.innerText = room;
div.appendChild(id);
const count = document.createElement('div');
count.className = 'count';
div.appendChild(count);
dom.peers.appendChild(div);
return { id: room, bar, count };
});
const sound = new Audio();
sound.src = 'sounds/ding.ogg';
sound.volume = 0.5;
let count = Infinity;
const updatePeers = () => {
fetch('https://rooms.trolltower.app/peers')
.then((res) => res.json())
.then((peers) => {
peers = Object.keys(peers).reduce((merged, key) => {
const [id] = key.split('-');
merged[id] = (merged[id] || 0) + peers[key];
return merged;
}, {});
const previous = count;
count = rooms.reduce((total, room) => {
room.count.innerText = peers[room.id] || 0;
return total + (peers[room.id] || 0);
}, 0);
if (count > previous) {
sound.play();
updateGraphs();
}
})
};
dom.graphA.width = dom.graphB.width = 400;
dom.graphA.height = dom.graphB.height = 64;
const updateGraphs = () => {
fetch('https://rooms.trolltower.app/stats')
.then((res) => res.json())
.then((stats) => {
{
const max = Object.keys(stats.room).reduce((max, id) => Math.max(max, stats.room[id]), 0);
rooms.forEach(({ id, bar }) => {
bar.style.width = `${((stats.room[id] || 0) * 90) / max}%`;
});
}
[
{ graph: dom.graphA, data: stats.hour, count: 24 * 2, step: { hours: -1 } },
{ graph: dom.graphB, data: stats.day, count: 14, step: { date: -1 } },
].forEach(({ graph, data, count, step }) => {
const { width, height } = graph;
const ctx = graph.getContext('2d');
ctx.clearRect(0, 0, width, height);
const max = Object.keys(data).reduce((max, date) => Math.max(max, data[date]), 0);
const size = width / count;
const ratio = (height - 10) / max;
const now = new Date();
let time = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours());
for (let i = count - 1; i >= 0; i -= 1) {
const year = time.getUTCFullYear();
const month = time.getUTCMonth();
const date = time.getUTCDate();
const hours = time.getUTCHours();
const localDay = time.getDay();
const localHours = time.getHours();
const key = `${year}${month < 9 ? '0' : ''}${month + 1}${date < 10 ? '0' : ''}${date}${data === stats.hour ? `${hours < 10 ? '0' : ''}${hours}` : ''}`;
time = new Date(Date.UTC(year, month, date + (step.date || 0), hours + (step.hours || 0)));
if (
(data === stats.day && localDay === 1)
|| (data === stats.hour && (localHours === 0 || localHours === 12))
) {
ctx.fillStyle = (data === stats.hour && localHours === 0) ? '#444' : '#333';
ctx.fillRect(
size * i, 0,
size, height
);
}
if (data[key]) {
const h = data[key] * ratio;
ctx.fillStyle = '#393';
ctx.fillRect(
size * i, height - h,
size, h
);
}
}
});
});
};
setInterval(updatePeers, 10000);
updatePeers();
updateGraphs();
</script>
<style>
body {
background: linear-gradient(#444, #222);
margin: 0;
font-family: monospace;
overflow-y: scroll;
color: #fff;
}
#wrapper {
display: flex;
flex-direction: column;
justify-content: center;
max-width: 400px;
margin: 0 auto;
height: 100vh;
}
h5 {
display: block;
text-transform: uppercase;
}
.graph {
border-top: 1px dashed #010;
border-bottom: 1px dashed #010;
vertical-align: middle;
margin-bottom: 1rem;
}
#peers {
display: flex;
flex-direction: column;
justify-content: center;
margin-bottom: 1rem;
}
.room {
position: relative;
display: flex;
padding: 0.5rem 1rem;
border-bottom: 1px dashed #010;
}
.room:first-child {
border-top: 1px dashed #010;
}
.bar {
position: absolute;
top: 0;
left: 0;
height: 100%;
background: #060;
z-index: -1;
}
.count {
margin-left: auto;
}
</style>
</body>
</html>