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

70 lines
2.0 KiB
Go
Raw Normal View History

2014-05-26 20:16:36 -07:00
// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Model and datastore CRUD methods for the results (score and fouls) from a match at an event.
package model
2014-05-26 20:16:36 -07:00
import (
2021-05-16 11:00:29 -07:00
"github.com/Team254/cheesy-arena-lite/game"
2014-05-26 20:16:36 -07:00
)
type MatchResult struct {
Id int `db:"id"`
MatchId int
2014-05-26 20:16:36 -07:00
PlayNumber int
MatchType string
RedScore *game.Score
BlueScore *game.Score
2014-05-26 20:16:36 -07:00
}
2014-07-07 22:42:17 -07:00
// Returns a new match result object with empty slices instead of nil.
func NewMatchResult() *MatchResult {
matchResult := new(MatchResult)
matchResult.RedScore = new(game.Score)
matchResult.BlueScore = new(game.Score)
2014-07-07 22:42:17 -07:00
return matchResult
}
2014-05-26 20:16:36 -07:00
func (database *Database) CreateMatchResult(matchResult *MatchResult) error {
return database.matchResultTable.create(matchResult)
2014-05-26 20:16:36 -07:00
}
func (database *Database) GetMatchResultForMatch(matchId int) (*MatchResult, error) {
var matchResults []MatchResult
if err := database.matchResultTable.getAll(&matchResults); err != nil {
2014-05-26 20:16:36 -07:00
return nil, err
}
var mostRecentMatchResult *MatchResult
for i, matchResult := range matchResults {
if matchResult.MatchId == matchId &&
(mostRecentMatchResult == nil || matchResult.PlayNumber > mostRecentMatchResult.PlayNumber) {
mostRecentMatchResult = &matchResults[i]
}
2014-05-26 20:16:36 -07:00
}
return mostRecentMatchResult, nil
2014-05-26 20:16:36 -07:00
}
func (database *Database) UpdateMatchResult(matchResult *MatchResult) error {
return database.matchResultTable.update(matchResult)
2014-05-26 20:16:36 -07:00
}
func (database *Database) DeleteMatchResult(id int) error {
return database.matchResultTable.delete(id)
2014-05-26 20:16:36 -07:00
}
func (database *Database) TruncateMatchResults() error {
return database.matchResultTable.truncate()
2014-05-26 20:16:36 -07:00
}
2014-05-26 22:56:32 -07:00
// Calculates and returns the summary fields used for ranking and display for the red alliance.
2020-04-14 19:38:14 -05:00
func (matchResult *MatchResult) RedScoreSummary() *game.ScoreSummary {
return matchResult.RedScore.Summarize()
2014-05-26 22:56:32 -07:00
}
// Calculates and returns the summary fields used for ranking and display for the blue alliance.
2020-04-14 19:38:14 -05:00
func (matchResult *MatchResult) BlueScoreSummary() *game.ScoreSummary {
return matchResult.BlueScore.Summarize()
2014-05-26 22:56:32 -07:00
}