2014-05-24 18:52:10 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
//
|
|
|
|
|
// Model and datastore CRUD methods for a match at an event.
|
|
|
|
|
|
2017-08-23 22:41:56 -07:00
|
|
|
package model
|
2014-05-24 18:52:10 -07:00
|
|
|
|
|
|
|
|
import (
|
2016-08-28 00:49:52 -07:00
|
|
|
"fmt"
|
2014-07-27 16:41:09 -07:00
|
|
|
"strings"
|
2014-05-24 18:52:10 -07:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Match struct {
|
|
|
|
|
Id int
|
|
|
|
|
Type string
|
|
|
|
|
DisplayName string
|
|
|
|
|
Time time.Time
|
2014-06-01 22:21:00 -07:00
|
|
|
ElimRound int
|
2015-05-30 23:58:42 -07:00
|
|
|
ElimGroup int
|
2014-06-01 22:21:00 -07:00
|
|
|
ElimInstance int
|
2019-08-16 22:52:36 -07:00
|
|
|
ElimRedAlliance int
|
|
|
|
|
ElimBlueAlliance int
|
2014-05-24 18:52:10 -07:00
|
|
|
Red1 int
|
|
|
|
|
Red1IsSurrogate bool
|
|
|
|
|
Red2 int
|
|
|
|
|
Red2IsSurrogate bool
|
|
|
|
|
Red3 int
|
|
|
|
|
Red3IsSurrogate bool
|
|
|
|
|
Blue1 int
|
|
|
|
|
Blue1IsSurrogate bool
|
|
|
|
|
Blue2 int
|
|
|
|
|
Blue2IsSurrogate bool
|
|
|
|
|
Blue3 int
|
|
|
|
|
Blue3IsSurrogate bool
|
|
|
|
|
StartedAt time.Time
|
2018-09-23 13:00:10 -07:00
|
|
|
ScoreCommittedAt time.Time
|
2020-03-29 00:04:15 -07:00
|
|
|
Status MatchStatus
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-29 00:04:15 -07:00
|
|
|
type MatchStatus string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
RedWonMatch MatchStatus = "R"
|
|
|
|
|
BlueWonMatch MatchStatus = "B"
|
|
|
|
|
TieMatch MatchStatus = "T"
|
|
|
|
|
MatchNotPlayed MatchStatus = ""
|
|
|
|
|
)
|
|
|
|
|
|
2017-08-23 22:41:56 -07:00
|
|
|
var ElimRoundNames = map[int]string{1: "F", 2: "SF", 4: "QF", 8: "EF"}
|
|
|
|
|
|
2014-05-24 18:52:10 -07:00
|
|
|
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()
|
|
|
|
|
}
|
2014-05-26 16:39:20 -07:00
|
|
|
|
2014-06-01 22:21:00 -07:00
|
|
|
func (database *Database) GetMatchByName(matchType string, displayName string) (*Match, error) {
|
|
|
|
|
var matches []Match
|
2018-08-24 20:51:37 -07:00
|
|
|
err := database.matchMap.Select(&matches, "SELECT * FROM matches WHERE type = ? AND displayname = ?",
|
2014-06-01 22:21:00 -07:00
|
|
|
matchType, displayName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return &matches[0], err
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-30 23:58:42 -07:00
|
|
|
func (database *Database) GetMatchesByElimRoundGroup(round int, group int) ([]Match, error) {
|
2014-06-01 22:21:00 -07:00
|
|
|
var matches []Match
|
2018-08-24 20:51:37 -07:00
|
|
|
err := database.matchMap.Select(&matches, "SELECT * FROM matches WHERE type = 'elimination' AND "+
|
2015-05-30 23:58:42 -07:00
|
|
|
"elimround = ? AND elimgroup = ? ORDER BY eliminstance", round, group)
|
2014-06-01 22:21:00 -07:00
|
|
|
return matches, err
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 16:39:20 -07:00
|
|
|
func (database *Database) GetMatchesByType(matchType string) ([]Match, error) {
|
|
|
|
|
var matches []Match
|
2018-08-24 20:51:37 -07:00
|
|
|
err := database.matchMap.Select(&matches,
|
2015-05-30 23:58:42 -07:00
|
|
|
"SELECT * FROM matches WHERE type = ? ORDER BY elimround desc, eliminstance, elimgroup, id", matchType)
|
2014-05-26 16:39:20 -07:00
|
|
|
return matches, err
|
|
|
|
|
}
|
2014-07-27 16:41:09 -07:00
|
|
|
|
2020-03-29 00:04:15 -07:00
|
|
|
func (match *Match) IsComplete() bool {
|
|
|
|
|
return match.Status != MatchNotPlayed
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 16:41:09 -07:00
|
|
|
func (match *Match) CapitalizedType() string {
|
|
|
|
|
if match.Type == "" {
|
|
|
|
|
return ""
|
2015-08-20 22:26:57 -07:00
|
|
|
} else if match.Type == "elimination" {
|
|
|
|
|
return "Playoff"
|
2014-07-27 16:41:09 -07:00
|
|
|
}
|
|
|
|
|
return strings.ToUpper(match.Type[0:1]) + match.Type[1:]
|
|
|
|
|
}
|
2016-08-28 00:49:52 -07:00
|
|
|
|
2019-07-20 22:42:56 -07:00
|
|
|
func (match *Match) TypePrefix() string {
|
|
|
|
|
if match.Type == "practice" {
|
|
|
|
|
return "P"
|
|
|
|
|
} else if match.Type == "qualification" {
|
|
|
|
|
return "Q"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 00:49:52 -07:00
|
|
|
func (match *Match) TbaCode() string {
|
|
|
|
|
if match.Type == "qualification" {
|
|
|
|
|
return fmt.Sprintf("qm%s", match.DisplayName)
|
|
|
|
|
} else if match.Type == "elimination" {
|
2017-08-23 22:41:56 -07:00
|
|
|
return fmt.Sprintf("%s%dm%d", strings.ToLower(ElimRoundNames[match.ElimRound]), match.ElimGroup,
|
2016-08-28 00:49:52 -07:00
|
|
|
match.ElimInstance)
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2019-07-20 22:42:56 -07:00
|
|
|
|
|
|
|
|
// Returns true if the match is of a type that allows substitution of teams.
|
|
|
|
|
func (match *Match) ShouldAllowSubstitution() bool {
|
|
|
|
|
return match.Type != "qualification"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the red and yellow cards should be updated as a result of the match.
|
|
|
|
|
func (match *Match) ShouldUpdateCards() bool {
|
|
|
|
|
return match.Type == "qualification" || match.Type == "elimination"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the rankings should be updated as a result of the match.
|
|
|
|
|
func (match *Match) ShouldUpdateRankings() bool {
|
|
|
|
|
return match.Type == "qualification"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns true if the elimination match set should be updated as a result of the match.
|
|
|
|
|
func (match *Match) ShouldUpdateEliminationMatches() bool {
|
|
|
|
|
return match.Type == "elimination"
|
|
|
|
|
}
|