-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
103 lines (94 loc) · 2.72 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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
io.configure(function() {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});
var questions = "";
for (var i = 2; i < 192; i++) {
if (i != 133 && i != 146) {
questions += fs.readFileSync(__dirname + '/trivia/tr'+zeroFill(i, 3)+'.txt').toString().replace(/\r\n|\r/g, "\n");
}
}
questions = questions.split('\n');
console.log(questions.length + ' questions loaded');
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
function zeroFill( number, width )
{
width -= number.toString().length;
if ( width > 0 )
{
return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
}
return number;
}
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var nextQuestion = function() {
var rawQuestion = questions[Math.floor(Math.random() * questions.length)].toString();
var answers = rawQuestion.split('*');
var question = {question: answers[0], answers: answers.slice(1)};
var clients = io.sockets.clients();
for (var i = 0; i < clients.length; i++) {
var socket = clients[i];
setQuestion(socket,question);
}
}
var setQuestion = function(socket,question) {
console.log('question: ' + question.question + ' answer: ' + question.answers[0])
socket.set('question', question, function() {
socket.emit('gotquestion', question);
});
}
io.sockets.on('connection', function (socket) {
if (io.sockets.clients().length == 1) {
nextQuestion();
}
else {
for (var i = 0; i < io.sockets.clients().length; i++) {
if (io.sockets.clients()[i] != socket) {
var other = io.sockets.clients()[i];
other.get('question', function(err, question) {
setQuestion(socket, question);
});
break;
}
}
}
socket.on('name', function(data) { socket.set('name', data); });
socket.on('nextquestion', function(data) { nextQuestion(socket);});
socket.on('userguess', function(data) {
socket.get('question', function(err, question) {
socket.get('name', function(err, name) {
var correct = false;
var guess = data.guess.toLowerCase();
for (var i = 0; i < question.answers.length; i++) {
if (question.answers[i].toLowerCase() === guess) {
correct = true;
break;
}
}
if (correct) {
nextQuestion(socket);
}
io.sockets.emit('gottext', {text: '<'+name+'>'+data.guess});
});
});
});
});