-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
72 lines (62 loc) · 2.23 KB
/
game.js
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
$(document).ready(function() {
var socket = io('/game', {
transports: ['websocket'],
upgrade: false
});
var textarea = document.getElementById('writer');
var turnHeader = document.getElementById('turn-header');
var writingPrevented = false;
var turnClient;
socket.on('stop-timer', function() {
socket.emit('stop-timer');
})
socket.on('turn-count', function(msg) {
turnClient = msg;
})
function preventWrite(event) {
event.preventDefault();
console.log('preventDefault still enabled');
}
socket.on('your-turn', function(msg) {
if (socket.id == msg) {
console.log('its your turn!');
var secondsLeft = 7;
turnHeader.innerHTML = "Your Turn! (7)";
textarea.addEventListener('keyup', function enabledWrite() {
socket.emit('write', textarea.value);
});
if (writingPrevented) {
textarea.removeEventListener('keyup', preventWrite);
textarea.removeEventListener('keydown', preventWrite);
console.log('allowing you to write');
writingPrevented = false;
console.log('writingPrevented: ' + writingPrevented);
}
countDown();
} else {
console.log('wait your turn!');
turnHeader.innerHTML = "Wait Your Turn...";
console.log('preventing your writing');
textarea.addEventListener('keyup', preventWrite);
textarea.addEventListener('keydown', preventWrite);
writingPrevented = true;
console.log('writingPrevented: ' + writingPrevented);
}
function countDown() {
if (!secondsLeft) {
turnClient++;
socket.emit('change-turn', turnClient);
console.log('turnClient: ' + turnClient);
return;
}
setTimeout(function writeTimer() {
secondsLeft--;
turnHeader.innerHTML = "Your Turn! " + "(" + secondsLeft + ")";
countDown();
}, 1000);
}
})
socket.on('write', function(msg) {
textarea.value = msg;
})
})