-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
322 lines (300 loc) · 10.1 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tic Tac Tournament</title>
<!-- Meta tags -->
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- React, React DOM, Babel, & Bootstrap CSS -->
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<style>
body {
background-color: whitesmoke;
}
.game-view {
display: flex;
justify-content: space-around;
align-items: flex-start;
}
.game-result {
font-size: 72px;
text-transform: capitalize;
color: palegreen;
text-shadow: 1px 1px 2px black, 0 0 25px forestgreen, 0 0 5px darkgreen;
text-align: center;
visibility: hidden;
position: absolute;
left: 50%;
top: 50%;
width: 100%;
margin: auto;
transform: translateX(-50%) rotateZ(-15deg);
}
.relative {
position: relative;
}
</style>
<!-- React -->
<script type="text/babel">
// Keep references to DOM elements.
const game = document.getElementById('live-game');
const table = document.getElementById('live-league-table');
const winnerEl = document.querySelector('.game-result');
const LiveGame = ({ game }) => {
const { team1, team2, gameState } = game;
const styles = {
gridContainer: {
backgroundColor: 'slategray',
width: '695px',
height: '695px',
},
grid: {
display: 'grid',
gridTemplateColumns: 'repeat(3, 1fr)',
gap: '10px 10px', // shorthand for grid-row-gap and grid-column-gap
placeItems: 'center', // shorthand for justify-items and align-items
justifyContent: 'center', // centers the grid within its container
alignContent: 'start', // positions the grid vertically within its container
width: '695px',
height: '695px',
},
gridItem: {
fontSize: '150px',
backgroundColor: 'whitesmoke', // match page background color
color: 'lightgray',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100%',
},
teams: {
display: 'flex',
justifyContent: 'space-between',
marginBottom: '25px',
},
team1Container: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
padding: '10px',
borderRadius: '0px 0px 10px 10px',
boxShadow: '-5px 5px silver',
zIndex: '1',
width: '45%',
backgroundColor: '#920A36',
color: 'whitesmoke',
},
team2Container: {
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
padding: '10px',
borderRadius: '0px 0px 10px 10px',
boxShadow: '5px 5px silver',
zIndex: '1',
width: '45%',
backgroundColor: '#3D18AE',
color: 'whitesmoke',
},
team1Name: {
color: '#920A36',
width: '100%',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
team2Name: {
color: '#3D18AE',
width: '100%',
whiteSpace: 'nowrap',
overflow: 'hidden',
textOverflow: 'ellipsis',
},
team1: {
color: '#920A36',
},
team2: {
color: '#3D18AE',
},
};
const squares = gameState.map((gameSquare, index) => {
let value, finalStyle;
if (gameSquare === '-') {
value = index;
finalStyle = styles.gridItem;
}
else if (gameSquare === team1.token) {
value = team1.token;
finalStyle = { ...styles.gridItem, ...styles.team1 };
}
else if (gameSquare === team2.token) {
value = team2.token;
finalStyle = { ...styles.gridItem, ...styles.team2 };
}
return <div key={index} style={finalStyle}>{value}</div>;
});
return (
<div style={styles.container}>
<div style={styles.teams}>
<div style={styles.team1Container}>
<h4 style={styles.team1Name}>{team1.name}</h4>
<p style={styles.team1} className="h1 text-center">{team1.token}</p>
</div>
<h2 style={{ alignSelf: 'center' }}>vs.</h2>
<div style={styles.team2Container}>
<h4 style={styles.team2Name}>{team2.name}</h4>
<p style={styles.team2} className="h1 text-center">{team2.token}</p>
</div>
</div>
<div style={styles.gridContainer}>
<div style={styles.grid}>
{squares}
</div>
</div>
</div>
);
};
const LiveTable = ({ tableInfo }) => {
const styles = {
table: {
border: 'solid 1px silver',
borderCollapse: 'collapse',
borderSpacing: '0',
width: '600px',
},
rowEven: {
backgroundColor: 'whitesmoke',
borderCollapse: 'collapse',
},
rowOdd: {
backgroundColor: 'lightgray',
borderCollapse: 'collapse',
},
cell: {
padding: '10px',
color: '#111',
textAlign: 'center',
border: 'solid 1px #ddd'
},
header: {
backgroundColor: 'lightseagreen',
color: 'whitesmoke',
border: '1px solid #ddd',
},
headerRow: {
padding: '10px',
},
headerCell: {
padding: '10px',
textAlign: 'center',
},
};
const rows = tableInfo
.sort((a, b) => a.points < b.points)
.map((row, index) => {
const rowClass = (index % 2 === 0) ? styles.rowEven : styles.rowOdd;
return (
<tr key={index} style={rowClass}>
<th scope="row" style={styles.cell}>{row.name}</th>
<td style={styles.cell}>{row.gamesPlayed}</td>
<td style={styles.cell}>{row.wins}</td>
<td style={styles.cell}>{row.losses}</td>
<td style={styles.cell}>{row.ties}</td>
<td style={styles.cell}>{row.points}</td>
</tr>
);
});
return (
<table style={styles.table}>
<thead style={styles.header}>
<tr>
<td style={styles.headerCell}>Team</td>
<td style={styles.headerCell}>Games Played</td>
<td style={styles.headerCell}>Wins</td>
<td style={styles.headerCell}>Losses</td>
<td style={styles.headerCell}>Ties</td>
<td style={styles.headerCell}>Points</td>
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
);
};
function updateGame (gameInfo) {
ReactDOM.render(<LiveGame game={gameInfo} />, game);
}
function updateTable (tableInfo) {
ReactDOM.render(<LiveTable tableInfo={tableInfo} />, table);
}
const team1 = { name: 'Team 1 Name TBD', token: 'X' };
const team2 = { name: 'Team 2 Name TBD', token: 'O' };
const gameState = ['-','-','-','-','-','-','-','-','-',];
updateGame({ team1, team2, gameState });
</script>
<!-- Web Socket communication -->
<script>
window.ticTacQueue = [];
// Connect to the Web Socket server.
const host = location.origin.replace(/^http/, 'ws');
const socket = new WebSocket(host + '?name=viewer');
// Handle messages from the server.
socket.onmessage = (event) => {
const message = JSON.parse(event.data);
// Add this message to the queue, handle it in the show runner.
const queue = [...window.ticTacQueue];
queue.push(message);
window.ticTacQueue = queue;
}
</script>
<!-- Show Runner -->
<script>
window.ticTacQueueInterval = setInterval(() => {
if (window.ticTacQueue.length === 0) { return; }
winnerEl.style.visibility = 'hidden';
const queue = [...window.ticTacQueue];
const update = queue.shift();
window.ticTacQueue = queue;
switch (update.type) {
case 'gameUpdate':
updateGame(update.gameInfo);
break;
case 'gameResult':
let display = update.result.isTie ? 'Tie' : `${update.result.winner.substring(0, 25)} wins!`;
winnerEl.innerText = display;
winnerEl.style.visibility = 'visible';
break;
case 'tableUpdate':
updateTable(update.tableInfo);
break;
}
}, 500); //1500
</script>
</head>
<body>
<div class="container-fluid">
<div class="jumbotron">
<h1>Tic Tac Tournament</h1>
<p class="lead">Welcome to the Tic Tac Tournament!</p>
</div>
<div>
<div class="game-view">
<div class="relative">
<h3>Active Game</h3>
<div id="live-game"></div>
<h1 class="game-result"></h1>
</div>
<div>
<h3>League Table</h3>
<div id="live-league-table"></div>
</div>
</div>
</div>
</div>
</body>
</html>