|
|
|
@ -151,10 +151,40 @@ exports.editCmd = (rl, id) => {
|
|
|
|
|
* @param id Clave del quiz a probar.
|
|
|
|
|
*/
|
|
|
|
|
exports.testCmd = (rl, id) => {
|
|
|
|
|
log('Probar el quiz indicado.', 'red');
|
|
|
|
|
if (typeof id === "undefined") {
|
|
|
|
|
errorlog(`Falta el parámetro id.`);
|
|
|
|
|
rl.prompt();
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
const quiz = model.getByIndex(id);
|
|
|
|
|
|
|
|
|
|
rl.question(colorize(quiz.question + " ", 'red'), answer => {
|
|
|
|
|
if ( quiz.answer == answer ) {
|
|
|
|
|
biglog("correct", "green");
|
|
|
|
|
} else {
|
|
|
|
|
biglog("incorrect", "red");
|
|
|
|
|
}
|
|
|
|
|
rl.prompt();
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
errorlog(error.message);
|
|
|
|
|
rl.prompt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shuffles array in place. ES6 version
|
|
|
|
|
* @param {Array} a items An array containing the items.
|
|
|
|
|
*/
|
|
|
|
|
function shuffle(a) {
|
|
|
|
|
for (let i = a.length - 1; i > 0; i--) {
|
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
|
[a[i], a[j]] = [a[j], a[i]];
|
|
|
|
|
}
|
|
|
|
|
return a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pregunta todos los quizzes existentes en el modelo en orden aleatorio.
|
|
|
|
@ -163,8 +193,34 @@ exports.testCmd = (rl, id) => {
|
|
|
|
|
* @param rl Objeto readline usado para implementar el CLI.
|
|
|
|
|
*/
|
|
|
|
|
exports.playCmd = rl => {
|
|
|
|
|
log('Jugar.', 'red');
|
|
|
|
|
let aciertos = 0;
|
|
|
|
|
let questions = shuffle(model.getAll());
|
|
|
|
|
|
|
|
|
|
const keepAsking = () => {
|
|
|
|
|
if (questions.length<1){
|
|
|
|
|
log(`Fin del juego - Aciertos: ${aciertos}`, "red");
|
|
|
|
|
rl.prompt();
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
let quiz = questions.pop();
|
|
|
|
|
|
|
|
|
|
rl.question(colorize(quiz.question + " ", 'red'), answer => {
|
|
|
|
|
if ( quiz.answer == answer ) {
|
|
|
|
|
aciertos += 1;
|
|
|
|
|
log(`CORRECTO - lleva ${aciertos} aciertos`, "green");
|
|
|
|
|
} else {
|
|
|
|
|
log("INCORRECTO", "red");
|
|
|
|
|
questions = [];
|
|
|
|
|
}
|
|
|
|
|
keepAsking()
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
errorlog(error.message);
|
|
|
|
|
rl.prompt();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
keepAsking();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|