diff --git a/betting.go b/betting.go index 9bc25f3..615136f 100644 --- a/betting.go +++ b/betting.go @@ -1,5 +1,7 @@ package gofair +import "time" + type eventType struct { Id string `json:"id"` Name string `json:"name"` @@ -7,7 +9,7 @@ type eventType struct { type eventTypeResult struct { MarketCount int `json:"marketCount"` - EventType *eventType `json:"eventType"` + EventType eventType `json:"eventType"` } type competition struct { @@ -18,17 +20,17 @@ type competition struct { type competitionResult struct { MarketCount int `json:"marketCount"` CompetitionRegion string `json:"competitionRegion"` - Competition *competition `json:"competition"` + Competition competition `json:"competition"` } type timeRange struct { - From string `json:"from"` - To string `json:"to"` + From time.Time `json:"from"` + To time.Time `json:"to"` } type timeRangeResult struct { MarketCount int `json:"marketCount"` - TimeRange *timeRange `json:"timeRange"` + TimeRange timeRange `json:"timeRange"` } type event struct { @@ -42,7 +44,7 @@ type event struct { type eventResult struct { MarketCount int `json:"marketCount"` - Event *event `json:"event"` + Event event `json:"event"` } type marketTypeResult struct { @@ -61,21 +63,21 @@ type venueResult struct { } type marketCatalogueDescription struct { - BettingType string `json:"bettingType"` - BSPMarket bool `json:"bspMarket"` - DiscountAllowed bool `json:"discountAllowed"` - MarketBaseRate float32 `json:"marketBaseRate"` - MarketTime string `json:"marketTime"` - MarketType string `json:"marketType"` - PersistenceEnabled bool `json:"persistenceEnabled"` - Regulator string `json:"regulator"` - Rules string `json:"rules"` - RulesHasDate bool `json:"rulesHasDate"` - SuspendDate string `json:"suspendTime"` - TurnInPlayEnabled bool `json:"turnInPlayEnabled"` - Wallet string `json:"wallet"` - EachWayDivisor float32 `json:"eachWayDivisor"` - Clarifications string `json:"clarifications"` + BettingType string `json:"bettingType"` + BSPMarket bool `json:"bspMarket"` + DiscountAllowed bool `json:"discountAllowed"` + MarketBaseRate float32 `json:"marketBaseRate"` + MarketTime time.Time `json:"marketTime"` + MarketType string `json:"marketType"` + PersistenceEnabled bool `json:"persistenceEnabled"` + Regulator string `json:"regulator"` + Rules string `json:"rules"` + RulesHasDate bool `json:"rulesHasDate"` + SuspendDate time.Time `json:"suspendTime"` + TurnInPlayEnabled bool `json:"turnInPlayEnabled"` + Wallet string `json:"wallet"` + EachWayDivisor float32 `json:"eachWayDivisor"` + Clarifications string `json:"clarifications"` } type metadata struct { @@ -94,12 +96,12 @@ type marketCatalogue struct { MarketId string `json:"marketId"` MarketName string `json:"marketName"` TotalMatched float32 `json:"totalMatched"` - MarketStartTime string `json:"marketStartTime"` - Competition *competition `json:"competition"` - Event *event `json:"event"` - EventType *eventType `json:"eventType"` - MarketCatalogueDescription *marketCatalogueDescription `json:"description"` - Runners *[]runnerCatalogue `json:"runners"` + MarketStartTime time.Time `json:"marketStartTime"` + Competition competition `json:"competition"` + Event event `json:"event"` + EventType eventType `json:"eventType"` + MarketCatalogueDescription marketCatalogueDescription `json:"description"` + Runners []runnerCatalogue `json:"runners"` } func (b *Betting) ListEventTypes(filter MarketFilter) ([]eventTypeResult, error) { diff --git a/streaming/cache.go b/streaming/cache.go index a6a90da..5f64418 100644 --- a/streaming/cache.go +++ b/streaming/cache.go @@ -1,13 +1,17 @@ package streaming -import "log" +import ( + "log" + "strconv" + "time" +) func CreateMarketCache(changeMessage MarketChangeMessage, marketChange MarketChange) *MarketCache { cache := MarketCache{ changeMessage.PublishTime, marketChange.MarketId, marketChange.TradedVolume, - marketChange.MarketDefinition, + *marketChange.MarketDefinition, make(map[int64]RunnerCache), } for _, runnerChange := range marketChange.RunnerChange { @@ -328,10 +332,10 @@ func (cache *RunnerCache) UpdateCache(change RunnerChange) { } type MarketCache struct { - PublishTime int64 + PublishTime int MarketId string - TradedVolume *float64 - MarketDefinition *MarketDefinition + TradedVolume float64 + MarketDefinition MarketDefinition Runners map[int64]RunnerCache } @@ -339,9 +343,10 @@ func (cache *MarketCache) UpdateCache(changeMessage MarketChangeMessage, marketC cache.PublishTime = changeMessage.PublishTime if marketChange.MarketDefinition != nil { - cache.MarketDefinition = marketChange.MarketDefinition + cache.MarketDefinition = *marketChange.MarketDefinition + log.Println("Update", marketChange.MarketDefinition) } - if marketChange.TradedVolume != nil { + if marketChange.TradedVolume != 0 { cache.TradedVolume = marketChange.TradedVolume } if marketChange.RunnerChange != nil { @@ -353,7 +358,17 @@ func (cache *MarketCache) UpdateCache(changeMessage MarketChangeMessage, marketC } } } - tem, _ := cache.Runners[7424945] - log.Println(tem.SelectionId, *tem.LastTradedPrice, len(tem.Traded.Prices), *tem.TradedVolume, - len(tem.AvailableToBack.Prices)) + tem, _ := cache.Runners[12787754] + s := strconv.Itoa(cache.PublishTime) + log.Println(MsToTime(s), tem.SelectionId, *tem.LastTradedPrice, len(tem.Traded.Prices), *tem.TradedVolume, + len(tem.AvailableToBack.Prices), cache.MarketDefinition.Status, cache.MarketDefinition.BetDelay, cache.MarketDefinition.CrossMatching) +} + +func MsToTime(ms string) (time.Time) { + msInt, err := strconv.ParseInt(ms, 10, 64) + if err != nil { + return time.Time{} + } + + return time.Unix(0, msInt*int64(time.Millisecond)) } diff --git a/streaming/listener.go b/streaming/listener.go index 1bb2f3d..a88ac21 100644 --- a/streaming/listener.go +++ b/streaming/listener.go @@ -19,7 +19,7 @@ func (l *Listener) OnData(ChangeMessage MarketChangeMessage) { //todo check unique id //todo error handler - switch *ChangeMessage.Operation { + switch ChangeMessage.Operation { case "connection": l.onConnection(ChangeMessage) case "status": diff --git a/streaming/resources.go b/streaming/resources.go index fa15708..a58cbb6 100644 --- a/streaming/resources.go +++ b/streaming/resources.go @@ -34,7 +34,7 @@ type MarketDefinition struct { SuspendTime string `json:"suspendTime"` DiscountAllowed bool `json:"discountAllowed"` PersistenceEnabled bool `json:"persistenceEnabled"` - Runners *[]RunnerDefinition `json:"runners"` + Runners []RunnerDefinition `json:"runners"` Version int64 `json:"version"` EventTypeId string `json:"eventTypeId"` Complete bool `json:"complete"` @@ -67,15 +67,15 @@ type MarketChange struct { Image bool `json:"img"` Conflated bool `json:"con"` MarketId string `json:"id"` - TradedVolume *float64 `json:"tv"` + TradedVolume float64 `json:"tv"` RunnerChange []RunnerChange `json:"rc"` MarketDefinition *MarketDefinition `json:"marketDefinition"` } type MarketChangeMessage struct { MarketChanges []MarketChange `json:"mc"` - PublishTime int64 `json:"pt"` - Operation *string `json:"op"` + PublishTime int `json:"pt"` + Operation string `json:"op"` ChangeType string `json:"ct"` InitialClk string `json:"initialClk"` Clk string `json:"clk"` diff --git a/streaming/stream.go b/streaming/stream.go index e26b90b..cdf412e 100644 --- a/streaming/stream.go +++ b/streaming/stream.go @@ -1,8 +1,6 @@ package streaming -import ( - "log" -) +import "log" type Stream interface { OnSubscribe(ChangeMessage MarketChangeMessage)