-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
145 lines (113 loc) · 3.98 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dice Auction Room</title>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<h1>Dice Auction Room</h1>
<p>Welcome! Please enter a name and click the button when you're ready.</p>
<div id="error"></div>
<input type="hidden" name="id" id="id">
<input type="text" name="name" id="name">
<button id="update-name">Update</button>
<button id="roll">Start Auction</button>
<button id="reveal">Reveal Rolls</button>
<hr>
<table id="users">
<tr>
<th></th>
<th>User</th>
<th>Roll</th>
</tr>
</table>
<script>
function populateError(message) {
var el = document.getElementById('error');
el.innerHTML = message;
if (el.classList.contains('hidden')) {
el.classList.remove('hidden');
}
}
function clearError() {
var el = document.getElementById('error');
el.innerHTML = '';
if (!el.classList.contains('hidden')) {
el.classList.add('hidden');
}
}
function updateUsers(users) {
// Refresh the users table.
var table = document.getElementById('users');
var innerHTML = `
<tr>
<th></th>
<th>User</th>
<th>Roll</th>
</tr>`;
for (const key in users) {
let user = users[key];
innerHTML += `<tr>
<td> ${user.referee ? '🎲' : ''} </td>
<td> ${user.name} </td>
<td> ${user.roll ? user.roll : ''} </td>
</tr>`;
}
table.innerHTML = innerHTML;
return;
}
const ws = new WebSocket('ws://localhost:8080'); // Should change to wss for prod (secure form)
ws.addEventListener('open', () => {
// Get current list of users.
ws.send(JSON.stringify({
event: 'queryUsers',
}));
// Add listeners to buttons so they can send updates to the Node server.
document.getElementById('update-name').addEventListener('click', () => {
// Send new name to server.
ws.send(JSON.stringify({
event: 'updateName',
name: document.getElementById('name').value,
}));
});
document.getElementById('roll').addEventListener('click', () => {
// Send roll event to server.
ws.send(JSON.stringify({
event: 'roll',
}));
});
document.getElementById('reveal').addEventListener('click', () => {
// Send reveal event to server.
ws.send(JSON.stringify({
event: 'reveal',
}));
});
});
ws.addEventListener('message', e => {
// Listen for changes from the server.
var data = JSON.parse(e.data);
if (data.event === 'update') {
if (data.hasOwnProperty('name')) {
document.getElementById('name').value = data.name;
}
updateUsers(data.users);
}
if (data.event === 'error') {
// Populate an error message.
populateError(data.message);
if (data.errorType === 'nameCollision') {
// Replace name input field with actual name according to server.
document.getElementById('name').value = data.name;
}
}
});
</script>
</body>
</html>