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

58 lines
1.1 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() {
oldLevel, err := roles.GetOldLevel(s, id)
if err != nil {
log.Error(s, err)
return
}
if level > oldLevel {
gacha.RollReward(s, id, level-oldLevel)
roles.GiveLevelRoles(s, id, oldLevel, level)
}
}()
}
}
levelMutex.Unlock()
}
}