From 741afbc1e8c8d6f6699b0464e9fd284ed9a2d5bd Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 7 May 2020 16:48:57 -0400 Subject: [PATCH] Time multiplayer games out if both players drop out --- commands/games-mp/balloon-pop.js | 12 ++++++++++++ commands/games-mp/battle.js | 9 +++++++++ commands/games-mp/connect-four.js | 13 +++++++++++-- commands/games-mp/dots-and-boxes.js | 13 +++++++++++-- commands/games-mp/emoji-emoji-revolution.js | 9 ++++++++- commands/games-mp/guesspionage.js | 12 ++++++++++-- commands/games-mp/lie-swatter.js | 9 ++++++++- commands/games-mp/quiz-duel.js | 11 ++++++++++- commands/games-mp/russian-roulette.js | 2 +- commands/games-mp/tic-tac-toe.js | 13 +++++++++++-- package.json | 2 +- 11 files changed, 92 insertions(+), 13 deletions(-) diff --git a/commands/games-mp/balloon-pop.js b/commands/games-mp/balloon-pop.js index e24e010a..b3132015 100644 --- a/commands/games-mp/balloon-pop.js +++ b/commands/games-mp/balloon-pop.js @@ -42,6 +42,7 @@ module.exports = class BalloonPopCommand extends Command { let winner = null; let remains = 500; let turns = 0; + let lastTurnTimeout = false; while (!winner) { const user = userTurn ? msg.author : opponent; let pump; @@ -54,6 +55,7 @@ module.exports = class BalloonPopCommand extends Command { pump = await verify(msg.channel, user); } if (pump) { + if (lastTurnTimeout) lastTurnTimeout = false; remains -= randomRange(25, 75); const popped = Math.floor(Math.random() * remains); if (popped <= 0) { @@ -67,11 +69,21 @@ module.exports = class BalloonPopCommand extends Command { userTurn = !userTurn; } } else { + if (pump !== 0 && lastTurnTimeout) lastTurnTimeout = false; + if (pump === 0) { + if (lastTurnTimeout) { + winner = 'time'; + break; + } else { + lastTurnTimeout = true; + } + } turns = 0; userTurn = !userTurn; } } this.client.games.delete(msg.channel.id); + if (winner === 'time') return msg.say('Game ended due to inactivity.'); return msg.say(`And the winner is... ${winner}! Great job!`); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-mp/battle.js b/commands/games-mp/battle.js index cd8a03d7..ac5585c3 100644 --- a/commands/games-mp/battle.js +++ b/commands/games-mp/battle.js @@ -36,6 +36,7 @@ module.exports = class BattleCommand extends Command { return msg.say('Looks like they declined...'); } } + let lastTurnTimeout = false; while (!battle.winner) { const choice = await battle.attacker.chooseAction(msg); if (choice === 'attack') { @@ -80,11 +81,19 @@ module.exports = class BattleCommand extends Command { } else if (choice === 'failed:time') { await msg.say(`Time's up, ${battle.attacker}!`); battle.reset(); + if (lastTurnTimeout) { + battle.winner = 'time'; + break; + } else { + lastTurnTimeout = true; + } } else { await msg.say('I do not understand what you want to do.'); } + if (choice !== 'failed:time' && lastTurnTimeout) lastTurnTimeout = false; } this.client.games.delete(msg.channel.id); + if (battle.winner === 'time') return msg.say('Game ended due to inactivity.'); return msg.say(`The match is over! Congrats, ${battle.winner}!`); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-mp/connect-four.js b/commands/games-mp/connect-four.js index 807fecb5..6e087979 100644 --- a/commands/games-mp/connect-four.js +++ b/commands/games-mp/connect-four.js @@ -49,6 +49,7 @@ module.exports = class ConnectFourCommand extends Command { let userTurn = true; let winner = null; const colLevels = [5, 5, 5, 5, 5, 5, 5]; + let lastTurnTimeout = false; while (!winner && board.some(row => row.includes(null))) { const user = userTurn ? msg.author : opponent; const sign = userTurn ? 'user' : 'oppo'; @@ -71,8 +72,14 @@ module.exports = class ConnectFourCommand extends Command { }); if (!turn.size) { await msg.say('Sorry, time is up!'); - userTurn = !userTurn; - continue; + if (lastTurnTimeout) { + winner = 'time'; + break; + } else { + lastTurnTimeout = true; + userTurn = !userTurn; + continue; + } } const choice = turn.first().content; if (choice.toLowerCase() === 'end') { @@ -83,9 +90,11 @@ module.exports = class ConnectFourCommand extends Command { board[colLevels[i]][i] = sign; colLevels[i] -= 1; if (this.verifyWin(board)) winner = userTurn ? msg.author : opponent; + if (lastTurnTimeout) lastTurnTimeout = false; userTurn = !userTurn; } this.client.games.delete(msg.channel.id); + if (winner === 'time') return msg.say('Game ended due to inactivity.'); return msg.say(winner ? `Congrats, ${winner}!` : 'Looks like it\'s a draw...'); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/commands/games-mp/dots-and-boxes.js b/commands/games-mp/dots-and-boxes.js index e19e8214..7e98d30e 100644 --- a/commands/games-mp/dots-and-boxes.js +++ b/commands/games-mp/dots-and-boxes.js @@ -41,6 +41,7 @@ module.exports = class DotsAndBoxesCommand extends Command { const oppoOwned = []; let userTurn = true; let winner = null; + let lastTurnTimeout = false; while (taken.length < 40) { const user = userTurn ? msg.author : opponent; await msg.say(stripIndents` @@ -83,8 +84,14 @@ module.exports = class DotsAndBoxesCommand extends Command { }); if (!turn.size) { await msg.say('Sorry, time is up!'); - userTurn = !userTurn; - continue; + if (lastTurnTimeout) { + winner = 'time'; + break; + } else { + lastTurnTimeout = true; + userTurn = !userTurn; + continue; + } } const choice = turn.first().content; if (choice.toLowerCase() === 'end') { @@ -112,7 +119,9 @@ module.exports = class DotsAndBoxesCommand extends Command { } else { userTurn = !userTurn; } + if (lastTurnTimeout) lastTurnTimeout = false; } + if (winner === 'time') return msg.say('Game ended due to inactivity.'); winner = userOwned.length === oppoOwned.length ? null : userOwned.length > oppoOwned.length ? msg.author : opponent; diff --git a/commands/games-mp/emoji-emoji-revolution.js b/commands/games-mp/emoji-emoji-revolution.js index 165b8f46..6a9a5c0f 100644 --- a/commands/games-mp/emoji-emoji-revolution.js +++ b/commands/games-mp/emoji-emoji-revolution.js @@ -46,6 +46,7 @@ module.exports = class EmojiEmojiRevolutionCommand extends Command { let turn = 0; let aPts = 0; let oPts = 0; + let lastTurnTimeout = false; while (turn < 10) { ++turn; const num = Math.floor(Math.random() * emojis.length); @@ -58,7 +59,12 @@ module.exports = class EmojiEmojiRevolutionCommand extends Command { }); if (!win.size) { await msg.say('Hmm... No one even tried that round.'); - continue; + if (lastTurnTimeout) { + break; + } else { + lastTurnTimeout = true; + continue; + } } const winner = win.first().author; if (winner.id === msg.author.id) ++aPts; @@ -68,6 +74,7 @@ module.exports = class EmojiEmojiRevolutionCommand extends Command { **${msg.author.username}:** ${aPts} **${opponent.username}:** ${oPts} `); + if (lastTurnTimeout) lastTurnTimeout = false; } this.client.games.delete(msg.channel.id); if (aPts === oPts) return msg.say('It\'s a tie!'); diff --git a/commands/games-mp/guesspionage.js b/commands/games-mp/guesspionage.js index b0a529ff..340801aa 100644 --- a/commands/games-mp/guesspionage.js +++ b/commands/games-mp/guesspionage.js @@ -61,6 +61,7 @@ module.exports = class GuesspionageCommand extends Command { } const used = []; const userTurn = awaitedPlayers.slice(0); + let lastTurnTimeout = false; while (userTurn.length) { ++turn; const mainUser = pts.get(userTurn[0]).user; @@ -110,8 +111,14 @@ module.exports = class GuesspionageCommand extends Command { time: 30000 }); if (!everyoneElse.size) { - await msg.say('Come on guys, get in the game!'); - continue; + if (lastTurnTimeout) { + await msg.say('Game ended due to inactivity.'); + break; + } else { + await msg.say('Come on guys, get in the game!'); + lastTurnTimeout = true; + continue; + } } const higherLower = everyoneElse.map(res => ({ guess: res.content.toLowerCase(), id: res.author.id })); for (const answer of higherLower) { @@ -136,6 +143,7 @@ module.exports = class GuesspionageCommand extends Command { ${userTurn.length ? '_Next round starting in 10 seconds..._' : ''} `); + if (lastTurnTimeout) lastTurnTimeout = false; if (userTurn.length) await delay(10000); } this.client.games.delete(msg.channel.id); diff --git a/commands/games-mp/lie-swatter.js b/commands/games-mp/lie-swatter.js index 2b8aa1d5..e230707c 100644 --- a/commands/games-mp/lie-swatter.js +++ b/commands/games-mp/lie-swatter.js @@ -56,6 +56,7 @@ module.exports = class LieSwatterCommand extends Command { }); } const questions = await this.fetchQuestions(); + let lastTurnTimeout = false; while (questions.length) { ++turn; const question = questions[0]; @@ -81,7 +82,12 @@ module.exports = class LieSwatterCommand extends Command { }); if (!msgs.size) { await msg.say(`No answers? Well, it was ${question.answer ? 'true' : 'a lie'}.`); - continue; + if (lastTurnTimeout) { + break; + } else { + lastTurnTimeout = true; + continue; + } } const answers = msgs.map(res => { let answer; @@ -102,6 +108,7 @@ module.exports = class LieSwatterCommand extends Command { ${questions.length ? '_Next round starting in 5 seconds..._' : ''} `); + if (lastTurnTimeout) lastTurnTimeout = false; if (questions.length) await delay(5000); } this.client.games.delete(msg.channel.id); diff --git a/commands/games-mp/quiz-duel.js b/commands/games-mp/quiz-duel.js index aeccbb80..5a0c10a7 100644 --- a/commands/games-mp/quiz-duel.js +++ b/commands/games-mp/quiz-duel.js @@ -54,6 +54,7 @@ module.exports = class QuizDuelCommand extends Command { let winner = null; let userPoints = 0; let oppoPoints = 0; + let lastTurnTimeout = false; while (!winner) { const question = await this.fetchQuestion(); await msg.say(stripIndents` @@ -79,7 +80,13 @@ module.exports = class QuizDuelCommand extends Command { }); if (!msgs.size) { await msg.say(`Sorry, time is up! It was ${question.correct}.`); - continue; + if (lastTurnTimeout) { + winner = 'time'; + break; + } else { + lastTurnTimeout = true; + continue; + } } const result = msgs.first(); const userWin = result.author.id === msg.author.id; @@ -92,8 +99,10 @@ module.exports = class QuizDuelCommand extends Command { ${userWin ? '' : '**'}${oppoPoints}${userWin ? '' : '**'} `; await msg.say(`Nice one, ${result.author}! The score is now ${score}!`); + if (lastTurnTimeout) lastTurnTimeout = false; } this.client.games.delete(msg.channel.id); + if (winner === 'time') return msg.say('Game ended due to inactivity.'); if (!winner) return msg.say('Aww, no one won...'); return msg.say(`Congrats, ${winner}, you won!`); } catch (err) { diff --git a/commands/games-mp/russian-roulette.js b/commands/games-mp/russian-roulette.js index 1b1e73a6..3a3fdd21 100644 --- a/commands/games-mp/russian-roulette.js +++ b/commands/games-mp/russian-roulette.js @@ -13,7 +13,7 @@ module.exports = class RussianRouletteCommand extends Command { args: [ { key: 'opponent', - prompt: 'What user would you like to gunfight?', + prompt: 'What user would you like to play against?', type: 'user', default: () => this.client.user } diff --git a/commands/games-mp/tic-tac-toe.js b/commands/games-mp/tic-tac-toe.js index 5944c2c9..565be573 100644 --- a/commands/games-mp/tic-tac-toe.js +++ b/commands/games-mp/tic-tac-toe.js @@ -37,6 +37,7 @@ module.exports = class TicTacToeCommand extends Command { const taken = []; let userTurn = true; let winner = null; + let lastTurnTimeout = false; while (!winner && taken.length < 9) { const user = userTurn ? msg.author : opponent; const sign = userTurn ? 'X' : 'O'; @@ -62,8 +63,14 @@ module.exports = class TicTacToeCommand extends Command { }); if (!turn.size) { await msg.say('Sorry, time is up!'); - userTurn = !userTurn; - continue; + if (lastTurnTimeout) { + winner = 'time'; + break; + } else { + userTurn = !userTurn; + lastTurnTimeout = true; + continue; + } } const choice = turn.first().content; if (choice.toLowerCase() === 'end') { @@ -73,9 +80,11 @@ module.exports = class TicTacToeCommand extends Command { sides[Number.parseInt(choice, 10) - 1] = sign; taken.push(choice); if (this.verifyWin(sides)) winner = userTurn ? msg.author : opponent; + if (lastTurnTimeout) lastTurnTimeout = false; userTurn = !userTurn; } this.client.games.delete(msg.channel.id); + if (winner === 'time') return msg.say('Game ended due to inactivity.'); return msg.say(winner ? `Congrats, ${winner}!` : 'Oh... The cat won.'); } catch (err) { this.client.games.delete(msg.channel.id); diff --git a/package.json b/package.json index c77655f1..f610e5d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "114.7.2", + "version": "114.7.3", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": {