Added US states to Geo game.

This commit is contained in:
Niels Simenon
2022-11-06 02:06:04 +01:00
parent 5499dbf1f1
commit d41a0384d6
115 changed files with 830 additions and 27 deletions

View File

@@ -4,10 +4,16 @@ const config = require('config');
const timers = require('timers/promises');
const countries = require('../../assets/countries-curated.json');
const states = require('../../assets/states-curated.json');
const pickRandom = require('../utils/pick-random');
const style = require('../utils/style');
const games = new Map();
const data = { countries, states };
const games = {
countries: new Map(),
states: new Map(),
};
const questions = [
'What\'s this country?',
@@ -27,12 +33,13 @@ function hint(game, context) {
context.sendMessage(`${style.bold(`${game.name.slice(0, 1)}${game.name.slice(1).replace(/\s/g, '').replace(/[^\s]/g, '_').trim()}`.toUpperCase())}, the country code is ${style.bold(game.alpha2)}`, context.room.id); // eslint-disable-line no-irregular-whitespace
}
function play(context) {
const country = pickRandom(countries);
const url = `${config.geo.url}${country.file}`;
function play(context, type = 'countries') {
const country = pickRandom(data[type]);
const url = `${config.geo.url}${type}/${country.file}`;
games.set(context.room.id, {
games[type].set(context.room.id, {
...country,
type,
url,
regexp: new RegExp(country.fullName ? `(${country.name})|(${country.fullName})` : country.name, 'i'),
});
@@ -42,21 +49,23 @@ function play(context) {
}
async function onCommand(args, context) {
if (context.subcommand && !games.has(context.room.id)) {
const type = ['state', 'america'].includes(context.command) ? 'states' : 'countries';
if (context.subcommand && !games[type].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);
const game = games[type].get(context.room.id);
games.delete(context.room.id);
games[type].delete(context.room.id);
context.sendMessage(`Geo was skipped by ${context.user.prefixedUsername}. The country was ${style.bold(game.fullName ? `${game.fullName} (${game.name})` : game.name)}.`, context.room.id);
await timers.setTimeout(2000);
}
const game = games.get(context.room.id);
const game = games[type].get(context.room.id);
if (context.subcommand === 'hint') {
hint(game, context);
@@ -68,31 +77,33 @@ async function onCommand(args, context) {
return;
}
play(context);
play(context, type);
}
async function onMessage(message, context) {
const game = games.get(context.room.id);
['countries', 'states'].forEach(async (type) => {
const game = games[type].get(context.room.id);
if (!game) {
return;
}
if (!game) {
return;
}
if (game.regexp.test(message.body)) {
context.sendMessage(`${style.answer(game.fullName || game.name)} is the right answer! ${style.username(context.user.prefixedUsername)} gets a point.`, context.room.id);
context.setPoints(context.user);
if (game.regexp.test(message.body)) {
context.sendMessage(`${style.answer(game.fullName || 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);
games[type].delete(context.room.id);
await timers.setTimeout(3000);
await timers.setTimeout(3000);
play(context);
}
play(context, type);
}
});
}
module.exports = {
onCommand,
onMessage,
commands: ['country', 'atlas'],
commands: ['country', 'atlas', 'state', 'america'],
help: 'Name the country on the map! Too hard? Try ~geo:hint or ~geo:skip.',
};