This commit is contained in:
filifa 2025-04-08 00:52:21 -04:00
parent d622060355
commit 3ddb5e2632
1 changed files with 46 additions and 23 deletions

View File

@ -32,6 +32,20 @@ import (
var gamePk int32
func extractTimestamp(body []byte) (any, error) {
var v any
err := json.Unmarshal(body, &v)
if err != nil {
return nil, err
}
vobj := v.(map[string]any)
metaData := vobj["metaData"].(map[string]any)
timestamp := metaData["timeStamp"]
return timestamp, nil
}
func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
var patches []jsonpatch.Patch
@ -49,31 +63,30 @@ func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
return patches, nil
}
func patch(body []byte, client *api.APIClient) ([]byte, error) {
var v any
err := json.Unmarshal(body, &v)
if err != nil {
return body, err
}
vobj := v.(map[string]any)
metaData := vobj["metaData"].(map[string]any)
timestamp := metaData["timeStamp"]
func requestPatches(timestamp any, client *api.APIClient) ([]jsonpatch.Patch, error) {
req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk)
req = req.StartTimecode(timestamp)
resp, err := req.Execute()
if err != nil {
return body, err
return nil, err
}
diffPatch, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return extractPatches(diffPatch)
}
func patch(body []byte, client *api.APIClient) ([]byte, error) {
timestamp, err := extractTimestamp(body)
if err != nil {
return body, err
}
patches, err := extractPatches(diffPatch)
patches, err := requestPatches(timestamp, client)
if err != nil {
return body, err
}
@ -100,40 +113,42 @@ func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) {
return ws, ch, nil
}
func handleUnexpectedClose(body []byte, client *api.APIClient) ([]byte, *gamedayWebsocket, error) {
func handleUnexpectedClose(client *api.APIClient) ([]byte, *gamedayWebsocket, error) {
ws, _, err := newWebsocket(gamePk)
if err != nil {
return body, nil, err
return nil, nil, err
}
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
if err != nil {
return body, ws, err
return nil, ws, err
}
body, err = io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
return body, ws, err
}
func updateFeed(body []byte, ws *gamedayWebsocket, client *api.APIClient) ([]byte, error) {
func handlePush(ws *gamedayWebsocket, client *api.APIClient) ([]byte, error) {
var p push
err := ws.ReadJSON(&p)
if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
log.Println(err)
body, newWs, err := handleUnexpectedClose(body, client)
body, newWs, err := handleUnexpectedClose(client)
if err != nil {
return body, err
}
*ws = *newWs
return body, nil
} else if err != nil {
return body, err
}
body, err = patch(body, client)
return nil, err
}
func updateFeed(body []byte, client *api.APIClient) ([]byte, error) {
body, err := patch(body, client)
if err != nil {
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
resp, err := req.Execute()
@ -172,7 +187,15 @@ func subscribe(cmd *cobra.Command, args []string) {
for {
fmt.Println(string(body))
body, err = updateFeed(body, ws, statsAPIClient)
newBody, err := handlePush(ws, statsAPIClient)
if err != nil {
log.Fatal(err)
} else if newBody != nil {
body = newBody
continue
}
body, err = updateFeed(body, statsAPIClient)
if err != nil {
log.Fatal(err)
}