diff --git a/VERSION b/VERSION index 297280ff..527d8d97 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.9-32-gc18cb82-master +0.9.9-13-ge0f5b12-production diff --git a/orm/contest.go b/orm/contest.go index f9dcc2ef..8687f120 100644 --- a/orm/contest.go +++ b/orm/contest.go @@ -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 } diff --git a/orm/response.go b/orm/response.go index 014dcb48..26ecd671 100644 --- a/orm/response.go +++ b/orm/response.go @@ -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) diff --git a/templates/contests_show.html.tpl b/templates/contests_show.html.tpl index 9ecc4a71..0295f3f1 100644 --- a/templates/contests_show.html.tpl +++ b/templates/contests_show.html.tpl @@ -66,6 +66,20 @@ +
+
+ + {{$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")}} +
+
+ {{ end }}