/
play.js
executable file
·167 lines (144 loc) · 3.92 KB
/
play.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
#!/usr/bin/env forever -w
/*global require:false*/
(function( express, _, fs, q, events ) {
'use strict';
var game = express();
game.set( 'title', 'Scores' );
game.set( 'scores file', 'scores' );
game.set( 'scores', undefined );
game.set( 'default scores', JSON.stringify({
"1": "zero_cool",
"2": "abigail",
"3": "abbie",
"5": "aby",
"7": "abby",
"9": "abbey",
"13": "aby",
"21": "abbie",
"35": "abigail"
}));
game.set( 'event', new events.EventEmitter() );
game.use( express.bodyParser() );
game.use( express.static( __dirname + '/public' ) );
function watch() {
try {
fs.watch(game.get( 'scores file' ), function ( err ) {
var oldScores = _.clone( game.get( 'scores' ) );
freshScores().then( function ( scores ) {
if ( !_.isEqual( oldScores, scores ) ) {
game.get( 'event' ).emit( 'refresh', scores );
}
game.set( 'scores', scores );
});
});
} catch ( error ) {
console.warn( 'fuck, no scores file.' );
fs.writeFile( game.get( 'scores file' ), game.get( 'default scores' ), watch );
}
}
watch();
game.get( '/', function ( request, response ) {
response.redirect( '/index.html' );
});
game.get( '/longget', function ( request, response ) {
var oldScores;
readScores().then(function ( text ) {
oldScores = text;
});
response.header( 'Content-Type', 'text/event-stream' );
response.header( 'Cache-Control', 'no-cache' );
response.header( 'Connection', 'keep-alive' );
function sendEvent ( text ) {
response.write( 'id: ' + new Date().getTime() + '\n' );
response.write( 'event: data\n' );
response.write( 'data: ' + text + '\n\n' );
}
function newData ( scores ) {
var amount = _.keys( JSON.parse( scores ) ).length - _.keys( JSON.parse( oldScores ) ).length;
sendEvent( JSON.stringify( topScores( scores, 1 ) ) );
oldScores = _.clone( scores );
}
game.get( 'event' ).on( 'refresh', newData );
});
game.get( '/get', function ( request, response ) {
var scoresFile = readScores();
scoresFile.then( function ( text ) {
response.header( 'Content-Type', 'application/json' );
response.header( 'Cache-Control', 'no-cache' );
response.send( 200, topScores( text ) );
}).fail( function () {
response.send( 200, game.get( 'default scores' ) );
});
});
function topScores ( text, amount ) {
var scores = {};
var json = JSON.parse( text );
amount = amount || 9;
_( json ).keys().sort( function ( a, b ) {
return b - a;
}).each( function ( points, i ) {
if ( i > ( amount - 1 ) ) {
return;
}
var name = json[ points ];
scores[ points ] = name;
});
return scores;
}
function readScores () {
var scores = game.get( 'scores' );
if ( scores ) {
return q.fcall( function () {
return scores;
});
} else {
return freshScores();
}
}
function freshScores () {
var readFile = q.nfcall( fs.readFile, game.get( 'scores file' ), 'utf-8' );
return readFile.then( function ( text ) {
game.set( 'scores', text );
return q.fcall( function () {
return text;
});
});
}
game.post( '/put', function ( request, response ) {
var name = request.body.player;
if ( name && name.length < 11 ) {
compete( name, response ).then( function () {
response.send( 200, 'yay' );
});
} else {
response.send( 404, 'FUCK' );
}
});
function compete ( name ) {
return readScores().then( function ( text ) {
var scores = JSON.parse( text );
var points = Object.keys( scores );
var highPoint = points[ points.length - 1 ];
var score = +highPoint + _.random( 1, 24 );
return addScore({
score: +score,
name: name
});
});
}
function addScore ( scoreObject ) {
return readScores().then( function ( text ) {
var scores = JSON.parse( text );
scores[ scoreObject.score ] = scoreObject.name;
return writeScores( JSON.stringify( scores ) );
});
}
function writeScores ( scores ) {
return q.nfcall( fs.writeFile, game.get( 'scores file' ), scores );
}
return game;
})( require( 'express' ),
require( 'lodash' ),
require( 'fs' ),
require( 'q' ),
require( 'events' ) ).listen( 3000 );