55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package level
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
|
|
"PermissionGacha/db"
|
|
"PermissionGacha/modules/config"
|
|
)
|
|
|
|
var levelUpRequirementCache = make([]int, len(config.LevelRoles))
|
|
|
|
func init() {
|
|
for i := 1; i <= len(config.LevelRoles); i++ {
|
|
//Constructed on Desmos:
|
|
//y=2^{\frac{x}{2}}+.1x^{2}+1.2x-1
|
|
fi := float64(i)
|
|
levelUpRequirementCache[i-1] = int(10000 * (math.Pow(2, fi/2) + 0.1*fi*fi + 1.2*fi - 1))
|
|
}
|
|
}
|
|
|
|
func GetLevelAndXP(discordID string) (int, int, error) {
|
|
rawXP, err := db.GetXP(discordID)
|
|
if err != nil {
|
|
return 1, 0, fmt.Errorf("level: GetLevelAndXP: error requesting XP: %w", err)
|
|
}
|
|
level, xp := ConvertXPIntoLevel(rawXP)
|
|
return level, xp, nil
|
|
}
|
|
|
|
func GetLevelUpRequirement(level int) int {
|
|
if level <= 0 {
|
|
return 0
|
|
}
|
|
if level == 1 {
|
|
return levelUpRequirementCache[0]
|
|
}
|
|
if level > len(config.LevelRoles) {
|
|
return levelUpRequirementCache[len(levelUpRequirementCache)-1]
|
|
}
|
|
return levelUpRequirementCache[level-1] - levelUpRequirementCache[level-2]
|
|
}
|
|
|
|
func ConvertXPIntoLevel(xp int) (int, int) {
|
|
if xp < levelUpRequirementCache[0] {
|
|
return 1, xp
|
|
}
|
|
for i := 0; i < len(config.LevelRoles); i++ {
|
|
if levelUpRequirementCache[i] > xp {
|
|
return i + 1, xp - levelUpRequirementCache[i-1]
|
|
}
|
|
}
|
|
return len(config.LevelRoles), xp - levelUpRequirementCache[len(config.LevelRoles)-1]
|
|
}
|