diff options
author | Wim <wim@42.be> | 2017-06-22 01:00:27 +0200 |
---|---|---|
committer | Wim <wim@42.be> | 2017-06-22 01:00:27 +0200 |
commit | 1f914618538920db4bfec7b106ee97038b157c9b (patch) | |
tree | 6bd0ab107fe1673dbacdf9dfd10004289cd7bfab /vendor/github.com/Philipp15b/go-steam/trade/trade.go | |
parent | 1f9874102aaca09ce5e0289beff376c307b8c57b (diff) | |
download | matterbridge-msglm-1f914618538920db4bfec7b106ee97038b157c9b.tar.gz matterbridge-msglm-1f914618538920db4bfec7b106ee97038b157c9b.tar.bz2 matterbridge-msglm-1f914618538920db4bfec7b106ee97038b157c9b.zip |
Add vendor (steam)
Diffstat (limited to 'vendor/github.com/Philipp15b/go-steam/trade/trade.go')
-rw-r--r-- | vendor/github.com/Philipp15b/go-steam/trade/trade.go | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/vendor/github.com/Philipp15b/go-steam/trade/trade.go b/vendor/github.com/Philipp15b/go-steam/trade/trade.go new file mode 100644 index 00000000..4889f407 --- /dev/null +++ b/vendor/github.com/Philipp15b/go-steam/trade/trade.go @@ -0,0 +1,122 @@ +package trade + +import ( + "errors" + "time" + + "github.com/Philipp15b/go-steam/steamid" + "github.com/Philipp15b/go-steam/trade/tradeapi" +) + +const pollTimeout = time.Second + +type Trade struct { + ThemId steamid.SteamId + + MeReady, ThemReady bool + + lastPoll time.Time + queuedEvents []interface{} + api *tradeapi.Trade +} + +func New(sessionId, steamLogin, steamLoginSecure string, other steamid.SteamId) *Trade { + return &Trade{ + other, + false, false, + time.Unix(0, 0), + nil, + tradeapi.New(sessionId, steamLogin, steamLoginSecure, other), + } +} + +func (t *Trade) Version() uint { + return t.api.Version +} + +// Returns all queued events and removes them from the queue without performing a HTTP request, like Poll() would. +func (t *Trade) Events() []interface{} { + qe := t.queuedEvents + t.queuedEvents = nil + return qe +} + +func (t *Trade) onStatus(status *tradeapi.Status) error { + if !status.Success { + return errors.New("trade: returned status not successful! error message: " + status.Error) + } + + if status.NewVersion { + t.api.Version = status.Version + + t.MeReady = status.Me.Ready == true + t.ThemReady = status.Them.Ready == true + } + + switch status.TradeStatus { + case tradeapi.TradeStatus_Complete: + t.addEvent(&TradeEndedEvent{TradeEndReason_Complete}) + case tradeapi.TradeStatus_Cancelled: + t.addEvent(&TradeEndedEvent{TradeEndReason_Cancelled}) + case tradeapi.TradeStatus_Timeout: + t.addEvent(&TradeEndedEvent{TradeEndReason_Timeout}) + case tradeapi.TradeStatus_Failed: + t.addEvent(&TradeEndedEvent{TradeEndReason_Failed}) + case tradeapi.TradeStatus_Open: + // nothing + default: + // ignore too + } + + t.updateEvents(status.Events) + return nil +} + +func (t *Trade) updateEvents(events tradeapi.EventList) { + if len(events) == 0 { + return + } + + var lastLogPos uint + for i, event := range events { + if i < t.api.LogPos { + continue + } + if event.SteamId != t.ThemId { + continue + } + + if lastLogPos < i { + lastLogPos = i + } + + switch event.Action { + case tradeapi.Action_AddItem: + t.addEvent(&ItemAddedEvent{newItem(event)}) + case tradeapi.Action_RemoveItem: + t.addEvent(&ItemRemovedEvent{newItem(event)}) + case tradeapi.Action_Ready: + t.ThemReady = true + t.addEvent(new(ReadyEvent)) + case tradeapi.Action_Unready: + t.ThemReady = false + t.addEvent(new(UnreadyEvent)) + case tradeapi.Action_SetCurrency: + t.addEvent(&SetCurrencyEvent{ + newCurrency(event), + event.OldAmount, + event.NewAmount, + }) + case tradeapi.Action_ChatMessage: + t.addEvent(&ChatEvent{ + event.Text, + }) + } + } + + t.api.LogPos = uint(lastLogPos) + 1 +} + +func (t *Trade) addEvent(event interface{}) { + t.queuedEvents = append(t.queuedEvents, event) +} |