Added coin to dice.

This commit is contained in:
Niels Simenon 2022-11-06 03:11:39 +01:00
parent a47b8adf99
commit cdfcd8e4fa
2 changed files with 22 additions and 9 deletions

View File

@ -6,8 +6,10 @@ const crypto = require('crypto');
const style = require('../utils/style');
const dieFaces = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅'];
const coinFaces = ['🗿', '🏛️'];
function onCommand(args, context) {
const type = ['coin', 'flip'].includes(context.command) ? 'coin' : 'dice';
const pattern = args[0]?.match(/(\d+)?d(\d+)?/i);
if (pattern) {
@ -16,30 +18,33 @@ function onCommand(args, context) {
}
const rolls = Math.max(Number(args[0]) || 1, 1);
const faces = Math.max(Number(args[1]) || 6, 1);
const faces = type === 'coin' ? 2 : Math.max(Number(args[1]) || 6, 1);
if (rolls > config.dice.maxRolls) {
context.sendMessage(`You can only roll ${config.dice.maxRolls} dice at one time`, context.room.id);
context.sendMessage(`You can only roll ${config.dice.maxRolls} dice at one time`, context.room.id, { label: type });
return;
}
if (faces > config.dice.maxFaces) {
context.sendMessage(`Your dice can have at most ${config.dice.maxFaces} faces`, context.room.id);
context.sendMessage(`Your dice can have at most ${config.dice.maxFaces} faces`, context.room.id, { label: type });
return;
}
const results = Array.from({ length: rolls }, () => {
const result = crypto.randomInt(1, faces + 1);
// using U+2003 Em Space after dice to create double space that doesn't get filtered in SChat
if (type === 'coin') {
return `${style.yellow(`(${coinFaces[result - 1]})`)} ${style.bold(result === 1 ? 'heads' : 'tails')}`; // eslint-disable-line no-irregular-whitespace
}
return `${style.grey(dieFaces[result - 1] || '☐')} ${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace
});
context.sendMessage(results.join(style.grey(' | ')), context.room.id);
context.sendMessage(results.join(style.grey(' | ')), context.room.id, { label: type });
}
module.exports = {
onCommand,
commands: ['dice', 'die', 'roll'],
help: 'What\'s your next move? Try ~dice [rolls] [faces], ~die or ~roll',
commands: ['dice', 'die', 'roll', 'coin', 'coins', 'flip'],
help: 'What\'s your next move? Try ~dice [rolls] [faces], ~die, ~roll, ~coin or ~flip',
};

View File

@ -90,6 +90,10 @@ function styleCommands(rawBody) {
}
/* eslint-enable no-irregular-whitespace */
function capitalize(string) {
return `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;
}
function curateMessageBody(rawBody, game, key, options) {
const body = options?.styleCommands ? styleCommands(rawBody) : rawBody;
@ -97,7 +101,11 @@ function curateMessageBody(rawBody, game, key, options) {
return body;
}
return `${style.grey(`[${game.name || `${key.slice(0, 1).toUpperCase()}${key.slice(1)}`}]`)} ${body}`;
const label = typeof options.label === 'string'
? capitalize(options.label)
: game.name || capitalize(key);
return `${style.grey(`[${label}]`)} ${body}`;
}
async function getGames(bot, identifier) {
@ -124,7 +132,7 @@ async function getGames(bot, identifier) {
}
};
const setGamePoints = (userId, score = 1, options) => setPoints(identifier, key, userId, score, options);
const setGamePoints = (user, score = 1, options) => setPoints(identifier, key, user, score, options);
const curatedGame = {
...game,