-
Notifications
You must be signed in to change notification settings - Fork 0
/
trader.go
111 lines (91 loc) · 2.54 KB
/
trader.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"log"
"time"
"go.uber.org/zap"
steam "github.com/Philipp15b/go-steam"
"github.com/pkg/errors"
"github.com/playnet-public/flagenv"
)
var (
steamAPIKey = flagenv.String("apiKey", "", "The Steam Web-API key needed to fetch specific information.")
steamLogin = flagenv.String("login", "", "The Steam user name of the trader account.")
steamPass = flagenv.String("password", "", "The Steam password of the trader account.")
steamSharedSecret = flagenv.String("sharedSecret", "", "The shared secret of a Steam account.")
session *steam.Session
processTimeout = flagenv.Int("processTimeout", 60, "The timeout to continue processing all offers.")
logger *zap.Logger
)
func main() {
flagenv.Parse()
l, err := zap.NewProductionConfig().Build()
if err != nil {
log.Fatalln(err.Error())
}
logger = l
logger.Info("starting new Steam session")
session = steam.NewSessionWithAPIKey(*steamAPIKey)
if err := login(); err != nil {
logger.Fatal(err.Error())
}
logger.Info("starting trade offer loop")
for {
if err := processActiveOffers(); err != nil {
logger.Fatal(err.Error())
}
time.Sleep(time.Duration(*processTimeout) * time.Second)
}
}
func getTimeDiff() (time.Duration, error) {
timeTip, err := steam.GetTimeTip()
if err != nil {
return 0, err
}
return time.Duration(timeTip.Time - time.Now().Unix()), nil
}
func login() error {
timeDiff, err := getTimeDiff()
if err != nil {
return err
}
return errors.Wrap(session.Login(*steamLogin, *steamPass, *steamSharedSecret, timeDiff), "steam login")
}
func processActiveOffers() error {
logger.Info("fetching tradeoffers")
tOffers, err := session.GetTradeOffers(
steam.TradeFilterRecvOffers|steam.TradeFilterActiveOnly,
time.Now(),
)
if err != nil {
return err
}
for _, v := range tOffers.Descriptions {
fmt.Println(v.Name)
}
logger.Info(fmt.Sprintf("fetched %d tradeoffers", len(tOffers.ReceivedOffers)))
for _, v := range tOffers.ReceivedOffers {
var partnerSID steam.SteamID
partnerSID.ParseDefaults(v.Partner)
l := logger.With(
zap.Uint64("offer_id", v.ID),
zap.Uint64("partner_steamid64", uint64(partnerSID)),
zap.Uint8("state", v.State),
)
l.Info("checking offer")
if v.State == steam.TradeStateActive {
if len(v.SendItems) > 0 {
l.Info("cancel offer: no items received!")
if err := v.Cancel(session); err != nil {
return err
}
} else {
l.Info("accepting offer")
if err := v.Accept(session); err != nil {
return err
}
}
}
}
return nil
}