Added question skip to trivia.
This commit is contained in:
parent
43478b3cc6
commit
baa2246245
|
@ -40,21 +40,23 @@ function scoreRound(context, round) {
|
|||
|
||||
async function playRound(context, round = 0) {
|
||||
const game = games.get(context.room.id);
|
||||
const ac = new AbortController(); // eslint-disable-line no-undef
|
||||
const now = new Date();
|
||||
|
||||
game.answers = new Map();
|
||||
game.round = round;
|
||||
game.ac = ac;
|
||||
game.skipped = null;
|
||||
game.ac = new AbortController(); // eslint-disable-line no-undef
|
||||
|
||||
const question = game.questions[round];
|
||||
|
||||
// ASK QUESTION
|
||||
context.sendMessage(`${style.bold(style.pink(`Question ${round + 1}/${game.questions.length}`))} ${style.grey(`(${question.category})`)}: ${question.question}`, context.room.id);
|
||||
context.logger.info(`Trivia asked "${question.question}" with answer: ${question.answer}`);
|
||||
|
||||
try {
|
||||
// SEND HINTS
|
||||
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
||||
signal: ac.signal,
|
||||
signal: game.ac.signal,
|
||||
});
|
||||
|
||||
// replace space with U+2003 Em Space to separate words, since a single space separates the placeholders, and double spaces are removed during Markdown render
|
||||
|
@ -66,7 +68,7 @@ async function playRound(context, round = 0) {
|
|||
}
|
||||
|
||||
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
||||
signal: ac.signal,
|
||||
signal: game.ac.signal,
|
||||
});
|
||||
|
||||
if (question.answer.length >= 4) {
|
||||
|
@ -74,24 +76,29 @@ async function playRound(context, round = 0) {
|
|||
}
|
||||
|
||||
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
||||
signal: ac.signal,
|
||||
signal: game.ac.signal,
|
||||
});
|
||||
} catch (error) {
|
||||
// abort expected, probably not an error
|
||||
}
|
||||
|
||||
/* not sure why this was deemed necessary, the timeouts should be either finalized or aborted already
|
||||
if (!ac.signal.aborted) {
|
||||
ac.abort();
|
||||
}
|
||||
*/
|
||||
|
||||
// EVALUATE RESULTS
|
||||
if (game.stopped) {
|
||||
context.sendMessage(`The game was stopped by ${style.cyan(`${config.usernamePrefix}${game.stopped.username}`)}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(game.points)}`, context.room.id);
|
||||
context.sendMessage(`The game was stopped by ${style.cyan(game.stopped)}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(game.points)}`, context.room.id);
|
||||
games.delete(context.room.id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (game.answers.size === 0) {
|
||||
if (game.skipped) {
|
||||
context.sendMessage(`The question was skipped by ${style.cyan(game.skipped)}. The answer was: ${style.bold(question.answer)}.`, context.room.id);
|
||||
} else if (game.answers.size === 0) {
|
||||
context.sendMessage(`${style.bold(style.red('TIME\'S UP!'))} No one guessed the answer: ${style.bold(question.answer)}`, context.room.id);
|
||||
} else {
|
||||
const scores = scoreRound(context, round);
|
||||
|
@ -105,11 +112,22 @@ async function playRound(context, round = 0) {
|
|||
}
|
||||
}
|
||||
|
||||
// COOL DOWN AND RESTART
|
||||
if (round < game.questions.length - 1) {
|
||||
await timers.setTimeout(5000);
|
||||
// present abort controller is expended if the question was answered or skipped
|
||||
game.ac = new AbortController(); // eslint-disable-line no-undef
|
||||
|
||||
try {
|
||||
await timers.setTimeout(5000, null, {
|
||||
signal: game.ac.signal,
|
||||
});
|
||||
} catch (error) {
|
||||
// abort expected, probably not an error
|
||||
}
|
||||
|
||||
if (game.stopped) {
|
||||
context.sendMessage(`The game was stopped by ${config.usernamePrefix}${game.stopped.username}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(game.points)}`, context.room.id);
|
||||
// stop was used between questions
|
||||
context.sendMessage(`The game was stopped by ${style.cyan(game.stopped)}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(game.points)}`, context.room.id);
|
||||
games.delete(context.room.id);
|
||||
|
||||
return;
|
||||
|
@ -143,7 +161,15 @@ async function start(context) {
|
|||
async function stop(context) {
|
||||
const game = games.get(context.room.id);
|
||||
|
||||
game.stopped = context.user;
|
||||
game.stopped = context.user.prefixedUsername;
|
||||
game.ac.abort();
|
||||
}
|
||||
|
||||
async function skip(context) {
|
||||
const game = games.get(context.room.id);
|
||||
|
||||
// game.stopped = context.user;
|
||||
game.skipped = context.user.prefixedUsername;
|
||||
game.ac.abort();
|
||||
}
|
||||
|
||||
|
@ -162,10 +188,17 @@ function onCommand(args, context) {
|
|||
|
||||
if (context.subcommand === 'stop' && game) {
|
||||
stop(context);
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.subcommand === 'stop' && !game) {
|
||||
if (context.subcommand === 'skip' && game) {
|
||||
skip(context);
|
||||
return;
|
||||
}
|
||||
|
||||
if (['stop', 'skip'].includes(context.subcommand) && !game) {
|
||||
context.sendMessage(`There is no game going on at the moment. Start one with ${config.prefix}trivia!`, context.room.id);
|
||||
return;
|
||||
}
|
||||
|
||||
const subcommand = context.subcommand?.toLowerCase();
|
||||
|
|
Loading…
Reference in New Issue