Improve ranking

This commit is contained in:
Andrea Fazzi 2021-01-05 12:28:40 +01:00
parent 5a7e8e433b
commit c97792e762
3 changed files with 13 additions and 7 deletions

View file

@ -110,13 +110,13 @@ func (c *Contest) Read(db *Database, args map[string]string, w http.ResponseWrit
} }
for _, r := range contest.Rank { for _, r := range contest.Rank {
if err := calcScore(db, r); err != nil { if err := calcScoreAndDuration(db, r); err != nil {
return nil, err return nil, err
} }
} }
sort.SliceStable(contest.Rank, func(i, j int) bool { 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 return &contest, nil

View file

@ -40,6 +40,7 @@ type Response struct {
Score int `gorm:"-"` Score int `gorm:"-"`
ScoreString string `gorm:"-"` ScoreString string `gorm:"-"`
Duration time.Duration `gorm:"-"`
Questions []*Question Questions []*Question
SingleResponses []*SingleResponse `gorm:"-"` SingleResponses []*SingleResponse `gorm:"-"`
@ -104,7 +105,7 @@ func (model *Response) BeforeSave(tx *gorm.DB) error {
return nil return nil
} }
func calcScore(db *Database, response *Response) error { func calcScoreAndDuration(db *Database, response *Response) error {
response.Score = 0 response.Score = 0
if response.AnswersIDs != "" { if response.AnswersIDs != "" {
srIDs := strings.Split(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 return nil
} }

View file

@ -40,7 +40,7 @@
<dt class="col-sm-3">Durata della gara</dt> <dt class="col-sm-3">Durata della gara</dt>
<dd class="col-sm-9"> <dd class="col-sm-9">
{{if not .Data.Contest.IsAlwaysActive}} {{if not .Data.Contest.IsAlwaysActive}}
{{.Data.Contest.Duration}} min (la prova dovrà comunque essere consegnata <strong>al massimo</strong> 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}} {{else}}
La gara ha una durata illimitata. La gara ha una durata illimitata.
{{end}} {{end}}