Home > Software design >  Needed XP and Total XP for leveling system, discord.js
Needed XP and Total XP for leveling system, discord.js

Time:12-15

I'm building a bot that stores xp for every message entered. I'm good with my needed xp counter for the next level. My question is should I also add a key value pair for Total XP? My Mongo doc only stores XP and then once it is equal or greater than the needed XP it increments the level by 1, and then it resets xp to 0 while retaining the extra.

I might need the total XP at some point, so I was wondering whether I should just add a key for it on the Mongo Doc or maybe just query it with math like this:

Math.floor(50 * (previous level ^ 2)   xp)

Here's my Mongo Document:

{
"name": "Ben",
"discordId": "1234567890",
"level": 1,
"xp": 15
}

Here's a sample equation:

Math.floor(50 * (level ^ 2))

XP needed to level up from 1 to 5:

  1. 50
  2. 200
  3. 450
  4. 800
  5. 1250

Total XP per level:

  1. 50
  2. 250
  3. 700
  4. 1500
  5. 2750

CodePudding user response:

one day I made a bot like this and I've seen the XP was broken and people were levelup too quickly. In that case, I need to change all the ranks manually (Too boring).

So I can recommend you to don't store level / current level xp but store only a key with the totalXP. And with this totalXP, make a function to "parse" this xp by calculating the actual level.

Example: I want level up each 200 XP and the user has 860xp.

function convertXP(xp) {
 return { level: Math.floor(xp / 200), xp: xp % 200, totalXP: xp };
}

And you'll call this function just after getting the user xp By example with mongoose:

User.findOne({discordId: user.discordId}, function(err,obj) {
 interaction.reply({content: `You're level ${convertXP(obj.xp).level} with ${convertXP(obj.xp).xp} / 200 for the next level`
});
  • Related