diff --git a/src/app/channel/tokebot.js b/src/app/channel/tokebot.js index fc47814..438bf0b 100644 --- a/src/app/channel/tokebot.js +++ b/src/app/channel/tokebot.js @@ -16,6 +16,7 @@ along with this program. If not, see .*/ //Local Imports const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema'); +const {userModel} = require('../../schemas/userSchema'); module.exports = class tokebot{ @@ -37,7 +38,7 @@ module.exports = class tokebot{ this.cooldownCounter = 0; //Create tokers list - this.tokers = []; + this.tokers = new Map(); //Load in toke commands from the DB this.refreshCommands(); @@ -53,34 +54,37 @@ module.exports = class tokebot{ //If we found a toke if(foundToke != -1){ - //If there is no active toke or cooldown + //If there is no active toke or cooldown (new toke) if(this.tokeTimer == null && this.cooldownTimer == null){ //Call-out toke start this.chatHandler.relayTokeCallout(`A group toke has been started by ${commandObj.socket.user.user} from #${commandObj.socket.chan}! We'll be taking a toke in 60 seconds - join in by posting !${commandObj.argumentArray[0]}`); //Set a full minute on our toke timer this.tokeCounter = this.tokeTime; - //Add the toking user to - this.tokers.push(commandObj.socket.user.user); + //Add the toking user to the tokers map + this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0]); //kick-off the count-down this.tokeTimer = setTimeout(this.countdown.bind(this), 1000) + //If the tokeTimer is popping but the cooldownTimer has fucked off (a toke is in progress) }else if(this.cooldownTimer == null){ - //If the tokeTimer is popping but the cooldownTimer has fucked off (a toke is in progress) - const foundToker = this.tokers.indexOf(commandObj.socket.user.user); - + //look for user in tokers map + const foundToker = this.tokers.get(commandObj.socket.user.user); //if the user has not yet joined the toke - if(foundToker == -1){ + if(foundToker == null){ //Call-out toke join this.chatHandler.relayTokeCallout(`${commandObj.socket.user.user} has joined the toke from #${commandObj.socket.chan}! Post !${commandObj.argumentArray[0]} to take part!`); - //Add the toking user to - this.tokers.push(commandObj.socket.user.user); + //Add the toking user to the tokers map + this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0]); + //If the user is already in the toke }else{ + //Tell them to fuck off this.chatHandler.relayTokeWhisper(commandObj.socket, "You're already taking part in this toke!"); } + //Otherwise (there isn't a toke timer, but there is a cooldown timer. AKA: we're in cooldown) }else{ //if the cooldownTimer exists (we're cooling down the toke) this.chatHandler.relayTokeWhisper(commandObj.socket, `Please wait ${this.cooldownCounter} seconds before starting a new group toke.`); @@ -101,22 +105,25 @@ module.exports = class tokebot{ this.chatHandler.relayTokeCallout(`${this.tokeCounter}...`); //if the toke is over }else if(this.tokeCounter < 0){ - //if we only had one toker - if(this.tokers.length > 1){ - //call out the solo toke - this.chatHandler.relayTokeCallout(`Take a toke ${this.tokers.join(', ')}! ${this.tokers.length} tokers!`); //if we had multiple tokers - }else{ + if(this.tokers.size > 1){ //call out the toke - this.chatHandler.relayTokeCallout(`Take a toke ${this.tokers[0]}.`); + this.chatHandler.relayTokeCallout(`Take a toke ${Array.from(this.tokers.keys()).join(', ')}! ${this.tokers.size} tokers!`); + //if we only had one toker + }else{ + //call out the solo toke + this.chatHandler.relayTokeCallout(`Take a toke ${Array.from(this.tokers.keys())[0]}.`); } + //Asynchronously tattoo the toke into the users documents within the database so that tokebot doesn't have to wait or worry about DB transactions + userModel.tattooToke(this.tokers); + //Set the toke cooldown this.cooldownCounter = this.cooldownTime; this.cooldownTimer = setTimeout(this.cooldown.bind(this), 1000); //Empty out the tokers array - this.tokers = []; + this.tokers = new Map; //Null out our timer this.tokeTimer = null; diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index efe2cca..7051b30 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -174,6 +174,35 @@ userSchema.statics.authenticate = async function(user, pass){ } } +userSchema.statics.tattooToke = function(tokemap){ + //For each toke, asynchronously: + tokemap.forEach(async (toke, user) => { + //get user + const userDB = await this.findOne({user}); + + //Check that the user exists (might come in handy for future treez.one integration?) + if(userDB != null){ + var tokeCount = userDB.tokes.get(toke); + + //if this is the first time using this toke command + if(tokeCount == null){ + //set toke count to one + tokeCount = 1; + //otherwise + }else{ + //increment tokecount + tokeCount++; + } + + //Set the toke count for the specific command + userDB.tokes.set(toke, tokeCount); + + //Save the user doc to the database + await userDB.save(); + } + }); +} + //methods userSchema.methods.checkPass = function(pass){ return hashUtil.comparePassword(pass, this.pass);