2019-11-27 09:07:48 +07:00
|
|
|
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
|
2019-11-29 14:32:17 +07:00
|
|
|
go func(checkID string, newLevel int) {
|
|
|
|
oldLevel, err := roles.GetOldLevel(s, checkID)
|
2019-11-27 09:07:48 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Error(s, err)
|
|
|
|
return
|
|
|
|
}
|
2019-11-29 14:32:17 +07:00
|
|
|
if newLevel > oldLevel {
|
2019-12-13 10:14:32 +07:00
|
|
|
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)
|
|
|
|
}
|
2019-11-27 09:07:48 +07:00
|
|
|
}
|
2019-11-29 14:32:17 +07:00
|
|
|
}(id, level)
|
2019-11-27 09:07:48 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
levelMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|