我如何获得下一级别所需的XP

时间:2019-12-13 03:31:27

标签: node.js discord discord.js

我正在制造一个不和谐的bot,该机器人可以跟踪消息级别和xp;

if (message.guild.id == "509850431640305664") {
const key = `${message.guild.id}-${message.author.id}`;

villa.points.ensure(`${message.guild.id}-${message.author.id}`, {
user: message.author.id,
guild: message.guild.id,
points: 0,
level: 0
});

villa.points.inc(key, "points");

const curLevel = Math.floor(.25 * Math.sqrt(villa.points.get(key, "points")));
if (villa.points.get(key, "level") < curLevel) {
    message.author.send({embed: {color: '0x188088',description: `\\? **Level-up** | You advanced to level **${curLevel}**!`,footer: {text: `${villa.footerText}`, icon_url: `${villa.footerImage}`}}});
    villa.points.set(key, curLevel, "level");
}

if (message.author.id == "153638972013281282") {
    console.log(Math.floor(.25 * Math.sqrt(villa.points.get(key, "points"))));
    console.log(Math.floor(Math.sqrt(villa.points.get(key, "level")) / .25));
}
}

但是我不确定如何使XP达到升级的水平;我用下面的行计算水平;

const curLevel = Math.floor(.25 * Math.sqrt(villa.points.get(key, "points")));

有没有一种方法可以反向执行该表达式以使XP升至下一级别?

1 个答案:

答案 0 :(得分:0)

每个等级所需的经验值可以通过以下方式计算:

  

XP =(等级* 4)^ 2

当我们知道当前的XP时,我们只需要从所需数量中减去该数量即可找到余数:

var curPoints = villa.points.get(key, "points");         //Our current XP
var curLevel = Math.floor(.25 * Math.sqrt(curPoints));

var pointsNextLevel = Math.pow((curLevel + 1) * 4, 2);   //Required XP
var pointsRequired = pointsNextLevel - curPoints;        //Result