-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
70 lines (56 loc) · 2.06 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
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tug O' War</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
</head>
<body>
<button id="reset">Reset</button>
<button id="playerOne" style="padding: 50px; font-size: 20px;">Player 1</button>
<input id="score" type="text" value="0" disabled>
<button id="playerTwo" style="padding: 50px; font-size: 20px;">Player 2</button>
<h1 id="winnerLabel"></h1>
<img id="winner" src="https://media.giphy.com/media/oFI7FttD0iC8V2Iqmy/giphy.gif" hidden>
<script>
const socket = io(location.origin);
const reset = document.getElementById('reset');
const playerOne = document.getElementById('playerOne');
const playerTwo = document.getElementById('playerTwo');
const score = document.getElementById('score');
const winnerLabel = document.getElementById('winnerLabel');
const winnerGif = document.getElementById('winner');
socket.on('connect', () => {
console.log('Connection successful...');
});
socket.on('score', (data) => {
const total = data.playerTwo - data.playerOne;
winnerLabel.innerText = total >= 50
? 'Player 2 is the winner!'
: total <= -50
? 'Player 1 is the winner1'
: 'TUG!';
if (total >= 50 || total <= -50) {
winnerGif.hidden = false;
playerOne.disabled = true;
playerTwo.disabled = true;
};
score.setAttribute('value', total);
});
reset.addEventListener('click', () => {
socket.emit('reset');
winnerGif.hidden = true;
playerOne.disabled = false;
playerTwo.disabled = false;
winnerLabel.innerText = "TUG!";
});
playerOne.addEventListener('click', () => {
socket.emit('tug', { team: '1' });
});
playerTwo.addEventListener('click', () => {
socket.emit('tug', { team: '2' });
});
</script>
</body>
</html>