-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.go
167 lines (138 loc) · 4.74 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package yflib
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/famendola1/yfquery"
"github.com/famendola1/yfquery/schema"
)
func findTeam(teams *schema.Teams, teamName string) (*schema.Team, error) {
for _, tm := range teams.Team {
if strings.ToLower(tm.Name) == strings.ToLower(teamName) {
return &tm, nil
}
}
return nil, fmt.Errorf("team %q not found", teamName)
}
// GetTeamKey returns the key of the team, if the team is found.
func GetTeamKey(client *http.Client, leagueKey, teamName string) (string, error) {
fc, err := yfquery.League().Key(leagueKey).Teams().Get(client)
if err != nil {
return "", err
}
tm, err := findTeam(fc.League.Teams, teamName)
if err != nil {
return "", err
}
return tm.TeamKey, nil
}
// GetTeam searches the given league for a team with the provided team name.
// If the team is not found an error is returned.
func GetTeam(client *http.Client, leagueKey, teamName string) (*schema.Team, error) {
fc, err := yfquery.League().Key(leagueKey).Teams().Get(client)
if err != nil {
return nil, err
}
return findTeam(fc.League.Teams, teamName)
}
// GetTeamRoster searches the given league for a team with the provided team name
// and return's its roster. If the team is not found an error is returned.
func GetTeamRoster(client *http.Client, leagueKey, teamName string) (*schema.Team, error) {
fc, err := yfquery.League().Key(leagueKey).Teams().Roster().Get(client)
if err != nil {
return nil, err
}
return findTeam(fc.League.Teams, teamName)
}
// GetTeamStats searches the given league for a team with the provided team name
// and return's its stats. If the team is not found an error is returned.
func GetTeamStats(client *http.Client, leagueKey, teamName string, statsType int) (*schema.Team, error) {
q, err := addStatsTypeToQuery(yfquery.League().Key(leagueKey).Teams().Stats(), statsType)
if err != nil {
return nil, err
}
fc, err := q.Get(client)
if err != nil {
return nil, err
}
return findTeam(fc.League.Teams, teamName)
}
// GetTeamMatchups searches the given league for a team with the provided team name
// and returns all of their matchups. If the team is not found an error is returned.
func GetTeamMatchups(client *http.Client, leagueKey, teamName string) (*schema.Team, error) {
fc, err := yfquery.League().Key(leagueKey).Teams().AllMatchups().Get(client)
if err != nil {
return nil, err
}
return findTeam(fc.League.Teams, teamName)
}
// CategoryMatchupResult represents the results from a matchup in a category
// from the perspective of the HomeTeam.
type CategoryMatchupResult struct {
HomeTeam string
AwayTeam string
CategoriesWon []int
CategoriesLost []int
CategoriesTied []int
}
// CalculateCategoryMathchupResultsVsLeague computes the results of the provided
// team against every other team in the league for the given week based on the
// provided stats to use.
func CalculateCategoryMathchupResultsVsLeague(client *http.Client, leagueKey, teamName string, eligibleStats StatIDSet, week int) ([]CategoryMatchupResult, error) {
fc, err := yfquery.League().Key(leagueKey).Teams().Stats().Week(week).Get(client)
if err != nil {
return nil, err
}
return computeCategoryMatchupResultsVsLeague(teamName, fc.League.Teams, eligibleStats)
}
func computeCategoryMatchupResultsVsLeague(teamName string, teams *schema.Teams, eligibleStats StatIDSet) ([]CategoryMatchupResult, error) {
_, err := findTeam(teams, teamName)
if err != nil {
return nil, err
}
teamStats := make(map[string]map[int]float64)
for _, tm := range teams.Team {
teamStats[tm.Name] = make(map[int]float64)
for _, stat := range tm.TeamStats.Stats.Stat {
if !eligibleStats[stat.StatID] {
continue
}
val, _ := strconv.ParseFloat(stat.Value, 64)
teamStats[tm.Name][stat.StatID] = val
}
}
results := []CategoryMatchupResult{}
for _, tm := range teams.Team {
if tm.Name == teamName {
continue
}
res := CategoryMatchupResult{HomeTeam: teamName, AwayTeam: tm.Name}
for stat := range eligibleStats {
if inverseStats[stat] {
if teamStats[teamName][stat] < teamStats[tm.Name][stat] {
res.CategoriesWon = append(res.CategoriesWon, stat)
continue
}
if teamStats[teamName][stat] > teamStats[tm.Name][stat] {
res.CategoriesLost = append(res.CategoriesLost, stat)
continue
}
res.CategoriesTied = append(res.CategoriesTied, stat)
continue
}
if teamStats[teamName][stat] > teamStats[tm.Name][stat] {
res.CategoriesWon = append(res.CategoriesWon, stat)
continue
}
if teamStats[teamName][stat] < teamStats[tm.Name][stat] {
res.CategoriesLost = append(res.CategoriesLost, stat)
continue
}
res.CategoriesTied = append(res.CategoriesTied, stat)
continue
}
results = append(results, res)
}
return results, nil
}