forked from isadorabk/werewolf-back-end
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocket.js
216 lines (203 loc) · 7.93 KB
/
socket.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
const IO = require('socket.io');
const chalk = require('chalk');
const Game = require('./models/game');
module.exports = (server) => {
io = IO(server);
io.on('connection', (socket) => {
// eslint-disable-next-line
console.log(chalk.bgGreen('User connected', socket.id));
socket.on('disconnect', () => {
//if admin disconnect --> game.resetgamerunning
// eslint-disable-next-line
console.log(chalk.bgRed('User disconnected', socket.id));
});
socket.on('createGame', (gameId, adminCode) => {
Game.setAdmin(gameId, adminCode, socket);
socket.join(gameId);
// eslint-disable-next-line
console.log(chalk.green(Game.get(gameId).log()));
});
socket.on('joinGame', (gameCode, userId) => {
if (!gameCode) return;
const game = Game.get(gameCode);
if (!game) return;
const players = game.players;
socket.join(gameCode);
if (players && players[userId]) {
Game.updatePlayerSocket(gameCode, userId, socket);
let { socket: _, ...playerInfo } = players[userId];
let ended;
const started = game.started;
if (started) ended = Game.checkGameFinished(gameCode);
const info = { playerInfo, started, ended };
io.to(players[userId].socket.id).emit('gameCommand', 'playerInfo', info);
io.to(players[userId].socket.id).emit('gameCommand', 'updateRound', game.round);
} else {
const player = Game.addPlayer(gameCode, userId, socket);
let { socket: _, ...playerData } = player;
socket.to(gameCode).emit('gameCommand', 'playerCreated', playerData);
}
});
socket.on('retrieveGame', (gameId) => {
const game = Game.get(gameId);
const started = game.started;
let ended;
if (started) ended = Game.checkGameFinished(gameId);
const players = game.players;
let playersList = [];
let werewolves = [];
let specialRoles = [];
let villagers = [];
for (let id in players) {
if (players.hasOwnProperty(id)) {
let { socket: _, ...playerInfo } = players[id];
playersList.push(playerInfo);
if (playerInfo.role === 'werewolf') werewolves.push(playerInfo);
if (playerInfo.role !== 'werewolf' && playerInfo.role !== 'villager') specialRoles.push(playerInfo);
if (playerInfo.role === 'villager') villagers.push(playerInfo);
}
}
const allPlayers = {
werewolves,
specialRoles,
villagers,
playersList,
started,
ended,
};
io.to(game.admin.id).emit('gameCommand','retrieveGame',allPlayers);
io.to(game.admin.id).emit('gameCommand', 'updateRound', game.round);
});
socket.on('startGame', (gameId) => {
Game.assignRoles(gameId);
const game = Game.get(gameId);
const players = game.players;
Game.setStarted(gameId);
let werewolves = [];
let specialRoles = [];
let villagers = [];
for (let id in players) {
if (players.hasOwnProperty(id)) {
let { socket: _, ...playerInfo } = players[id];
const started = game.started;
const info = { playerInfo, started };
io.to(players[id].socket.id).emit('gameCommand', 'playerInfo', info);
if (playerInfo.role === 'werewolf') werewolves.push(playerInfo);
if (playerInfo.role !== 'werewolf' && playerInfo.role !== 'villager') specialRoles.push(playerInfo);
if (playerInfo.role === 'villager') villagers.push(playerInfo);
}
}
const allPlayers = {
werewolves,
specialRoles,
villagers
};
io.to(game.admin.id).emit('gameCommand', 'playersList', allPlayers);
// eslint-disable-next-line
console.log(chalk.bgGreen('Game started: ', gameId));
});
socket.on('startRound', (gameId, round) => {
const adminId = Game.get(gameId).admin.id;
if (socket.id === adminId) {
// eslint-disable-next-line
if (round === 'day') console.log(chalk.bgYellow('Day round: ', gameId));
// eslint-disable-next-line
if (round === 'night') console.log(chalk.bgBlue('Night round: ', gameId));
Game.startRound(gameId, round);
io.in(gameId).emit('gameCommand', 'updateRound', round);
}
});
socket.on('killPlayer', (gameId, playerId) => {
const adminId = Game.get(gameId).admin.id;
if (socket.id === adminId) {
const playerKilled = Game.killPlayer(gameId, playerId);
const game = Game.get(gameId);
let {
socket: _,
...playerInfo
} = playerKilled;
io.to(playerKilled.socket.id).emit('gameCommand', 'updateLifeStatus', playerKilled.lifeStatus);
io.to(game.admin.id).emit('gameCommand', 'updateLifeStatus', playerInfo);
// eslint-disable-next-line
console.log(chalk.bgMagenta('Kill player: ', playerId));
const gameFinished = Game.checkGameFinished(gameId);
if (gameFinished) {
const players = Game.get(gameId).players;
// let playersAlive = [];
let werewolves = [];
let specialRoles = [];
let villagers = [];
for (let id in players) {
if (players.hasOwnProperty(id)) {
let { socket: _, ...playerInfo } = players[id];
if (playerInfo.role === 'werewolf') werewolves.push(playerInfo);
if (playerInfo.role !== 'werewolf' && playerInfo.role !== 'villager') specialRoles.push(playerInfo);
if (playerInfo.role === 'villager') villagers.push(playerInfo);
// if (players[id].lifeStatus === 'alive') {
// playersAlive.push(playerInfo);
io.to(players[id].socket.id).emit('gameCommand', 'gameEnd', playerInfo);
// }
}
}
const allPlayers = { werewolves, specialRoles, villagers };
io.to(game.admin.id).emit('gameCommand', 'gameEnd', allPlayers);
// eslint-disable-next-line
console.log(chalk.bgRed('Game finished: ', gameId));
}
}
});
socket.on('startVote', gameId => {
const adminId = Game.get(gameId).admin.id;
if (socket.id === adminId) {
const players = Game.get(gameId).players;
const playersAlive = [];
for (let id in players) {
if (players.hasOwnProperty(id)) {
const { socket: _, ...playerInfo } = players[id];
if (playerInfo.lifeStatus === 'alive') {
playersAlive.push(playerInfo);
}
}
}
socket.to(gameId).emit('gameCommand', 'startVote', playersAlive);
}
});
socket.on('finishVote', gameId => {
const adminId = Game.get(gameId).admin.id;
if (socket.id === adminId) {
socket.to(gameId).emit('gameCommand', 'finishVote', false);
const players = Game.get(gameId).players;
for (let id in players) {
if (players.hasOwnProperty(id)) {
players[id].votes = 0;
}
}
}
});
socket.on('voteToKill', (gameId, playerId, voterId) => {
const voter = Game.get(gameId).players[voterId];
if (voter.lifeStatus === 'alive') {
const game = Game.get(gameId);
const players = game.players;
players[playerId].votes ++;
const werewolves = [];
const specialRoles = [];
const villagers = [];
for (let id in players) {
if (players.hasOwnProperty(id)) {
let { socket: _, ...playerInfo } = players[id];
if (playerInfo.role === 'werewolf') werewolves.push(playerInfo);
if (playerInfo.role !== 'werewolf' && playerInfo.role !== 'villager') specialRoles.push(playerInfo);
if (playerInfo.role === 'villager') villagers.push(playerInfo);
}
}
const allPlayers = {
werewolves,
specialRoles,
villagers
};
io.to(game.admin.id).emit('gameCommand', 'updateVotes', allPlayers);
}
});
});
};