Toke statistics now recorded in user DB document.
This commit is contained in:
parent
3902554e6b
commit
0b84c51cbf
|
|
@ -16,6 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||||
|
|
||||||
//Local Imports
|
//Local Imports
|
||||||
const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema');
|
const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema');
|
||||||
|
const {userModel} = require('../../schemas/userSchema');
|
||||||
|
|
||||||
|
|
||||||
module.exports = class tokebot{
|
module.exports = class tokebot{
|
||||||
|
|
@ -37,7 +38,7 @@ module.exports = class tokebot{
|
||||||
this.cooldownCounter = 0;
|
this.cooldownCounter = 0;
|
||||||
|
|
||||||
//Create tokers list
|
//Create tokers list
|
||||||
this.tokers = [];
|
this.tokers = new Map();
|
||||||
|
|
||||||
//Load in toke commands from the DB
|
//Load in toke commands from the DB
|
||||||
this.refreshCommands();
|
this.refreshCommands();
|
||||||
|
|
@ -53,34 +54,37 @@ module.exports = class tokebot{
|
||||||
|
|
||||||
//If we found a toke
|
//If we found a toke
|
||||||
if(foundToke != -1){
|
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){
|
if(this.tokeTimer == null && this.cooldownTimer == null){
|
||||||
//Call-out toke start
|
//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]}`);
|
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
|
//Set a full minute on our toke timer
|
||||||
this.tokeCounter = this.tokeTime;
|
this.tokeCounter = this.tokeTime;
|
||||||
|
|
||||||
//Add the toking user to
|
//Add the toking user to the tokers map
|
||||||
this.tokers.push(commandObj.socket.user.user);
|
this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0]);
|
||||||
|
|
||||||
//kick-off the count-down
|
//kick-off the count-down
|
||||||
this.tokeTimer = setTimeout(this.countdown.bind(this), 1000)
|
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){
|
}else if(this.cooldownTimer == null){
|
||||||
//If the tokeTimer is popping but the cooldownTimer has fucked off (a toke is in progress)
|
//look for user in tokers map
|
||||||
const foundToker = this.tokers.indexOf(commandObj.socket.user.user);
|
const foundToker = this.tokers.get(commandObj.socket.user.user);
|
||||||
|
|
||||||
|
|
||||||
//if the user has not yet joined the toke
|
//if the user has not yet joined the toke
|
||||||
if(foundToker == -1){
|
if(foundToker == null){
|
||||||
//Call-out toke join
|
//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!`);
|
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
|
//Add the toking user to the tokers map
|
||||||
this.tokers.push(commandObj.socket.user.user);
|
this.tokers.set(commandObj.socket.user.user, commandObj.argumentArray[0]);
|
||||||
|
//If the user is already in the toke
|
||||||
}else{
|
}else{
|
||||||
|
//Tell them to fuck off
|
||||||
this.chatHandler.relayTokeWhisper(commandObj.socket, "You're already taking part in this toke!");
|
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{
|
}else{
|
||||||
//if the cooldownTimer exists (we're cooling down the toke)
|
//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.`);
|
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}...`);
|
this.chatHandler.relayTokeCallout(`${this.tokeCounter}...`);
|
||||||
//if the toke is over
|
//if the toke is over
|
||||||
}else if(this.tokeCounter < 0){
|
}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
|
//if we had multiple tokers
|
||||||
}else{
|
if(this.tokers.size > 1){
|
||||||
//call out the toke
|
//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
|
//Set the toke cooldown
|
||||||
this.cooldownCounter = this.cooldownTime;
|
this.cooldownCounter = this.cooldownTime;
|
||||||
this.cooldownTimer = setTimeout(this.cooldown.bind(this), 1000);
|
this.cooldownTimer = setTimeout(this.cooldown.bind(this), 1000);
|
||||||
|
|
||||||
//Empty out the tokers array
|
//Empty out the tokers array
|
||||||
this.tokers = [];
|
this.tokers = new Map;
|
||||||
|
|
||||||
//Null out our timer
|
//Null out our timer
|
||||||
this.tokeTimer = null;
|
this.tokeTimer = null;
|
||||||
|
|
|
||||||
|
|
@ -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
|
//methods
|
||||||
userSchema.methods.checkPass = function(pass){
|
userSchema.methods.checkPass = function(pass){
|
||||||
return hashUtil.comparePassword(pass, this.pass);
|
return hashUtil.comparePassword(pass, this.pass);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue