-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
422 lines (390 loc) · 14.9 KB
/
index.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// express stuffs
const express = require("express");
const app = express();
// server stuffs
const http = require("http");
const server = http.createServer(app);
const port = process.env.PORT || 3000;
// io stuffs
const socketio = require("socket.io");
const io = socketio(server);
// util stuffs
const path = require("path");
const moment = require("moment");
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.get("/time/:time", (req, res)=>{
maxTime = req.params.time;
res.send(`Maximum time set to ${maxTime}`);
});
app.get("/roles/", (req, res)=>{
rolesURL = req.query.names.split(';');
roles = rolesURL;
let result = "Roles set to ";
roles.forEach((role)=>{result+=role + " "});
res.send(result);
});
class User{
constructor(username, password, role, status){
this.id = -1;
this.username = username;
this.password = password;
this.role = role;
this.status = status;
this.vote = 0;
this.werewolfVote = 0;
this.protected = false;
}
}
// const users = [new User("Murat", "12345", "Villager", 1),
// new User("Ahmet", "6789", "Werewolf", 1),
// new User("Mehmet", "asdasd", "Doctor", 1),
// new User("Kardelen", "asdasd", "Werewolf", 1),
// new User("Musa", "asdasd", "Werewolf", 1),
// new User("Alper", "asdasd", "Villager", 1),
// new User("Can", "asdasd", "Villager", 1)];
let users = [];
let usersChat = {All:[], Werewolf:[]};
let readyGame = false;
let time = 0;
let status = "Day";
let rolesURL;
let roles = ["Villager","Doctor","Veteran","Villager","Werewolf","Werewolf","Werewolf", "Lookout"];
let gameFinished = false;
let winner = "";
let gameLoop;
let maxTime = 10;
io.on("connection",(socket)=>{
//console.log("Someone connected with id:" + socket.id );
socket.on("userJoined",(newUserSocket)=>{
if(readyGame){
//socket.emit("error","You cannot join, game is already started.");
}else{
//console.log("User joined");
let existingUser = undefined;
users.forEach((user)=>{
if(user.username === newUserSocket.player.username)
existingUser = user;
});
if(existingUser == undefined){
newUserSocket.player.id = socket.id;
newUserSocket.player.role = randomRole();
users.push(newUserSocket.player);
io.emit("current_users", {users});
socket.emit("role_change", newUserSocket.player.role);
}else{
socket.id = newUserSocket.player.id;
}
// MESSAGE CHAT
//get user and send it to room
//socket.join(newUserSocket.player.role);
socket.join("All");
//console.log(newUserSocket.player.role);
//console.log(newUserSocket.player.role == "Werewolf");
if(newUserSocket.player.role == "Werewolf"){
socket.join("Werewolf");
usersChat.Werewolf.push(newUserSocket.player.username);
io.to("Werewolf").emit("users", usersChat.Werewolf);
}
usersChat.All.push(newUserSocket.player.username);
io.to("All").emit("users", usersChat.All);
socket.emit("messageD", createMessage("Server", `Welcome to chat room ${newUserSocket.player.username}.`, 0));
socket.on("messageD", (socket3)=>{
io.to("All").emit("messageD", createMessage(socket3.username, socket3.message,1));
});
socket.on("messageN", (socket3)=>{
//console.log(`Gece mesaji ${socket3.username} mesaj: ${socket3.message}`);
//console.log(usersChat);
io.to("Werewolf").emit("messageN", createMessage(socket3.username, socket3.message,1));
});
socket.on("messageS", (socket3)=>{
if(socket3.type == 0){
// system to everyone
io.to("All").emit("messageD", createMessage("System", socket3.message, 1));
}else if(socket3.type == 1){
// system to socket3.to
if(socket3.to == "Werewolf"){
io.to("Werewolf").emit("messageD", createMessage("System", socket3.message, 1));
}else if(socket3.to == "Villager"){
io.to("All").emit("messageD", createMessage("System", socket3.message, 1));
}else{
// to a person
socket.emit("messageD", createMessage("System", socket3.message, 1));
}
}
});
//socket.broadcast.to(newUserSocket.player.role).emit("message", createMessage("Server", `${newUserSocket.player.username} joined chat!`, 0));
socket.on("disconnect", (socket4)=>{
if(newUserSocket.player.role == "Werewolf"){
usersChat.Werewolf = usersChat.Werewolf.filter(item => item !== newUserSocket.player.username);
io.to("Werewolf").emit("users", usersChat.Werewolf);
}else{
usersChat.All = usersChat.All.filter(item => item !== newUserSocket.player.username);
io.to("All").emit("users", usersChat.All);
}
});
// CHAT END
}
//console.log(readyGame);
});
socket.on("userReady",(readyUserSocket)=>{
users.forEach((user)=>{
if(user.username == readyUserSocket.player.username){
//console.log(`User ${user.username} status changed to ${readyUserSocket.player.status}`);
user.status = readyUserSocket.player.status;
}
});
let isOk = true;
users.forEach((user)=>{
//console.log(`User ${user.username} status is ${user.status}`);
if(user.status == 0){
isOk = false;
}
});
if(isOk) readyGame = true; else readyGame = false;
if(readyGame){
gameLoop = setInterval(()=>{
if(time == "0"){
if(status == "Day"){
status = "Vote";
}else if(status == "Vote"){
killMostVoted();
status = "Night";
}else if(status == "Night"){
killMostVoted();
status = "Day";
}
time = maxTime;
}else{
time = time - 1;
}
//console.log(`Time:${time}`);
//console.log(`Status:${status}`);
io.emit("updateGame", {time, status});
},1000);
}
io.emit("gameStarted", readyGame);
//console.log(readyGame);
});
socket.on("finishGameS",()=>{
console.log("game finished");
users = [];
usersChat = {All:[], Werewolf:[]};
readyGame = false;
time = 0;
status = "Day";
if(rolesURL == undefined)
roles = ["Villager","Doctor","Veteran","Villager","Werewolf","Werewolf","Werewolf", "Lookout"];
else
roles = rolesURL;
gameFinished = false;
winner = "";
clearInterval(gameLoop);
})
socket.on("vote_update_user",(afterVoteSocket)=>{
newUser = afterVoteSocket.user;
users.forEach((user)=>{
if(user.username == newUser.username){
user.vote = newUser.vote;
user.werewolfVote = newUser.werewolfVote;
}
});
io.emit("current_users", {users});
});
socket.on("doctor_update_user",(afterVoteSocket)=>{
newUser = afterVoteSocket.user;
users.forEach((user)=>{
if(user.username == newUser.username){
user.protected = newUser.protected;
if(user.role == "Veteran")
youDied = true;
}
});
if(youDied){
let killedUser = afterVoteSocket.player;
let selectedUserId;
users.forEach((user, index)=>{
if(user.username == killedUser.username)
selectedUserId = index;
});
users.splice(selectedUserId, 1);
voteClear();
io.emit("after_killed_users", {users,killedUser, type:0});
}else{
io.emit("current_users", {users});
}
});
socket.on("veteran_update_user",(afterVoteSocket)=>{
newUser = afterVoteSocket.player;
users.forEach((user)=>{
if(user.username == newUser.username){
user.protected = newUser.protected;
}
});
io.emit("current_users", {users});
});
socket.on("lookout_update_user",(afterVoteSocket)=>{
newUser = afterVoteSocket.user;
let selectedRole;
let youDied = false;
users.forEach((user)=>{
if(user.username == newUser.username){
selectedRole = user.role;
if(user.role == "Veteran")
youDied = true;
}
});
if(youDied){
let killedUser = afterVoteSocket.player;
let selectedUserId;
users.forEach((user, index)=>{
if(user.username == killedUser.username)
selectedUserId = index;
});
users.splice(selectedUserId, 1);
voteClear();
io.emit("after_killed_users", {users,killedUser, type:0});
}else{
socket.emit("messageD", createMessage("System", "The role of "+newUser.username + " is " + selectedRole, 1));
io.emit("current_users", {users});
}
});
socket.on("disconnect",(exitedUser)=>{
users.forEach((value, index)=>{
if(value.id == socket.id){
users.splice(index, 1);
}
});
//console.log(users);
io.emit("current_users", {users});
});
});
app.use(express.static(path.join(__dirname, "public")));
server.listen(port, ()=> console.log("Server established on port " + port));
function voteClear(){
users.forEach((user)=>{
user.werewolfVote = 0;
user.vote = 0;
user.protected = false;
});
}
function killMostVoted() {
let max = 0;
let selectedUserId;
if(status == "Vote"){
users.forEach((user, index) => {
if(max < user.vote){
selectedUserId = index;
max = user.vote;
}else if(max = user.vote){
selectedUserId = undefined;
max = user.vote;
}
});
if(selectedUserId != undefined){
//document.querySelector("button#"+users[selectedUserId].username).remove();
//if(player.username == users[selectedUserId]){
// userKilled = true;
//}
//console.log(`User ${users[selectedUserId].username} killed with votes`);
let killedUser = users[selectedUserId];
users.splice(selectedUserId, 1);
voteClear();
io.emit("after_killed_users", {users,killedUser, type:1});
}else{
voteClear();
io.to("All").emit("messageD", createMessage("System", "No one hanged because of equality", 1));
io.emit("current_users",{users});
}
}else if(status == "Night"){
//console.log(users);
users.forEach((user, index) => {
if(max < user.werewolfVote){
selectedUserId = index;
max = user.werewolfVote;
}else if(max = user.werewolfVote){
selectedUserId = undefined;
max = user.werewolfVote;
}
});
//console.log(selectedUserId);
if(selectedUserId != undefined){
if(!users[selectedUserId].protected){
// document.querySelector("button#"+users[selectedUserId].username).remove();
// if(player.username == users[selectedUserId]){
// killUser();
// }
let killedUser = users[selectedUserId];
users.splice(selectedUserId, 1);
// console.log("AFTER DEATH USERS");
voteClear();
io.emit("after_killed_users", {users,killedUser, type:0});
}else{
if(users[selectedUserId].role == "Veteran"){
users.forEach((user, index)=>{
if(user.role == "Werewolf")
selectedUserId = index;
});
if(selectedUserId != undefined){
let killedUser = users[selectedUserId];
users.splice(selectedUserId, 1);
// console.log("AFTER DEATH USERS");
voteClear();
io.emit("after_killed_users", {users,killedUser, type:0});
}else{
voteClear();
io.to("All").emit("messageD", createMessage("System", "There is no death, interesting...", 1));
io.emit("current_users",{users});
}
}else{
voteClear();
io.to("All").emit("messageD", createMessage("System", "There is no death, interesting...", 1));
io.emit("current_users",{users});
}
}
}else{
voteClear();
io.to("All").emit("messageD", createMessage("System", "There is no death, interesting...", 1));
io.emit("current_users",{users});
}
}
// check win after every kill attempt
let villageWin = true;
let werewolfWin = true;
users.forEach((user) =>{
if(user.role == "Villager" || user.role == "Doctor" || user.role == "Lookout"){
werewolfWin = false;
}else if(user.role == "Werewolf"){
villageWin = false;
}
});
//console.log(villageWin);
//console.log(werewolfWin);
if(villageWin && !werewolfWin){
gameFinished = true;
winner = "Villager";
io.emit("finishGame", winner);
clearInterval(gameLoop);
}else if(!villageWin && werewolfWin){
gameFinished = true;
winner = "Werewolf";
io.emit("finishGame", winner);
}else{
gameFinished = false;
}
}
function randomRole(){
let randomIndex = Math.floor(Math.random() * roles.length);
let randomString = roles[randomIndex];
roles.splice(randomIndex, 1);
//console.log(roles);
return randomString;
}
/////////// CHAT //////////////
function createMessage(username, message, type){
return {username:username,
message: message,
date: moment().format("LT"),
type: type};
}