-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.js
130 lines (110 loc) · 3.19 KB
/
broker.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
function Broker(socket) {
var serverSocket = socket;
var session = {};
this.init = function() {
serverSocket.on('connection', function(clientSocket) {
var room;
var user;
var isHost;
clientSocket.on('host', function(roomName) {
isHost = true;
room = roomName;
session[room] = {roomName: room, users: {}};
clientSocket.join(room);
clientSocket.emit('update', session[room]);
});
clientSocket.on('end host', function() {
killRoom();
});
clientSocket.on('join', function(data) {
isHost = false;
room = data.room;
user = data.user;
if (session[room] === undefined) {
clientSocket.emit('join fail');
} else {
session[room].users[user] = -1;
clientSocket.join(room);
clientSocket.emit('join success');
serverSocket.to(room).emit('update', session[room]);
}
});
clientSocket.on('request estimate', function(data) {
var users = Object.keys(session[room].users);
for (var i = 0; i < users.length; i ++) {
session[room].users[users[i]] = -1;
}
serverSocket.to(room).emit('request estimate', data);
serverSocket.to(room).emit('update', session[room]);
});
clientSocket.on('estimate', function(estimate) {
session[room].users[user] = estimate;
serverSocket.to(room).emit('update', session[room]);
var estimates = Object.keys(session[room].users).map(function(key) {
return session[room].users[key];
});
if (hasAllEstimates(estimates)) {
serverSocket.to(room).emit('estimate complete', getAverageEstimate(estimates));
}
});
clientSocket.on('disconnect', function() {
if (isHost) {
killRoom();
} else {
if (session[room] !== undefined) {
delete session[room].users[user];
}
serverSocket.to(room).emit('update', session[room]);
clientSocket.leave(room);
}
});
clientSocket.on('ping', function() {
clientSocket.emit('ping');
});
function killRoom() {
delete session[room];
serverSocket.to(room).emit('kick out');
clientSocket.leave(room);
}
});
};
this.createRoom = function(room) {
if (session[room] === undefined) {
session[room] = {};
}
};
this.getSession = function() {
return session;
};
function hasAllEstimates(estimates) {
for (var i = 0; i < estimates.length; i ++) {
if (estimates[i] == -1) {
return false;
}
}
return true;
}
function getAverageEstimate(estimates) {
var map = {};
var maxCount = 1;
for (var i = 0; i < estimates.length; i ++) {
var estimate = estimates[i];
if (map[estimate] == undefined) {
map[estimate] = 1;
} else {
map[estimate]++;
if (map[estimate] > maxCount) {
maxCount = map[estimate];
}
}
}
var mode = -1;
for (var key in map) {
if (map[key] == maxCount && key > mode) {
mode = key;
}
}
return mode;
}
};
module.exports = Broker;