refactor
This commit is contained in:
parent
d8bc8be4f1
commit
cddcf64dde
|
@ -32,6 +32,20 @@ import (
|
||||||
|
|
||||||
var gamePk int32
|
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) {
|
func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
|
||||||
var patches []jsonpatch.Patch
|
var patches []jsonpatch.Patch
|
||||||
|
|
||||||
|
@ -49,31 +63,30 @@ func extractPatches(resp []byte) ([]jsonpatch.Patch, error) {
|
||||||
return patches, nil
|
return patches, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func patch(body []byte, client *api.APIClient) ([]byte, error) {
|
func requestPatches(timestamp any, client *api.APIClient) ([]jsonpatch.Patch, 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"]
|
|
||||||
|
|
||||||
req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk)
|
req := client.GameAPI.LiveGameDiffPatchV1(context.Background(), gamePk)
|
||||||
req = req.StartTimecode(timestamp)
|
req = req.StartTimecode(timestamp)
|
||||||
|
|
||||||
resp, err := req.Execute()
|
resp, err := req.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return body, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
diffPatch, err := io.ReadAll(resp.Body)
|
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 {
|
if err != nil {
|
||||||
return body, err
|
return body, err
|
||||||
}
|
}
|
||||||
|
|
||||||
patches, err := extractPatches(diffPatch)
|
patches, err := requestPatches(timestamp, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return body, err
|
return body, err
|
||||||
}
|
}
|
||||||
|
@ -100,40 +113,42 @@ func newWebsocket(gamePk int32) (*gamedayWebsocket, <-chan error, error) {
|
||||||
return ws, ch, nil
|
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)
|
ws, _, err := newWebsocket(gamePk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return body, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
|
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
|
||||||
resp, err := req.Execute()
|
resp, err := req.Execute()
|
||||||
if err != nil {
|
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
|
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
|
var p push
|
||||||
err := ws.ReadJSON(&p)
|
err := ws.ReadJSON(&p)
|
||||||
if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
|
if websocket.IsUnexpectedCloseError(err, GameFinalCode, GameUnavailableCode) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
||||||
body, newWs, err := handleUnexpectedClose(body, client)
|
body, newWs, err := handleUnexpectedClose(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return body, err
|
return body, err
|
||||||
}
|
}
|
||||||
|
|
||||||
*ws = *newWs
|
*ws = *newWs
|
||||||
return body, nil
|
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 {
|
if err != nil {
|
||||||
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
|
req := client.GameAPI.LiveGameV1(context.Background(), gamePk)
|
||||||
resp, err := req.Execute()
|
resp, err := req.Execute()
|
||||||
|
@ -172,7 +187,15 @@ func subscribe(cmd *cobra.Command, args []string) {
|
||||||
for {
|
for {
|
||||||
fmt.Println(string(body))
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue