From 11ab3ae45fb50342e87bbf2ca452a668a1599a8f Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 24 Jan 2021 11:17:21 -0500 Subject: [PATCH] Add domineering and cram color support --- assets/json/domineering.json | 14 ++++++++++++++ commands/games-mp/cram.js | 27 +++++++++++++++++++++++---- commands/games-mp/domineering.js | 27 +++++++++++++++++++++++---- package.json | 2 +- 4 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 assets/json/domineering.json diff --git a/assets/json/domineering.json b/assets/json/domineering.json new file mode 100644 index 00000000..2e902d7e --- /dev/null +++ b/assets/json/domineering.json @@ -0,0 +1,14 @@ +{ + "red": "🟥", + "yellow": "🟨", + "blue": "🟦", + "brown": "🟫", + "green": "🟩", + "orange": "🟧", + "purple": "🟪", + "black": "⬛", + "button": "🔳", + "sunset": "🌅", + "star": "🌠", + "galaxy": "🌌" +} diff --git a/commands/games-mp/cram.js b/commands/games-mp/cram.js index c45de146..a157256d 100644 --- a/commands/games-mp/cram.js +++ b/commands/games-mp/cram.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { verify } = require('../../util/Util'); +const { verify, list } = require('../../util/Util'); +const colors = require('../../assets/json/domineering'); const blankEmoji = '⬜'; const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟']; const turnRegex = /^(v|h) ?(\d+), ?(\d+)/i; @@ -20,6 +21,13 @@ module.exports = class CramCommand extends Command { prompt: 'What user would you like to challenge?', type: 'user' }, + { + key: 'color', + prompt: `What color do you want to be? Either ${list(Object.keys(colors), 'or')}.`, + type: 'string', + oneOf: Object.keys(colors), + parse: color => color.toLowerCase() + }, { key: 'size', prompt: 'What board size do you want to use?', @@ -32,25 +40,36 @@ module.exports = class CramCommand extends Command { }); } - async run(msg, { opponent, size }) { + async run(msg, { opponent, color, size }) { if (opponent.bot) return msg.reply('Bots may not be played against.'); if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.'); const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); this.client.games.set(msg.channel.id, { name: this.name }); + const userEmoji = colors[color]; + let oppoEmoji = userEmoji === colors.blue ? colors.red : colors.blue; try { + const available = Object.keys(colors).filter(clr => color !== clr); await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } + await msg.say(`${opponent}, what color do you want to be? Either ${list(available, 'or')}.`); + const filter = res => { + if (res.author.id !== opponent.id) return false; + return available.includes(res.content.toLowerCase()); + }; + const p2Color = await msg.channel.awaitMessages(filter, { + max: 1, + time: 30000 + }); + if (p2Color.size) oppoEmoji = colors[p2Color.first().content.toLowerCase()]; const board = this.generateBoard(size); let userTurn = true; let winner = null; let lastTurnTimeout = false; - const userEmoji = '🟥'; - const oppoEmoji = '🟦'; while (!winner) { const user = userTurn ? msg.author : opponent; await msg.say(stripIndents` diff --git a/commands/games-mp/domineering.js b/commands/games-mp/domineering.js index 2f3935d1..88a1664b 100644 --- a/commands/games-mp/domineering.js +++ b/commands/games-mp/domineering.js @@ -1,6 +1,7 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); -const { verify } = require('../../util/Util'); +const { verify, list } = require('../../util/Util'); +const colors = require('../../assets/json/domineering'); const blankEmoji = '⬜'; const nums = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟']; const turnRegex = /^(\d+), ?(\d+)/i; @@ -20,6 +21,13 @@ module.exports = class DomineeringCommand extends Command { prompt: 'What user would you like to challenge?', type: 'user' }, + { + key: 'color', + prompt: `What color do you want to be? Either ${list(Object.keys(colors), 'or')}.`, + type: 'string', + oneOf: Object.keys(colors), + parse: color => color.toLowerCase() + }, { key: 'size', prompt: 'What board size do you want to use?', @@ -32,25 +40,36 @@ module.exports = class DomineeringCommand extends Command { }); } - async run(msg, { opponent, size }) { + async run(msg, { opponent, color, size }) { if (opponent.bot) return msg.reply('Bots may not be played against.'); if (opponent.id === msg.author.id) return msg.reply('You may not play against yourself.'); const current = this.client.games.get(msg.channel.id); if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); this.client.games.set(msg.channel.id, { name: this.name }); + const userEmoji = colors[color]; + let oppoEmoji = userEmoji === colors.blue ? colors.red : colors.blue; try { + const available = Object.keys(colors).filter(clr => color !== clr); await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } + await msg.say(`${opponent}, what color do you want to be? Either ${list(available, 'or')}.`); + const filter = res => { + if (res.author.id !== opponent.id) return false; + return available.includes(res.content.toLowerCase()); + }; + const p2Color = await msg.channel.awaitMessages(filter, { + max: 1, + time: 30000 + }); + if (p2Color.size) oppoEmoji = colors[p2Color.first().content.toLowerCase()]; const board = this.generateBoard(size); let userTurn = true; let winner = null; let lastTurnTimeout = false; - const userEmoji = '🟥'; - const oppoEmoji = '🟦'; while (!winner) { const user = userTurn ? msg.author : opponent; await msg.say(stripIndents` diff --git a/package.json b/package.json index 5fd80ef5..d2c37d0c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "126.14.0", + "version": "126.14.1", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {