-
Notifications
You must be signed in to change notification settings - Fork 2
/
pick.go
107 lines (87 loc) · 2.34 KB
/
pick.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
package nflpickem
import "errors"
// PickRetriever is the interface implemented by types that can retrieve Pick information
type PickRetriever interface {
Picks(year int, week int) (PickSet, error)
UserPicks(username string, year int, week int) (PickSet, error)
}
// Picker is the interface implemented by a type that can make/update picks
type Picker interface {
MakePicks(PickSet) error
}
// PickCreater is the interface implemented by a type that can add picks to a
// data source.
type PickCreater interface {
CreatePicks(username string, year int, week int) error
}
// A Pick represents a user's selection for a given game.
//
// The Pick can stand on its own since it contains embedded game information
type Pick struct {
Game Game `json:"game"`
User User `json:"user"`
Selection Team `json:"selection"`
Points int `json:"points"`
}
func (p Pick) Equal(other Pick) bool {
return p.Game.Equal(other.Game) && p.User.Equal(other.User)
}
const (
maxSevens = 1
maxFives = 2
maxThrees = 5
)
// A PickSet represents the set of all picks for a user for a given Week
type PickSet []Pick
// IsLegal returns whether or not the current set of picks is considered legal.
//
// A PickSet is legal if it doesn't contain too many "special" point values,
// and contains picks for the same year, week, and user.
func (picks PickSet) IsLegal() bool {
threes := 0
fives := 0
sevens := 0
for _, p := range picks {
switch p.Points {
case 3:
threes++
case 5:
fives++
case 7:
sevens++
}
}
return threes <= maxThrees && fives <= maxFives && sevens <= maxSevens
}
var (
ErrGameLocked = errors.New("game has already started - pick locked")
ErrUnknownSelection = errors.New("selection does not match a game in the given pick set")
)
type PickFilterFunc func(p Pick) bool
func (picks PickSet) Filter(f PickFilterFunc) PickSet {
matches := make(PickSet, 0, len(picks))
for _, p := range picks {
if f(p) {
matches = append(matches, p)
}
}
return matches
}
// Merge compares the original PickSet to the new PickSet, updating any picks
// that differ.
func (picks PickSet) Merge(other PickSet) error {
for i, o := range other {
originalFound := false
for _, p := range picks {
if o.Equal(p) {
picks[i] = o
originalFound = true
break
}
}
if !originalFound {
return ErrUnknownSelection
}
}
return nil
}