107 lines
2.7 KiB
JavaScript
107 lines
2.7 KiB
JavaScript
'use strict';
|
|
|
|
const config = require('config');
|
|
const timers = require('timers/promises');
|
|
|
|
const countries = require('../../assets/countries-curated.json');
|
|
const pickRandom = require('../utils/pick-random');
|
|
const style = require('../utils/style');
|
|
|
|
const games = new Map();
|
|
|
|
const questions = [
|
|
'What\'s this country?',
|
|
'Name this country.',
|
|
'Where are we going on holiday?',
|
|
'What\'s our destination?',
|
|
'I\'d never heard of that place, what\'s it called?',
|
|
];
|
|
|
|
function hint(game, context) {
|
|
if (game.name.length >= 5) {
|
|
context.sendMessage(`${style.bold(game.name
|
|
.split('')
|
|
.map((letter, index) => (index === 0 || index === game.name.length - 1 ? letter : '_'))
|
|
.join(' ')
|
|
.toUpperCase())}, the country code is ${style.bold(game.alpha2)}`, context.room.id);
|
|
|
|
return;
|
|
}
|
|
|
|
context.sendMessage(`${style.bold(game.name
|
|
.split('')
|
|
.map((letter, index) => (index === 0 ? letter : '_'))
|
|
.join(' ')
|
|
.toUpperCase())}, the country code is ${style.bold(game.alpha2)}`, context.room.id);
|
|
}
|
|
|
|
function play(context) {
|
|
const country = pickRandom(countries);
|
|
const url = `${config.geo.url}${country.file}`;
|
|
|
|
games.set(context.room.id, {
|
|
...country,
|
|
url,
|
|
regexp: new RegExp(country.name, 'i'),
|
|
});
|
|
|
|
context.sendMessage(`${url} ${pickRandom(questions)}`, context.room.id);
|
|
context.logger.info(`Geo played '${country.name}' (${url})`);
|
|
}
|
|
|
|
async function onCommand(args, context) {
|
|
if (context.subcommand && !games.has(context.room.id)) {
|
|
context.sendMessage(`There is no country in play right now. Start a new round with ${config.prefix}geo!`, context.room.id);
|
|
return;
|
|
}
|
|
|
|
if (['skip', 'stop', 'reset'].includes(context.subcommand)) {
|
|
const game = games.get(context.room.id);
|
|
|
|
games.delete(context.room.id);
|
|
context.sendMessage(`Geo was skipped by ${context.user.prefixedUsername}. The country was ${style.bold(game.name)}.`, context.room.id);
|
|
|
|
await timers.setTimeout(2000);
|
|
}
|
|
|
|
const game = games.get(context.room.id);
|
|
|
|
if (context.subcommand === 'hint') {
|
|
hint(game, context);
|
|
return;
|
|
}
|
|
|
|
if (game) {
|
|
context.sendMessage(game.url, context.room.id);
|
|
return;
|
|
}
|
|
|
|
play(context);
|
|
}
|
|
|
|
async function onMessage(message, context) {
|
|
const game = games.get(context.room.id);
|
|
|
|
if (!game) {
|
|
return;
|
|
}
|
|
|
|
if (game.regexp.test(message.body)) {
|
|
context.sendMessage(`${style.answer(game.name)} is the right answer! ${style.username(context.user.prefixedUsername)} gets a point.`, context.room.id);
|
|
context.setPoints(context.user);
|
|
|
|
games.delete(context.room.id);
|
|
|
|
await timers.setTimeout(3000);
|
|
|
|
play(context);
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
onCommand,
|
|
onMessage,
|
|
commands: ['country', 'atlas'],
|
|
help: 'Name the country on the map! Too hard? Try ~geo:hint or ~geo:skip.',
|
|
};
|