This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
forked from faceit/go-steam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tf2.go
91 lines (74 loc) · 2.1 KB
/
tf2.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
/*
Provides access to TF2 Game Coordinator functionality.
*/
package tf2
import (
"github.com/13k/go-steam"
pbtf2 "github.com/13k/go-steam-resources/protobuf/tf2"
"github.com/13k/go-steam/protocol/gc"
"github.com/13k/go-steam/tf2/protocol"
)
const AppID = 440
// To use any methods of this, you'll need to SetPlaying(true) and wait for
// the GCReadyEvent.
type TF2 struct {
client *steam.Client
}
// Creates a new TF2 instance and registers it as a packet handler
func New(client *steam.Client) *TF2 {
t := &TF2{client}
client.GC.RegisterPacketHandler(t)
return t
}
func (t *TF2) SetPlaying(playing bool) {
if playing {
t.client.GC.SetGamesPlayed(AppID)
} else {
t.client.GC.SetGamesPlayed()
}
}
func (t *TF2) SetItemPosition(itemID, position uint64) error {
msgType := uint32(pbtf2.EGCItemMsg_k_EMsgGCSetSingleItemPosition)
gcMsg := gc.NewStructMessage(AppID, msgType, &protocol.MsgGCSetItemPosition{
AssetID: itemID,
Position: position,
})
return t.client.GC.Write(gcMsg)
}
// recipe -2 = wildcard
func (t *TF2) CraftItems(items []uint64, recipe int16) error {
msgType := uint32(pbtf2.EGCItemMsg_k_EMsgGCCraft)
return t.client.GC.Write(gc.NewStructMessage(AppID, msgType, &protocol.MsgGCCraft{
Recipe: recipe,
Items: items,
}))
}
func (t *TF2) DeleteItem(itemID uint64) error {
msgType := uint32(pbtf2.EGCItemMsg_k_EMsgGCDelete)
gcMsg := gc.NewStructMessage(AppID, msgType, &protocol.MsgGCDeleteItem{
ItemID: itemID,
})
return t.client.GC.Write(gcMsg)
}
func (t *TF2) NameItem(toolID, target uint64, name string) error {
msgType := uint32(pbtf2.EGCItemMsg_k_EMsgGCNameItem)
return t.client.GC.Write(gc.NewStructMessage(AppID, msgType, &protocol.MsgGCNameItem{
Tool: toolID,
Target: target,
Name: name,
}))
}
type GCReadyEvent struct{}
func (t *TF2) HandleGCPacket(packet *gc.Packet) {
if packet.AppID != AppID {
return
}
switch pbtf2.EGCBaseClientMsg(packet.MsgType) {
case pbtf2.EGCBaseClientMsg_k_EMsgGCClientWelcome:
t.handleWelcome(packet)
}
}
func (t *TF2) handleWelcome(_ *gc.Packet) {
// the packet's body is pretty useless
t.client.Emit(&GCReadyEvent{})
}