From e0f5b125750d71ab6d1985a2f5ac4c70913f1d77 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 5 Jan 2021 11:37:49 +0100 Subject: [PATCH 1/2] Add rank in contest show view --- orm/contest.go | 15 ++++++++++++++ orm/response.go | 35 +++++++++++++++++++++++++++++++- templates/contests_show.html.tpl | 14 +++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) 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 1c1e223c..6c9d0227 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 }} From 55ffb028a28029421919c0e0f5555bc20571223a Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 5 Jan 2021 11:37:49 +0100 Subject: [PATCH 2/2] [auto] bumped version to 0.9.9-13-ge0f5b12-production --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index f45e1514..527d8d97 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.9-11-g2e4cb01-production +0.9.9-13-ge0f5b12-production