diff --git a/config/default.js b/config/default.js index 9d00c3b..1fa05ed 100644 --- a/config/default.js +++ b/config/default.js @@ -22,7 +22,7 @@ module.exports = { greeting: 'Hi, I am aisha, your game host!', usernamePrefix: '@', channels: ['GamesNight'], - games: ['mash', 'trivia', 'letters', 'duck', 'ping', 'say', 'kill', 'uptime', 'help'], + games: ['mash', 'trivia', 'letters', 'duck', 'dice', 'ping', 'say', 'kill', 'uptime', 'help'], schatColors: { red: 'red', orange: 'orange', @@ -53,4 +53,8 @@ module.exports = { length: 9, timeout: 60, }, + dice: { + maxRolls: 10, + maxFaces: 1000, + }, }; diff --git a/src/games/dice.js b/src/games/dice.js new file mode 100644 index 0000000..b3ed81f --- /dev/null +++ b/src/games/dice.js @@ -0,0 +1,37 @@ +'use strict'; + +const config = require('config'); +const crypto = require('crypto'); + +const style = require('../utils/style'); + +const dieFaces = ['⚀', '⚁', '⚂', '⚃', '⚄', '⚅']; + +function onCommand(args, context) { + const rolls = Number(args[0]) || 1; + const faces = Number(args[1]) || 6; + + if (rolls > config.dice.maxRolls) { + context.sendMessage(`You can only roll ${config.dice.maxRolls} dice at one time`, context.room.id); + return; + } + + if (rolls > config.dice.maxFaces) { + context.sendMessage(`Your dice can have at most ${config.dice.maxFace} faces`, context.room.id); + return; + } + + const results = Array.from({ length: rolls }, () => { + const result = crypto.randomInt(1, faces); + + // using U+2003 Em Space after dice to create double space that doesn't get filtered in SChat + return `${dieFaces[result - 1] ? `${style.grey(dieFaces[result - 1])}  ` : ''}${style.bold(result)}`; // eslint-disable-line no-irregular-whitespace + }); + + context.sendMessage(results.join(' | '), context.room.id); +} + +module.exports = { + onCommand, + commands: ['dice', 'roll'], +}; diff --git a/src/utils/get-leaders.js b/src/utils/get-leaders.js index 8441d7d..0dc80f0 100644 --- a/src/utils/get-leaders.js +++ b/src/utils/get-leaders.js @@ -3,7 +3,7 @@ const config = require('config'); const style = require('./style'); -function getLeaders(points, user, ping = false) { +function getLeaders(points, user, ping = true) { return Object.entries(points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([userKey, score], index) => { const username = userKey.split(':')[1] || userKey; // process the points file