Merge pull request #1 from anhgelus/feat/xp-reducer
[Feat] Add XP reducer
This commit is contained in:
commit
6b595b8b11
8 changed files with 303 additions and 9 deletions
|
@ -31,6 +31,7 @@ func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||
return
|
||||
}
|
||||
c := GetCopaing(m.Author.ID, m.GuildID)
|
||||
LastEventUpdate(s, c)
|
||||
// add xp
|
||||
trimmed := utils.TrimMessage(strings.ToLower(m.Content))
|
||||
m.Member.User = m.Author
|
||||
|
@ -67,6 +68,7 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
|
|||
if e.Member.User.Bot {
|
||||
return
|
||||
}
|
||||
LastEventUpdate(s, GetCopaing(e.UserID, e.GuildID))
|
||||
cfg := config.GetGuildConfig(e.GuildID)
|
||||
if cfg.IsDisabled(e.ChannelID) {
|
||||
return
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package xp
|
||||
|
||||
import "math"
|
||||
import (
|
||||
"github.com/anhgelus/gokord"
|
||||
"math"
|
||||
)
|
||||
|
||||
func XPMessage(length uint, diversity uint) uint {
|
||||
return uint(math.Floor(
|
||||
|
@ -27,7 +30,12 @@ func XPForLevel(level uint) uint {
|
|||
}
|
||||
|
||||
func Lose(time uint, xp uint) uint {
|
||||
if gokord.Debug {
|
||||
return uint(math.Floor(
|
||||
math.Pow(float64(time), 3) * math.Pow(10, -2+math.Log(float64(time))) * math.Floor(float64(xp/500)+1),
|
||||
)) // a little bit faster to lose xp
|
||||
}
|
||||
return uint(math.Floor(
|
||||
math.Pow(float64(time), 2) * math.Pow(10, -2+math.Log(float64(time/85))) * math.Floor(float64(xp/500)),
|
||||
math.Pow(float64(time), 2) * math.Pow(10, -2+math.Log(float64(time/85))) * math.Floor(float64(xp/500)+1),
|
||||
))
|
||||
}
|
||||
|
|
118
xp/level.go
118
xp/level.go
|
@ -5,6 +5,8 @@ import (
|
|||
"github.com/anhgelus/les-copaings-bot/config"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
|
||||
|
@ -42,3 +44,119 @@ func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Copaing) OnNewLevel(s *discordgo.Session, level uint) {
|
||||
m, err := s.GuildMember(c.GuildID, c.DiscordID)
|
||||
if err != nil {
|
||||
utils.SendAlert(
|
||||
"xp/level.go - Getting member for new level",
|
||||
err.Error(),
|
||||
"discord_id",
|
||||
c.DiscordID,
|
||||
"guild_id",
|
||||
c.GuildID,
|
||||
)
|
||||
return
|
||||
}
|
||||
onNewLevel(s, m, level)
|
||||
}
|
||||
|
||||
func LastEventUpdate(s *discordgo.Session, c *Copaing) {
|
||||
h := c.HourSinceLastEvent()
|
||||
l := Lose(h, c.XP)
|
||||
xp := c.XPAlreadyRemoved()
|
||||
oldXP := c.XP
|
||||
if l-xp < 0 {
|
||||
utils.SendWarn("lose - xp already removed is negative", "lose", l, "xp", xp)
|
||||
c.XP = 0
|
||||
} else {
|
||||
calc := int(c.XP) - int(l) + int(c.XPAlreadyRemoved())
|
||||
if calc < 0 {
|
||||
c.XP = 0
|
||||
} else {
|
||||
c.XP = uint(calc)
|
||||
}
|
||||
}
|
||||
if oldXP != c.XP {
|
||||
lvl := Level(c.XP)
|
||||
if Level(oldXP) != lvl {
|
||||
utils.SendDebug(
|
||||
"Level changed",
|
||||
"old",
|
||||
Level(oldXP),
|
||||
"new",
|
||||
lvl,
|
||||
"discord_id",
|
||||
c.DiscordID,
|
||||
"guild_id",
|
||||
c.GuildID,
|
||||
)
|
||||
c.OnNewLevel(s, lvl)
|
||||
}
|
||||
c.Save()
|
||||
}
|
||||
c.SetLastEvent()
|
||||
}
|
||||
|
||||
func XPUpdate(s *discordgo.Session, c *Copaing) {
|
||||
oldXP := c.XP
|
||||
if oldXP == 0 {
|
||||
return
|
||||
}
|
||||
h := c.HourSinceLastEvent()
|
||||
l := Lose(h, c.XP)
|
||||
xp := c.XPAlreadyRemoved()
|
||||
if l-xp < 0 {
|
||||
utils.SendWarn("lose - xp_removed is negative", "lose", l, "xp removed", xp)
|
||||
c.AddXPAlreadyRemoved(0)
|
||||
} else {
|
||||
calc := int(c.XP) - int(l) + int(xp)
|
||||
if calc < 0 {
|
||||
c.AddXPAlreadyRemoved(c.XP)
|
||||
c.XP = 0
|
||||
} else {
|
||||
c.XP = uint(calc)
|
||||
c.AddXPAlreadyRemoved(l - xp)
|
||||
}
|
||||
}
|
||||
if oldXP != c.XP {
|
||||
lvl := Level(c.XP)
|
||||
if Level(oldXP) != lvl {
|
||||
utils.SendDebug(
|
||||
"Level updated",
|
||||
"old",
|
||||
Level(oldXP),
|
||||
"new",
|
||||
lvl,
|
||||
"discord_id",
|
||||
c.DiscordID,
|
||||
"guild_id",
|
||||
c.GuildID,
|
||||
)
|
||||
c.OnNewLevel(s, lvl)
|
||||
}
|
||||
utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID)
|
||||
c.Save()
|
||||
}
|
||||
}
|
||||
|
||||
func PeriodicReducer(s *discordgo.Session) {
|
||||
var wg sync.WaitGroup
|
||||
for _, g := range s.State.Guilds {
|
||||
for _, m := range utils.FetchGuildUser(s, g.ID) {
|
||||
if m.User.Bot {
|
||||
continue
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
c := GetCopaing(m.User.ID, g.ID)
|
||||
XPUpdate(s, c)
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait() // finish the entire guild before starting another
|
||||
utils.SendDebug("Periodic reduce, guild finished", "guild", g.Name)
|
||||
time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API and the database
|
||||
}
|
||||
utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds))
|
||||
}
|
||||
|
|
143
xp/member.go
143
xp/member.go
|
@ -1,11 +1,17 @@
|
|||
package xp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/anhgelus/gokord"
|
||||
"github.com/anhgelus/gokord/utils"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/gorm"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Copaing struct {
|
||||
|
@ -17,6 +23,11 @@ type Copaing struct {
|
|||
|
||||
var redisClient *redis.Client
|
||||
|
||||
const (
|
||||
LastEvent = "last_event"
|
||||
AlreadyRemoved = "already_removed"
|
||||
)
|
||||
|
||||
func GetCopaing(discordID string, guildID string) *Copaing {
|
||||
c := Copaing{DiscordID: discordID, GuildID: guildID}
|
||||
return c.Load()
|
||||
|
@ -42,6 +53,138 @@ func (c *Copaing) AddXP(s *discordgo.Session, m *discordgo.Member, xp uint, fn f
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Copaing) SetLastEvent() {
|
||||
client, err := getRedisClient()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error())
|
||||
return
|
||||
}
|
||||
u := c.GetUserBase()
|
||||
t := time.Now().Unix()
|
||||
err = client.Set(context.Background(), fmt.Sprintf(
|
||||
"%s:%s",
|
||||
u.GenKey(),
|
||||
LastEvent,
|
||||
), strconv.FormatInt(t, 10), 0).Err()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Setting last event", err.Error(), "time", t, "base_key", u.GenKey())
|
||||
return
|
||||
}
|
||||
err = client.Set(context.Background(), fmt.Sprintf(
|
||||
"%s:%s",
|
||||
u.GenKey(),
|
||||
AlreadyRemoved,
|
||||
), "0", 0).Err()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Setting already removed to 0", err.Error(), "time", t, "base_key", u.GenKey())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Copaing) HourSinceLastEvent() uint {
|
||||
client, err := getRedisClient()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Getting redis client (get)", err.Error())
|
||||
return 0
|
||||
}
|
||||
u := c.GetUserBase()
|
||||
res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), LastEvent))
|
||||
if errors.Is(res.Err(), redis.Nil) {
|
||||
return 0
|
||||
} else if res.Err() != nil {
|
||||
utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", u.GenKey())
|
||||
return 0
|
||||
}
|
||||
t := time.Now().Unix()
|
||||
last, err := strconv.Atoi(res.Val())
|
||||
if err != nil {
|
||||
utils.SendAlert(
|
||||
"xp/member.go - Converting time fetched into int (last event)",
|
||||
err.Error(),
|
||||
"base_key",
|
||||
u.GenKey(),
|
||||
"val",
|
||||
res.Val(),
|
||||
)
|
||||
return 0
|
||||
}
|
||||
if gokord.Debug {
|
||||
return uint(math.Floor(float64(t-int64(last)) / 60)) // not hours of unix, is minutes of unix
|
||||
}
|
||||
return utils.HoursOfUnix(t - int64(last))
|
||||
}
|
||||
|
||||
func (c *Copaing) AddXPAlreadyRemoved(xp uint) uint {
|
||||
client, err := getRedisClient()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Getting redis client (set)", err.Error())
|
||||
return 0
|
||||
}
|
||||
u := c.GetUserBase()
|
||||
exp := xp + c.XPAlreadyRemoved()
|
||||
err = client.Set(context.Background(), fmt.Sprintf(
|
||||
"%s:%s",
|
||||
u.GenKey(),
|
||||
AlreadyRemoved,
|
||||
), exp, 0).Err()
|
||||
if err != nil {
|
||||
utils.SendAlert(
|
||||
"xp/member.go - Setting already removed",
|
||||
err.Error(),
|
||||
"xp already removed",
|
||||
exp,
|
||||
"base_key",
|
||||
u.GenKey(),
|
||||
)
|
||||
return 0
|
||||
}
|
||||
return exp
|
||||
}
|
||||
|
||||
func (c *Copaing) XPAlreadyRemoved() uint {
|
||||
client, err := getRedisClient()
|
||||
if err != nil {
|
||||
utils.SendAlert("xp/member.go - Getting redis client (xp)", err.Error())
|
||||
return 0
|
||||
}
|
||||
u := c.GetUserBase()
|
||||
res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), AlreadyRemoved))
|
||||
if errors.Is(res.Err(), redis.Nil) {
|
||||
return 0
|
||||
} else if res.Err() != nil {
|
||||
utils.SendAlert("xp/member.go - Getting already removed", res.Err().Error(), "base_key", u.GenKey())
|
||||
return 0
|
||||
}
|
||||
xp, err := strconv.Atoi(res.Val())
|
||||
if err != nil {
|
||||
utils.SendAlert(
|
||||
"xp/member.go - Converting time fetched into int (already removed)",
|
||||
err.Error(),
|
||||
"base_key",
|
||||
u.GenKey(),
|
||||
"val",
|
||||
res.Val(),
|
||||
)
|
||||
return 0
|
||||
}
|
||||
if xp < 0 {
|
||||
utils.SendAlert(
|
||||
"xp/member.go - Assertion xp >= 0",
|
||||
"xp is negative",
|
||||
"base_key",
|
||||
u.GenKey(),
|
||||
"xp",
|
||||
xp,
|
||||
)
|
||||
return 0
|
||||
}
|
||||
return uint(xp)
|
||||
}
|
||||
|
||||
func (c *Copaing) GetUserBase() *gokord.UserBase {
|
||||
return &gokord.UserBase{DiscordID: c.DiscordID, GuildID: c.GuildID}
|
||||
}
|
||||
|
||||
func getRedisClient() (*redis.Client, error) {
|
||||
if redisClient == nil {
|
||||
var err error
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue