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 (
|
2022-08-21 14:42:47 -07:00
|
|
|
"github.com/Team254/cheesy-arena-lite/game"
|
2021-05-12 16:40:07 -07:00
|
|
|
"sort"
|
2014-07-27 16:41:09 -07:00
|
|
|
"strings"
|
2014-05-24 18:52:10 -07:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Match struct {
|
2021-05-12 17:49:05 -07:00
|
|
|
Id int `db:"id"`
|
2014-05-24 18:52:10 -07:00
|
|
|
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
|
2022-08-21 14:42:47 -07:00
|
|
|
Status game.MatchStatus
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) CreateMatch(match *Match) error {
|
2021-05-12 16:40:07 -07:00
|
|
|
return database.matchTable.create(match)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 17:49:05 -07:00
|
|
|
func (database *Database) GetMatchById(id int) (*Match, error) {
|
2022-04-04 20:39:19 -07:00
|
|
|
return database.matchTable.getById(id)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:40:07 -07:00
|
|
|
func (database *Database) UpdateMatch(match *Match) error {
|
|
|
|
|
return database.matchTable.update(match)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 17:49:05 -07:00
|
|
|
func (database *Database) DeleteMatch(id int) error {
|
2021-05-12 16:40:07 -07:00
|
|
|
return database.matchTable.delete(id)
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (database *Database) TruncateMatches() error {
|
2021-05-12 16:40:07 -07:00
|
|
|
return database.matchTable.truncate()
|
2014-05-24 18:52:10 -07:00
|
|
|
}
|
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) {
|
2022-04-04 20:39:19 -07:00
|
|
|
matches, err := database.matchTable.getAll()
|
|
|
|
|
if err != nil {
|
2014-06-01 22:21:00 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-05-12 16:40:07 -07:00
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
|
if match.Type == matchType && match.DisplayName == displayName {
|
|
|
|
|
return &match, nil
|
|
|
|
|
}
|
2014-06-01 22:21:00 -07:00
|
|
|
}
|
2021-05-12 16:40:07 -07:00
|
|
|
return nil, nil
|
2014-06-01 22:21:00 -07:00
|
|
|
}
|
|
|
|
|
|
2015-05-30 23:58:42 -07:00
|
|
|
func (database *Database) GetMatchesByElimRoundGroup(round int, group int) ([]Match, error) {
|
2021-05-12 16:40:07 -07:00
|
|
|
matches, err := database.GetMatchesByType("elimination")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchingMatches []Match
|
|
|
|
|
for _, match := range matches {
|
|
|
|
|
if match.ElimRound == round && match.ElimGroup == group {
|
|
|
|
|
matchingMatches = append(matchingMatches, match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return matchingMatches, nil
|
2014-06-01 22:21:00 -07:00
|
|
|
}
|
|
|
|
|
|
2014-05-26 16:39:20 -07:00
|
|
|
func (database *Database) GetMatchesByType(matchType string) ([]Match, error) {
|
2022-04-04 20:39:19 -07:00
|
|
|
matches, err := database.matchTable.getAll()
|
|
|
|
|
if err != nil {
|
2021-05-12 16:40:07 -07:00
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var matchingMatches []Match
|
|
|
|
|
for _, match := range matches {
|
|
|
|
|
if match.Type == matchType {
|
|
|
|
|
matchingMatches = append(matchingMatches, match)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(matchingMatches, func(i, j int) bool {
|
|
|
|
|
if matchingMatches[i].ElimRound == matchingMatches[j].ElimRound {
|
|
|
|
|
if matchingMatches[i].ElimInstance == matchingMatches[j].ElimInstance {
|
|
|
|
|
if matchingMatches[i].ElimGroup == matchingMatches[j].ElimGroup {
|
|
|
|
|
return matchingMatches[i].Id < matchingMatches[j].Id
|
|
|
|
|
}
|
|
|
|
|
return matchingMatches[i].ElimGroup < matchingMatches[j].ElimGroup
|
|
|
|
|
}
|
|
|
|
|
return matchingMatches[i].ElimInstance < matchingMatches[j].ElimInstance
|
|
|
|
|
}
|
2022-08-16 20:03:51 -07:00
|
|
|
return matchingMatches[i].ElimRound < matchingMatches[j].ElimRound
|
2021-05-12 16:40:07 -07:00
|
|
|
})
|
|
|
|
|
return matchingMatches, nil
|
2014-05-26 16:39:20 -07:00
|
|
|
}
|
2014-07-27 16:41:09 -07:00
|
|
|
|
2020-03-29 00:04:15 -07:00
|
|
|
func (match *Match) IsComplete() bool {
|
2022-08-21 14:42:47 -07:00
|
|
|
return match.Status != game.MatchNotPlayed
|
2020-03-29 00:04:15 -07:00
|
|
|
}
|
|
|
|
|
|
2014-07-27 16:41:09 -07:00
|
|
|
func (match *Match) CapitalizedType() string {
|
2022-09-29 18:03:14 -07:00
|
|
|
if match.Type == "" || match.Type == "test" {
|
2014-07-27 16:41:09 -07:00
|
|
|
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 ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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"
|
|
|
|
|
}
|