/
voetbalpoules.go
97 lines (76 loc) · 2.27 KB
/
voetbalpoules.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
package voetbalpoulesclient
import (
"time"
"github.com/gocolly/colly/v2"
)
//A client that manages communication with Voetbalpoules
type Client struct {
client *colly.Collector
baseURL string
time time.Time
wedstrijden *wedstrijdService
pool *poolService
voorspellingen *voorspellingenService
}
type service struct {
*Client
}
//A struct containing a Wedstrijd and its DeelnemerVoorspellingen
type VoorspeldeWedstrijd struct {
Wedstrijd Wedstrijd
DeelnemerVoorspellingen map[Deelnemer]Voorspelling
}
//NewClientt returns a new Voetbalpoules Client instance
func NewClient(url string) *Client {
col := colly.NewCollector(
colly.AllowURLRevisit(),
)
c := &Client{
client: col,
baseURL: url + "/"}
c.time = time.Now()
c.wedstrijden = &wedstrijdService{c}
c.pool = &poolService{c}
c.voorspellingen = &voorspellingenService{c}
return c
}
//GetWedstrijden returns wedstrijden for a timerange
func (c Client) GetWedstrijden(t1 time.Time, t2 time.Time, poolid int, competitie string) ([]Wedstrijd, error) {
w, err := c.wedstrijden.get(competitie, t1, t2)
if err != nil {
return []Wedstrijd{}, err
}
return w, nil
}
//GetStand returns a sorted ranking for a pool
func (c Client) GetStand(poolID int, competitie string) []Deelnemer {
return c.pool.getStand(poolID, competitie)
}
//GetPoolVoorspelling returns a wedstrijd and all deelnemer predictions for a pool for a competitie within a specified timerange
func (c Client) GetPoolVoorspelling(t1 time.Time, t2 time.Time, poolid int, competitie string) ([]VoorspeldeWedstrijd, error) {
var vw []VoorspeldeWedstrijd
//First check whether any wedstrijden are played within the time range.
w, err := c.wedstrijden.get(competitie, t1, t2)
if err != nil {
return []VoorspeldeWedstrijd{}, err
}
if len(w) == 0 {
return []VoorspeldeWedstrijd{}, nil
}
//Now get the deelnemers for the pool
d := c.pool.getDeelnemers(poolid, competitie)
//Now iterate through the slice of wedstrijden and create a map
for _, w2 := range w {
m := make(map[Deelnemer]Voorspelling)
for _, d2 := range d {
v, _ := c.voorspellingen.get(d2, w2)
m[d2] = v
}
tempVW := VoorspeldeWedstrijd{
Wedstrijd: w2,
DeelnemerVoorspellingen: m,
}
vw = append(vw, tempVW)
}
return vw, nil
}