forked from paralin/go-dota2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_lobby.go
69 lines (61 loc) · 1.71 KB
/
client_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
package dota2
import (
"context"
"github.com/paralin/go-dota2/cso"
gcccm "github.com/paralin/go-dota2/protocol"
gcmm "github.com/paralin/go-dota2/protocol"
gcm "github.com/paralin/go-dota2/protocol"
)
// CreateLobby attempts to create a lobby with details.
func (d *Dota2) CreateLobby(details *gcccm.CMsgPracticeLobbySetDetails) {
d.write(uint32(gcm.EDOTAGCMsg_k_EMsgGCPracticeLobbyCreate), &gcccm.CMsgPracticeLobbyCreate{
PassKey: details.PassKey,
LobbyDetails: details,
})
}
// LeaveCreateLobby attempts to leave any current lobby and creates a new one.
func (d *Dota2) LeaveCreateLobby(ctx context.Context, details *gcccm.CMsgPracticeLobbySetDetails, destroyOldLobby bool) error {
cacheCtr, err := d.cache.GetContainerForTypeID(uint32(cso.Lobby))
if err != nil {
return err
}
eventCh, eventCancel, err := cacheCtr.Subscribe()
if err != nil {
return err
}
defer eventCancel()
var wasInNoLobby bool
for {
lobbyObj := cacheCtr.GetOne()
if lobbyObj != nil {
lob := lobbyObj.(*gcmm.CSODOTALobby)
le := d.le.WithField("lobby-id", lob.GetLobbyId())
if wasInNoLobby {
le.Debug("successfully created lobby")
return nil
}
le.Debug("attempting to leave lobby")
if destroyOldLobby && lob.GetLeaderId() == d.client.SteamId().ToUint64() {
resp, err := d.DestroyLobby(ctx)
if err != nil {
return err
}
le.WithField("result", resp.GetResult().String()).Debug("destroy lobby result")
}
if lob.GetState() != gcmm.CSODOTALobby_UI {
d.AbandonLobby()
}
d.LeaveLobby()
} else {
wasInNoLobby = true
d.le.Debug("creating lobby")
d.CreateLobby(details)
}
select {
case <-ctx.Done():
return ctx.Err()
case event := <-eventCh:
_ = event
}
}
}