-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvotes.html
105 lines (94 loc) · 3.12 KB
/
votes.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
<!DOCTYPE html>
<html>
<head>
<title>Votes</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
body {
background: #161512 linear-gradient(to bottom, #2e2a24, #161512 116px) no-repeat;
margin: 0px;
overflow: hidden;
color: white;
font-size: 2rem;
font-family: 'Roboto', sans-serif;
}
#timer {
position: fixed;
top: 0;
left: 0;
margin: 0px;
}
#text {
position: fixed;
top: 1.5em;
left: 0;
margin: 0px;
}
ul {
padding: 0;
margin: 0;
margin-bottom: 0.5em;
}
</style>
</head>
<body>
<div id="timer"></div>
<div id="text"></div>
<script src="/socket.io/socket.io.js"></script>
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const streamer = urlParams.get('streamer');
if (urlParams.get('transparent')) document.body.style.background = 'transparent';
const socket = io();
let timerInterval;
let timer = 0;
let state = 'waiting';
const appendMove = (text) => {
const ul = document.createElement('ul');
ul.appendChild(document.createTextNode(text));
window.text.appendChild(ul);
};
setInterval(() => socket.emit('streamer', streamer), 2000); // interval so it can reconnect in case the bot goes down temporarily
window.timer.innerHTML = 'Waiting for game...';
appendMove('Example:');
appendMove('Ke2: 42 votes');
appendMove('e4: 15 votes');
appendMove('resign: 2 votes');
const updateTimer = () => {
if (timer <= 0) {
clearInterval(timerInterval);
timerInterval = null;
window.timer.innerHTML = 'Voting over';
} else window.timer.innerHTML = `${timer--} seconds left`;
};
socket.on('status', (status) => {
if (status.timer) {
if (timerInterval) clearInterval(timerInterval);
timer = status.timer;
updateTimer();
timerInterval = setInterval(updateTimer, 1000);
state = 'started';
} else if (status.state && status.state !== state) {
state = status.state;
if (state === 'started') window.timer.innerHTML = 'Game started';
else window.timer.innerHTML = state;
}
});
socket.on('candidates', (candidates) => {
if (!candidates.total) return;
window.text.innerHTML = '';
if ('draw' in candidates)
appendMove(`Offer/accept draw: ${Math.floor((candidates.draw.votes / candidates.total) * 100)}%`);
const sortedCandidates = Object.entries(candidates)
.filter(([k, _]) => k !== 'total' && k !== 'draw')
.sort(([_, a], [__, b]) => b.votes - a.votes);
for (const [key, candidate] of sortedCandidates) {
const { votes, san } = candidate;
const text = `${san ? san : key}: ${votes} vote${votes === 1 ? '' : 's'}`;
appendMove(text);
}
});
</script>
</body>
</html>