From c97792e7621ed3e81b70e51c3d0af255b028ae2a Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 5 Jan 2021 12:28:40 +0100 Subject: [PATCH] Improve ranking --- orm/contest.go | 4 ++-- orm/response.go | 14 ++++++++++---- templates/responses_show.html.tpl | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/orm/contest.go b/orm/contest.go index 8687f120..48fa9d76 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -110,13 +110,13 @@ func (c *Contest) Read(db *Database, args map[string]string, w http.ResponseWrit } for _, r := range contest.Rank { - if err := calcScore(db, r); err != nil { + if err := calcScoreAndDuration(db, r); err != nil { return nil, err } } sort.SliceStable(contest.Rank, func(i, j int) bool { - return contest.Rank[i].Score < contest.Rank[j].Score + return (contest.Rank[i].Score >= contest.Rank[j].Score) && (contest.Rank[i].Duration < contest.Rank[j].Duration) }) return &contest, nil diff --git a/orm/response.go b/orm/response.go index 26ecd671..18c437eb 100644 --- a/orm/response.go +++ b/orm/response.go @@ -38,8 +38,9 @@ type Response struct { EndTime time.Time TimeLeft time.Duration - Score int `gorm:"-"` - ScoreString string `gorm:"-"` + Score int `gorm:"-"` + ScoreString string `gorm:"-"` + Duration time.Duration `gorm:"-"` Questions []*Question SingleResponses []*SingleResponse `gorm:"-"` @@ -104,7 +105,7 @@ func (model *Response) BeforeSave(tx *gorm.DB) error { return nil } -func calcScore(db *Database, response *Response) error { +func calcScoreAndDuration(db *Database, response *Response) error { response.Score = 0 if response.AnswersIDs != "" { srIDs := strings.Split(response.AnswersIDs, " ") @@ -130,7 +131,12 @@ func calcScore(db *Database, response *Response) error { } - response.ScoreString = fmt.Sprintf("%s punti %v", strconv.Itoa(response.Score), response.EndTime.Sub(response.StartTime)) + if response.StartTime.IsZero() { + response.ScoreString = "non ancora visualizzata" + } else { + response.Duration = response.UpdatedAt.Sub(response.StartTime) + response.ScoreString = fmt.Sprintf("%s punti %v", strconv.Itoa(response.Score), response.Duration) + } return nil } diff --git a/templates/responses_show.html.tpl b/templates/responses_show.html.tpl index 9d37aff2..be7eac78 100644 --- a/templates/responses_show.html.tpl +++ b/templates/responses_show.html.tpl @@ -40,7 +40,7 @@
Durata della gara
{{if not .Data.Contest.IsAlwaysActive}} - {{.Data.Contest.Duration}} min (la prova dovrà comunque essere consegnata al massimo entro le ore {{.Data.Contest.EndTime|convertTime}}) + {{.Data.Contest.Duration}} min {{if .Data.StartTime|zeroTime}}(la prova dovrà comunque essere consegnata al massimo entro le ore {{.Data.Contest.EndTime|convertTime}}){{end}} {{else}} La gara ha una durata illimitata. {{end}}