-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
586 lines (549 loc) · 17 KB
/
server.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// includes
var express = require('express');
var http = require('http');
var socket_io = require('socket.io');
var fs = require('fs');
/*date*/ require('datejs');
var mt = require('mersenne');
var _ = require('underscore');
// bootstrap
var app = express();
var server = http.createServer( app );
var io = socket_io.listen( server );
// static configs
io.set( 'log level', 1 ); // 0 - 4, ascending verbosity
app.use( express.compress() ); // gzip compression
mt = new mt.MersenneTwister19937;
// config-file config
var cfg = JSON.parse( fs.readFileSync( 'app.cfg.json', 'utf8' ));
// // route utility
// function primary_host_check( request, response ) {
// if( request.headers.host == 'www.lunchvote.net' // normal
// || request.headers.host == 'analytics.bizlitics.com' ) { // completely random, possibly previous domain owner
// response.redirect( 301, 'http://lunchvote.net' );
// return false;
// } else {
// return true;
// }
// }
// routes
// index page
app.get( '/', function( request, response ) {
// if( !primary_host_check( request, response ) )
// return;
response.sendfile( 'index.html' );
});
// status
app.get('/health', function( request, response ) {
// if( !primary_host_check( request, response ) )
// return;
response.send({
version: '1.1.0',
pid: process.pid,
memory: process.memoryUsage(),
uptime: process.uptime()
})
});
// request to start a new group
app.post( '/new', function( request, response ) {
// if( !primary_host_check( request, response ) )
// return;
var group_config = new GroupConfig();
group_config.id = generate_new_group_id();
save_group( group_config );
response.redirect( '/vote?'+group_config.id );
});
// vote page
app.get( '/vote', function( request, response ) {
// if( !primary_host_check( request, response ) )
// return;
var group_id = is_valid_group_request( request );
if( group_id === false )
response.send( 404 );
else {
response.sendfile( 'vote.html' );
}
});
// static fileserver
app.use( express.static( __dirname+'/public', {
maxAge: 31557600000 // one year
}));
// start webserver
server.listen( cfg.server_port );
// global server state; GROUP_ID => GroupState
var server = {};
// global task scheduler
var scheduler = {};
// define socket handlers
io.sockets.on( 'connection', function( socket ) {
// client joins group and completes initialization
// info : object
// group_id : string matching existing group id
// id : a previously-set socket id meant to link sessions
socket.once( 'register', function( info ) {
if( socket.locked )
return; // already joined a room; suspicious!
if( ! info )
return;
var group_id = info.group_id;
var id = info.id;
if( !id )
id = socket.id;
if( ! is_valid_group_id( group_id )) {
socket.disconnect();
return; // bad group id; suspicious!
}
socket.join( group_id );
socket.locked = true;
// send id to client for cookie persistence
socket.emit( 'id', id );
// initialize group state
var group_state = null;
if( group_id in server ) {
group_state = server[group_id];
} else {
group_state = new GroupState();
server[group_id] = group_state;
}
var group_config = load_group( group_id );
// load previous poll, if any, into group state
group_state.state.poll.previous = group_config.previous_poll;
// send current state and config to new user
socket.emit( 'state', group_state.state );
socket.emit( 'config', group_config );
// broadcast user_count
group_state.user_count = io.sockets.in( group_id ).clients().length;
io.sockets.in( group_id ).emit( 'user_count', group_state.user_count );
// send server time so client can calculate client/server time offset
socket.emit( 'time', now().getTime() );
// now that there is a group set, define other event listeners
// ----
// relay/broadcast chat message
// message : string; chat mesage body
socket.on( 'chat', function( message ) {
if( typeof message !== 'string' || message.length == 0 )
return;
io.sockets.in( group_id ).emit( 'chat', {
author: id,
message: message
});
});
// vote on current poll
// entity : string; name of entity to set vote to
socket.on( 'vote', function( entity ) {
var state = group_state.state;
if( typeof entity != 'string' )
return; // bad input
if( state.poll.current ) {
var poll = state.poll.current;
if( poll.entities.indexOf( entity ) == -1 )
return; // given entity is unknown to this poll
// register vote
poll.votes[id] = entity;
// perform complete recount
tally_votes( poll );
// send updated state to all users
io.sockets.in( group_id ).emit( 'state', group_state.state );
}
});
// update maps location (address)
// location : object
// address: string,
// viewport: object
// ne: object
// lat: float,
// lng: float
// sw: object
// lat: float,
// lng: float
socket.on( 'update_location', function( location ) {
group_config = load_group( group_id );
if( !_.isEqual( group_config.location, location )) {
group_config.location = location;
save_group( group_config );
socket.emit( 'config', group_config );
}
});
// start new poll
// poll_data : object
// start_ts : timestamp string; (parseable by date.js)
// duration : number; time, in minutes (optional)
socket.on( 'add_poll', function( poll_data ) {
if( poll_data == null )
return;
var start_ts = poll_data.start_ts;
if( !start_ts )
return;
var end_ts = start_ts + 1000.0*60.0*cfg.default_poll_duration_min;
// optional fields
if( 'duration' in poll_data
&& poll_data.duration != null
&& isNumber( poll_data.duration )) {
var duration = parseFloat( poll_data.duration )
end_ts = start_ts + 1000.0*60.0*duration;
}
// poll construction
var poll = new Poll();
poll.group_id = group_id;
poll.start_ts = start_ts;
poll.end_ts = end_ts;
// schedule poll to start
at( start_ts, start_poll, poll, fetch_entities, group_id );
// register as an upcoming poll
register_next( start_ts, group_id );
// send updated state to all users
io.sockets.in( group_id ).emit( 'state', group_state.state );
});
// cancel existing poll
// poll_start_ts : int, associated with previously added future poll that is not active
socket.on( 'cancel_poll', function( poll_start_ts ) {
if( poll_start_ts == -1 ) { // means cancel current
var end_ts = group_state.state.poll.current.end_ts;
clearTimeout( scheduler[end_ts] );
delete scheduler[end_ts];
group_state.state.poll.current = null;
} else {
clearTimeout( scheduler[poll_start_ts] );
delete scheduler[poll_start_ts];
deregister_next( poll_start_ts, group_id );
// send updated state to all users
}
io.sockets.in( group_id ).emit( 'state', group_state.state );
});
// add entity
// new_entity : object, derived from google maps api v3 Place object (filtered fields)
// name : string, name of business
// address : string, full, formatted address
// phone : string, full, formatted phone number
// lat : float, latitude coordinate
// lng : float, longitude coordinate
socket.on( 'add_entity', function( new_entity ) {
var group_config = load_group( group_id );
if( _.indexOfObjectByProperty( group_config.entities, 'name', new_entity.name ) == -1 ) {
group_config.entities.push( new_entity );
save_group( group_config );
// send updated config to all users
io.sockets.in( group_id ).emit( 'config', group_config );
}
});
// update entity
// entity_name : name of previously saved entity object
// data : object, will be merged with existing entry if found, replacing conflicting fields with newer data
socket.on( 'update_entity', function( entity_name, data ) {
var group_config = load_group( group_id );
var idx = _.indexOfObjectByProperty( group_config.entities, 'name', entity_name );
if( idx != -1 ) {
_.extend( group_config.entities[idx], data );
save_group( group_config );
// send updated config to all users
io.sockets.in( group_id ).emit( 'config', group_config );
}
});
// remove entity
// entity_name : name of previously saved entity object
socket.on( 'remove_entity', function( entity_name ) {
var group_config = load_group( group_id );
var idx = _.indexOfObjectByProperty( group_config.entities, 'name', entity_name );
if( idx != -1 ) {
group_config.entities.splice( idx, 1 );
save_group( group_config );
// send updated config to all users
io.sockets.in( group_id ).emit( 'config', group_config );
}
});
// disconnect
socket.on( 'disconnect', function() {
// // delete this user's vote, if they have one
// var state = group_state.state;
// var poll = state.poll.current;
// if( poll ) {
// delete poll.votes[id];
// // perform complete recount
// tally_votes( poll );
// // send updated state to all users
// io.sockets.in( group_id ).emit( 'state', group_state.state );
// }
});
});
});
// periodic processes
var user_count_monitoring = setInterval( function() {
for( var group_id in server ) {
var s = server[group_id];
var c = io.sockets.clients( group_id ).length;
if( s.user_count != c ) { // user count check
s.user_count = c;
io.sockets.in( group_id ).emit( 'user_count', c );
}
var poll = server[group_id].state.poll;
if( s.user_count == 0 && poll.current == null && poll.next_ts.length == 0 ) {
// group completely inactive; unload safely
for( var i = 0; i < poll.next_ts.length; ++i ) {
clearTimeout( scheduler[poll.next_ts[i]] );
delete scheduler[poll.next_ts[i]];
}
delete server[group_id];
}
}
}, 807 ); // arbitrary delay
// classes
function GroupState() {
this.user_count = 0;
this.state = {
poll: {
current: null,
previous: null,
next_ts: []
}
};
}
function GroupConfig() {
this.id = null;
this.location = null;
this.previous_poll = null;
this.entities = [];
}
function Poll() {
this.group_id = null;
this.entities = [];
this.votes = {};
this.start_ts = 0;
this.end_ts = 0;
this.locked = false;
this.winner = null;
this.tally = {};
this.vote_count = 0;
this.tie = false;
}
// higher-order functions
function save_group( group ) {
var string_data = JSON.stringify( group, null, 2 );
var path = get_group_data_path( group.id );
fs.writeFileSync( path, string_data );
}
function load_group( group_id ) {
if( typeof group_id == 'undefined' )
throw new Error( "group_id is undefined" );
var path = get_group_data_path( group_id );
var group = JSON.parse( fs.readFileSync( path, 'utf8' ));
return group;
}
function start_poll( poll, entity_fn, group_id ) {
var state = server[group_id].state;
if( typeof entity_fn != 'undefined' && entity_fn ) // signal to pull latest
poll.entities = entity_fn( group_id );
state.poll.current = poll;
deregister_next( poll.start_ts, group_id );
// later, resolve this poll
at( poll.end_ts, resolve_current_poll, group_id );
// update clients
io.sockets.in( poll.group_id ).emit( 'state', state );
}
function resolve_current_poll( group_id ) {
if( !(group_id in server ))
return; // group unloaded
var state = server[group_id].state;
if( state.poll.current == null )
return; // hmm
lock_poll( state.poll.current );
state.poll.previous = state.poll.current;
state.poll.current = null;
// runoff check
if( !state.poll.previous.winner
&& state.poll.previous.vote_count > 0 ) {
var poll = create_runoff_poll( state.poll.previous );
start_poll( poll, null, group_id );
}
// persist previous poll in case group is unloaded
var group_config = load_group( group_id );
group_config.previous_poll = state.poll.previous;
save_group( group_config );
// update clients
io.sockets.in( group_id ).emit( 'state', state );
}
function create_runoff_poll( origin_poll ) {
var start_ts = origin_poll.end_ts;
var end_ts = start_ts + 1000.0*60.0*cfg.default_runoff_duration_min;
var poll = new Poll();
poll.start_ts = start_ts;
poll.end_ts = end_ts;
if( Object.keys( origin_poll.tally ).length > 2 ) {
// determine the minimum tally
var min = null;
var min_count = 0;
for( var entity in origin_poll.tally ) {
if( min == null || min > origin_poll.tally[entity] ) {
min = origin_poll.tally[entity];
min_count = 1;
}
else if( min == origin_poll.tally[entity] )
++min_count;
}
// do not add lowest-performing entities to the pool
// unless all entities performed equally
if( min_count < origin_poll.entities.length ) {
for( var entity in origin_poll.tally ) {
if( origin_poll.tally[entity] > min )
poll.entities.push( entity );
}
}
}
else { // length == 2
poll.entities = Array.prototype.slice.call( origin_poll.entities );
}
return poll;
}
function lock_poll( poll ) {
// locked bit
poll.locked = true;
poll.winner = null;
// count votes against entities
tally_votes( poll );
// determine
var leader = find_leader( poll );
poll.tie = leader.tie;
poll.highest_pct = 100.0*(leader.vote_count / poll.vote_count);
if( !poll.tie && poll.highest_pct >= cfg.required_majority_pct )
poll.winner = leader.entity;
}
function tally_votes( poll ) {
poll.tally = {};
poll.vote_count = 0;
for( var i = 0; i < poll.entities.length; ++i )
poll.tally[poll.entities[i]] = 0;
for( var voter in poll.votes ) {
++poll.tally[poll.votes[voter]];
++poll.vote_count;
}
}
function find_leader( poll ) {
var leader = {
entity: null,
vote_count: 0,
tie: false
}
for( var entity in poll.tally ) {
if( poll.tally[entity] > leader.vote_count ) {
leader.vote_count = poll.tally[entity];
leader.entity = entity;
leader.tie = false;
} else if( poll.tally[entity] == leader.vote_count ) {
leader.entity = null;
leader.tie = true;
}
}
return leader;
}
function register_next( start_ts, group_id ) {
var state = server[group_id].state;
state.poll.next_ts.push( start_ts );
state.poll.next_ts.sort( function( a, b ) {
return a - b;
});
}
function deregister_next( start_ts, group_id ) {
var state = server[group_id].state;
var idx = state.poll.next_ts.indexOf( start_ts );
if( idx != -1 )
state.poll.next_ts.splice( idx, 1 );
}
function fetch_entities( group_id ) { // fetches just the names; client resolves names to configs later
var group_config = load_group( group_id );
var entities = group_config.entities;
var entity_names = _.map( entities, function( elem ) {
return elem.name;
});
return entity_names;
}
// lower-order functions
function generate_new_group_id() {
mt.init_genrand( now().getTime() % 1000000000 );
var r = mt.genrand_real2();
var id = Math.floor( r*(62*62*62*62*62) );
var b62_id = base62_encode( id );
var p_b62_id = pad( b62_id, 5 );
return p_b62_id;
// TODO: ensure that it does not already exist
}
function get_group_data_path( group_id ) {
return 'data/'+group_id+'.group.json';
}
function is_valid_group_request( request ) {
var keys = Object.keys( request.query );
if( keys.length != 1 )
return false;
var group_id = keys[0];
if( ! is_valid_group_id( group_id ))
return false;
// OK
return group_id;
}
function is_valid_group_id( group_id ) {
if( group_id == null || typeof group_id != 'string' || group_id.length != 5 )
return false;
if( ! /^[a-zA-Z0-9]{5}$/.test( group_id ))
return false;
var path = get_group_data_path( group_id );
if( ! fs.existsSync( path ))
return false;
// OK
return true;
}
function now() {
// var d = new Date();
// var debugging_offset = (-1*60*60*1000); // -1 hour
// d.setTime( d.getTime() + debugging_offset );
// return d;
return new Date();
}
function at( ts, fn ) { // ...
var from_now_ms = ts - now().getTime();
var fn_args = Array.prototype.slice.call( arguments, 2 );
fn_wrapper = function() {
fn.apply( this, fn_args );
delete scheduler[ts];
};
scheduler[ts] = setTimeout( fn_wrapper, from_now_ms );
}
function pad( val, len )
{
var str = String( val );
if( len > str.length )
str = Array( len + 1 - str.length ).join( "0" ) + str;
return str;
}
// enhancements & patches
_.indexOfObject = function( array, obj ) {
for( var i = 0; i < array.length; ++i )
if( _.isEqual( array[i], obj ))
return i;
return -1;
}
_.indexOfObjectByProperty = function( array, prop, val ) {
for( var i = 0; i < array.length; ++i )
if( array[i][prop] === val )
return i;
return -1;
}
// 3rd-party
function base62_encode(
a, // positive base10 encoded integer
b, // placeholder for result
c // placeholder for modulo
) {
for (
a = a !== +a || a % 1 ? -1 : a, b = ""; // if not a base10 integer, 'a' will be '-1'
// for example, '.5 % 1 == .5' but '1 % 1 == 0'
a >= 0; // also prevents the user to use negative base10 integers
a = Math.floor(a / 62) || -1 // using a bitwise hack here will fail with great numbers
)
// a%62 -> 0-61
// 0-9 | 36-61 | 10-35
// 48-57 | 65-90 | 97-121
// 0-9 | A-Z | a-z
b = String.fromCharCode(((c = a % 62) > 9 ? c > 35 ? 29 : 87 : 48) + c) + b;
return b // will return either an empty or a base62-encoded string
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}