-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
126 lines (110 loc) · 3.61 KB
/
server.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
const express = require("express");
const app = express();
const path = require("path");
const server = require("http").createServer(app);
const io = require("socket.io")(server);
const PORT = process.env.PORT || 3000;
const { generateId, findWinner } = require("./utils.js");
const games = {};
const boardState = {};
let gamesList = Object.keys(games);
app.use(express.static(path.join(__dirname, "./client")));
io.on("connection", (client) => {
gamesList = gamesList.filter((game) => games[game].length < 2);
io.emit("games", gamesList);
client.on("newgame", (playerName) => {
const gameId = generateId();
client.join(gameId);
client.playerLetter = "X";
client.gameId = gameId;
// initial board state for new game
boardState[gameId] = [
[null, null, null],
[null, null, null],
[null, null, null],
];
boardState[gameId].playerOneName = playerName;
games[gameId] = [client];
client.emit("gameid", gameId);
gamesList = Object.keys(games).filter((game) => games[game].length < 2);
io.emit("games", gamesList);
client.emit("playerletter", client.playerLetter);
});
client.on("joingame", (gameId, playerName) => {
// ensure no more than 2 players can join a game
if (games[gameId].length === 2) return;
client.join(gameId);
client.playerLetter = "O";
client.gameId = gameId;
boardState[gameId].playerTwoName = playerName;
boardState[gameId].currentPlayer = games[client.gameId][0].playerLetter;
games[gameId].push(client);
io.to(gameId).emit(
"playernames",
boardState[gameId].playerOneName,
playerName
);
client.emit("playerletter", client.playerLetter);
io.to(gameId).emit("currentplayer", boardState[gameId].currentPlayer);
gamesList = Object.keys(games).filter((game) => games[game].length < 2);
io.emit("games", gamesList);
});
client.on("cellclick", (cellId) => {
const [x, y] = JSON.parse(cellId);
if (boardState[client.gameId].hasBeenWon) return;
if (client.playerLetter === boardState[client.gameId].currentPlayer) {
boardState[client.gameId][x][y] = client.playerLetter;
io.to(client.gameId).emit(
"updatecell",
`[${x}, ${y}]`,
client.playerLetter
);
// determine whose turn it is next
if (
games[client.gameId][0].playerLetter ===
boardState[client.gameId].currentPlayer
) {
boardState[client.gameId].currentPlayer =
games[client.gameId][1].playerLetter;
} else {
boardState[client.gameId].currentPlayer =
games[client.gameId][0].playerLetter;
}
// check if there's a winner
if (findWinner(boardState[client.gameId])) {
io.to(client.gameId).emit(
"gameover",
findWinner(boardState[client.gameId])
);
boardState[client.gameId].hasBeenWon = true;
} else {
io.to(client.gameId).emit(
"currentplayer",
boardState[client.gameId].currentPlayer
);
}
}
});
client.on("replay", (playerLetter) => {
boardState[client.gameId] = [
[null, null, null],
[null, null, null],
[null, null, null],
];
delete boardState[client.gameId].hasBeenWon;
boardState[client.gameId].currentPlayer = playerLetter;
io.to(client.gameId).emit(
"currentplayer",
boardState[client.gameId].currentPlayer
);
io.to(client.gameId).emit("restartgame");
});
client.on("disconnect", () => {
io.to(client.gameId).emit("offline", client.playerLetter);
delete boardState[client.gameId];
delete games[client.gameId];
gamesList = Object.keys(games).filter((game) => games[game].length < 2);
io.emit("games", gamesList);
});
});
server.listen(PORT);