This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/modules/level/level.go

54 lines
1.1 KiB
Go

package level
import (
"fmt"
"math"
"PermissionGacha/db"
)
var levelUpRequirementCache [30]int
func init() {
for i := 1; i <= 30; 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(levelUpRequirementCache) {
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 < 30; i++ {
if levelUpRequirementCache[i] > xp {
return i + 1, xp - levelUpRequirementCache[i-1]
}
}
return 30, xp - levelUpRequirementCache[29]
}