Files
cheesy-arena-lite/model/match_result.go
2021-05-16 11:00:29 -07:00

126 lines
3.7 KiB
Go
Executable File

// 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
import (
"encoding/json"
"github.com/Team254/cheesy-arena-lite/game"
)
type MatchResult struct {
Id int
MatchId int
PlayNumber int
MatchType string
RedScore *game.Score
BlueScore *game.Score
}
type MatchResultDb struct {
Id int
MatchId int
PlayNumber int
MatchType string
RedScoreJson string
BlueScoreJson string
}
// 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)
return matchResult
}
func (database *Database) CreateMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.Serialize()
if err != nil {
return err
}
err = database.matchResultMap.Insert(matchResultDb)
if err != nil {
return err
}
matchResult.Id = matchResultDb.Id
return nil
}
func (database *Database) GetMatchResultForMatch(matchId int) (*MatchResult, error) {
var matchResults []MatchResultDb
query := "SELECT * FROM match_results WHERE matchid = ? ORDER BY playnumber DESC LIMIT 1"
err := database.matchResultMap.Select(&matchResults, query, matchId)
if err != nil {
return nil, err
}
if len(matchResults) == 0 {
return nil, nil
}
matchResult, err := matchResults[0].Deserialize()
if err != nil {
return nil, err
}
return matchResult, err
}
func (database *Database) SaveMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.Serialize()
if err != nil {
return err
}
_, err = database.matchResultMap.Update(matchResultDb)
return err
}
func (database *Database) DeleteMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.Serialize()
if err != nil {
return err
}
_, err = database.matchResultMap.Delete(matchResultDb)
return err
}
func (database *Database) TruncateMatchResults() error {
return database.matchResultMap.TruncateTables()
}
// Calculates and returns the summary fields used for ranking and display for the red alliance.
func (matchResult *MatchResult) RedScoreSummary() *game.ScoreSummary {
return matchResult.RedScore.Summarize()
}
// Calculates and returns the summary fields used for ranking and display for the blue alliance.
func (matchResult *MatchResult) BlueScoreSummary() *game.ScoreSummary {
return matchResult.BlueScore.Summarize()
}
// Converts the nested struct MatchResult to the DB version that has JSON fields.
func (matchResult *MatchResult) Serialize() (*MatchResultDb, error) {
matchResultDb := MatchResultDb{Id: matchResult.Id, MatchId: matchResult.MatchId,
PlayNumber: matchResult.PlayNumber, MatchType: matchResult.MatchType}
if err := serializeHelper(&matchResultDb.RedScoreJson, matchResult.RedScore); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.BlueScoreJson, matchResult.BlueScore); err != nil {
return nil, err
}
return &matchResultDb, nil
}
// Converts the DB MatchResult with JSON fields to the nested struct version.
func (matchResultDb *MatchResultDb) Deserialize() (*MatchResult, error) {
matchResult := MatchResult{Id: matchResultDb.Id, MatchId: matchResultDb.MatchId,
PlayNumber: matchResultDb.PlayNumber, MatchType: matchResultDb.MatchType}
if err := json.Unmarshal([]byte(matchResultDb.RedScoreJson), &matchResult.RedScore); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.BlueScoreJson), &matchResult.BlueScore); err != nil {
return nil, err
}
return &matchResult, nil
}