Added Geo game.
This commit is contained in:
63
src/games/geo.js
Normal file
63
src/games/geo.js
Normal file
@@ -0,0 +1,63 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
|
||||
const countries = require('../../assets/countries.json');
|
||||
const pickRandom = require('../utils/pick-random');
|
||||
const style = require('../utils/style');
|
||||
|
||||
const games = new Map();
|
||||
|
||||
async function onCommand(args, context) {
|
||||
const game = games.get(context.room.id);
|
||||
|
||||
if (['stop', 'reset'].includes(context.subcommand)) {
|
||||
if (game) {
|
||||
games.delete(context.room.id);
|
||||
context.sendMessage(`Geo was stopped by ${context.user.prefixedUsername}. The country was ${style.bold(game.name)}.`, context.room.id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
context.sendMessage(`There is no country in play right now. Start a new round with ${config.prefix}geo!`, context.room.id);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (game) {
|
||||
context.sendMessage(game.url, context.room.id);
|
||||
return;
|
||||
}
|
||||
|
||||
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, context.room.id);
|
||||
context.logger.info(`Geo played '${country.name}' (${url})`);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
onCommand,
|
||||
onMessage,
|
||||
};
|
||||
Reference in New Issue
Block a user