-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
155 lines (135 loc) · 3.7 KB
/
app.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
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
const http = require('http');
const server = http.createServer();
const sockets = require('socket.io')
const io = sockets(server, {
cors: {
origin: "http://127.0.0.1:8000",
methods: ["GET", "POST"]
}
});
const Statuses = {
WAITING: 'waiting',
PLAYING: 'playing',
DRAW: 'draw',
WIN: 'win'
}
let gameState = {
board: new Array(9).fill(null),
currentPlayer: null,
players : [],
result : {
status : Statuses.WAITING
}
}
io.on('connection', function (connection) {
console.log(connection);
connection.on('addPlayer', addPlayer(connection.id));
connection.on('action', action(connection.id));
connection.on('disconnect', disconnect(connection.id));
connection.on('rematch', rematch(connection.id));
});
function disconnect(socketId){
return (reason) => {
gameState.players = gameState.players.filter(p => p.id != socketId);
if (gameState.players !== 2){
resetGame();
io.emit('gameState', gameState);
}
}
}
function addPlayer(socketId){
return (data)=>{
const numberOfPlayers = gameState.players.length;
if (numberOfPlayers >= 2){
return;
}
let nextSymbol = 'X';
if (numberOfPlayers === 1){
if (gameState.players[0].symbol === 'X'){
nextSymbol = 'O';
}
}
const newPlayer = {
playerName: data.playerName,
id: socketId,
symbol: nextSymbol
};
gameState.players.push(newPlayer);
if (gameState.players.length == 2){
gameState.result.status = Statuses.PLAYING;
gameState.currentPlayer = newPlayer;
}
io.emit('gameState', gameState);
}
}
function action(socketId){
return (data)=> {
if (gameState.result.status === Statuses.PLAYING && gameState.currentPlayer.id === socketId){
const player = gameState.players.find(p => p.id === socketId);
if (gameState.board[data.gridIndex] == null){
gameState.board[data.gridIndex] = player;
gameState.currentPlayer = gameState.players.find(p => p !== player);
checkForEndOfGame();
}
}
io.emit('gameState', gameState);
}
}
function rematch(socketId){
return (data) => {
if (gameState.players.findIndex(p=> p.id === socketId) < 0) return; // Don't let observers rematch
if (gameState.result.status === Statuses.WIN || gameState.result.status === Statuses.DRAW){
resetGame();
io.emit('gameState', gameState);
}
}
}
function resetGame(){
gameState.board = new Array(9).fill(null);
if (gameState.players.length > 1){
gameState.result.status = Statuses.PLAYING;
const randPlayer = Math.floor(Math.random() * gameState.players.length);
gameState.currentPlayer = gameState.players[randPlayer];
} else {
gameState.result.status = Statuses.WAITING;
gameState.currentPlayer = null;
}
console.log(gameState);
}
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
function checkForEndOfGame(){
// Check for a win
gameState.players.forEach(player => {
winPatterns.forEach(seq => {
if (gameState.board[seq[0]] == player
&& gameState.board[seq[1]] == player
&& gameState.board[seq[2]] == player){
gameState.result.status = Statuses.WIN;
gameState.result.winner = player;
}
});
});
// Check for a draw
if (gameState.result.status != Statuses.WIN){
const emptyBlock = gameState.board.indexOf(null);
if (emptyBlock == -1){
gameState.result.status = Statuses.DRAW;
}
}
}
function message(data){
console.log('new message: ' + data);
io.emit("broadcast", data);
}
server.listen(3042, function() {
console.log('listening on 3042');
});