server/libfic/stats.go

141 lines
4.2 KiB
Go

package fic
import (
"database/sql"
"fmt"
"time"
)
// FirstBlood is the coefficient added to the challenge coefficient when a Team is the first to solve a challenge.
var FirstBlood = 0.12
// SubmissionCostBase is the basis amount of point lost per submission
var SubmissionCostBase = 0.5
func exoptsQuery(whereExo string) string {
return "SELECT S.id_team, S.time, E.gain AS points, coeff FROM (SELECT id_team, id_exercice, MIN(time) AS time, " + fmt.Sprintf("%f", FirstBlood) + " AS coeff FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, coefficient AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice " + whereExo + " UNION ALL SELECT id_team, MAX(time) AS time, (FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)) AS points, " + fmt.Sprintf("%f", SubmissionCostBase * -1) + " AS coeff FROM exercice_tries S " + whereExo + " GROUP BY id_exercice"
}
func rankQuery(whereTeam string) string {
return "SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (" + exoptsQuery("") + " UNION ALL SELECT D.id_team, D.time, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint HAVING points != 0) A " + whereTeam + " GROUP BY A.id_team ORDER BY score DESC, time ASC"
}
// Points
// EstimateGain calculates the amount of point the Team has (or could have, if not already solved) won.
func (e Exercice) EstimateGain(t Team, solved bool) (float64, error) {
var pts float64
err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (" + exoptsQuery("WHERE S.id_team = ? AND S.id_exercice = ?") + ") A GROUP BY id_team", t.Id, e.Id, t.Id, e.Id).Scan(&pts)
if solved {
return pts, err
} else {
pts += float64(e.Gain) * e.Coefficient
if e.SolvedCount() <= 0 {
pts += float64(e.Gain) * FirstBlood
}
return pts, nil
}
}
// GetPoints returns the score for the Team.
func (t Team) GetPoints() (float64, error) {
var tid *int64
var nb *float64
var tzzz *time.Time
err := DBQueryRow(rankQuery("WHERE A.id_team = ?"), t.Id).Scan(&tid, &nb, &tzzz)
if nb != nil {
return *nb, err
} else {
return 0, err
}
}
// GetRank returns a map which associates team ID their rank.
func GetRank() (map[int64]int, error) {
if rows, err := DBQuery(rankQuery("")); 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 float64
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
}
}
// Attempts
// GetTries retrieves all attempts made by the matching Team or challenge (both can be nil to not filter).
func GetTries(t *Team, e *Exercice) (times []time.Time, err error) {
var rows *sql.Rows
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
} else {
defer rows.Close()
for rows.Next() {
var tm time.Time
if err = rows.Scan(&tm); err != nil {
return
}
times = append(times, tm)
}
err = rows.Err()
return
}
}
// GetTryRank generates a special rank based on number of attempts
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
}
}