-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery.js
55 lines (45 loc) · 1.05 KB
/
battery.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
'use strict';
var _ = require('underscore');
//Easy aliasing
var io;
var battery;
var IO = {
//Stored array of users
currentUsers: [],
//Update a user
userChanged: function(data){
_.each(IO.currentUsers, function(v, i) {
if (v.id === data.id) {
IO.currentUsers[i].battery[data.type] = data.value;
}
});
IO.usersUpdated();
},
//Add a user
userJoined: function(data){
IO.currentUsers.push(data);
IO.usersUpdated();
},
//Remove a user
userQuit: function(data){
_.each(IO.currentUsers, function(v, i) {
if (v.id === data.id) {
IO.currentUsers.splice(i, 1);
}
});
IO.usersUpdated();
},
//Trigger an update
usersUpdated: function() {
io.emit('usersUpdated', IO.currentUsers);
}
};
exports.init = function(sio, socket) {
io = sio;
battery = socket;
battery.on('userChanged', IO.userChanged);
battery.on('userJoined', IO.userJoined);
battery.on('userQuit', IO.userQuit);
//Kick it off with the current users
io.emit('playersUpdated', IO.currentUsers);
};