flesh out game struct
This commit is contained in:
parent
ff5f7f3a6d
commit
0a49223173
100
game.go
100
game.go
|
@ -1,17 +1,107 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "scm.dairydemon.net/filifa/mlblive/internal/statsapi"
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"scm.dairydemon.net/filifa/mlblive/internal/statsapi"
|
||||||
|
)
|
||||||
|
|
||||||
type Team struct {
|
type Team struct {
|
||||||
City string
|
Location string
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
GUID string
|
GamePk string
|
||||||
HomeTeam Team
|
HomeTeam Team
|
||||||
AwayTeam Team
|
AwayTeam Team
|
||||||
HomeScore int
|
HomeScore uint
|
||||||
AwayScore int
|
AwayScore uint
|
||||||
Plays []statsapi.Play
|
Plays []statsapi.Play
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func feedUpdate(gamePk string, ts *string) (statsapi.Play, error) {
|
||||||
|
feed, err := statsapi.Feed(gamePk)
|
||||||
|
if err != nil {
|
||||||
|
return statsapi.Play{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
*ts = feed.MetaData.TimeStamp
|
||||||
|
return feed.LiveData.Plays.CurrentPlay, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func patchUpdate(patches statsapi.DiffPatchResponse, ts *string) []statsapi.Play {
|
||||||
|
var plays []statsapi.Play
|
||||||
|
var err error
|
||||||
|
for _, patch := range patches {
|
||||||
|
var play statsapi.Play
|
||||||
|
play.Patch(patch)
|
||||||
|
plays = append(plays, play)
|
||||||
|
|
||||||
|
*ts, err = patch.Timestamp()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return plays
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *Game) Update(ts *string, updateId string) error {
|
||||||
|
var plays []statsapi.Play
|
||||||
|
|
||||||
|
patches, err := statsapi.DiffPatch(g.GamePk, *ts, updateId)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("feed update")
|
||||||
|
var p statsapi.Play
|
||||||
|
p, err = feedUpdate(g.GamePk, ts)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
plays = []statsapi.Play{p}
|
||||||
|
} else {
|
||||||
|
log.Println("patch update")
|
||||||
|
plays = patchUpdate(patches, ts)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, play := range plays {
|
||||||
|
i := play.AtBatIndex
|
||||||
|
if i >= len(g.Plays) {
|
||||||
|
g.Plays = append(g.Plays, play)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitGame(feed statsapi.FeedResponse) (*Game, error) {
|
||||||
|
hometeam := Team{
|
||||||
|
Location: feed.GameData.Teams.Home.LocationName,
|
||||||
|
Name: feed.GameData.Teams.Home.TeamName,
|
||||||
|
}
|
||||||
|
awayteam := Team{
|
||||||
|
Location: feed.GameData.Teams.Away.LocationName,
|
||||||
|
Name: feed.GameData.Teams.Away.TeamName,
|
||||||
|
}
|
||||||
|
|
||||||
|
homescore, err := feed.LiveData.Linescore.Teams.Home.Runs.Int64()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
awayscore, err := feed.LiveData.Linescore.Teams.Away.Runs.Int64()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
game := Game{
|
||||||
|
GamePk: feed.GamePk,
|
||||||
|
HomeTeam: hometeam,
|
||||||
|
AwayTeam: awayteam,
|
||||||
|
HomeScore: uint(homescore),
|
||||||
|
AwayScore: uint(awayscore),
|
||||||
|
Plays: feed.LiveData.Plays.AllPlays,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &game, nil
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,9 @@ type FeedParams struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type FeedResponse struct {
|
type FeedResponse struct {
|
||||||
|
GamePk string
|
||||||
MetaData metadata
|
MetaData metadata
|
||||||
|
GameData gamedata
|
||||||
LiveData livedata
|
LiveData livedata
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +19,23 @@ type metadata struct {
|
||||||
TimeStamp string
|
TimeStamp string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type gamedata struct {
|
||||||
|
Teams teams
|
||||||
|
}
|
||||||
|
|
||||||
|
type teams struct {
|
||||||
|
Away team
|
||||||
|
Home team
|
||||||
|
}
|
||||||
|
|
||||||
|
type team struct {
|
||||||
|
LocationName string
|
||||||
|
TeamName string
|
||||||
|
}
|
||||||
|
|
||||||
type livedata struct {
|
type livedata struct {
|
||||||
Plays plays
|
Plays plays
|
||||||
|
Linescore linescore
|
||||||
}
|
}
|
||||||
|
|
||||||
type plays struct {
|
type plays struct {
|
||||||
|
@ -49,6 +66,22 @@ type about struct {
|
||||||
CaptivatingIndex json.Number
|
CaptivatingIndex json.Number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type linescore struct {
|
||||||
|
Teams teamLines
|
||||||
|
}
|
||||||
|
|
||||||
|
type teamLines struct {
|
||||||
|
Home teamline
|
||||||
|
Away teamline
|
||||||
|
}
|
||||||
|
|
||||||
|
type teamline struct {
|
||||||
|
Runs json.Number
|
||||||
|
Hits json.Number
|
||||||
|
Errors json.Number
|
||||||
|
LeftOnBase json.Number
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Play) Patch(patch Patch) {
|
func (p *Play) Patch(patch Patch) {
|
||||||
instructions := patch.Diff
|
instructions := patch.Diff
|
||||||
stem := "/liveData/plays/currentPlay"
|
stem := "/liveData/plays/currentPlay"
|
||||||
|
|
64
main.go
64
main.go
|
@ -12,54 +12,6 @@ import (
|
||||||
"scm.dairydemon.net/filifa/mlblive/internal/statsapi"
|
"scm.dairydemon.net/filifa/mlblive/internal/statsapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func feedUpdate(gamePk string, ts *string) (statsapi.Play, error) {
|
|
||||||
feed, err := statsapi.Feed(gamePk)
|
|
||||||
if err != nil {
|
|
||||||
return statsapi.Play{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
*ts = feed.MetaData.TimeStamp
|
|
||||||
return feed.LiveData.Plays.CurrentPlay, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func patchUpdate(patches statsapi.DiffPatchResponse, ts *string) []statsapi.Play {
|
|
||||||
var plays []statsapi.Play
|
|
||||||
var err error
|
|
||||||
for _, patch := range patches {
|
|
||||||
var play statsapi.Play
|
|
||||||
play.Patch(patch)
|
|
||||||
plays = append(plays, play)
|
|
||||||
|
|
||||||
*ts, err = patch.Timestamp()
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return plays
|
|
||||||
}
|
|
||||||
|
|
||||||
func update(gamePk string, ts *string, updateId string) ([]statsapi.Play, error) {
|
|
||||||
var plays []statsapi.Play
|
|
||||||
|
|
||||||
patches, err := statsapi.DiffPatch(gamePk, *ts, updateId)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("feed update")
|
|
||||||
var p statsapi.Play
|
|
||||||
p, err = feedUpdate(gamePk, ts)
|
|
||||||
if err != nil {
|
|
||||||
return plays, err
|
|
||||||
}
|
|
||||||
|
|
||||||
plays = []statsapi.Play{p}
|
|
||||||
} else {
|
|
||||||
log.Println("patch update")
|
|
||||||
plays = patchUpdate(patches, ts)
|
|
||||||
}
|
|
||||||
|
|
||||||
return plays, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeTest(v any, ts string) {
|
func writeTest(v any, ts string) {
|
||||||
b, err := json.Marshal(v)
|
b, err := json.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -108,6 +60,17 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: we're calling feed twice now
|
||||||
|
feed, err := statsapi.Feed(gamePk)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
game, err := InitGame(feed)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var p statsapi.Push
|
var p statsapi.Push
|
||||||
err := ws.ReadJSON(&p)
|
err := ws.ReadJSON(&p)
|
||||||
|
@ -115,13 +78,14 @@ func main() {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
plays, err := update(gamePk, &ts, p.UpdateId)
|
err = game.Update(&ts, p.UpdateId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, play := range plays {
|
// FIXME: this will output the same play multiple times
|
||||||
|
for _, play := range game.Plays {
|
||||||
result := play.Result
|
result := play.Result
|
||||||
if result.Event != "" {
|
if result.Event != "" {
|
||||||
// TODO: figure out format
|
// TODO: figure out format
|
||||||
|
|
Loading…
Reference in New Issue