feat: Implement level

master
Luther Wen Xu 2020-05-30 21:31:50 +07:00
parent 11fed61073
commit 879b482b7a
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
3 changed files with 115 additions and 2 deletions

@ -4,6 +4,7 @@ import (
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/level"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
@ -24,11 +25,18 @@ func handleLevel(dg *discordgo.Session, m *discordgo.MessageCreate, arguments []
util.SendFailEmbed(dg, m.ChannelID, "Error", "Cannot find requested member.")
return
}
rank, level := level.GetLevelFromXP(xp)
embed := &discordgo.MessageEmbed{
Fields: []*discordgo.MessageEmbedField{
&discordgo.MessageEmbedField{
Name: "Active Time",
Value: util.FormatPlural(xp, "second", "seconds") + " (or " + util.FormatTime(xp) + ")",
Name: "Level",
Value: rank.String() + " " + util.FormatRomanNumeral(level),
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Active Time",
Value: util.FormatPlural(xp, "second", "seconds") + " (or " + util.FormatTime(xp) + ")",
Inline: true,
},
&discordgo.MessageEmbedField{
Name: "Internal User ID",

@ -0,0 +1,73 @@
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
}

@ -44,3 +44,35 @@ func FormatPlural(count int64, singular string, plural string) string {
}
return strconv.FormatInt(count, 10) + " " + plural
}
func FormatRomanNumeral(number int) string {
if number < 1 || number > 3999 {
return strconv.Itoa(number)
}
conversions := []struct {
value int
digit string
}{
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"},
}
roman := ""
for _, conversion := range conversions {
for number >= conversion.value {
roman += conversion.digit
number -= conversion.value
}
}
return roman
}