forked from paralin/go-dota2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_chat.go
96 lines (80 loc) · 2.83 KB
/
client_chat.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
86
87
88
89
90
91
92
93
94
95
96
package dota2
import (
"context"
"github.com/faceit/go-steam/protocol/gamecoordinator"
"github.com/paralin/go-dota2/events"
gcmcc "github.com/paralin/go-dota2/protocol/dota_gcmessages_client_chat"
gcm "github.com/paralin/go-dota2/protocol/dota_gcmessages_msgid"
"github.com/paralin/go-dota2/protocol/dota_shared_enums"
)
// SendChannelMessage attempts to send a message in a channel.
func (d *Dota2) SendChannelMessage(channelID uint64, message string) {
d.write(uint32(gcm.EDOTAGCMsg_k_EMsgGCChatMessage), &gcmcc.CMsgDOTAChatMessage{
ChannelId: &channelID,
Text: &message,
})
}
// SendChannelMessageEx attempts to send a message in a channel with explicit details.
// At minimum ChannelId and Text must be set.
func (d *Dota2) SendChannelMessageEx(channelID uint64, msg *gcmcc.CMsgDOTAChatMessage) {
d.write(uint32(gcm.EDOTAGCMsg_k_EMsgGCChatMessage), msg)
}
// JoinChatChannel attempts to join a chat channel by name and type.
func (d *Dota2) JoinChatChannel(ctx context.Context, name string, channelType dota_shared_enums.DOTAChatChannelTypeT) (*gcmcc.CMsgDOTAJoinChatChannelResponse, error) {
d.write(uint32(gcm.EDOTAGCMsg_k_EMsgGCJoinChatChannel), &gcmcc.CMsgDOTAJoinChatChannel{
ChannelName: &name,
ChannelType: &channelType,
})
defer d.joinChatChannelHandlers.Delete(name)
ch := make(chan *gcmcc.CMsgDOTAJoinChatChannelResponse, 1)
d.joinChatChannelHandlers.Store(name, ch)
select {
case <-ctx.Done():
return nil, ctx.Err()
case v := <-ch:
return v, nil
}
}
// handleJoinChatChannelResponse handles the response to the join chat channel request
func (d *Dota2) handleJoinChatChannelResponse(packet *gamecoordinator.GCPacket) error {
resp := &gcmcc.CMsgDOTAJoinChatChannelResponse{}
if err := d.unmarshalBody(packet, resp); err != nil {
return err
}
chanName := resp.GetChannelName()
reqCh, ok := d.joinChatChannelHandlers.Load(chanName)
if ok {
reqCh.(chan *gcmcc.CMsgDOTAJoinChatChannelResponse) <- resp
}
return nil
}
// handleChatMessage handles an incoming chat message.
func (d *Dota2) handleChatMessage(packet *gamecoordinator.GCPacket) error {
eve := &events.ChatMessage{}
resp := &eve.CMsgDOTAChatMessage
if err := d.unmarshalBody(packet, resp); err != nil {
return err
}
d.emit(eve)
return nil
}
// handleLeftChannel handles a channel leave event.
func (d *Dota2) handleLeftChannel(packet *gamecoordinator.GCPacket) error {
eve := &events.LeftChatChannel{}
resp := &eve.CMsgDOTAOtherLeftChatChannel
if err := d.unmarshalBody(packet, resp); err != nil {
return err
}
d.emit(eve)
return nil
}
// handleJoinedChannel handles a channel join event.
func (d *Dota2) handleJoinedChannel(packet *gamecoordinator.GCPacket) error {
eve := &events.JoinedChatChannel{}
resp := &eve.CMsgDOTAOtherJoinedChatChannel
if err := d.unmarshalBody(packet, resp); err != nil {
return err
}
d.emit(eve)
return nil
}