-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
157 lines (130 loc) · 3.95 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
const db = require("./db");
const { uuid } = require('uuidv4');
function socket(io){
const rooms = {};
io.on("connection", socket => {
console.log("user connected", socket.id);
let curRoom = null;
socket.on("joinRoom", data => {
const { room } = data;
if (!rooms[room]) {
rooms[room] = {
name: room,
occupants: {},
};
}
const joinedTime = Date.now();
rooms[room].occupants[socket.id] = joinedTime;
curRoom = room;
console.log(`${socket.id} joined room ${room}`);
socket.join(room);
socket.emit("connectSuccess", { joinedTime });
const occupants = rooms[room].occupants;
io.in(curRoom).emit("occupantsChanged", { occupants });
});
socket.on("send", data => {
io.to(data.to).emit("send", data);
});
socket.on("broadcast", data => {
socket.to(curRoom).broadcast.emit("broadcast", data);
});
socket.on("disconnect", () => {
db.get("users")
.find({ name: socket.name })
.assign({ isPlaying: false })
.write();
console.log('disconnected: ', socket.id, curRoom);
if (rooms[curRoom]) {
console.log("user disconnected", socket.id);
delete rooms[curRoom].occupants[socket.id];
const occupants = rooms[curRoom].occupants;
socket.to(curRoom).broadcast.emit("occupantsChanged", { occupants });
if (occupants == {}) {
console.log("everybody left room");
delete rooms[curRoom];
}
}
});
socket.on("login", function(data) {
console.log(
`User attempting to login with name: ${data.name} and password: ${data.pw}`
);
let user = db.get("users").find({ name: data.name, pw: data.pw });
if (user.value()) {
if (user.value().isPlaying) {
socket.emit("login-results", {
success: false,
name: "cheater",
msg: "You are already logged in!"
});
console.log(
`${
user.value().name
} has provided valid login credentials but is already playing. :(`
);
return;
}
console.log(
`${user.value().name} has provided valid login credentials.`
);
socket.auth = true;
socket.name = data.name;
user.assign({ isPlaying: true }).write();
socket.dbid = user.value().id;
socket.emit("login-results", { success: true, name: data.name });
} else {
console.log(
"Failed login attempt. 30 seconds before socket is disconnected."
);
socket.emit("login-results", {
success: false,
msg: "Invalid credentials!"
});
setTimeout(() => {
if (!socket.auth) socket.disconnect(true);
}, 30000);
}
});
socket.on("add-user", function(data, cb) {
const adminKey = db
.get("users")
.find({ name: "admin" })
.value().pw;
if (data.key == adminKey) {
if (
db
.get("users")
.find({ name: data.name })
.value()
) {
let msg = "User cannot be added. Name already exists in database.";
console.log(msg);
if (typeof cb == "function") cb("fail");
return;
}
let result = db
.get("users")
.push({
id: uuid(),
name: data.name,
pw: data.pw,
isPlaying: false
})
.write();
if (result) {
let msg = "User successfully added to database";
console.log(msg);
if (typeof cb == "function") cb("success");
} else {
let msg = "Error adding user to database";
console.log(msg);
if (typeof cb == "function") cb("fail");
}
} else {
socket.emit("log", "Invalid key submitted.");
if (typeof cb == "function") cb("fail");
}
});
});
}
module.exports = socket