This repository was archived by the owner on Jun 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
296 lines (245 loc) · 7.6 KB
/
Copy pathindex.js
File metadata and controls
296 lines (245 loc) · 7.6 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
function Round() {
this.placeA; // yelp IDs (or similar)
this.placeB;
this.votes = {}; // array of objects, each object is user and their vote
// return either placeA's Yelp ID or placeB's Yelp Id whichever side win
this.getWinner = function () {
var sum = 0;
for (var key in this.votes) {
sum += this.votes[key].score;
}
if (sum == 0) return 0;
if (sum > 0) return 1
if (sum < 0) return -1;
}
}
function User(socketId, name) {
this.socketId = socketId;
this.userName = name;
}
function Room() {
this.roomId = guid();
this.roomName;
this.rounds = [];
this.users = {};
this.radius;
this.center;
this.time;
this.options = []; // array of all places in consideration, initialized with seed places from client
this.keep = [];
}
var rooms = {};
io.on('connection', function (socket) {
var user = null;
var room = null;
/*
A client enters the room.
{
room_name: '' ;; the room name that a client intends to join/create
user_name: '' ;; the client user's name
}
specify a call back:
function callback(roomExists: boolean) { ... }
*/
socket.on('join room', function (data) {
var roomName = data.room_name;
var userName = data.user_name;
console.log("user " + userName + " wish to join '"+ roomName + "'");
// check if either is empty
if (!roomName || !userName)
return;
user = new User(socket.id, userName);
// Case: roomName exists
if (rooms[roomName] != null) {
room = rooms[roomName];
socket.join(room.roomName);
// emits to this socket when room is joined
// {
// room: {} ; current room
// }
console.log("emit joined room message");
socket.emit('joined room', { room: room });
room.users[user.socketId] = user;
// emits to all in this room
// {
// new_user: {}
// all_users: []
// }
console.log("emit user joined room message");
setTimeout(function () {
io.to(room.roomName).emit('user joined room', {
new_user: user,
all_users: room.users
});
},1000);
}
else
// Case: roomName does not exists
{
console.log("emit room available message");
socket.emit('room available', { room_name: roomName });
}
});
/*
should only ever be called if 'room available' is emitted to client
to prevent multiple rooms with same name
{
room_name: '' ; the room's name
radius: '' ; radius around center
center: ''
time: '' ; time of the meal
options: [] ; the array of Yelp IDs as search results done by client
}
emits "room created" event when done
*/
socket.on('creator setup', function(data) {
if (rooms[data.roomName] != null) return;
if (!user) return;
room = new Room();
room.roomName = data.room_name;
room.radius = data.radius;
room.center = data.center;
room.time = data.time;
room.options = data.options;
room.users[user.socketId] = user;
rooms[data.room_name] = room;
socket.join(room.roomName);
socket.emit('room created', { room: room });
console.log("emit user joined room message");
setTimeout(function () {
io.to(room.roomName).emit('user joined room', {
new_user: user,
all_users: room.users
});
},1000);
});
/*
Used to add locations specified by users.
TODO: add in logic to remove already existing default Yelp ID in
place of the user's choices.
{
locations: [] ; the array of Yelp IDs user's choice
}
*/
socket.on('add locations', function(data) {
if (!room || !user) return;
room.options = room.options.concat(data.locations);
});
socket.on('creator start', function (data) {
if (!room || !user) return;
/*
should notify all users in the room that voting begins.
emit the current room informations (with new Yelp IDs)
*/
io.to(room.roomName).emit('voting start', {room: room, yelpResults: data.yelpResults});
// create a new Round and sends to user:
var round = new Round();
// round's place A should be the first item in options
round.placeA = room.options[0];
round.placeB = room.options[1];
room.rounds.push(round);
/*
sends the first round information:
{
roundNum: 0 ; this is the first round
this_round: rounds
}
*/
io.to(room.roomName).emit('new round', { roundNum: 0, this_round: round });
// user should now vote...
});
/*
{
roundNum: x ; x = current round (0-based)
score: y ; y = score given by user, -5 is in favor of A, +5 is in favor of B.
}
*/
socket.on('register vote', function (data) {
if (!room || !user) return;
var roundNum = data.roundNum;
var score = data.score;
// user voted on a round number not on this round..
if (roundNum + 1 != room.rounds.length) return;
// record vote in this round's object:
var round = room.rounds[roundNum];
if (!round) return;
round.votes[user.socketId] = {
user: user,
score: score
};
io.to(room.roomName).emit('user voted', {
user: user,
round: round
});
// check if all users have voted
if (Object.keys(room.users).length == Object.keys(round.votes).length) {
// winner/ -1 A wins +1 B wins 0 ties
var winner = round.getWinner();
var indexOfB = room.options.indexOf(round.placeB);
var indexOfA = room.options.indexOf(round.placeA);
io.to(room.roomName).emit('round ended', {
result: winner,
round: round
});
if (winner == -1) {
console.log("A wins last time");
// remove placeB from options
room.options.splice(indexOfB, 1);
} else if (winner == 1) {
console.log("B wins last time");
room.options.splice(indexOfA, 1);
} else {
console.log("Ties last time");
room.keep.push(round.placeB);
room.options.splice(indexOfB, 1);
}
if (room.options.length < 2) {
var winningPlaces = room.options.concat(room.keep);
io.to(room.roomName).emit('voting end', {
rounds: room.rounds,
winners: winningPlaces
});
return;
}
var nextRound = new Round();
nextRound.placeA = room.options[indexOfA];
nextRound.placeB = room.options[indexOfA + 1];
room.rounds.push(nextRound);
// emit new round
io.to(room.roomName).emit('new round', { roundNum: roundNum + 1, this_round: nextRound });
}
});
socket.on('disconnect', function () {
console.log("user disconnected");
if (!user || !room)
return;
if (room.users[socket.id]) {
delete room.users[socket.id];
console.log("user disconnect, delete from room users list, assert(null):");
console.log(room.users[socket.id]);
}
// if no more people in room, delete room
if (Object.keys(rooms[room.roomName].users).length <= 0) {
console.log("room " + room.roomName + " will be deleted");
delete rooms[room.roomName];
}
});
console.log("a user connected");
});
/// Utility functions
function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
}
server.listen(app.get('port'));