74 lines
1.4 KiB
Go
74 lines
1.4 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 ClearCache(discordID string) {
|
|
levelMutex.Lock()
|
|
defer levelMutex.Unlock()
|
|
delete(lastKnownLevel, discordID)
|
|
}
|
|
|
|
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 {
|
|
timeToRoll := newLevel - oldLevel
|
|
if oldLevel == 0 {
|
|
timeToRoll--
|
|
}
|
|
err = gacha.RollReward(s, checkID, timeToRoll)
|
|
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()
|
|
}
|
|
}
|