-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfantasy.go
More file actions
148 lines (128 loc) · 2.84 KB
/
fantasy.go
File metadata and controls
148 lines (128 loc) · 2.84 KB
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
package main
import (
"fmt"
"sort"
)
type fantasyTeam struct {
teams []string
perc float64
Name string `json:"name"`
Owner string `json:"owner"`
Wins int64 `json:"wins"`
Losses int64 `json:"losses"`
Perc string `json:"perc"`
Rank int `json:"rank"`
Teams []mlbTeam `json:"teams"`
}
type fantasypctLeague interface {
sort.Interface
GetTeams() []*fantasyTeam
Rank()
}
type pctLeague struct {
Teams []*fantasyTeam `json:"teams"`
}
func (l *pctLeague) GetTeams() []*fantasyTeam {
return l.Teams
}
func (l *pctLeague) Len() int {
return len(l.Teams)
}
func (l *pctLeague) Less(i, j int) bool {
if l.Teams[i].perc > l.Teams[j].perc {
return true
}
return false
}
func (l *pctLeague) Swap(i, j int) {
l.Teams[i], l.Teams[j] = l.Teams[j], l.Teams[i]
}
func (l *pctLeague) Rank() {
sort.Sort(l)
var currRank int
var prevPerc float64
for i, team := range l.Teams {
if prevPerc != team.perc {
currRank = i + 1
}
team.Rank = currRank
prevPerc = team.perc
}
}
type winLeague struct {
Teams []*fantasyTeam `json:"teams"`
}
func (l *winLeague) GetTeams() []*fantasyTeam {
return l.Teams
}
func (l *winLeague) Len() int {
return len(l.Teams)
}
func (l *winLeague) Less(i, j int) bool {
if l.Teams[i].Wins > l.Teams[j].Wins {
return true
}
return false
}
func (l *winLeague) Swap(i, j int) {
l.Teams[i], l.Teams[j] = l.Teams[j], l.Teams[i]
}
func (l *winLeague) Rank() {
sort.Sort(l)
var currRank int
var prevWin int64
for i, team := range l.Teams {
if prevWin != team.Wins {
currRank = i + 1
}
team.Rank = currRank
prevWin = team.Wins
}
}
func populateScores(l fantasypctLeague, mlbScores mlbStandings) {
for _, f := range l.GetTeams() {
for _, t := range f.teams {
f.Wins = f.Wins + mlbScores.Standing[t].Won
f.Losses = f.Losses + mlbScores.Standing[t].Lost
if f.Losses != 0 {
f.perc = float64(f.Wins) / float64(f.Losses+f.Wins)
} else {
f.perc = 1
}
f.Teams = append(f.Teams, mlbScores.Standing[t])
f.Perc = fmt.Sprintf("%.3f", f.perc)
}
}
}
const (
NYY = "new-york-yankees"
NYM = "new-york-mets"
CHIC = "chicago-cubs"
CHIW = "chicago-white-sox"
MI = "miami-marlins"
WA = "washington-nationals"
SL = "st-louis-cardinals"
LAA = "los-angeles-angels"
LAD = "los-angeles-dodgers"
CIN = "cincinnati-reds"
DE = "detroit-tigers"
HO = "houston-astros"
MIL = "milwaukee-brewers"
OAK = "oakland-athletics"
MIN = "minnesota-twins"
TX = "texas-rangers"
PHIL = "philadelphia-phillies"
SD = "san-diego-padres"
BO = "boston-red-sox"
SE = "seattle-mariners"
CO = "colorado-rockies"
BA = "baltimore-orioles"
TO = "toronto-blue-jays"
SF = "san-francisco-giants"
CL = "cleveland-indians"
ATL = "atlanta-braves"
TB = "tampa-bay-rays"
AZ = "arizona-diamondbacks"
PITT = "pittsburgh-pirates"
KC = "kansas-city-royals"
)