-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
240 lines (160 loc) · 5.11 KB
/
game.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var express = require('express');
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/help', (req, res) => {
res.sendFile(__dirname + '/help.html');
});
io.on('connection', (socket) => {
const socketID = socket.id;
console.log('a user connected with the ID of'+socketID);
io.to(socketID).emit("getSocketID", socketID);
// Any Client -> The Entire Planet
socket.on('createRoom', (msg) => {
io.emit('createRoom', msg);
console.log(msg+' - created room');
socket.join(msg);
console.log(msg+' - join by creator');
// TODO: I need to figure out how to get a list of all clients in a room.
});
// Any Client -> The Rest of the Room
socket.on('joinRoom', (msg) => {
console.log(msg+' - guest joined');
socket.join(msg);
// Broadcast a join message gloablly.
io.emit('joinRoom', msg);
// Request players from the host.
socket.to(msg).emit("requestPlayers");
});
// Any Client -> The Rest of the Room
socket.on('updatePlayers', msg => {
console.log(msg.roomCode+' - players updated');
console.table(msg.players);
const d = {
players: msg.players,
gameStarted: msg.gameStarted
};
// TODO: Probably change this to go from game host to anybody else in the room?
io.in(msg.roomCode).emit('updatePlayers', d);
});
// SysAdmin -> ALL
socket.on('startTheGame', msg => {
console.log(msg.roomCode+' - game started');
io.in(msg.roomCode).emit('startTheGame', {
players: msg.players,
maxRounds: msg.maxRounds,
sysAdminIndex: msg.sysAdminIndex,
allowNaughty: msg.allowNaughty
});
console.table(msg.players);
});
// SysAdmin -> Employees
socket.on("updatePasswordChallenge", msg => {
console.log(msg.roomCode+' - challenge is '+msg.challenge.name);
io.to(msg.roomCode).emit("updatePasswordChallenge", {
challenge: msg.challenge
});
});
// SysAdmin -> Employees
socket.on("updatePasswordRules", msg => {
console.log(msg.roomCode+' - new rule');
console.table(msg.rules);
io.to(msg.roomCode).emit("updatePasswordRules", {
rules: msg.rules,
shibboleth: msg.shibboleth
});
});
// SysAdmin -> Employees
socket.on("summonThePig", msg => {
console.log(msg.roomCode+' - The Flying Pig has been summoned!');
io.to(msg.roomCode).emit("summonThePig");
});
// SysAdmin -> Employees
socket.on("updateBugs", msg => {
console.log(msg.roomCode+' - new bug');
console.table(msg.bugs);
io.to(msg.roomCode).emit("updateBugs", {
bugs: msg.bugs
});
});
// SysAdmin -> ALL
socket.on("startGuessing", msg => {
console.log(msg.roomCode+' - start guessing');
io.in(msg.roomCode).emit("startGuessing", {
sysAdminIndex: msg.sysAdminIndex
});
});
// An Employee -> Rest of Game
socket.on("triedPassword", msg => {
console.log(msg.roomCode+' - bad guess');
io.to(msg.roomCode).emit("triedPassword", {
playerIndex: msg.playerIndex,
pwAttempt: msg.pwAttempt,
attemptCount: msg.attemptCount,
result: msg.result
});
});
// An Employee -> ALL
socket.on("crashedServer", msg => {
console.log(msg.roomCode+' - server crash!');
io.in(msg.roomCode).emit("crashedServer", {
playerIndex: msg.playerIndex,
pwAttempt: msg.pwAttempt,
attemptCount: msg.attemptCount,
result: msg.result
});
});
// An Employee -> ALL
socket.on("passwordSuccess", msg => {
console.log(msg.roomCode+' - password success');
io.in(msg.roomCode).emit("passwordSuccess", {
playerIndex: msg.playerIndex,
pwAttempt: msg.pwAttempt,
attemptCount: msg.attemptCount,
playerScore: msg.playerScore,
result: msg.result
});
});
// Any Player -> ALL
socket.on("roundOver", msg => {
console.log(msg.roomCode+' - round over');
io.in(msg.roomCode).emit("roundOver");
});
// SysAdmin -> ALL
socket.on("startNewRound", msg => {
console.log(msg.roomCode+' - new round started');
io.in(msg.roomCode).emit("startNewRound", {
playerIndex: msg.playerIndex,
players: msg.players,
summary: msg.summary
});
});
// Any Player -> Rest of Game
socket.on("passwordCracked", msg => {
console.log(msg.roomCode+' - password cracked');
io.to(msg.roomCode).emit("passwordCracked", {
players: msg.players,
allEmployeePasswords: msg.allEmployeePasswords,
crackSummary: msg.crackSummary
});
});
// Any Player -> ALL
socket.on("gameOver", msg => {
console.log(msg.roomCode+' - GAME OVER ⚰️');
io.in(msg.roomCode).emit("gameOver", {
playerIndex: msg.playerIndex,
passwordAttempts: msg.passwordAttempts
});
});
socket.on('disconnect', () => {
console.log("The socket "+socketID+ " disconnected.");
io.emit('clientDisconnect', socketID);
});
});
http.listen((process.env.PORT || 3000), () => {
console.log('listening on *:'+ (process.env.PORT || 3000));
});