-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappUsers.js
186 lines (185 loc) · 6.38 KB
/
appUsers.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
//NOTE: Tracking users to make sure they are only able to join one lobby/game
const User = require('../models/user');
const roomUtils = require('./appRooms');
const constants = require('./appConstants');
const users = [];
//Join user to chat
async function userJoin(socketId, username, room) {
try {
console.log('users.js - userJoin----------------------------------------');
//TODO: need to find a way to see if user is already in room
// console.log('userJoin-------------------------------------');
const usersInRoom = getUsersInRoom(room);
// console.log('usersInRoom before =', usersInRoom);
let canEnter = true;
for (let i = 0; i < usersInRoom.length; i++) {
const user = usersInRoom[i];
if (user.username === username) {
canEnter = false;
}
}
// console.log('canEnter =', canEnter);
if (canEnter){
try {
const foundUser = await User.findOne({username});
if (foundUser) {
const user = constants.getNewUser(socketId, username, room, foundUser.preferences);
if (!users.includes(user)){
users.push(user);
}
return user;
}
console.log('No user found in userJoin in userutils');
return null;
} catch (error) {
console.log("Error finding user in Database");
return null;
}
}
else {
console.log('need to do something here to remove users room');
// console.log('socket id predicted: someValue; actual =', socketId);
//get the name of the room the user is supposedly in
const roomUserIsIn = roomUtils.getRoom(room);
if (!roomUserIsIn) {
const userObj = getUser(socketId);
if (!userObj) return null;
// console.log(`removing ${userObj.username} from ${userObj.room}`);
userObj.room = "";
}
else {
if (roomUserIsIn.users.length <= 0) {
roomUtils.removeRoom(roomUserIsIn.name);
// console.log('removing as is empty room name =', roomUserIsIn.name);
}
}
}
return null;
} catch (error) {
console.error('error =', error);
}
}
function getUser(socketId) {
try {
console.log('users.js - getUser----------------------------------------');
const foundUser = users.find(user => user.socketId === socketId);
// console.log('foundUser =', foundUser);
if (foundUser) return foundUser;
return null;
} catch (error) {
console.error('error =', error);
}
}
function getUserObj (usernameFromOutside) {
try {
console.log('users.js - getUserObj----------------------------------------');
if (!usernameFromOutside) return null;
const index = users.findIndex(user => {
console.log(user.username === usernameFromOutside);
return user.username === usernameFromOutside;
});
if (index !== -1) return users[index];
return null;
} catch (error) {
console.error('error =', error);
}
}
function getUserId(username) {
try {
console.log('users.js - getUserId----------------------------------------');
const foundUser = users.find(user => user.username === username);
if (foundUser) {
return foundUser.socketId;
}
return -1;
} catch (error) {
console.error('error =', error);
}
}
function getAllUsers(){
console.log('users.js - getAllUsers----------------------------------------');
return users;
}
function removeUser(socketId){
try {
console.log('users.js - removeUser----------------------------------------');
const index = users.findIndex(user => user.socketId === socketId);
if (index !== -1) {
return users.splice(index, 1)[0];
}
} catch (error) {
console.error('error =', error);
}
}
function getUsersInRoom(room) {
try {
console.log(`users.js - getUsersInRoom ${room}----------------------------------------`);
const usersToReturn = users.filter(user => user.room === room);
return usersToReturn;
} catch (error) {
console.error('error =', error);
}
}
function getUsernamesOfUsersInRoom(room) {
try {
console.log('users.js - getUsernamesOfUsersInRoom----------------------------------------');
if (!room) return null;
const userObjs = getUsersInRoom(room);
// console.log('userObjs =', userObjs);
return userObjs.reduce((arrayOfUsernames, userObj) => {
if (!arrayOfUsernames.includes(userObj.username)) arrayOfUsernames.push(userObj.username);
// console.log('arrayOfUsernames =', arrayOfUsernames);
return arrayOfUsernames;
}, []);
} catch (error) {
console.error('error =', error);
}
}
async function isRegisteredUser(username) {
console.log('users.js - isRegisteredUser----------------------------------------');
try {
const foundUser = await User.findOne({username});
if (foundUser) return true;
} catch (error) {
return -1;
}
return false;
}
function addUserObj(userObj) {
try {
console.log('users.js - addUserObj----------------------------------------');
if (!userObj) return null;
if (!users.includes(userObj)) {
users.push(userObj);
// console.log('adding UserObj-----------------------------');
}
} catch (error) {
console.error('error =', error);
}
}
function updateStatusOfUsers(usernames, status) {
try {
console.log('users.js - updateStatusOfUsers----------------------------------------');
if (!usernames || !status) return;
usernames.forEach(username => {
const userObj = getUserObj(username);
userObj.status = status;
});
} catch (error) {
console.error('error =', error);
}
}
module.exports = {
userJoin,
getUserId,
getUser,
removeUser,
getRoomUsers: getUsersInRoom,
getAllUsers,
isRegisteredUser,
addUserObj,
getUsernamesOfUsersInRoom,
getUserObj,
updateStatusOfUsers,
users,
}