-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotHelper.js
134 lines (121 loc) · 4.84 KB
/
botHelper.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
const fs = require('fs');
const quizDb = require('./questions');
const { xml } = require("@xmpp/client");
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
module.exports = function (messageHandler, roomName, master) {
var questionsArr = shuffle(quizDb);
var currentQuest = null, idleTry = 0;
var groupScores = { room: roomName, scores: {} };
var setting = {
quiz: true,
qTlimit: 12000,
qIndex: 0,
masters: [master]
},
questionsArrLen = questionsArr.length;
var commands = ['quiz@start', 'quiz@stop', 'qspeed@'];
var quizInterval, currentAns = '', showScoreInterval, quesStartTime = 0;
function sendMsg(msg) {
const message = xml(
"message",
{ type: "groupchat", to: roomName },
xml("body", {}, msg),
);
messageHandler.send(message)
.then(i => undefined).catch(e => undefined);
}
function handleBotCmd(msg, from) {
switch (msg) {
case 'quiz@start':
setting.quiz = true;
quizInterval = setInterval(function () {
var data = questionsArr[setting.qIndex].split('|');
if (currentQuest === null) {
currentAns = data[1];
sendMsg(data[0])
} else {
if (idleTry > 5) {
idleTry=0;
currentAns=null;
sendMsg('Fools Correct answer is ' + currentAns);
} else {
idleTry++;
sendMsg(data[0]);
}
}
}, setting.qTlimit);
showScoreInterval = setInterval(function () {
var scores = `Top 5 Scores:`;
}, 300 * 1000);
sendMsg('Quiz started.');
break;
case 'quiz@stop':
clearInterval(quizInterval);
clearInterval(showScoreInterval);
setting.quiz = false;
sendMsg('Quiz stopped.');
break;
case 'quiz@next':
currentAns =null;
sendMsg('correct answer is '+currentAns);
break;
default:
if (msg.indexOf('addmas@') === 0 && msg.split('@').length > 1) {
if (setting.masters.indexOf(msg.split('@')[1]) === -1)
setting.masters.push(msg.split('@')[1]);
sendMsg(msg.split('@')[1] + ' added to commanders.');
} else if (msg.indexOf('delmas@') === 0 && msg.split('@').length > 1) {
setting.masters.splice(setting.masters.indexOf(msg.split('@')[1]), 1);
sendMsg(`${msg.split('@')[1]} deleted from commanders.`);
} else if (msg.indexOf('qtime@') === 0 && msg.split('@').length > 1) {
if (!isNaN(msg.split('@')[1]) && msg.split('@')[1] > 3)
setting.qTlimit = parseInt(msg.split('@')[1]) * 1000;
else
sendMsg('Must be greater than 3 sec.');
}
break;
}
}
var cnfg = {
correct: 0,
score: 0
};
function handleMsg(msg, from) {
if (msg.trim().toLowerCase() == currentAns.trim().toLowerCase()) {
currentAns = null;
idleTry=0;
if (groupScores.scores.hasOwnProperty(from)) {
groupScores.scores[from].correct++;
groupScores.scores[from].score += 100;
} else {
groupScores.scores[from] = Object.assign({}, cnfg);
groupScores.scores[from].correct++;
groupScores.scores[from].score += 100;
}
sendMsg(`${from}: you have answered correctly!`);
setting.qIndex = setting.qIndex < questionsArrLen - 1 ? setting.qIndex + 1 : 0;
}
}
messageHandler.on("stanza", async (stanza) => {
if (stanza.is('message') && stanza.getChildText('body') && stanza.attrs.type === 'groupchat') {
if (stanza.getChildText('body').trim().indexOf('@') !== -1 && setting.masters.indexOf(stanza.attrs.from.split('/')[1]) > -1) {
handleBotCmd(stanza.getChildText('body').trim());
} else if (setting.quiz) {
handleMsg(stanza.getChildText('body').trim(), stanza.attrs.from.split('/')[1]);
}
}
})
};