-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
94 lines (92 loc) · 2.92 KB
/
quiz.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
var sys=require('sys');
function QuizServer(statModule)
{
this.statModule;
this.questionHandler = require('./question.handler').create(statModule);
this.statisticHandler = require('./statistic.handler');
this.historyHandler = require('./history.handler');
this.timeoutId;
}
QuizServer.prototype.sendCurrentQuestion=function(socket)
{
var self=this;
this.questionHandler.getCurrent(function(question)
{
if(question===undefined)
{
self.sendNextQuestion(socket);
}
else
{
socket.send(botMessage(question.question,false));
}
});
}
QuizServer.prototype.sendNextQuestion=function(socket)
{
var self=this;
clearTimeout(this.timeoutId);
this.questionHandler.getNext(function(question)
{
sendAll(question.question,socket,true);
self.timeoutId=setTimeout(function()
{
sendAll('Время истекло',socket,false);
var answer=self.questionHandler.getCurrentAnswer();
if(answer!='')
{
sendAll('Ответ: '+answer,socket);
}
self.sendNextQuestion(socket);
//reseting history
self.historyHandler.reset();
}, 60*1000);
});
}
QuizServer.prototype.handleByBot=function(message,socket,user)
{
//todo extract escaping to the separate module
var text=message.message.replace(/</g, '<').replace(/>/g, '>');
var userId=user.userId;
var userDisplayName=user.userDisplayName;
var message={message:text,user:userDisplayName};
//resend message for all
socket.broadcast(JSON.stringify(message));
//checking that answer was correct
if(this.questionHandler.isCorrect(text))
{
//just resend current message for all
this.historyHandler.updateRun(userId);
var userRun=this.historyHandler.getRun(userId);
var userPoints=this.historyHandler.getPoints(userId);
this.statisticHandler.updateStat(userId,userRun,userPoints,socket,userDisplayName);
//var roundTotalTime=new Date().getTime()-this.questionHandler.getQuestionStartTime();
socket.broadcast(botMessage(userDisplayName+' ответил правильно и получил ' + userPoints + ' очков'));
socket.send(botMessage('Вы дали правильный ответ и получили '+ userPoints + ' очков'));
this.sendNextQuestion(socket);
}
}
sendAll=function(text,socket,isQuestion)
{
var message=botMessage(text,isQuestion);
socket.broadcast(message);
socket.send(message);
}
botMessage=function(text,isQuestion)
{
var message;
if(isQuestion)
{
message={message:text,user:'робот',isQuestion:true};
}
else
{
message={message:text,user:'робот'}
}
return JSON.stringify(message);
}
//public factory method
exports.create=function(statModule)
{
return new QuizServer(statModule);
}