package models import ( "cmp" "slices" "time" "github.com/mergestat/timediff" ) type MapScoreList []MapScore type Capture struct { Id uint Host string Port string Name string Active bool Start time.Time MapScores MapScoreList } type MapScore struct { Id uint StartTime time.Time Map string ScoreList []Score } type Score struct { Id uint Name string Score int Ping int } type ResultTable struct { Header []ResultTableHeader Rows []ResultTableRow } type ResultTableHeader struct { Title string Subtitle string } type ResultTableRow struct { Name string Total int Individual []int } func (msl MapScoreList) BuildTable() *ResultTable { rt := &ResultTable{ Header: []ResultTableHeader{{Title: "Name"}, {Title: "Total"}}, } userMapRows := make(map[string]*ResultTableRow) for mapIndex, mapScore := range msl { rt.Header = append(rt.Header, ResultTableHeader{ Title: mapScore.Map, Subtitle: timediff.TimeDiff(mapScore.StartTime), }) for _, score := range mapScore.ScoreList { if score.Ping > 0 { if _, ok := userMapRows[score.Name]; !ok { rt.Rows = append(rt.Rows, ResultTableRow{ Name: score.Name, Total: 0, Individual: make([]int, len(msl)), }) userMapRows[score.Name] = &rt.Rows[len(rt.Rows)-1] } row := userMapRows[score.Name] row.Total = row.Total + score.Score row.Individual[mapIndex] = score.Score } } } slices.SortFunc(rt.Rows, func(a, b ResultTableRow) int { return cmp.Compare(b.Total, a.Total) }) return rt }