modules/level: Make total number of levels flexible

master
Luther Wen Xu 2019-12-13 18:48:51 +07:00
parent eb23870513
commit e9d2db3023
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
1 changed files with 6 additions and 5 deletions

@ -5,12 +5,13 @@ import (
"math"
"PermissionGacha/db"
"PermissionGacha/modules/config"
)
var levelUpRequirementCache [30]int
var levelUpRequirementCache = make([]int, len(config.LevelRoles))
func init() {
for i := 1; i <= 30; i++ {
for i := 1; i <= len(config.LevelRoles); i++ {
//Constructed on Desmos:
//y=2^{\frac{x}{2}}+.1x^{2}+1.2x-1
fi := float64(i)
@ -34,7 +35,7 @@ func GetLevelUpRequirement(level int) int {
if level == 1 {
return levelUpRequirementCache[0]
}
if level > len(levelUpRequirementCache) {
if level > len(config.LevelRoles) {
return levelUpRequirementCache[len(levelUpRequirementCache)-1]
}
return levelUpRequirementCache[level-1] - levelUpRequirementCache[level-2]
@ -44,10 +45,10 @@ func ConvertXPIntoLevel(xp int) (int, int) {
if xp < levelUpRequirementCache[0] {
return 1, xp
}
for i := 0; i < 30; i++ {
for i := 0; i < len(config.LevelRoles); i++ {
if levelUpRequirementCache[i] > xp {
return i + 1, xp - levelUpRequirementCache[i-1]
}
}
return 30, xp - levelUpRequirementCache[29]
return len(config.LevelRoles), xp - levelUpRequirementCache[len(config.LevelRoles)-1]
}