Merge branch 'production'

This commit is contained in:
Andrea Fazzi 2021-01-05 11:43:42 +01:00
commit 7faac499e3
4 changed files with 64 additions and 2 deletions

View file

@ -1 +1 @@
0.9.9-32-gc18cb82-master
0.9.9-13-ge0f5b12-production

View file

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"math/rand"
"sort"
"strconv"
"strings"
@ -38,6 +39,7 @@ type Contest struct {
Responses []*Response
StartedResponses []*Response
SavedResponses []*Response
Rank []*Response
prevStartTime time.Time
}
@ -99,11 +101,24 @@ func (c *Contest) Read(db *Database, args map[string]string, w http.ResponseWrit
Preload("SavedResponses", func(tx *gorm.DB) *gorm.DB {
return db.DB().Where("end_time <> ?", time.Time{})
}).
Preload("Rank", func(tx *gorm.DB) *gorm.DB {
return db.DB().Order("end_time ASC")
}).
Preload("Questions").
First(&contest, id).Error; err != nil {
return nil, err
}
for _, r := range contest.Rank {
if err := calcScore(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, nil
}

View file

@ -38,7 +38,8 @@ type Response struct {
EndTime time.Time
TimeLeft time.Duration
Score int `gorm:"-"`
Score int `gorm:"-"`
ScoreString string `gorm:"-"`
Questions []*Question
SingleResponses []*SingleResponse `gorm:"-"`
@ -103,6 +104,37 @@ func (model *Response) BeforeSave(tx *gorm.DB) error {
return nil
}
func calcScore(db *Database, response *Response) error {
response.Score = 0
if response.AnswersIDs != "" {
srIDs := strings.Split(response.AnswersIDs, " ")
for _, srID := range srIDs {
id, err := strconv.Atoi(srID)
if err != nil {
return err
}
response.SingleResponses = append(response.SingleResponses, &SingleResponse{uint(id)})
}
for _, sr := range response.SingleResponses {
var answer Answer
if err := db._db.First(&answer, sr.AnswerID).Error; err != nil {
return err
}
if answer.Correct {
response.Score++
}
}
}
response.ScoreString = fmt.Sprintf("%s punti %v", strconv.Itoa(response.Score), response.EndTime.Sub(response.StartTime))
return nil
}
func (model *Response) Create(db *Database, args map[string]string, w http.ResponseWriter, r *http.Request) (interface{}, error) {
if r.Method == "GET" {
response := new(Response)
@ -151,6 +183,7 @@ func (model *Response) Read(db *Database, args map[string]string, w http.Respons
}
if response.AnswersIDs != "" {
response.Score = 0
srIDs := strings.Split(response.AnswersIDs, " ")
for _, srID := range srIDs {
id, err := strconv.Atoi(srID)

View file

@ -66,6 +66,20 @@
</div>
</div>
<div class="row">
<div class="col-md-12">
{{$options := `
title: "Classifica"
model: "Response"
icon: "fa fa-hourglass"
`}}
{{$noElements := "Nessun partecipante ha ancora consegnato la prova."}}
{{template "relation_list" dict "options" ($options|yaml) "data" .Data.Rank "noElements" $noElements "small" (toSlice "ScoreString")}}
</div>
</div>
</div>
{{ end }}