libfic: Update ScoreGridFormat format and expose stats

This commit is contained in:
nemunaire 2022-06-07 16:05:41 +02:00
parent 6aa0f4da95
commit 6d9fd1ff12
2 changed files with 32 additions and 16 deletions

View File

@ -2,7 +2,9 @@ package fic
import ( import (
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
"os"
"time" "time"
) )
@ -48,7 +50,15 @@ func rankQuery(whereTeam string) string {
) A ` + whereTeam + ` GROUP BY A.id_team ORDER BY score DESC, time ASC` ) 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" 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 { if rows, err := DBQuery(q, t.Id); err != nil {
return nil, err return nil, err
@ -56,28 +66,25 @@ func (t *Team) ScoreGrid() (grid []map[string]interface{}, err error) {
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var reason string var sgr ScoreGridRow
var exercice int64
var time time.Time
var points float64
var coeff float64
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 return nil, err
} }
grid = append(grid, map[string]interface{}{ grid = append(grid, sgr)
"reason": reason,
"id_exercice": exercice,
"time": time,
"points": points,
"coeff": coeff,
})
} }
} }
return return
} }
func ReadScoreGrid(fd *os.File) (grid []ScoreGridRow, err error) {
jdec := json.NewDecoder(fd)
err = jdec.Decode(&grid)
return
}
// Points // Points
// EstimateGain calculates the amount of point the Team has (or could have, if not already solved) won. // EstimateGain calculates the amount of point the Team has (or could have, if not already solved) won.

View File

@ -2,8 +2,10 @@ package fic
import ( import (
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt" "fmt"
"log" "log"
"os"
"path" "path"
"sort" "sort"
"strconv" "strconv"
@ -81,7 +83,7 @@ type myTeamExercice struct {
Issue string `json:"issue,omitempty"` Issue string `json:"issue,omitempty"`
IssueKind string `json:"issuekind,omitempty"` IssueKind string `json:"issuekind,omitempty"`
} }
type myTeam struct { type MyTeam struct {
Id int64 `json:"team_id"` Id int64 `json:"team_id"`
Name string `json:"name"` Name string `json:"name"`
Points int64 `json:"score"` 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 (a ByOrder) Less(i, j int) bool { return a[i].order < a[j].order }
func MyJSONTeam(t *Team, started bool) (interface{}, error) { func MyJSONTeam(t *Team, started bool) (interface{}, error) {
ret := myTeam{} ret := MyTeam{}
// Fill information about the team // Fill information about the team
if t == nil { if t == nil {
@ -401,3 +403,10 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
return ret, nil return ret, nil
} }
func ReadMyJSON(fd *os.File) (my MyTeam, err error) {
jdec := json.NewDecoder(fd)
err = jdec.Decode(&my)
return
}