-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
231 lines (197 loc) · 6.22 KB
/
app.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
require.paths.unshift(__dirname + '/node_modules/express/node_modules'); // wtf-fix
require('nko')('L+FRhsyUr5+PPVlu');
var path = require('path'),
express = require('express'),
connect = require('connect'),
app = express.createServer(),
io = require('socket.io').listen(app);
io.configure('production', function(){
io.set('transports', [
// , 'websocket'
, 'xhr-polling'
, 'jsonp-polling'
, 'flashsocket'
, 'htmlfile'
]);
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.set('log level', 1);
});
io.configure('development', function(){
io.set('transports', [
// , 'websocket'
, 'xhr-polling'
, 'jsonp-polling'
, 'flashsocket'
, 'htmlfile'
]);
io.set('log level', 2);
});
var config = require('./config.js'),
utils = require('./lib/utils.js'),
currentShow = require('./lib/show.js'),
skripts = require('./lib/skript.js').loadAllSkripts(config.skriptsPath),
votes = {},
SHOW_INTERVAL = config.showInterval,
sessionStore = new express.session.MemoryStore();
var args = process.argv.slice(2);
var theaters = {
irc: require('./lib/irctheater.js')
};
app.configure(function(){
app.use(express.cookieParser());
app.use(express.session({ store: sessionStore, secret: config.sessionSecret }));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
});
app.configure('development', function() {
app.use(express.static(__dirname + '/static'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.get('/__trigger__', function(req, res) {
nextShow();
res.send("");
});
});
app.configure('production', function() {
var oneYear = 31557600000;
app.use(express.static(__dirname + '/static', { maxAge: oneYear }));
app.use(express.errorHandler());
});
app.get('/api/theatres', function(req, res) {
res.send(JSON.stringify(theaters));
});
app.get('/api/skripts', function(req, res) {
res.send(JSON.stringify(utils.massageSkripts(skripts)));
});
app.post('/api/vote/:id', function(req, res) {
skript = skripts[req.params.id];
if (!skript) {
res.send(404);
return;
}
var voteId = req.session.voteId;
if (process.env.NODE_ENV === 'production' && voteId) {
res.send(403);
return;
}
skript.votes += 1;
// todo: test channel via regex
if (!skript.nextShowChannel && req.body && req.body.channel) {
skript.nextShowChannel = req.body.channel;
}
utils.calculateVotePercentage(skripts);
emitVotes(io.sockets);
req.session.voteId = req.params.id;
res.send(200);
});
function emitVotes(socket) {
socket.emit('votes', utils.stripForVotes(skripts));
socket.emit('next show', utils.massageSkript(getNextShow()));
}
function getVoteId(request, cb) {
var cookieString = (request && request.headers.cookie) || "";
var parsedCookies = connect.utils.parseCookie(cookieString);
var sid = parsedCookies['connect.sid'];
if (sid) {
sessionStore.get(sid, function (error, session) {
session && cb(session.voteId);
});
}
}
app.get('/', function(req, res) {
res.sendfile(path.join(__dirname, 'static', 'index.html'));
});
app.listen(config.port, config.host);
io.sockets.on('connection', function(socket) {
emitShowTimes(socket);
emitVotes(socket);
getVoteId(socket.handshake, function(voteId) {
socket.emit('my vote', voteId || null);
});
});
var showTimeout = null;
var nextShowTime = Date.now() + SHOW_INTERVAL;
function getNextShow() {
var skript, winnerSkript, maxVotes = 0;
for (var id in skripts) {
skript = skripts[id];
if (skript.votes > maxVotes) {
winnerSkript = skript;
maxVotes = skript.votes;
}
skript = null;
}
return winnerSkript;
}
function nextShow() {
clearTimeout(showTimeout);
var winnerSkript = getNextShow();
if (winnerSkript) {
console.log("Starting scheduled show, winner is ", winnerSkript.title);
var theater = theaters.irc.getTheater();
currentShow.startShow(theater, winnerSkript, function() {
scheduleShow(15);
resetVotes();
}, function doneClb() {
emitShowTimes(io.sockets);
});
} else {
console.log("No votes, no show.");
scheduleShow(15);
}
}
function scheduleShow(timeDelay) {
clearTimeout(showTimeout);
if (timeDelay && nextShowTime) {
// Schedule timeDelay minutes later than currently
nextShowTime.setMinutes(nextShowTime.getMinutes() + 15);
} else {
// Schedule to the next full 15 minutes
nextShowTime = new Date();
nextQuarterH = nextShowTime.getMinutes();
nextQuarterH -= (nextQuarterH % 15) - 15;
nextShowTime.setMinutes(nextQuarterH);
nextShowTime.setSeconds(0); nextShowTime.setMilliseconds(0);
}
showTimeout = setTimeout(nextShow, Math.max(nextShowTime - Date.now(), 0));
console.log("Scheduled show for ", new Date(nextShowTime));
emitShowTimes(io.sockets);
}
scheduleShow();
function emitShowTimes(socket) {
socket.emit('show times', {
'current': currentShow.export(),
'next': nextShowTime
});
}
function resetVotes() {
for (var id in skripts) {
skripts[id].votes = 0;
skripts[id].votePercentage = 0;
skripts[id].nextShowChannel = null;
}
sessionStore.clear(function() {
io.sockets.emit("my vote", null);
emitVotes(io.sockets);
});
}
if (args.length > 0){
console.log("Running with", args[0]);
var theater = theaters.irc.getTheater();
var play = skripts[args[0].replace(/\./, '_')];
if (args[1]){
console.log("Cut to", args[1]);
play.skript = play.skript.slice(parseInt( args[1] || 0, 10));
}
if (play) {
currentShow.startShow(theater, play, function(){
theater.run(play.skript);
}, function() {
console.log("Done callback: skript finished.")
});
} else {
console.log("Skript " + args[0] + " does not exist");
process.exit();
}
}