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 // SubmissionUniqueness don't count multiple times identical tries. var SubmissionUniqueness = false func exoptsQuery(whereExo string) string { tries_table := "exercice_tries" if SubmissionUniqueness { tries_table = "exercice_distinct_tries" } return `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 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, "Tries" AS reason, id_exercice FROM ` + tries_table + ` S ` + whereExo + ` GROUP BY id_exercice, id_team` } func teamptsQuery() string { return exoptsQuery("") + ` UNION ALL SELECT D.id_team, D.time, H.cost AS points, D.coefficient * -1 AS coeff, "Hint" AS reason, H.id_exercice FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint HAVING points != 0 UNION ALL SELECT W.id_team, W.time, F.choices_cost AS points, W.coefficient * -1 AS coeff, "Display choices" AS reason, F.id_exercice FROM team_wchoices W INNER JOIN exercice_flags F ON F.id_flag = W.id_flag HAVING points != 0` } func rankQuery(whereTeam string) string { return `SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM ( ` + teamptsQuery() + ` ) A ` + whereTeam + ` GROUP BY A.id_team ORDER BY score DESC, time ASC` } func (t Team) ScoreGrid() (grid []map[string]interface{}, 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 } else { defer rows.Close() for rows.Next() { var reason string var exercice int64 var time time.Time var points float64 var coeff float64 if err := rows.Scan(&reason, &exercice, &time, &points, &coeff); err != nil { return nil, err } grid = append(grid, map[string]interface{}{ "reason": reason, "id_exercice": exercice, "time": time, "points": points, "coeff": coeff, }) } } return } // 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 } }