2019-11-18 07:30:21 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-18 14:38:03 +07:00
|
|
|
"fmt"
|
2019-11-18 07:30:21 +07:00
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const guildID = "645550569573842945"
|
|
|
|
|
|
|
|
var levelMutex sync.Mutex
|
|
|
|
var levelQueue = make(map[string]bool)
|
|
|
|
var assignedLevels = make(map[string]int)
|
|
|
|
var rolesFromLevel []string
|
|
|
|
|
|
|
|
func init() {
|
2019-11-18 14:38:03 +07:00
|
|
|
levels, _ := ioutil.ReadFile("roles.txt")
|
2019-11-18 07:30:21 +07:00
|
|
|
rolesFromLevel = strings.Split(string(levels), "\n")
|
|
|
|
for k, v := range rolesFromLevel {
|
|
|
|
rolesFromLevel[k] = strings.TrimSpace(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addToLevelQueue(discordID string) {
|
|
|
|
levelMutex.Lock()
|
|
|
|
defer levelMutex.Unlock()
|
|
|
|
levelQueue[discordID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func listenToLevelUpdate(dg *discordgo.Session) {
|
|
|
|
for {
|
2019-11-18 14:38:03 +07:00
|
|
|
time.Sleep(5 * time.Second)
|
2019-11-18 07:30:21 +07:00
|
|
|
levelMutex.Lock()
|
|
|
|
for k, v := range levelQueue {
|
|
|
|
if !v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
levelQueue[k] = false
|
|
|
|
level, _ := getLevelAndXP(dg, k)
|
|
|
|
if assignedLevels[k] != level {
|
|
|
|
assignedLevels[k] = level
|
2019-11-18 14:38:03 +07:00
|
|
|
go checkForGachaEligibility(dg, k, rolesFromLevel[level-1])
|
2019-11-18 07:30:21 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
levelMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|