-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
99 lines (90 loc) · 2.72 KB
/
index.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
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
const express = require('express')
const app = express()
const http = require('http').Server(app)
const io = require('socket.io')(http)
const tictactoe = require('./tictactoe')
app.use(express.static('public'))
let player = 'x'
let lastPlayer = ''
let fieldArray = tictactoe.emptyField()
const players = []
const confirmed_players = []
/*
* creates connection with socket and pushes players socket.id in
* players array
*/
io.on('connection', socket => {
players.push(socket.id)
console.log('a user connected')
console.log(players)
/*
* if two players are connected, the game will start. Otherwise
* the game waits until the second player has connected
*/
if (players.length >= 2) {
io.emit('new state', players[0], players[1], socket.id, fieldArray)
} else {
io.emit('waiting for players')
}
/*
* when a player clicks on a cell the game checks if it's this
* players turn and the cell isn't already taken.
* If another player connects to the game enters in watch-mode
*/
socket.on('clicked', (row, col) => {
if (lastPlayer === socket.id) {
console.log('it is not your turn!')
return
}
if (fieldArray[row][col] !== null) {
console.log('this field is already taken!')
return
}
if (!(socket.id === players[0]) && !(socket.id === players[1])) {
console.log('The room is already full. You can watch the game, though')
return
}
lastPlayer = socket.id
fieldArray[row][col] = player
player = tictactoe.togglePlayer(player)
/*
* If a player wins the game will show a notification and the button "play
* again" will show up
*/
const winningPlayer = tictactoe.winner(fieldArray)
if (winningPlayer) {
io.emit('winner', winningPlayer, players[0], players[1], fieldArray)
tictactoe.swapArrayElements(players, 0, 1)
}
io.emit('new state', players[0], players[1], socket.id, fieldArray)
})
/*
* Restarts the game if both player click on the "play again" button
*/
socket.on('play again', () => {
socket.broadcast.emit('player confirmed', players[0], players[1])
if (
socket.id === confirmed_players[0] ||
socket.id === confirmed_players[1]
) {
return
}
confirmed_players.push(socket.id)
if (confirmed_players.length === 2) {
fieldArray = tictactoe.emptyField()
io.emit('new state', players[0], players[1], socket.id, fieldArray)
confirmed_players.length = 0
}
})
socket.on('disconnect', () => {
console.log('disconnect: ', socket.id)
const index = players.indexOf(socket.id)
if (index > -1) {
players.splice(index, 1)
}
console.log(players)
})
})
http.listen(3000, () => {
console.log('listening on *:3000')
})