2019-11-27 09:07:48 +07:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"PermissionGacha/db"
|
2019-12-13 10:48:51 +07:00
|
|
|
"PermissionGacha/modules/config"
|
2019-11-27 09:07:48 +07:00
|
|
|
)
|
|
|
|
|
2019-12-13 10:48:51 +07:00
|
|
|
var levelUpRequirementCache = make([]int, len(config.LevelRoles))
|
2019-11-27 09:07:48 +07:00
|
|
|
|
|
|
|
func init() {
|
2019-12-13 10:48:51 +07:00
|
|
|
for i := 1; i <= len(config.LevelRoles); i++ {
|
2019-11-27 09:07:48 +07:00
|
|
|
//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]
|
|
|
|
}
|
2019-12-13 10:48:51 +07:00
|
|
|
if level > len(config.LevelRoles) {
|
2019-11-27 09:07:48 +07:00
|
|
|
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
|
|
|
|
}
|
2019-12-13 10:48:51 +07:00
|
|
|
for i := 0; i < len(config.LevelRoles); i++ {
|
2019-11-27 09:07:48 +07:00
|
|
|
if levelUpRequirementCache[i] > xp {
|
|
|
|
return i + 1, xp - levelUpRequirementCache[i-1]
|
|
|
|
}
|
|
|
|
}
|
2019-12-13 10:48:51 +07:00
|
|
|
return len(config.LevelRoles), xp - levelUpRequirementCache[len(config.LevelRoles)-1]
|
2019-11-27 09:07:48 +07:00
|
|
|
}
|