From 2e5c468c8f495f064a7899799efdc959ccb6c420 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Thu, 19 Nov 2020 22:53:45 +0000 Subject: [PATCH] feat(*): Add start command for voting to start the adventure before the queue is full --- README.md | 1 + index.js | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 96d99a8..a932053 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Commands: * q pokemon ign - Join the queue (first player can set the pokemon and their in-game name) * lq - Leave the queue * sq - Show the queue +* start - Vote to start the adventure early, without the full number of players (all players in the queue must use this to start early) * cq - Clear the queue (admin-only, not to be shared with normal users) * ru - Remove a tagged user from the queue (admin-only, not to be shared with normal users) * dc - Delete an adventure channel (only the captain can use this) diff --git a/index.js b/index.js index fe5d99f..fe47fd6 100644 --- a/index.js +++ b/index.js @@ -78,6 +78,7 @@ const logger = winston.createLogger({ //} let playerQueue = []; +let playerVotes = []; let pokemonName = ''; let captainInGameName = ''; @@ -110,7 +111,7 @@ function generateRandomCode() { } function checkQueue(message) { - if (playerQueue.length === config.maxqueuesize) { + if (playerQueue.length === config.maxqueuesize || playerQueue.length == playerVotes.length) { // Queue is full logger.log('info', 'Queue is full'); @@ -308,6 +309,42 @@ client.on('message', function (message) { message.channel.send(`${message.author.username} you can't leave the queue before you've joined it!`); } + // Vote to start early with only the users currently in the queue + if(command === 'start') { + // Delete the message with the bot command + if (config.deletecommandstoggle) { + message.delete(); + } + + logger.info(`${message.author.username} used command ${command} in ${message.channel.name}`); + + if(playerQueue.length == 0) { + message.channel.send(`You can only vote to start an adventure early if there is a queue. You can start a queue by using the command ${config.prefix}help`); + } + else if(playerVotes.length == 0) { + // First player to vote + playerVotes.push(message.author); + + message.channel.send(`=====\n${message.author.username} voted to start the adventure.\nAll other players in the queue must also vote to start the adventure for it to begin without ${maxqueuesize} players.`); + } + else if (playerVotes.includes(message.author.username)) { + message.channel.send(`=====\n${message.author.username} has already voted to start the adventure.\nAll other players in the queue must also vote to start the adventure for it to begin without ${maxqueuesize} players.`); + } + else if (playerVotes.length < maxqueuesize) { + // Not the first player to vote + playerVotes.push(message.author); + + if (playerVotes.length < maxqueuesize) { + // Still not all players voted + message.channel.send(`=====\n${message.author.username} voted to start the adventure.\nAll other players in the queue must also vote to start the adventure for it to begin without ${maxqueuesize} players.`); + } + else { + // All players have voted, we can start the adventure early + checkQueue(message); + } + } + } + // Show the queue if(command === 'sq') { // Delete the message with the bot command @@ -393,6 +430,7 @@ The commands available to you are: - ${command}q - Join the queue (first player can set the pokemon and their in-game name) - ${command}lq - Leave the queue - ${command}sq - Show the queue +- ${command}start - Vote to start the adventure early, without the full number of players (all players in the queue must use this to start early) - ${command}dc - Delete an adventure channel (only the captain can use this) `; @@ -406,6 +444,7 @@ The commands available to you are: - ${command}q - Join the queue (first player can set the pokemon and their in-game name) - ${command}lq - Leave the queue - ${command}sq - Show the queue +- ${command}start - Vote to start the adventure early, without the full number of players (all players in the queue must use this to start early) - ${command}cq - Clear the queue (admin-only, not to be shared with normal users) - ${command}ru - Remove a tagged user from the queue (admin-only, not to be shared with normal users) - ${command}dc - Delete an adventure channel (only the captain can use this)