Home > Back-end >  Implement small % chance function?
Implement small % chance function?

Time:12-17

I have made a command for my discord bot (v13) that works like this:

user 1 pings user 2
bot checks db to see if user 1's reputation is higher than user 2
if higher or equal to, user 1 wins and user 2 receives health decrease
if lower, user 1 loses and receives health decrease.

However, I want to add to it so that even if user 1 has 50 points less max, they still have a (small, 25%) chance of winning. So if user 1 has 70 and user 2 has 100, user 2 could still be defeated. I'm not sure how exactly to go about this, any help is appreciated.

const profileModel = require("../models/profileSchema");
module.exports = {
  name: "challenge",
  description: "challenge a user",
  async execute(client, message, args, cmd, discord, profileData) {
    if (!args.length) return message.channel.send("You need to mention the user you are trying to challenge.");
    const randomNumber1 = Math.floor(Math.random() * 15)   1;
    const randomNumber2 = Math.floor(Math.random() * 15)   1;
    const target = message.mentions.users.first();
    const user = message.author || message.author;

    if (!target) return message.channel.send("This user does not exist.");

    try {
    const targetData = await profileModel.findOne({ userID: target.id });
    const userData = await profileModel.findOne({ userID: user.id });

    if (userData.reputation >= targetData.reputation){
        await profileModel.findOneAndUpdate(
            {
              userID: target.id,
            },
            {
              $inc: {
                health: -randomNumber1,
              },
            }
          );
        return message.channel.send(`You were victorious! ${target} took ${randomNumber2} damage!`);
    } else {
        await profileModel.findOneAndUpdate(
            {
              userID: user.id,
            },
            {
              $inc: {
                health: -randomNumber1,
              },
            }
          );
        return message.channel.send(`You were defeated! ${user} took ${randomNumber2} damage!`);
    }
  } catch (err) {
      console.log(err);
  }
  }
};
    let profileData;
    try {
        profileData = await profileModel.findOne({ userID: message.author.id });
        if(!profileData){
            let profile = await profileModel.create({
                name: message.author.id,
                userID: message.author.id, 
                serverID: message.guild.id, 
                reputation: 0,
                health: 100,
            });
            profile.save();
        }
    } catch (err) {
      console.log(err);
    }

CodePudding user response:

Using Math.random() is a way to do it. You could do something like:

// User 1 has challenged user 2
if(user1.reputation < user2.reputation) {
  // user1 presumably loses, but...
  if(Math.floor(Math.random() * 4) === 0) {
    // Here, we generate a random number, either 0, 1, 2, or 3. If 0 is picked (25% chance)...
    // User 1 is lucky!
    // User 1 still wins because of chance
  } else {
    // Oops, bad choice user1, you just lost. Luck didn't like you either
  }
} else {
  // User 1's reputation is higher
}

More documentation on Math.random(): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

  • Related