Skip to content

Commit

Permalink
pointer change to betting
Browse files Browse the repository at this point in the history
streaming seems to be buggy
  • Loading branch information
liampauling committed Jul 11, 2017
1 parent d9a6740 commit 4506a11
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 45 deletions.
56 changes: 29 additions & 27 deletions betting.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package gofair

import "time"

type eventType struct {
Id string `json:"id"`
Name string `json:"name"`
}

type eventTypeResult struct {
MarketCount int `json:"marketCount"`
EventType *eventType `json:"eventType"`
EventType eventType `json:"eventType"`
}

type competition struct {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down
35 changes: 25 additions & 10 deletions streaming/cache.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -328,20 +332,21 @@ 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
}

func (cache *MarketCache) UpdateCache(changeMessage MarketChangeMessage, marketChange MarketChange) {
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 {
Expand All @@ -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))
}
2 changes: 1 addition & 1 deletion streaming/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
8 changes: 4 additions & 4 deletions streaming/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down
4 changes: 1 addition & 3 deletions streaming/stream.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package streaming

import (
"log"
)
import "log"

type Stream interface {
OnSubscribe(ChangeMessage MarketChangeMessage)
Expand Down

0 comments on commit 4506a11

Please sign in to comment.