diff --git a/admin/api/stats.go b/admin/api/stats.go index 85a4b5b6..36addb45 100644 --- a/admin/api/stats.go +++ b/admin/api/stats.go @@ -30,6 +30,7 @@ func genStats() (interface{}, error) { exos[fmt.Sprintf("%d", exercice.Id)] = fic.ExportedExercice{ exercice.Title, exercice.Gain, + exercice.Coefficient, exercice.SolvedCount(), exercice.TriedTeamCount(), } diff --git a/admin/static/js/app.js b/admin/static/js/app.js index 004334c7..0f73a351 100644 --- a/admin/static/js/app.js +++ b/admin/static/js/app.js @@ -275,7 +275,7 @@ angular.module("FICApp") $scope.exercice = Exercice.get({ exerciceId: $routeParams.exerciceId }); } $scope.exercices = Exercice.query(); - $scope.fields = ["title", "statement", "depend", "gain", "videoURI"]; + $scope.fields = ["title", "statement", "depend", "gain", "coefficient", "videoURI"]; $scope.saveExercice = function() { if (this.exercice.id) { diff --git a/libfic/db.go b/libfic/db.go index 62f1c26a..3c1e2d0a 100644 --- a/libfic/db.go +++ b/libfic/db.go @@ -66,6 +66,7 @@ CREATE TABLE IF NOT EXISTS exercices( statement TEXT NOT NULL, depend INTEGER, gain INTEGER NOT NULL, + coefficient_cur FLOAT NOT NULL DEFAULT 1.0, video_uri VARCHAR(255) NOT NULL, FOREIGN KEY(id_theme) REFERENCES themes(id_theme), FOREIGN KEY(depend) REFERENCES exercices(id_exercice) @@ -126,7 +127,7 @@ CREATE TABLE IF NOT EXISTS exercice_solved( id_exercice INTEGER NOT NULL, id_team INTEGER NOT NULL, time TIMESTAMP NOT NULL, - coefficient FLOAT NOT NULL, + coefficient FLOAT NOT NULL DEFAULT 1.0, FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice), FOREIGN KEY(id_team) REFERENCES teams(id_team) ); diff --git a/libfic/exercice.go b/libfic/exercice.go index c237617c..f66faf69 100644 --- a/libfic/exercice.go +++ b/libfic/exercice.go @@ -8,17 +8,18 @@ import ( var PartialValidation bool type Exercice struct { - Id int64 `json:"id"` - Title string `json:"title"` - Statement string `json:"statement"` - Depend *int64 `json:"depend"` - Gain int64 `json:"gain"` - VideoURI string `json:"videoURI"` + Id int64 `json:"id"` + Title string `json:"title"` + Statement string `json:"statement"` + Depend *int64 `json:"depend"` + Gain int64 `json:"gain"` + Coefficient float64 `json:"coefficient"` + VideoURI string `json:"videoURI"` } func GetExercice(id int64) (Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, title, statement, depend, gain, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.VideoURI); err != nil { + if err := DBQueryRow("SELECT id_exercice, title, statement, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil { return Exercice{}, err } @@ -27,7 +28,7 @@ func GetExercice(id int64) (Exercice, error) { func (t Theme) GetExercice(id int) (Exercice, error) { var e Exercice - if err := DBQueryRow("SELECT id_exercice, title, statement, depend, gain, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.VideoURI); err != nil { + if err := DBQueryRow("SELECT id_exercice, title, statement, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil { return Exercice{}, err } @@ -35,7 +36,7 @@ func (t Theme) GetExercice(id int) (Exercice, error) { } func GetExercices() ([]Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, title, statement, depend, gain, video_uri FROM exercices"); err != nil { + if rows, err := DBQuery("SELECT id_exercice, title, statement, depend, gain, coefficient_cur, video_uri FROM exercices"); err != nil { return nil, err } else { defer rows.Close() @@ -43,7 +44,7 @@ func GetExercices() ([]Exercice, error) { var exos = make([]Exercice, 0) for rows.Next() { var e Exercice - if err := rows.Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.VideoURI); err != nil { + if err := rows.Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil { return nil, err } exos = append(exos, e) @@ -57,7 +58,7 @@ func GetExercices() ([]Exercice, error) { } func (t Theme) GetExercices() ([]Exercice, error) { - if rows, err := DBQuery("SELECT id_exercice, title, statement, depend, gain, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil { + if rows, err := DBQuery("SELECT id_exercice, title, statement, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil { return nil, err } else { defer rows.Close() @@ -65,7 +66,7 @@ func (t Theme) GetExercices() ([]Exercice, error) { var exos = make([]Exercice, 0) for rows.Next() { var e Exercice - if err := rows.Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.VideoURI); err != nil { + if err := rows.Scan(&e.Id, &e.Title, &e.Statement, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil { return nil, err } exos = append(exos, e) @@ -91,15 +92,15 @@ func (t Theme) AddExercice(title string, statement string, depend *Exercice, gai return Exercice{}, err } else { if depend == nil { - return Exercice{eid, title, statement, nil, gain, videoURI}, nil + return Exercice{eid, title, statement, nil, gain, 1.0, videoURI}, nil } else { - return Exercice{eid, title, statement, &depend.Id, gain, videoURI}, nil + return Exercice{eid, title, statement, &depend.Id, gain, 1.0, videoURI}, nil } } } func (e Exercice) Update() (int64, error) { - if res, err := DBExec("UPDATE exercices SET title = ?, statement = ?, depend = ?, gain = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.Statement, e.Depend, e.Gain, e.VideoURI, e.Id); err != nil { + if res, err := DBExec("UPDATE exercices SET title = ?, statement = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.Statement, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Id); err != nil { return 0, err } else if nb, err := res.RowsAffected(); err != nil { return 0, err @@ -157,7 +158,7 @@ func (e Exercice) NewTry(t Team) error { } func (e Exercice) Solved(t Team) error { - if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time) VALUES (?, ?, ?)", e.Id, t.Id, time.Now()); err != nil { + if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time, coefficient) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), e.Coefficient); err != nil { return err } else { return nil diff --git a/libfic/stats.go b/libfic/stats.go index 8ec8bdcc..1cbbe600 100644 --- a/libfic/stats.go +++ b/libfic/stats.go @@ -13,7 +13,7 @@ var SubmissionCostBase = 0.5 func (t Team) GetPoints() (float64, error) { var nb *float64 - err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, 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, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb) + err := DBQueryRow("SELECT SUM(A.points * A.coeff) AS score FROM (SELECT S.id_team, 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 UNION ALL SELECT D.id_team, H.cost AS points, -1.0 AS coeff FROM team_hints D INNER JOIN exercice_hints H ON H.id_hint = D.id_hint UNION ALL SELECT id_team, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A WHERE A.id_team = ? GROUP BY A.id_team", t.Id).Scan(&nb) if nb != nil { return *nb, err } else { @@ -22,7 +22,7 @@ func (t Team) GetPoints() (float64, error) { } func GetRank() (map[int64]int, error) { - if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (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, 1 AS coeff FROM exercice_solved) S INNER JOIN exercices E ON S.id_exercice = E.id_exercice 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 UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil { + if rows, err := DBQuery("SELECT A.id_team, SUM(A.points * A.coeff) AS score, MAX(A.time) AS time FROM (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 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 UNION ALL SELECT id_team, MAX(time) AS time, ((FLOOR(COUNT(*)/10 - 1) * (FLOOR(COUNT(*)/10)))/0.2 + (FLOOR(COUNT(*)/10) * (COUNT(*)%10)))/" + fmt.Sprintf("%f", 1/SubmissionCostBase) + " AS points, -1 AS coeff FROM exercice_tries GROUP BY id_exercice HAVING points != 0) A GROUP BY A.id_team ORDER BY score DESC, time ASC"); err != nil { return nil, err } else { defer rows.Close() diff --git a/libfic/theme.go b/libfic/theme.go index 7b2edeaa..3956fbee 100644 --- a/libfic/theme.go +++ b/libfic/theme.go @@ -72,10 +72,11 @@ func (t Theme) Delete() (int64, error) { } type ExportedExercice struct { - Title string `json:"title"` - Gain int64 `json:"gain"` - Solved int64 `json:"solved"` - Tried int64 `json:"tried"` + Title string `json:"title"` + Gain int64 `json:"gain"` + Coeff float64 `json:"curcoeff"` + Solved int64 `json:"solved"` + Tried int64 `json:"tried"` } type exportedTheme struct { @@ -98,6 +99,7 @@ func ExportThemes() (interface{}, error) { exos[fmt.Sprintf("%d", exercice.Id)] = ExportedExercice{ exercice.Title, exercice.Gain, + exercice.Coefficient, exercice.SolvedCount(), exercice.TriedTeamCount(), }