-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
198 lines (151 loc) · 4.14 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
require("dotenv").config();
const path = require("path");
const express = require("express");
const { createServer } = require("http");
const { Server } = require("socket.io");
const app = express();
const server = createServer(app);
const io = new Server(server, {});
class Game {
constructor(gamePin, gameName) {
this.gamePin = gamePin;
this.gameName = gameName;
this.activePlayer = null;
this.setActivePlayer = this.setActivePlayer.bind(this);
this.clearActivePlayer = this.clearActivePlayer.bind(this);
}
setActivePlayer(id, name) {
this.activePlayer = {
id,
name,
};
}
clearActivePlayer() {
this.activePlayer = null;
}
}
let games = {};
function generateGamePin() {
let gamePin;
do {
gamePin = (() => {
let pin = "";
let length = Math.floor(Math.random() * 3) + 6;
for (let i = 0; i < length; i++) {
pin += Math.floor(Math.random() * 10);
}
return pin;
})();
} while (games[gamePin]);
return gamePin;
}
app.use(express.static(path.join(__dirname, "build")));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
io.on("connection", (socket) => {
console.log("a wild socket appears! " + socket.id);
// MARK: Game join flow
socket.on("game/checkPin", (data, res) => {
if (!res) res = () => {};
console.log("game/checkPin", data.gamePin);
let game = games[data.gamePin];
if (!game) {
res({ err: "Game doesn't exist." });
} else {
res({
gamePin: game.gamePin,
gameName: game.gameName,
});
}
});
socket.on("game/join", (data, res) => {
if (!res) res = () => {};
console.log("game/join", data);
let game = games[data.gamePin];
if (!game) {
res({ err: "Game doesn't exist." });
} else {
res({
gamePin: game.gamePin,
gameName: game.gameName,
});
}
socket.join(`${data.gamePin}/player`);
socket.data.gamePin = data.gamePin;
socket.data.playerName = data.playerName;
res({
...game,
});
});
// MARK: Game create flow
socket.on("game/create", (data, res) => {
if (!res) res = () => {};
// Create a new game
console.log("game/create", data);
// Generate a new unique game pin
let gamePin = generateGamePin();
// Store the game pin and game data
let game = new Game(gamePin, data.gameName);
games[gamePin] = game;
// Subscribe the client to host events
socket.join(`${gamePin}/host`);
socket.data.hostedGame = gamePin;
// Return the game pin and game name as confirmation
res({
...game,
});
});
socket.on("game/buzz", (data, res) => {
if (!res) res = () => {};
console.log("game/buzz", data);
let game = games[socket.data.gamePin];
if (!game) {
res({ err: "Game doesn't exist." });
return;
}
if (!game.activePlayer) {
game.setActivePlayer(socket.id, socket.data.playerName);
socket
.to([`${game.gamePin}/player`, `${game.gamePin}/host`])
.emit("game/update", {
...game,
});
res({
...game,
});
}
});
socket.on("game/reset", (data, res) => {
if (!res) res = () => {};
console.log("game/reset", data);
let game = games[socket.data.hostedGame];
if (!game) {
// res({ err: "Game doesn't exist." });
console.log("game/reset", "Game doesn't exist.");
return;
}
game.clearActivePlayer();
socket
.to([`${game.gamePin}/player`, `${game.gamePin}/host`])
.emit("game/update", {
...game,
});
res({
...game,
});
});
socket.on("disconnect", (reason) => {
// If the client is hosting a game, remove the game
if (socket.data.hostedGame) {
console.log("ended game", socket.data.hostedGame);
delete games[socket.data.hostedGame];
// notify players that the game has ended
socket.to(`${socket.data.hostedGame}/player`).emit("game/end");
io.in(`${socket.data.hostedGame}/player`).socketsLeave(
`${socket.data.hostedGame}/player`
);
}
});
});
server.listen(process.env.PORT || 8080);