mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-02-23 06:26:38 +08:00
37 lines
686 B
Go
37 lines
686 B
Go
package tgc
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type BotWorker struct {
|
|
mu sync.RWMutex
|
|
bots map[int64][]string
|
|
currIdx map[int64]int
|
|
}
|
|
|
|
func NewBotWorker() *BotWorker {
|
|
return &BotWorker{
|
|
bots: make(map[int64][]string),
|
|
currIdx: make(map[int64]int),
|
|
}
|
|
}
|
|
|
|
func (w *BotWorker) Set(bots []string, channelID int64) {
|
|
w.mu.Lock()
|
|
defer w.mu.Unlock()
|
|
if _, ok := w.bots[channelID]; ok {
|
|
return
|
|
}
|
|
w.bots[channelID] = bots
|
|
w.currIdx[channelID] = 0
|
|
}
|
|
|
|
func (w *BotWorker) Next(channelID int64) (string, int) {
|
|
w.mu.RLock()
|
|
defer w.mu.RUnlock()
|
|
bots := w.bots[channelID]
|
|
index := w.currIdx[channelID]
|
|
w.currIdx[channelID] = (index + 1) % len(bots)
|
|
return bots[index], index
|
|
}
|