-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (89 loc) · 3.12 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
const path = require("path");
const sleep = require("sleep");
const http = require("http");
const express = require("express");
const socketIO = require("socket.io");
var connections = [];
const { generateMessage, generateLocationMessage } = require("./utils/message");
const { isRealString } = require("./utils/validation");
const { Users } = require("./utils/users");
const publicPath = path.join(__dirname, "./public");
const port = process.env.PORT || 3000;
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
var users = new Users();
app.use(express.static(publicPath));
io.on("connection", socket => {
socket.on("getrooms", () => {
let listofrooms = users.getRoomList();
socket.emit("return", listofrooms);
});
connections.push(socket.id);
socket.on("join", (params, callback) => {
if (!isRealString(params.name) || !isRealString(params.room)) {
return callback("Name and room name are required.");
} else {
socket.join(params.room);
users.removeUser(socket.id);
if (users.getUserName(params.name, params.room) === 1) {
return callback("sorry, room has someone with this name already!");
}
if (params.avatar) {
users.addUser(socket.id, params.name, params.room, params.avatar);
} else {
users.addUser(socket.id, params.name, params.room);
}
if (users.getUserSocketList(params.room).length >= 3) {
return callback("sorry, room has 2 players already");
}
}
io.in(params.room).emit("users", users.getUserList(params.room));
socket.on("ready", data => {
io.in(params.room).emit("users", users.getUserList(params.room));
socket.to(params.room).emit("ready", data);
});
socket.on("forcereload", () => {
io.in(params.room).emit("users", users.UserList(params.room));
});
if (users.getUserSocketList(params.room).length === 2) {
io.to(params.room).emit("start");
}
socket.on("dotsdata", data => {
socket.to(params.room).emit("opponentcircles", data);
});
socket.on("mouseLocation", data => {
socket.to(params.room).emit("mouselocation", data);
});
socket.on("clockended", data => {
socket.to(params.room).emit("gamewinner", data);
});
socket.on("score", score => {
socket.to(params.room).emit("score", score);
});
socket.on("hoveredVS", () => {
io.in(params.room).emit("hoveredVS");
});
socket.on("getuseravatar", data => {
avatar1 = users.getUserAvatar(data.name1, data.room);
avatar2 = users.getUserAvatar(data.name2, data.room);
socket.emit("getuseravatar", { avatar1: avatar1, avatar2: avatar2 });
});
socket.on("leftVS", () => {
io.in(params.room).emit("leftVS");
});
socket.on("winner", name => {
io.in(params.room).emit("winner", name);
});
socket.on("showSnackbar", dat => {
io.emit("showSnackbar", dat);
});
socket.on("disconnect", () => {
var user = users.removeUser(socket.id);
io.in(params.room).emit("clearlobby");
});
});
});
server.listen(port, () => {
console.log(`Server is up on ${port}`);
});