From 6d9fd1ff12e6bfe0230d362ccecf96feab3498f4 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 7 Jun 2022 16:05:41 +0200 Subject: [PATCH] libfic: Update ScoreGridFormat format and expose stats --- libfic/stats.go | 35 +++++++++++++++++++++-------------- libfic/team_my.go | 13 +++++++++++-- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/libfic/stats.go b/libfic/stats.go index 2eb78c51..f6f2dd81 100644 --- a/libfic/stats.go +++ b/libfic/stats.go @@ -2,7 +2,9 @@ package fic import ( "database/sql" + "encoding/json" "fmt" + "os" "time" ) @@ -48,7 +50,15 @@ func rankQuery(whereTeam string) string { ) A ` + whereTeam + ` GROUP BY A.id_team ORDER BY score DESC, time ASC` } -func (t *Team) ScoreGrid() (grid []map[string]interface{}, err error) { +type ScoreGridRow struct { + Reason string `json:"reason"` + IdExercice int64 `json:"id_exercice"` + Time time.Time `json:"time"` + Points float64 `json:"points"` + Coeff float64 `json:"coeff"` +} + +func (t *Team) ScoreGrid() (grid []ScoreGridRow, err error) { q := "SELECT G.reason, G.id_exercice, G.time, G.points, G.coeff FROM (" + teamptsQuery() + ") AS G WHERE G.id_team = ? AND G.points != 0" if rows, err := DBQuery(q, t.Id); err != nil { return nil, err @@ -56,28 +66,25 @@ func (t *Team) ScoreGrid() (grid []map[string]interface{}, err error) { defer rows.Close() for rows.Next() { - var reason string - var exercice int64 - var time time.Time - var points float64 - var coeff float64 + var sgr ScoreGridRow - if err := rows.Scan(&reason, &exercice, &time, &points, &coeff); err != nil { + if err := rows.Scan(&sgr.Reason, &sgr.IdExercice, &sgr.Time, &sgr.Points, &sgr.Coeff); err != nil { return nil, err } - grid = append(grid, map[string]interface{}{ - "reason": reason, - "id_exercice": exercice, - "time": time, - "points": points, - "coeff": coeff, - }) + grid = append(grid, sgr) } } return } +func ReadScoreGrid(fd *os.File) (grid []ScoreGridRow, err error) { + jdec := json.NewDecoder(fd) + + err = jdec.Decode(&grid) + return +} + // Points // EstimateGain calculates the amount of point the Team has (or could have, if not already solved) won. diff --git a/libfic/team_my.go b/libfic/team_my.go index edb42598..f1b0ac64 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -2,8 +2,10 @@ package fic import ( "encoding/hex" + "encoding/json" "fmt" "log" + "os" "path" "sort" "strconv" @@ -81,7 +83,7 @@ type myTeamExercice struct { Issue string `json:"issue,omitempty"` IssueKind string `json:"issuekind,omitempty"` } -type myTeam struct { +type MyTeam struct { Id int64 `json:"team_id"` Name string `json:"name"` Points int64 `json:"score"` @@ -96,7 +98,7 @@ func (a ByOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByOrder) Less(i, j int) bool { return a[i].order < a[j].order } func MyJSONTeam(t *Team, started bool) (interface{}, error) { - ret := myTeam{} + ret := MyTeam{} // Fill information about the team if t == nil { @@ -401,3 +403,10 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { return ret, nil } + +func ReadMyJSON(fd *os.File) (my MyTeam, err error) { + jdec := json.NewDecoder(fd) + + err = jdec.Decode(&my) + return +}