/
team.go
53 lines (44 loc) · 1.68 KB
/
team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package analysis
import (
"errors"
"github.com/ptdewey/frc-scouting-database-v2/internal/api"
"github.com/ptdewey/frc-scouting-database-v2/internal/utils"
)
// Function to get a subset of matches from an event containing only matches a
// specified team was a part of.
// Takes in a pointer to the event matches object and a team key to search for.
// Returns a slice of matches and error.
// This function does not modify the global state and has no side effects
func TeamMatches(matches []api.Match, teamKey string) ([]api.Match, error) {
// TODO: probably more memory efficient to just return indices of matches
// where match numbers appear
var out []api.Match
for _, m := range matches {
if utils.ContainsString(teamKey, m.Alliances.Blue.TeamKeys[:]) {
out = append(out, m)
} else if utils.ContainsString(teamKey, m.Alliances.Red.TeamKeys[:]) {
out = append(out, m)
}
}
// team name not found in any matches
if len(out) == 0 {
return nil, errors.New("Team key not found in event matches.")
}
return out, nil
}
// Function that writes the output of TeamMatches to a csv file.
// Takes in a pointer to the event matches object and a team key to search for.
// Returns an error if the write failed.
func TeamMatchesToCSV(matches []api.Match, teamKey string, filename string) ([][]string, error) {
// extract team matches from event matches
tm, err := TeamMatches(matches, teamKey)
if err != nil {
return nil, err
}
// convert matches struct to csv and write to file
out, err := MatchesToCSV(tm, filename)
if err != nil {
return nil, err
}
return out, nil
}