Milen/level/level.go

74 lines
1.2 KiB
Go

package level
type Rank uint8
const (
RankRecruit Rank = iota
RankMember
RankApprentice
RankAdept
RankExperienced
RankVeteran
RankProfessional
RankElite
RankEpic
RankElder
RankLegend
RankInsane
RankGodlike
RankDemigod
)
func (r Rank) String() string {
switch r {
case RankRecruit:
return "Recruit"
case RankMember:
return "Member"
case RankApprentice:
return "Apprentice"
case RankAdept:
return "Adept"
case RankExperienced:
return "Experienced"
case RankVeteran:
return "Veteran"
case RankProfessional:
return "Professional"
case RankElite:
return "Elite"
case RankEpic:
return "Epic"
case RankElder:
return "Elder"
case RankLegend:
return "Legend"
case RankInsane:
return "Insane"
case RankGodlike:
return "Godlike"
case RankDemigod:
return "Demigod"
}
return "Unknown"
}
func GetLevelFromXP(xp int64) (Rank, int) {
// Level starting from 1
level := int(xp/600 + 1)
rank := 0
// Get highest power of 2 we can go without touching it.
// 3 => 1 (2^1=2)
// 4 => 1 (2^1=2)
// 5 => 2 (2^2=4)
for level >= (1 << rank) {
if rank == int(RankDemigod) {
break
}
rank++
}
rank--
// Return it.
return Rank(rank), level - (1 << rank) + 1
}