forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.go
125 lines (111 loc) · 3.27 KB
/
match.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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Model and datastore CRUD methods for a match at an event.
package main
import (
"fmt"
"strings"
"time"
)
type Match struct {
Id int
Type string
DisplayName string
Time time.Time
ElimRound int
ElimGroup int
ElimInstance int
Red1 int
Red1IsSurrogate bool
Red2 int
Red2IsSurrogate bool
Red3 int
Red3IsSurrogate bool
Blue1 int
Blue1IsSurrogate bool
Blue2 int
Blue2IsSurrogate bool
Blue3 int
Blue3IsSurrogate bool
Status string
StartedAt time.Time
Winner string
RedDefense1 string
RedDefense2 string
RedDefense3 string
RedDefense4 string
RedDefense5 string
BlueDefense1 string
BlueDefense2 string
BlueDefense3 string
BlueDefense4 string
BlueDefense5 string
}
var placeableDefenses = []string{"CDF", "M", "R", "RW", "RT"}
var defenseNames = map[string]string{"LB": "Low Bar", "CDF": "Cheval de Frise", "M": "Moat",
"R": "Ramparts", "RW": "Rock Wall", "RT": "Rough Terrain"}
func (database *Database) CreateMatch(match *Match) error {
return database.matchMap.Insert(match)
}
func (database *Database) GetMatchById(id int) (*Match, error) {
match := new(Match)
err := database.matchMap.Get(match, id)
if err != nil && err.Error() == "sql: no rows in result set" {
match = nil
err = nil
}
return match, err
}
func (database *Database) SaveMatch(match *Match) error {
_, err := database.matchMap.Update(match)
return err
}
func (database *Database) DeleteMatch(match *Match) error {
_, err := database.matchMap.Delete(match)
return err
}
func (database *Database) TruncateMatches() error {
return database.matchMap.TruncateTables()
}
func (database *Database) GetMatchByName(matchType string, displayName string) (*Match, error) {
var matches []Match
err := database.teamMap.Select(&matches, "SELECT * FROM matches WHERE type = ? AND displayname = ?",
matchType, displayName)
if err != nil {
return nil, err
}
if len(matches) == 0 {
return nil, nil
}
return &matches[0], err
}
func (database *Database) GetMatchesByElimRoundGroup(round int, group int) ([]Match, error) {
var matches []Match
err := database.teamMap.Select(&matches, "SELECT * FROM matches WHERE type = 'elimination' AND "+
"elimround = ? AND elimgroup = ? ORDER BY eliminstance", round, group)
return matches, err
}
func (database *Database) GetMatchesByType(matchType string) ([]Match, error) {
var matches []Match
err := database.teamMap.Select(&matches,
"SELECT * FROM matches WHERE type = ? ORDER BY elimround desc, eliminstance, elimgroup, id", matchType)
return matches, err
}
func (match *Match) CapitalizedType() string {
if match.Type == "" {
return ""
} else if match.Type == "elimination" {
return "Playoff"
}
return strings.ToUpper(match.Type[0:1]) + match.Type[1:]
}
func (match *Match) TbaCode() string {
if match.Type == "qualification" {
return fmt.Sprintf("qm%s", match.DisplayName)
} else if match.Type == "elimination" {
return fmt.Sprintf("%s%dm%d", strings.ToLower(elimRoundNames[match.ElimRound]), match.ElimGroup,
match.ElimInstance)
}
return ""
}