Skip to content

Commit

Permalink
[FEATURE] show specific progress from one level to the next (#151)
Browse files Browse the repository at this point in the history
* add/minxp to reset the progressbar width

* add/minxp to reset the progressbar width
  • Loading branch information
Mittelblut9 authored Apr 14, 2023
1 parent 8f88667 commit e176c38
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/Rank.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ class Rank {
name: null,
color: "#FFFFFF"
},
renderEmojis: false
renderEmojis: false,
minXP: {
data: 0,
color: "#FFFFFF"
}
};

// Load default fonts
Expand Down Expand Up @@ -290,6 +294,19 @@ class Rank {
this.data.requiredXP.color = color && typeof color === "string" ? color : "#FFFFFF";
return this;
}

/**
* Set minimum xp of the current level
* @param {number} data Required xp
* @param {string} color Color
* @returns {Rank}
*/
setMinXP(data, color = "#FFFFFF") {
if (typeof data !== "number") throw new Error(`Min xp data type must be a number, received ${typeof data}!`);
this.data.minXP.data = data;
this.data.minXP.color = color && typeof color === "string" ? color : "#FFFFFF";
return this;
}

/**
* Set current xp
Expand Down Expand Up @@ -628,6 +645,15 @@ class Rank {
if (rx <= 0) return 1;
if (cx > rx) return parseInt(this.data.progressBar.width) || 0;

if (this.data.minXP.data > 0) {
const mx = this.data.minXP.data;
if (cx < mx) return 0;

const nx = cx - mx;
const nr = rx - mx;
return (nx * 615) / nr;
}

let width = (cx * 615) / rx;
if (width > this.data.progressBar.width) width = this.data.progressBar.width;
return parseInt(width) || 0;
Expand Down
Binary file modified test/card.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const rank = new canvacord.Rank()
.setProgressBar(["#14C49E", "#FF0000"], "GRADIENT", true)
.setProgressBarTrack("#FFFFFF")
.setUsername("Snowflake")
.setDiscriminator("0007");
.setDiscriminator("0007")
.setMinXP(300);

rank.build()
.then(data => {
Expand Down

0 comments on commit e176c38

Please sign in to comment.