This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlobby.go
85 lines (78 loc) · 2.11 KB
/
lobby.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"log"
"sort"
"time"
"github.com/maxsupermanhd/go-wz/lobby"
)
func routineLobbyKeepalive(closechan <-chan struct{}) {
interval := time.Duration(cfg.GetDSInt(5, "lobbyPollInterval")) * time.Second
for {
resp, err := lobby.LobbyLookup()
if err != nil {
log.Printf("Failed to lookup lobby: %s", err.Error())
}
log.Printf("Lobby has %d rooms", len(resp.Rooms))
populateLobby(resp.Rooms)
select {
case <-closechan:
return
case <-time.After(interval):
}
}
}
func populateLobby(lr []lobby.LobbyRoom) {
if !cfg.GetDSBool(false, "allowSpawn") {
log.Println("Room spawning disabled")
return
}
maxlobby := cfg.GetDSInt(8, "spawnCutoutLobbyRooms")
if len(lr) >= maxlobby {
log.Printf("Queue processing paused, too many rooms in lobby (%d >= %d)", len(lr), maxlobby)
return
}
maxrunning := cfg.GetDSInt(18, "spawnCutoutRunningRooms")
runningRooms := 0
instancesLock.Lock()
for _, v := range instances {
if v.state.Load() == int64(instanceStateInGame) {
runningRooms++
}
}
instancesLock.Unlock()
if runningRooms >= maxrunning {
log.Printf("Queue processing paused, too many running rooms (%d >= %d)", runningRooms, maxlobby)
return
}
queuesK, ok := cfg.GetKeys("queues")
if !ok {
log.Println("Queue processing paused, queues not defined in config")
return
}
sort.Strings(queuesK)
for _, queueName := range queuesK {
if cfg.GetDSBool(false, "queues", queueName, "disabled") {
continue
}
li := isQueueInLobby(queueName)
if li != 0 {
log.Printf("Queue %q in lobby with instance id %v", queueName, li)
continue
}
log.Printf("Queue %q is missing from lobby, spawning new one...", queueName)
gi, err := generateInstance(cfg.DupSubTree("queues", queueName))
if err != nil {
log.Printf("Failed to generate instance: %s", err.Error())
giid := int64(-1)
if gi != nil {
giid = gi.Id
releaseInstance(gi)
}
discordPostError("%s Lobby queue failed to generate instance %d: %s", time.Now(), giid, err.Error())
continue
}
gi.QueueName = queueName
// log.Printf("Generated instance: %s", spew.Sdump(gi))
go spawnRunner(gi)
}
}