Fix EstimateGain function

This commit is contained in:
nemunaire 2023-04-02 16:49:10 +02:00
parent 4451e41285
commit 3772af4965

View file

@ -38,12 +38,18 @@ func exoptsQuery(whereExo string) string {
exercices_table = "exercices_discounted" 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, 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 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 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 { func teamptsQuery() string {
@ -96,18 +102,18 @@ func ReadScoreGrid(fd *os.File) (grid []ScoreGridRow, err error) {
// 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.
func (e *Exercice) EstimateGain(t *Team, solved bool) (float64, error) { func (e *Exercice) EstimateGain(t *Team, solved bool) (pts float64, err 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 { if solved {
return pts, err 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)
} else { return
}
pts += float64(e.Gain) * e.Coefficient pts += float64(e.Gain) * e.Coefficient
if e.SolvedCount() <= 0 { if e.SolvedCount() <= 0 {
pts += float64(e.Gain) * FirstBlood pts += float64(e.Gain) * FirstBlood
} }
return pts, nil
} return
} }
// GetPoints returns the score for the Team. // GetPoints returns the score for the Team.