【问题标题】:Implement small % chance function?实现小百分比机会功能?
【发布时间】:2022-01-20 03:26:05
【问题描述】:

我为我的 discord bot (v13) 做了一个命令,如下所示:

用户 1 ping 用户 2
bot 检查 db 以查看用户 1 的信誉是否高于用户 2
如果高于或等于,则用户 1 获胜,用户 2 的生命值减少
如果较低,则用户 1 失去并获得健康减少。

但是,我想补充一点,即使用户 1 的最大分数减少 50 分,他们仍然有(很小,25%)获胜的机会。所以如果用户 1 有 70 并且用户 2 有 100,用户 2 仍然可能被击败。我不知道该怎么做,感谢任何帮助。

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);
    }

【问题讨论】:

    标签: node.js discord discord.js bots


    【解决方案1】:

    使用Math.random() 是一种方法。你可以这样做:

    // 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
    }
    

    更多关于Math.random()的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

    【讨论】:

      猜你喜欢
      • 2016-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2015-03-05
      相关资源
      最近更新 更多