Files
cheesy-arena-lite/model/match.go

183 lines
4.7 KiB
Go
Raw Normal View History

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.
package model
2014-05-24 18:52:10 -07:00
import (
"fmt"
"sort"
2014-07-27 16:41:09 -07:00
"strings"
2014-05-24 18:52:10 -07:00
"time"
)
type Match struct {
Id int64 `db:"id"`
2014-05-24 18:52:10 -07:00
Type string
DisplayName string
Time time.Time
ElimRound int
2015-05-30 23:58:42 -07:00
ElimGroup int
ElimInstance int
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
ScoreCommittedAt time.Time
Status MatchStatus
2014-05-24 18:52:10 -07:00
}
type MatchStatus string
const (
RedWonMatch MatchStatus = "R"
BlueWonMatch MatchStatus = "B"
TieMatch MatchStatus = "T"
MatchNotPlayed MatchStatus = ""
)
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.matchTable.create(match)
2014-05-24 18:52:10 -07:00
}
func (database *Database) GetMatchById(id int64) (*Match, error) {
var match *Match
err := database.matchTable.getById(id, &match)
2014-05-24 18:52:10 -07:00
return match, err
}
func (database *Database) UpdateMatch(match *Match) error {
return database.matchTable.update(match)
2014-05-24 18:52:10 -07:00
}
func (database *Database) DeleteMatch(id int64) error {
return database.matchTable.delete(id)
2014-05-24 18:52:10 -07:00
}
func (database *Database) TruncateMatches() error {
return database.matchTable.truncate()
2014-05-24 18:52:10 -07:00
}
2014-05-26 16:39:20 -07:00
func (database *Database) GetMatchByName(matchType string, displayName string) (*Match, error) {
var matches []Match
if err := database.matchTable.getAll(&matches); err != nil {
return nil, err
}
for _, match := range matches {
if match.Type == matchType && match.DisplayName == displayName {
return &match, nil
}
}
return nil, nil
}
2015-05-30 23:58:42 -07:00
func (database *Database) GetMatchesByElimRoundGroup(round int, group int) ([]Match, error) {
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-05-26 16:39:20 -07:00
func (database *Database) GetMatchesByType(matchType string) ([]Match, error) {
var matches []Match
if err := database.matchTable.getAll(&matches); err != nil {
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
}
return matchingMatches[i].ElimRound > matchingMatches[j].ElimRound
})
return matchingMatches, nil
2014-05-26 16:39:20 -07:00
}
2014-07-27 16:41:09 -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 ""
} 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:]
}
func (match *Match) TypePrefix() string {
if match.Type == "practice" {
return "P"
} else if match.Type == "qualification" {
return "Q"
}
return ""
}
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 ""
}
// 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"
}