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 (
"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.

View File

@ -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
}