This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/modules/level/scheduler.go

64 lines
1.2 KiB
Go

package level
import (
"sync"
"time"
"github.com/bwmarrin/discordgo"
"PermissionGacha/modules/gacha"
"PermissionGacha/modules/log"
"PermissionGacha/modules/roles"
)
var (
levelMutex sync.Mutex
levelQueue = make(map[string]bool)
lastKnownLevel = make(map[string]int)
)
func QueueCheck(discordID string) {
levelMutex.Lock()
defer levelMutex.Unlock()
levelQueue[discordID] = true
}
func ScheduleUpdates(s *discordgo.Session) {
for {
time.Sleep(5 * time.Second)
levelMutex.Lock()
for id, isQueued := range levelQueue {
if !isQueued {
continue
}
delete(levelQueue, id)
level, _, err := GetLevelAndXP(id)
if err != nil {
log.Error(s, err)
continue
}
if lastKnownLevel[id] != level {
lastKnownLevel[id] = level
go func(checkID string, newLevel int) {
oldLevel, err := roles.GetOldLevel(s, checkID)
if err != nil {
log.Error(s, err)
return
}
if newLevel > oldLevel {
err = gacha.RollReward(s, checkID, newLevel-oldLevel)
if err != nil {
log.Error(s, err)
}
err = roles.GiveLevelRoles(s, checkID, oldLevel, newLevel)
if err != nil {
log.Error(s, err)
}
}
}(id, level)
}
}
levelMutex.Unlock()
}
}