From c387acfab4ec2d945c053d17f5ec182d41b86abc Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sat, 15 Jun 2019 16:12:31 -0400 Subject: [PATCH] Only one game per channel for ALL games, --- commands/games/balloon-pop.js | 13 ++++++------- commands/games/battle.js | 15 +++++++-------- commands/games/blackjack.js | 21 ++++++++++----------- commands/games/box-choosing.js | 12 ++++++------ commands/games/doors.js | 11 +++++------ commands/games/emoji-emoji-revolution.js | 13 ++++++------- commands/games/google-feud.js | 11 +++++------ commands/games/gunfight.js | 13 ++++++------- commands/games/hangman.js | 11 +++++------ commands/games/hunger-games.js | 13 ++++++------- commands/games/mafia.js | 13 ++++++------- commands/games/quiz-duel.js | 13 ++++++------- commands/games/sorting-hat.js | 11 +++++------ commands/games/tic-tac-toe.js | 13 ++++++------- commands/games/wizard-convention.js | 13 ++++++------- commands/games/word-chain.js | 13 ++++++------- package.json | 4 ++-- structures/Client.js | 1 + 18 files changed, 100 insertions(+), 114 deletions(-) diff --git a/commands/games/balloon-pop.js b/commands/games/balloon-pop.js index 20384c21..de1b6665 100644 --- a/commands/games/balloon-pop.js +++ b/commands/games/balloon-pop.js @@ -22,20 +22,19 @@ module.exports = class BalloonPopCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { opponent }) { 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.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } let userTurn = false; @@ -71,10 +70,10 @@ module.exports = class BalloonPopCommand extends Command { userTurn = !userTurn; } } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say(`And the winner is... ${winner}! Great job!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/battle.js b/commands/games/battle.js index 1d0f7b15..ee7ed23d 100644 --- a/commands/games/battle.js +++ b/commands/games/battle.js @@ -19,21 +19,20 @@ module.exports = class BattleCommand extends Command { } ] }); - - this.battles = new Map(); } async run(msg, { opponent }) { if (opponent.id === msg.author.id) return msg.reply('You may not battle yourself.'); - if (this.battles.has(msg.channel.id)) return msg.reply('Only one battle may be occurring per channel.'); - this.battles.set(msg.channel.id, new Battle(msg.author, opponent)); - const battle = this.battles.get(msg.channel.id); + 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, data: new Battle(msg.author, opponent) }); + const battle = this.client.games.get(msg.channel.id).data; try { if (!opponent.bot) { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.battles.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } } @@ -82,10 +81,10 @@ module.exports = class BattleCommand extends Command { } } const { winner } = battle; - this.battles.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say(`The match is over! Congrats, ${winner}!`); } catch (err) { - this.battles.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/blackjack.js b/commands/games/blackjack.js index 91f17167..8b602ed3 100644 --- a/commands/games/blackjack.js +++ b/commands/games/blackjack.js @@ -24,14 +24,13 @@ module.exports = class BlackjackCommand extends Command { } ] }); - - this.decks = new Map(); } - async run(msg, { deckCount }) { - if (this.decks.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); + async run(msg, { deckCount }) { // eslint-disable-line complexity + 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.`); try { - this.decks.set(msg.channel.id, this.generateDeck(deckCount)); + this.client.games.set(msg.channel.id, { name: this.name, data: this.generateDeck(deckCount) }); const dealerHand = []; this.draw(msg.channel, dealerHand); this.draw(msg.channel, dealerHand); @@ -41,13 +40,13 @@ module.exports = class BlackjackCommand extends Command { const dealerInitialTotal = this.calculate(dealerHand); const playerInitialTotal = this.calculate(playerHand); if (dealerInitialTotal === 21 && playerInitialTotal === 21) { - this.decks.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Well, both of you just hit blackjack. Right away. Rigged.'); } else if (dealerInitialTotal === 21) { - this.decks.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Ouch, the dealer hit blackjack right away! Try again!'); } else if (playerInitialTotal === 21) { - this.decks.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Wow, you hit blackjack right away! Lucky you!'); } let playerTurn = true; @@ -104,11 +103,11 @@ module.exports = class BlackjackCommand extends Command { } } } - this.decks.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (win) return msg.say(`${reason}! You won!`); return msg.say(`${reason}! Too bad.`); } catch (err) { - this.decks.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } @@ -139,7 +138,7 @@ module.exports = class BlackjackCommand extends Command { } draw(channel, hand) { - const deck = this.decks.get(channel.id); + const deck = this.client.games.get(channel.id).data; const card = deck[0]; deck.shift(); hand.push(card); diff --git a/commands/games/box-choosing.js b/commands/games/box-choosing.js index 31c20957..63029906 100644 --- a/commands/games/box-choosing.js +++ b/commands/games/box-choosing.js @@ -27,21 +27,21 @@ module.exports = class BoxChoosingCommand extends Command { ] }); - this.playing = new Set(); this.blue = new Set(); this.red = new Set(); } async run(msg) { - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { let i = 0; let path = 'before'; while (true) { // eslint-disable-line no-constant-condition const line = script[path][i]; if (line.end) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say(line.text); } else { await msg.say(typeof line === 'object' ? line.text : stripIndents` @@ -75,10 +75,10 @@ module.exports = class BoxChoosingCommand extends Command { i++; } } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('See you soon!'); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/doors.js b/commands/games/doors.js index 316461ee..8d6e07e0 100644 --- a/commands/games/doors.js +++ b/commands/games/doors.js @@ -21,13 +21,12 @@ module.exports = class DoorsCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { door }) { - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { const win = doors[Math.floor(Math.random() * doors.length)]; const noWin = doors.filter(thisDoor => thisDoor !== win && door !== thisDoor)[0]; @@ -37,13 +36,13 @@ module.exports = class DoorsCommand extends Command { `); const stick = await verify(msg.channel, msg.author); if (!stick) door = doors.filter(thisDoor => door !== thisDoor && thisDoor !== noWin)[0]; - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(stripIndents` ${door === win ? 'You chose wisely.' : 'Hmm... Try again.'} ${this.emoji(1, noWin, win, door)} ${this.emoji(2, noWin, win, door)} ${this.emoji(3, noWin, win, door)} `); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/emoji-emoji-revolution.js b/commands/games/emoji-emoji-revolution.js index cae82f50..867803d5 100644 --- a/commands/games/emoji-emoji-revolution.js +++ b/commands/games/emoji-emoji-revolution.js @@ -20,20 +20,19 @@ module.exports = class EmojiEmojiRevolutionCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { opponent }) { 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.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one fight may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } let turn = 0; @@ -61,12 +60,12 @@ module.exports = class EmojiEmojiRevolutionCommand extends Command { **${opponent.username}:** ${oPts} `); } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (aPts === oPts) return msg.say('It\'s a tie!'); const userWin = aPts > oPts; return msg.say(`You win ${userWin ? msg.author : opponent} with ${userWin ? aPts : oPts} points!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/google-feud.js b/commands/games/google-feud.js index 55bae711..e7ab7246 100644 --- a/commands/games/google-feud.js +++ b/commands/games/google-feud.js @@ -25,13 +25,12 @@ module.exports = class GoogleFeudCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { question }) { - if (this.playing.has(msg.channel.id)) return msg.reply('Only one fight may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { const suggestions = await this.fetchSuggestions(question); if (!suggestions) return msg.say('Could not find any results.'); @@ -57,11 +56,11 @@ module.exports = class GoogleFeudCommand extends Command { if (suggestions.includes(choice)) display[suggestions.indexOf(choice)] = choice; else --tries; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (!display.includes('???')) return msg.say('You win! Nice job, master of Google!'); return msg.say('Better luck next time!'); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/games/gunfight.js b/commands/games/gunfight.js index 70a534a0..9860c456 100644 --- a/commands/games/gunfight.js +++ b/commands/games/gunfight.js @@ -19,20 +19,19 @@ module.exports = class GunfightCommand extends Command { } ] }); - - this.fighting = new Set(); } async run(msg, { opponent }) { if (opponent.bot) return msg.reply('Bots may not be fought.'); if (opponent.id === msg.author.id) return msg.reply('You may not fight yourself.'); - if (this.fighting.has(msg.channel.id)) return msg.reply('Only one fight may be occurring per channel.'); - this.fighting.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.fighting.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } await msg.say('Get Ready...'); @@ -44,11 +43,11 @@ module.exports = class GunfightCommand extends Command { max: 1, time: 30000 }); - this.fighting.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (!winner.size) return msg.say('Oh... No one won.'); return msg.say(`The winner is ${winner.first().author}!`); } catch (err) { - this.fighting.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/hangman.js b/commands/games/hangman.js index a334bc8c..1aad5cfc 100644 --- a/commands/games/hangman.js +++ b/commands/games/hangman.js @@ -16,13 +16,12 @@ module.exports = class HangmanCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg) { // eslint-disable-line complexity - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { const word = words[Math.floor(Math.random() * words.length)].toLowerCase(); let points = 0; @@ -75,11 +74,11 @@ module.exports = class HangmanCommand extends Command { points++; } } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (word.length === confirmation.length || guessed) return msg.say(`You won, it was ${word}!`); return msg.say(`Too bad... It was ${word}...`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/games/hunger-games.js b/commands/games/hunger-games.js index 358ad7d4..528cd8f4 100644 --- a/commands/games/hunger-games.js +++ b/commands/games/hunger-games.js @@ -27,16 +27,15 @@ module.exports = class HungerGamesCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { tributes }) { if (tributes.length < 2) return msg.say(`...${tributes[0]} wins, as they were the only tribute.`); if (tributes.length > 24) return msg.reply('Please do not enter more than 24 tributes.'); if (new Set(tributes).size !== tributes.length) return msg.reply('Please do not enter the same tribute twice.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { let sun = true; let turn = 0; @@ -63,17 +62,17 @@ module.exports = class HungerGamesCommand extends Command { await msg.say(text); const verification = await verify(msg.channel, msg.author, 120000); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('See you next time!'); } if (!bloodbath) sun = !sun; if (bloodbath) bloodbath = false; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); const remainingArr = Array.from(remaining); return msg.say(`And the winner is... ${remainingArr[0]}!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/mafia.js b/commands/games/mafia.js index deb591ad..be21822b 100644 --- a/commands/games/mafia.js +++ b/commands/games/mafia.js @@ -14,18 +14,17 @@ module.exports = class MafiaCommand extends Command { description: 'Who is the Mafia? Who is the doctor? Who is the detective? Will the Mafia kill them all?', guildOnly: true }); - - this.playing = new Set(); } async run(msg) { // eslint-disable-line complexity - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say('You will need at least 2 more players, at maximum 10. To join, type `join game`.'); const awaitedPlayers = await awaitPlayers(msg, 10, 3, { dmCheck: true }); if (!awaitedPlayers) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Game could not be started...'); } const players = await this.generatePlayers(awaitedPlayers); @@ -117,12 +116,12 @@ module.exports = class MafiaCommand extends Command { players.delete(hanged.id); ++turn; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); const mafia = players.find(p => p.role === 'mafia'); if (!mafia) return msg.say('The Mafia has been hanged! Thanks for playing!'); return msg.say(`Oh no, the Mafia wasn't caught in time... Nice job, ${mafia.user}!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/games/quiz-duel.js b/commands/games/quiz-duel.js index 28549e79..09b6f653 100644 --- a/commands/games/quiz-duel.js +++ b/commands/games/quiz-duel.js @@ -34,20 +34,19 @@ module.exports = class QuizDuelCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { opponent, maxPts }) { 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.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } let winner = null; @@ -92,11 +91,11 @@ module.exports = class QuizDuelCommand extends Command { `; await msg.say(`Nice one, ${result.author}! The score is now ${score}!`); } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (!winner) return msg.say('Aww, no one won...'); return msg.say(`Congrats, ${winner}, you won!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/games/sorting-hat.js b/commands/games/sorting-hat.js index 01bb7715..50630143 100644 --- a/commands/games/sorting-hat.js +++ b/commands/games/sorting-hat.js @@ -23,13 +23,12 @@ module.exports = class SortingHatCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg) { - if (this.playing.has(msg.channel.id)) return msg.reply('Only one quiz may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { const points = { g: 0, @@ -70,7 +69,7 @@ module.exports = class SortingHatCommand extends Command { ++turn; } const houseResult = Object.keys(points).filter(h => points[h] > 0).sort((a, b) => points[b] - points[a]); - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); const totalPoints = houseResult.reduce((a, b) => a + points[b], 0); return msg.say(stripIndents` You are a member of... **${houses[houseResult[0]]}**! @@ -79,7 +78,7 @@ module.exports = class SortingHatCommand extends Command { ${houseResult.map(house => `${houses[house]}: ${Math.round((points[house] / totalPoints) * 100)}%`).join('\n')} `); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/tic-tac-toe.js b/commands/games/tic-tac-toe.js index 47454aad..52cea465 100644 --- a/commands/games/tic-tac-toe.js +++ b/commands/games/tic-tac-toe.js @@ -18,20 +18,19 @@ module.exports = class TicTacToeCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { opponent }) { 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.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } const sides = ['0', '1', '2', '3', '4', '5', '6', '7', '8']; @@ -70,10 +69,10 @@ module.exports = class TicTacToeCommand extends Command { if (this.verifyWin(sides)) winner = userTurn ? msg.author : opponent; userTurn = !userTurn; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say(winner ? `Congrats, ${winner}!` : 'Oh... The cat won.'); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/commands/games/wizard-convention.js b/commands/games/wizard-convention.js index 40fcd1dd..7b1c70d8 100644 --- a/commands/games/wizard-convention.js +++ b/commands/games/wizard-convention.js @@ -14,18 +14,17 @@ module.exports = class WizardConventionCommand extends Command { description: 'Who is the Dragon? Who is the healer? Who is the mind reader? Will the Dragon eat them all?', guildOnly: true }); - - this.playing = new Set(); } async run(msg) { // eslint-disable-line complexity - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say('You will need at least 2 more players, at maximum 10. To join, type `join game`.'); const awaitedPlayers = await awaitPlayers(msg, 10, 3, { dmCheck: true }); if (!awaitedPlayers) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Game could not be started...'); } const players = await this.generatePlayers(awaitedPlayers); @@ -117,12 +116,12 @@ module.exports = class WizardConventionCommand extends Command { players.delete(expelled.id); ++turn; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); const dragon = players.find(p => p.role === 'dragon'); if (!dragon) return msg.say('The dragon has been vanquished! Thanks for playing!'); return msg.say(`Oh no, the dragon wasn't caught in time... Nice job, ${dragon.user}!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } diff --git a/commands/games/word-chain.js b/commands/games/word-chain.js index 640f1182..b23644a8 100644 --- a/commands/games/word-chain.js +++ b/commands/games/word-chain.js @@ -35,20 +35,19 @@ module.exports = class WordChainCommand extends Command { } ] }); - - this.playing = new Set(); } async run(msg, { opponent, time }) { 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.'); - if (this.playing.has(msg.channel.id)) return msg.reply('Only one game may be occurring per channel.'); - this.playing.add(msg.channel.id); + 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 }); try { await msg.say(`${opponent}, do you accept this challenge?`); const verification = await verify(msg.channel, opponent); if (!verification) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); return msg.say('Looks like they declined...'); } const startWord = startWords[Math.floor(Math.random() * startWords.length)]; @@ -98,11 +97,11 @@ module.exports = class WordChainCommand extends Command { lastWord = choice; userTurn = !userTurn; } - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); if (!winner) return msg.say('Oh... No one won.'); return msg.say(`The game is over! The winner is ${winner}!`); } catch (err) { - this.playing.delete(msg.channel.id); + this.client.games.delete(msg.channel.id); throw err; } } diff --git a/package.json b/package.json index 1375287b..e3d62d9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "105.3.2", + "version": "106.0.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { @@ -39,7 +39,7 @@ "discord.js-commando": "github:discordjs/Commando", "dotenv": "^8.0.0", "gifencoder": "^2.0.1", - "mathjs": "^6.0.1", + "mathjs": "^6.0.2", "moment": "^2.24.0", "moment-duration-format": "^2.3.2", "moment-timezone": "^0.5.25", diff --git a/structures/Client.js b/structures/Client.js index 4ad5f4e1..99a280fc 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -17,5 +17,6 @@ module.exports = class XiaoClient extends CommandoClient { }); this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableEveryone: true }); this.pokemon = new PokemonStore(); + this.games = new Map(); } };