-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
182 lines (151 loc) · 4.14 KB
/
index.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
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var unirest = require('unirest');
var path = require('path');
var game_is_running = false;
var players = {};
var questions;
var categories;
var category;
var current_question;
var question_is_answered = false;
getCategoryList();
app.get('/', function(req, res){
if( ! game_is_running ){
res.sendFile(__dirname + '/begin.html');
}
else {
res.sendFile(__dirname + '/question.html');
}
});
app.use("/lib", express.static(path.join(__dirname, 'lib')));
app.use("/css", express.static(path.join(__dirname, 'css')));
io.on('connection', function(socket){
console.log('new connection! ' + socket.id);
socket.on('start game', function(data){
console.log('game is starting!');
game_is_running = true;
io.emit('start game', {'status' : 'OK'});
});
socket.on('game started', function(data){
console.log('The game has started!');
sendQuestion();
});
socket.on('name', function(name){
//save the name
console.log('A player has set their name to: ' + name);
players[socket.id] = { name: name, points: 0 };
io.emit('ready players', players);
//Send status to the client
io.emit('status', {'status' : 'OK'});
});
socket.on('new game', function(data){
newGame();
});
socket.on('give me players', function(data){
io.emit('ready players', players);
});
socket.on('answer', function(answer){
console.log(question_is_answered);
if ( !question_is_answered && players[socket.id] ) {
question_is_answered = true;
var status;
if(answer == current_question.q_correct_option){
status = true;
players[socket.id].points++;
if(players[socket.id].points == 5){
io.emit('end game', {name: players[socket.id]});
return;
}
}
else{
status = false;
players[socket.id].points--;
}
console.log(players);
io.emit('answer', {
'status' : status,
'player' : players[socket.id].name || 'undefined',
'answer' : answer,
'players' : players
});
setTimeout(function(){
sendQuestion();
}, 3000);
}
});
socket.on('disconnect', function() {
if(players[socket.id]){
console.log(players[socket.id].name + " has left the game :(");
}
delete players[socket.id];
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
function getQuestions(){
category = categories[Math.floor( Math.random() * (categories.length) )];
var category_id = category.id;
//TODO remove used category
unirest.get("https://pareshchouhan-trivia-v1.p.mashape.com/v1/getQuizQuestionsByCategory?categoryId=178&limit=10&page=" + Math.floor(Math.random() * 5) )
.header("X-Mashape-Key", "rCHR14UA0imshTfNn7uYk7sMByXCp1YTB2Ijsny27oo25nA7oB")
.header("Accept", "application/json")
.end(function (result) {
console.log('new questions are here');
questions = result.body;
shuffle(questions);
});
}
function sendQuestion(){
console.log('sending question');
question_is_answered = false;
current_question = questions[ questions.length - 1 ];
// var question_data = {
// category : category.category_name,
// question : current_question
// };
var question_data = current_question;
io.emit('question', question_data);
questions.pop();
if( questions.length <= 4 ) {
console.log('getting new questions');
getQuestions();
}
}
function getCategoryList(){
unirest.get("https://pareshchouhan-trivia-v1.p.mashape.com/v1/getCategoryList")
.header("X-Mashape-Key", "rCHR14UA0imshTfNn7uYk7sMByXCp1YTB2Ijsny27oo25nA7oB")
.header("Accept", "application/json")
.end(function (result) {
categories = result.body;
getQuestions();
console.log(categories);
});
}
function shuffle(a) {
var j, x, i;
for (i = a.length; i; i -= 1) {
j = Math.floor(Math.random() * i);
x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
function newGame(){
game_is_running = false;
for(id in players){
players[id].points = 0;
}
questions = {};
category = {};
current_question = {};
question_is_answered = false;
getQuestions();
io.emit('restart', true);
}
//TODO абе кво ми пука бе
function endGame(){
}