-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockets.js
67 lines (54 loc) · 1.82 KB
/
sockets.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
let readyPlayerCount = 0
let roomNumber = 0
function listen (io) {
const fibNamespace = io.of('/fib') // choose fib namespace
io.on('connection', socket => {
console.log('New client connected', socket.id)
//let room = 'room' + roomNumber
let room = 'room' + Math.floor(readyPlayerCount / 2) // create rooms for sets of 2 players
// clientActions allows Cypress tests
const clientActions = {
onReady () {
readyPlayerCount++
socket.join(room)
if (readyPlayerCount % 2 === 0) {
io.to(room).emit('startGame', socket.id) // 2nd player will be referee
}
console.log(
'client joined room ',
room,
'playerCount ',
readyPlayerCount
)
}
}
socket.on('ready', clientActions.onReady)
socket.on('begin', beginData => {
socket.to(room).emit('begin', beginData)
})
socket.on('player2Name', player2Name => {
socket.to(room).emit('player2Name', player2Name)
})
socket.on('next turn', turnData => {
socket.to(room).emit('next turn', turnData, socket.id)
})
socket.on('leave game', () => {
socket.to(room).emit('player left') // let client know other player left
socket.leave(room)
readyPlayerCount--;
readyPlayerCount = readyPlayerCount < 0 ? 0 : readyPlayerCount;
})
socket.on('other player left', () => { // this player leaves if the other player left
socket.leave(room)
readyPlayerCount--;
readyPlayerCount = readyPlayerCount < 0 ? 0 : readyPlayerCount;
})
socket.on('disconnect', reason => {
console.log('Client', socket.id, ' disconnected: ', reason)
socket.leave(room) // rooms should not be perpetual, this is default behavior, just being explicit
})
})
}
module.exports = {
listen
}