-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
232 lines (163 loc) · 5.12 KB
/
users.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
var _ = require('underscore');
var UserHandler = function(io, ObjSet, timeout) {
timeout = timeout || 60;
var users = {};
var userDB = new ObjSet('user');
function cookieID(cookie) {
if (cookie) {
var values = cookie.split(";").map(function(value) { return value.trim().split("="); });
for (var i = 0; i < values.length; i++) {
var value = values[i];
if (value[0] === 'io') {
return value[1];
}
}
}
return;
}
function findUserAndThen(socket, cb) {
var newID = socket.id;
var oldID = cookieID(socket.handshake.headers.cookie);
if (users[newID]) {
console.log('found the same socket id: ' + newID)
if (cb)
cb(users[newID]);
} else if (oldID && users[oldID]) {
updateSocket(newID, oldID, cb);
} else { // Didn't find the user in memory, checking database
userFromDB(newID, oldID, function(user) {
users[newID].updateProp = userDB.makeUpdateProp(users[newID], newID);
users[newID] = user;
if (cb)
cb(users[newID]);
});
}
}
function updateSocket(newID, oldID, cb) {
console.log('updating socket for ' + users[oldID].name);
users[newID] = users[oldID];
deleteUser(oldID);
userDB.add(newID, users[newID], function() {
users[newID].updateProp = userDB.makeUpdateProp(users[newID], newID);
if (cb)
cb(users[newID]);
});
}
function newUser(id, cb) {
var dateAndTime = Date.now();
users[id] = {created: dateAndTime, name: "Anonymous"};
console.log('made new user with id ' + id);
userDB.add(id, users[id], function(err, reply) {
if (err) {
console.log(err);
} else if (cb) {
cb(users[id]);
}
});
}
function userFromDB(id, oldID, cb) {
userDB.get(id, function(err, user) {
if (err) {
console.log(err);
}
if (!user) { // Current ID not found in DB
if (oldID) { // User has a previous ID
userDB.get(oldID, function(err, user) {
if (err) {
console.log(err);
} else if (!user) { // User not in DB at all
newUser(id, cb);
} else { // User found under previous ID
users[oldID] = user;
updateSocket(id, oldID, cb);
}
});
} else { // No previous ID
console.log("Here!")
newUser(id, cb);
}
} else { // User found under current ID
users[id] = user;
if (cb)
cb(user);
}
});
}
function deleteUser(id) {
delete users[id];
userDB.delete(id);
}
this.makeGroup = function(name, userMessages, groupMethods) {
var nsp = name ? io.of('/' + name) : io;
var group = {
users: [],
sendAll: nsp.emit.bind(nsp),
emitNames: function() {
var userNames = this.users.map(function(user, index) {
return {id: user.num,
name: user.name};
});
this.sendAll('players', userNames);
console.log("Emitted " + userNames.length + " player name(s) to namespace " + name);
},
updateUsers: function() {
this.emitNames();
},
connected: function() {
return this.users;
},
userDisconnected: function(user) {
for (var i = 0; i < this.users.length; i++) {
if (this.users[i] === user) {
this.users.splice(i, 1);
break;
}
}
this.emitNames();
},
userDead: function(user) {},
clearUsers: function() {
this.users = [];
}
};
if (groupMethods) {
_.keys(groupMethods).forEach(function(methodName) {
group[methodName] = groupMethods[methodName](group);
});
}
nsp.on('connection', function(socket) {
var installUser = function(user) {
if (!_.find(group.users, function(groupUser) { return groupUser === user; })) {
// User isn't already in this group, so add it
group.users.push(user);
}
user.updateProp('connected', true);
socket.on('disconnect', function() {
user.updateProp('connected', false);
console.log("User(id=" + socket.id + ") disconnected - setting timeout for connection ");
group.userDisconnected(users[socket.id]);
setTimeout(function() {
if(!user.connected) {
group.userDead(users[socket.id]);
deleteUser(socket.id);
console.log("User with id " + socket.id + " deleted (timeout)");
}
}, 1000 * timeout)
});
if (userMessages) {
_.keys(userMessages).forEach(function(messageName) {
user[messageName] = userMessages[messageName](user, group);
socket.on(messageName, user[messageName]);
});
}
user.emit = socket.emit.bind(socket);
group.updateUsers();
};
findUserAndThen(socket, installUser);
});
return group;
};
}
module.exports = function(io, ObjSet, timeout) {
return new UserHandler(io, ObjSet, timeout);
};