/
matchmaker.go
124 lines (115 loc) · 3.11 KB
/
matchmaker.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
// +build pro free debug
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
"strings"
"time"
)
// Schedule is a series of Schedule Entries attached to a type (qual, playoff, practice).
// Supporting multiple types of matches on the view page, or in the master schedule is not implemented.
type Schedule struct {
Matches []ScheduleEntry
Type string
}
// ScheduleEntry is a Match paired with a time. Linked to a match data result when a match is completed.
type ScheduleEntry struct {
Number int
Time time.Time
Red1 string
Red2 string
Red3 string
Blue1 string
Blue2 string
Blue3 string
Completed bool
MatchData *XRCMatchData
MasterIndex int
}
// MatchesXRCMatch checks if the expected alliance members are the ones that were reported from the match data.
func (m *ScheduleEntry) MatchesXRCMatch(x XRCMatchData) bool {
blue := true
red := true
for _, b := range x.BlueAlliance {
if m.Blue1 == b.Name || m.Blue2 == b.Name || m.Blue3 == b.Name {
continue
}
blue = false
}
for _, r := range x.RedAlliance {
if m.Red1 == r.Name || m.Red2 == r.Name || m.Red3 == r.Name {
continue
}
red = false
}
return red && blue
}
// IsScheduledMatch handles checks for whether or not a match is within the given schedule.
func IsScheduledMatch(match *XRCMatchData, schedule []ScheduleEntry) (bool, int) {
debug("Checking whether match " + match.Summary() + " is scheduled.")
ret := false
index := 0
for i, s := range schedule {
if s.MatchesXRCMatch(*match) {
s.MatchData = match
s.Completed = true
debug("Match found. Updating.")
ret = true
index = i
}
}
if !ret {
debug("No match found for " + match.Summary())
}
return ret, index
}
// ImportSchedule handles conversion of the csv of matches into the ScheduleEntry and Schedule structs.
func ImportSchedule(file string) (schedule Schedule) {
f, err := os.Open(file)
if err != nil {
log.Fatal(err)
return
}
debug("Found schedule file: " + file)
rows, err := csv.NewReader(f).ReadAll()
defer f.Close()
if err != nil {
log.Fatal(err)
}
currentMatchTime := time.Now()
for _, row := range rows {
matchNum, _ := strconv.Atoi(row[0])
unixTS, _ := strconv.Atoi(row[1])
timeRaw := time.Unix(int64(unixTS), 0)
scheduleEntry := ScheduleEntry{}
debug("Row Length:" + strconv.Itoa(len(row)))
//
if len(row)-2 == 6 {
scheduleEntry = ScheduleEntry{
Number: matchNum + 1,
Time: timeRaw,
Red1: strings.TrimSpace(row[2]),
Red2: strings.TrimSpace(row[3]),
Red3: strings.TrimSpace(row[4]),
Blue1: strings.TrimSpace(row[5]),
Blue2: strings.TrimSpace(row[6]),
Blue3: strings.TrimSpace(row[7]),
}
} else if len(row)-2 == 4 {
scheduleEntry = ScheduleEntry{
Number: matchNum + 1,
Time: timeRaw,
Red1: strings.TrimSpace(row[2]),
Red2: strings.TrimSpace(row[3]),
Blue1: strings.TrimSpace(row[4]),
Blue2: strings.TrimSpace(row[5]),
}
}
debug("Valid formatted schedule entry.")
schedule.Matches = append(schedule.Matches, scheduleEntry)
currentMatchTime = currentMatchTime.Add(5 * time.Minute)
}
return
}