-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo_queue.go
More file actions
54 lines (50 loc) · 1.14 KB
/
Copy pathinfo_queue.go
File metadata and controls
54 lines (50 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"strings"
"sync"
"time"
)
// A tiny, throttled queue of /be-info lookups for players missing details.
var (
infoQueue = map[string]struct{}{}
infoQueueMu sync.Mutex
lastInfoSent time.Time
infoCooldown = 500 * time.Millisecond
)
// queueInfoRequest enqueues a be-info for name when details are incomplete.
func queueInfoRequest(name string) {
name = strings.TrimSpace(name)
if name == "" {
return
}
playersMu.RLock()
p, ok := players[name]
playersMu.RUnlock()
if ok {
if p.Class != "" && p.Gender != "" && p.Race != "" && p.clan != "" {
return // no need
}
}
infoQueueMu.Lock()
infoQueue[name] = struct{}{}
infoQueueMu.Unlock()
}
// maybeEnqueueInfo sets pendingCommand to "/be-info <name>" when throttled and
// a name is queued. Returns true if it queued a command.
func maybeEnqueueInfo() bool {
if pendingCommand != "" {
return false
}
if time.Since(lastInfoSent) < infoCooldown {
return false
}
infoQueueMu.Lock()
defer infoQueueMu.Unlock()
for name := range infoQueue {
pendingCommand = "/be-info " + name
delete(infoQueue, name)
lastInfoSent = time.Now()
return true
}
return false
}