-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration with Nexus for automatically pulling practice and pla…
…yoff match lineups.
- Loading branch information
Showing
11 changed files
with
306 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2023 Team 254. All Rights Reserved. | ||
// Author: [email protected] (Patrick Fairbank) | ||
// | ||
// Methods for pulling match lineups from Nexus for FRC. | ||
|
||
package partner | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/Team254/cheesy-arena/model" | ||
"io" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const nexusBaseUrl = "https://api.frc.nexus" | ||
|
||
type NexusClient struct { | ||
BaseUrl string | ||
eventCode string | ||
} | ||
|
||
type nexusLineup struct { | ||
Red [3]string `json:"red"` | ||
Blue [3]string `json:"blue"` | ||
} | ||
|
||
func NewNexusClient(eventCode string) *NexusClient { | ||
return &NexusClient{BaseUrl: nexusBaseUrl, eventCode: eventCode} | ||
} | ||
|
||
// Gets the team lineup for a given match from the Nexus API. Returns nil and an error if the lineup is not available. | ||
func (client *NexusClient) GetLineup(tbaMatchKey model.TbaMatchKey) (*[6]int, error) { | ||
path := fmt.Sprintf("/v1/%s/%s/lineup", client.eventCode, tbaMatchKey.String()) | ||
resp, err := client.getRequest(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Get the response and handle errors | ||
defer resp.Body.Close() | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("Error getting lineup from Nexus: %d, %s", resp.StatusCode, string(body)) | ||
} | ||
|
||
var nexusLineup nexusLineup | ||
if err = json.Unmarshal(body, &nexusLineup); err != nil { | ||
return nil, err | ||
} | ||
|
||
var lineup [6]int | ||
lineup[0], _ = strconv.Atoi(nexusLineup.Red[0]) | ||
lineup[1], _ = strconv.Atoi(nexusLineup.Red[1]) | ||
lineup[2], _ = strconv.Atoi(nexusLineup.Red[2]) | ||
lineup[3], _ = strconv.Atoi(nexusLineup.Blue[0]) | ||
lineup[4], _ = strconv.Atoi(nexusLineup.Blue[1]) | ||
lineup[5], _ = strconv.Atoi(nexusLineup.Blue[2]) | ||
|
||
return &lineup, err | ||
} | ||
|
||
// Sends a GET request to the Nexus API. | ||
func (client *NexusClient) getRequest(path string) (*http.Response, error) { | ||
url := client.BaseUrl + path | ||
httpClient := &http.Client{} | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return httpClient.Do(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2023 Team 254. All Rights Reserved. | ||
// Author: [email protected] (Patrick Fairbank) | ||
|
||
package partner | ||
|
||
import ( | ||
"github.com/Team254/cheesy-arena/model" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetLineup(t *testing.T) { | ||
// Mock the Nexus server. | ||
nexusServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
assert.Contains(t, r.URL.String(), "/v1/my_event_code/") | ||
if strings.Contains(r.URL.String(), "/v1/my_event_code/p1/lineup") { | ||
w.Write([]byte("{\"red\":[\"101\",\"102\",\"103\"],\"blue\":[\"104\",\"105\",\"106\"]}")) | ||
} else { | ||
http.Error(w, "Match not found", 404) | ||
} | ||
})) | ||
defer nexusServer.Close() | ||
client := NewNexusClient("my_event_code") | ||
client.BaseUrl = nexusServer.URL | ||
|
||
tbaMatchKey := model.TbaMatchKey{CompLevel: "p", SetNumber: 0, MatchNumber: 1} | ||
lineup, err := client.GetLineup(tbaMatchKey) | ||
if assert.Nil(t, err) { | ||
assert.Equal(t, [6]int{101, 102, 103, 104, 105, 106}, *lineup) | ||
} | ||
|
||
tbaMatchKey = model.TbaMatchKey{CompLevel: "sf", SetNumber: 6, MatchNumber: 1} | ||
lineup, err = client.GetLineup(tbaMatchKey) | ||
assert.Nil(t, lineup) | ||
if assert.NotNil(t, err) { | ||
assert.Contains(t, err.Error(), "Match not found") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.