From 3772af496552ad53f0c4e7e846ab9dfb0f2cdff8 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sun, 2 Apr 2023 16:49:10 +0200 Subject: [PATCH] Fix EstimateGain function --- libfic/stats.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libfic/stats.go b/libfic/stats.go index b1ae2314..5961e960 100644 --- a/libfic/stats.go +++ b/libfic/stats.go @@ -38,12 +38,18 @@ func exoptsQuery(whereExo string) string { exercices_table = "exercices_discounted" } - return `SELECT S.id_team, S.time, E.gain AS points, coeff, S.reason, S.id_exercice FROM ( + query := `SELECT S.id_team, S.time, E.gain AS points, coeff, S.reason, S.id_exercice FROM ( SELECT id_team, id_exercice, MIN(time) AS time, ` + fmt.Sprintf("%f", FirstBlood) + ` AS coeff, "First blood" AS reason FROM exercice_solved GROUP BY id_exercice UNION SELECT id_team, id_exercice, time, coefficient AS coeff, "Validation" AS reason FROM exercice_solved - ) S INNER JOIN ` + exercices_table + ` E ON S.id_exercice = E.id_exercice ` + whereExo + ` UNION ALL + ) S INNER JOIN ` + exercices_table + ` E ON S.id_exercice = E.id_exercice UNION ALL SELECT B.id_team, B.time, F.bonus_gain AS points, 1 AS coeff, "Bonus flag" AS reason, F.id_exercice FROM flag_found B INNER JOIN exercice_flags F ON F.id_flag = B.id_flag WHERE F.bonus_gain != 0 HAVING points != 0 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, "Tries" AS reason, id_exercice FROM ` + tries_table + ` S ` + whereExo + ` GROUP BY id_exercice, id_team` + 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, "Tries" AS reason, id_exercice FROM ` + tries_table + ` S GROUP BY id_exercice, id_team` + + if whereExo != "" { + query = "SELECT W.* FROM (" + query + ") W " + whereExo + } + + return query } func teamptsQuery() string { @@ -96,18 +102,18 @@ func ReadScoreGrid(fd *os.File) (grid []ScoreGridRow, err error) { // 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) +func (e *Exercice) EstimateGain(t *Team, solved bool) (pts float64, err error) { if solved { - return pts, err - } else { - pts += float64(e.Gain) * e.Coefficient - if e.SolvedCount() <= 0 { - pts += float64(e.Gain) * FirstBlood - } - return pts, nil + err = DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM ("+exoptsQuery("WHERE W.id_team = ? AND W.id_exercice = ?")+") A GROUP BY id_team", t.Id, e.Id).Scan(&pts) + return } + + pts += float64(e.Gain) * e.Coefficient + if e.SolvedCount() <= 0 { + pts += float64(e.Gain) * FirstBlood + } + + return } // GetPoints returns the score for the Team.