2017-01-29 12:38:39 +00:00
|
|
|
package fic
|
|
|
|
|
2018-05-12 09:36:23 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2017-01-29 12:38:39 +00:00
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// truncateTable performs an insecure wipe on the given tables.
|
2017-01-29 12:38:39 +00:00
|
|
|
func truncateTable(tables ...string) (error) {
|
2018-05-12 09:36:23 +00:00
|
|
|
if tx, err := db.BeginTx(context.TODO(), nil); err != nil {
|
2017-01-29 12:38:39 +00:00
|
|
|
return err
|
2018-05-12 09:36:23 +00:00
|
|
|
} else {
|
|
|
|
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, table := range tables {
|
|
|
|
if _, err := tx.Exec("TRUNCATE TABLE " + table + ";"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec("SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
2017-01-29 12:38:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// ResetGame resets all tables containing team attempts and solves.
|
2017-01-29 12:38:39 +00:00
|
|
|
func ResetGame() (error) {
|
2017-12-16 00:16:30 +00:00
|
|
|
return truncateTable("team_hints", "key_found", "mcq_found", "exercice_solved", "exercice_tries")
|
2017-01-29 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// ResetExercices wipes out all challenges (both attempts and statements).
|
2017-01-29 12:38:39 +00:00
|
|
|
func ResetExercices() (error) {
|
2017-12-16 00:16:30 +00:00
|
|
|
return truncateTable("team_hints", "exercice_files", "key_found", "exercice_keys", "exercice_solved", "exercice_tries", "exercice_hints", "mcq_found", "mcq_entries", "exercice_mcq", "exercices", "themes")
|
2017-01-29 12:38:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 18:07:08 +00:00
|
|
|
// ResetTeams wipes out all teams, incluings members and attempts.
|
2017-01-29 12:38:39 +00:00
|
|
|
func ResetTeams() (error) {
|
2017-12-16 00:16:30 +00:00
|
|
|
return truncateTable("team_hints", "key_found", "mcq_found", "exercice_solved", "exercice_tries", "team_members", "teams")
|
2017-01-29 12:38:39 +00:00
|
|
|
}
|