Split team.go into multiple files
This commit is contained in:
parent
f0621fa191
commit
c57b612205
4 changed files with 249 additions and 222 deletions
109
libfic/stats.go
Normal file
109
libfic/stats.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Points
|
||||
|
||||
func (t Team) GetPoints() (int64, error) {
|
||||
var nb *int64
|
||||
err := DBQueryRow("SELECT SUM(E.gain) AS gains FROM exercice_solved S INNER JOIN exercices E ON E.id_exercice = S.id_exercice WHERE id_team = ?", t.Id).Scan(&nb)
|
||||
if nb != nil {
|
||||
return *nb, err
|
||||
} else {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
func GetRank() (map[int64]int, error) {
|
||||
if rows, err := DBQuery("SELECT id_team, SUM(E.gain) AS score, MAX(S.time) FROM exercice_solved S INNER JOIN exercices E ON E.id_exercice = S.id_exercice GROUP BY id_team HAVING score > 0 ORDER BY score DESC, time ASC"); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
rank := map[int64]int{}
|
||||
nteam := 0
|
||||
for rows.Next() {
|
||||
nteam += 1
|
||||
var tid int64
|
||||
var score int64
|
||||
var tzzz time.Time
|
||||
if err := rows.Scan(&tid, &score, &tzzz); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rank[tid] = nteam
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rank, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Tries
|
||||
|
||||
func GetTries(t *Team, e *Exercice) ([]time.Time, error) {
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
|
||||
if t == nil {
|
||||
if e == nil {
|
||||
rows, err = DBQuery("SELECT time FROM exercice_tries ORDER BY time ASC")
|
||||
} else {
|
||||
rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_exercice = ? ORDER BY time ASC", e.Id)
|
||||
}
|
||||
} else {
|
||||
if e == nil {
|
||||
rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_team = ? ORDER BY time ASC", t.Id)
|
||||
} else {
|
||||
rows, err = DBQuery("SELECT time FROM exercice_tries WHERE id_team = ? AND id_exercice = ? ORDER BY time ASC", t.Id, e.Id)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
times := make([]time.Time, 0)
|
||||
for rows.Next() {
|
||||
var tm time.Time
|
||||
if err := rows.Scan(&tm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
times = append(times, tm)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return times, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetTryRank() ([]int64, error) {
|
||||
if rows, err := DBQuery("SELECT id_team, COUNT(*) AS score FROM exercice_tries GROUP BY id_team HAVING score > 0 ORDER BY score DESC"); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
rank := make([]int64, 0)
|
||||
for rows.Next() {
|
||||
var tid int64
|
||||
var score int64
|
||||
if err := rows.Scan(&tid, &score); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rank = append(rank, tid)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rank, nil
|
||||
}
|
||||
}
|
Reference in a new issue