This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
206 lines (169 loc) · 5.39 KB
/
index.ts
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
console.log(`
888 .d888 d8888 888 888888b. 888
888 d88P" d88888 888 888 "88b 888
888 888 d88P888 888 888 .88P 888
888888 88888b. 888888 d88P 888 88888b. .d88b. .d88b. 888 8888888K. .d88b. 888888
888 888 "88b 888 d88P 888 888 "88b d88P"88b d8P Y8b 888 888 "Y88b d88""88b 888
888 888 888 888 d88P 888 888 888 888 888 88888888 888 888 888 888 888 888
Y88b. 888 888 888 d8888888888 888 888 Y88b 888 Y8b. 888 888 d88P Y88..88P Y88b.
"Y888 888 888 888 d88P 888 888 888 "Y88888 "Y8888 888 8888888P" "Y88P" "Y888
888
Y8b d88P
"Y88P"
-> Kahoot Bot v1.1
`);
const Kahoot = require('kahoot.js-updated');
const readline = require('readline');
const readlines = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const log = {
success: (message: string): void =>
console.log(`\x1B[1;32m[+]\x1B[1;0m ${message}`),
error: (message: string): void =>
console.log(`\x1B[1;31m[-]\x1B[1;0m ${message}`),
question: (message: string): string => `\x1B[1;34m[?]\x1B[1;0m ${message}`
};
const wait = (time: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, time));
function formatNumber(number: number): string {
if (number < 10) return `0${number}`;
return `${number}`;
}
function stdinQuestion(question: string): Promise<string> {
return new Promise((resolve): void => {
readlines.question(log.question(question), (input: string): void =>
resolve(input)
);
});
}
async function start() {
const leaderNameInput = await stdinQuestion('Nombre del bot líder: ');
if (!leaderNameInput) {
log.error('No ingresaste ningún nombre del bot líder.');
return start();
}
const nameInput = await stdinQuestion('Nombre de los bots a crear: ');
if (!nameInput) {
log.error('No ingresaste ningún nombre de los bots.');
return start();
}
const botsInput = await stdinQuestion('Número de bots a crear: ');
if (!botsInput) {
log.error('No ingresaste ningún número de bots.');
return start();
}
const bots = parseInt(botsInput);
if (isNaN(bots)) {
log.error('No ingresaste un número valido de bots a crear.');
return start();
}
if (bots < 1) {
log.error('El número de bots es muy pequeño.');
return start();
}
const pinInput = await stdinQuestion('Pin de la partida: ');
if (!pinInput) {
log.error('No ingresaste ningún pin de la partida.');
return start();
}
const leader = new Kahoot();
await wait(1000);
const successLeaderSession = await leader
.join(pinInput, leaderNameInput)
.then(() => {
log.success(`[LEADER] Sesión líder iniciada como ${leader.name}.`);
return true;
})
.catch(() => {
log.error(
`[LEADER] Error intentando acceder a la sala ${pinInput} como ${leaderNameInput}.`
);
return false;
});
if (!successLeaderSession) return start();
leader.on('QuizStart', (quiz) => {
log.success(
`[LEADER] Nuevo quiz empezado de ${quiz.questionCount} preguntas.`
);
});
leader.on('QuestionReady', async (question: any) => {
log.success(
`[LEADER] Nueva pregunta de ${question.numberOfChoices} respuestas.`
);
});
leader.on('QuestionStart', async (question: any) => {
const answer = Math.floor(Math.random() * question.numberOfChoices);
await question
.answer(answer)
.then(() => {
log.success(
`[LEADER] Respondiendo con número ${answer} como ${leader.name}.`
);
})
.catch(() => {
log.error(
`[LEADER] No he podido responder con el núimero ${answer} como ${leader.name}.`
);
});
});
leader.on('QuestionEnd', async (question: any) => {
log.success(
`[LEADER] Pregunta acabada con las respuestas correctas: ${
question.correctChoices
? question.correctChoices
.map((choice) => choice + 1)
.join(', ')
: 'Ninguna'
}.`
);
});
leader.on('QuizEnd', (quiz) => {
log.success(`[LEADER] Quiz acabado (${quiz.quizTitle}).`);
stop();
});
for (let i = 0; i < bots; i++) {
const clientNumber = formatNumber(i + 1);
const name = `${nameInput} ${clientNumber}`;
const client = new Kahoot();
await wait(1000);
client
.join(pinInput, name)
.then(() => {
log.success(
`[SLAVE ${clientNumber}] Sesión iniciada como ${client.name}.`
);
return true;
})
.catch(() => {
log.error(
`[SLAVE ${clientNumber}] Error intentando iniciar sesión como ${name}`
);
return false;
});
client.on('QuestionStart', async (question: any) => {
const answer = Math.floor(Math.random() * question.numberOfChoices);
await question
.answer(answer)
.then(() => {
log.success(
`[SLAVE ${clientNumber}] Respondiendo con número ${answer} como ${client.name}.`
);
})
.catch(() => {
log.error(
`[SLAVE ${clientNumber}] No he podido responder con el número ${answer} como ${client.name}.`
);
});
});
}
}
function stop() {
console.log('\n');
log.error(`Saliendo...`);
readlines.pause();
process.exit(0);
}
start();
readlines.on('SIGINT', () => stop());