Use pointer receiver more offen

This commit is contained in:
nemunaire 2021-11-22 15:35:07 +01:00
parent 6999b4e728
commit c7569b5e54
59 changed files with 688 additions and 672 deletions

View file

@ -24,15 +24,15 @@ type Team struct {
Password *string `json:"password"`
}
func getTeams(filter string) ([]Team, error) {
func getTeams(filter string) ([]*Team, error) {
if rows, err := DBQuery("SELECT id_team, name, color, active, external_id, password FROM teams " + filter); err != nil {
return nil, err
} else {
defer rows.Close()
var teams = make([]Team, 0)
var teams []*Team
for rows.Next() {
var t Team
t := &Team{}
if err := rows.Scan(&t.Id, &t.Name, &t.Color, &t.Active, &t.ExternalId, &t.Password); err != nil {
return nil, err
}
@ -47,18 +47,18 @@ func getTeams(filter string) ([]Team, error) {
}
// GetTeams returns a list of registered Team from the database.
func GetTeams() ([]Team, error) {
func GetTeams() ([]*Team, error) {
return getTeams("")
}
// GetActiveTeams returns a list of registered Team from the database, limited to team to generate.
func GetActiveTeams() ([]Team, error) {
func GetActiveTeams() ([]*Team, error) {
return getTeams("WHERE active = 1")
}
// GetTeam retrieves a Team from its identifier.
func GetTeam(id int64) (Team, error) {
var t Team
func GetTeam(id int64) (*Team, error) {
t := &Team{}
if err := DBQueryRow("SELECT id_team, name, color, active, external_id, password FROM teams WHERE id_team = ?", id).Scan(&t.Id, &t.Name, &t.Color, &t.Active, &t.ExternalId, &t.Password); err != nil {
return t, err
}
@ -67,8 +67,8 @@ func GetTeam(id int64) (Team, error) {
}
// GetTeamBySerial retrieves a Team from one of its associated certificates.
func GetTeamBySerial(serial int64) (Team, error) {
var t Team
func GetTeamBySerial(serial int64) (*Team, error) {
t := &Team{}
if err := DBQueryRow("SELECT T.id_team, T.name, T.color, T.active, T.external_id, T.password FROM certificates C INNER JOIN teams T ON T.id_team = C.id_team WHERE id_cert = ?", serial).Scan(&t.Id, &t.Name, &t.Color, &t.Active, &t.ExternalId, &t.Password); err != nil {
return t, err
}
@ -77,18 +77,18 @@ func GetTeamBySerial(serial int64) (Team, error) {
}
// CreateTeam creates and fills a new struct Team and registers it into the database.
func CreateTeam(name string, color uint32, externalId string) (Team, error) {
func CreateTeam(name string, color uint32, externalId string) (*Team, error) {
if res, err := DBExec("INSERT INTO teams (name, color, external_id) VALUES (?, ?, ?)", name, color, externalId); err != nil {
return Team{}, err
return nil, err
} else if tid, err := res.LastInsertId(); err != nil {
return Team{}, err
return nil, err
} else {
return Team{tid, name, color, true, "", nil}, nil
return &Team{tid, name, color, true, "", nil}, nil
}
}
// Update applies modifications back to the database.
func (t Team) Update() (int64, error) {
func (t *Team) Update() (int64, error) {
if res, err := DBExec("UPDATE teams SET name = ?, color = ?, active = ?, external_id = ?, password = ? WHERE id_team = ?", t.Name, t.Color, t.Active, t.ExternalId, t.Password, t.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -99,7 +99,7 @@ func (t Team) Update() (int64, error) {
}
// Delete the challenge from the database
func (t Team) Delete() (int64, error) {
func (t *Team) Delete() (int64, error) {
if _, err := DBExec("DELETE FROM team_members WHERE id_team = ?", t.Id); err != nil {
return 0, err
} else if res, err := DBExec("DELETE FROM teams WHERE id_team = ?", t.Id); err != nil {
@ -112,7 +112,7 @@ func (t Team) Delete() (int64, error) {
}
// HasAccess checks if the Team has access to the given challenge.
func (t Team) HasAccess(e Exercice) bool {
func (t *Team) HasAccess(e *Exercice) bool {
if UnlockedChallengeDepth < 0 {
return true
}
@ -123,11 +123,11 @@ func (t Team) HasAccess(e Exercice) bool {
return true
}
ed := Exercice{}
ed := &Exercice{}
ed.Id = *e.Depend
s, _ := t.HasSolved(ed)
if s {
return s
s := t.HasSolved(ed)
if s != nil {
return true
}
// Prepare next iteration
@ -141,7 +141,7 @@ func (t Team) HasAccess(e Exercice) bool {
}
// CanDownload checks if the Team has access to the given file.
func (t Team) CanDownload(f EFile) bool {
func (t *Team) CanDownload(f *EFile) bool {
if deps, err := f.GetDepends(); err != nil {
log.Printf("Unable to retrieve file dependencies: %s\n", err)
return false
@ -160,7 +160,7 @@ func (t Team) CanDownload(f EFile) bool {
}
// CanSeeHint checks if the Team has access to the given hint.
func (t Team) CanSeeHint(h EHint) bool {
func (t *Team) CanSeeHint(h *EHint) bool {
if deps, err := h.GetDepends(); err != nil {
log.Printf("Unable to retrieve flag dependencies: %s\n", err)
return false
@ -179,7 +179,7 @@ func (t Team) CanSeeHint(h EHint) bool {
}
// CanSeeFlag checks if the Team has access to the given flag.
func (t Team) CanSeeFlag(k Flag) bool {
func (t *Team) CanSeeFlag(k Flag) bool {
if deps, err := k.GetDepends(); err != nil {
log.Printf("Unable to retrieve flag dependencies: %s\n", err)
return false
@ -198,7 +198,7 @@ func (t Team) CanSeeFlag(k Flag) bool {
}
// NbTry retrieves the number of attempts made by the Team to the given challenge.
func NbTry(t *Team, e Exercice) int {
func NbTry(t *Team, e *Exercice) int {
tries_table := "exercice_tries"
if SubmissionUniqueness {
tries_table = "exercice_distinct_tries"
@ -220,33 +220,33 @@ func NbTry(t *Team, e Exercice) int {
}
// HasHint checks if the Team has revealed the given Hint.
func (t Team) HasHint(h EHint) bool {
func (t *Team) HasHint(h *EHint) bool {
var tm *time.Time
DBQueryRow("SELECT MIN(time) FROM team_hints WHERE id_team = ? AND id_hint = ?", t.Id, h.Id).Scan(&tm)
return tm != nil
}
// OpenHint registers to the database that the Team has now revealed.
func (t Team) OpenHint(h EHint) error {
func (t *Team) OpenHint(h *EHint) error {
_, err := DBExec("INSERT INTO team_hints (id_team, id_hint, time, coefficient) VALUES (?, ?, ?, ?)", t.Id, h.Id, time.Now(), math.Max(HintCoefficient, 0.0))
return err
}
// SeeChoices checks if the Team has revealed the given choices.
func (t Team) SeeChoices(k FlagKey) bool {
func (t *Team) SeeChoices(k *FlagKey) bool {
var tm *time.Time
DBQueryRow("SELECT MIN(time) FROM team_wchoices WHERE id_team = ? AND id_flag = ?", t.Id, k.Id).Scan(&tm)
return tm != nil
}
// DisplayChoices registers to the database that the Team has now revealed.
func (t Team) DisplayChoices(k FlagKey) error {
func (t *Team) DisplayChoices(k *FlagKey) error {
_, err := DBExec("INSERT INTO team_wchoices (id_team, id_flag, time, coefficient) VALUES (?, ?, ?, ?)", t.Id, k.Id, time.Now(), math.Max(WChoiceCoefficient, 0.0))
return err
}
// CountTries gets the amount of attempts made by the Team and retrieves the time of the latest attempt.
func (t Team) CountTries(e Exercice) (int64, time.Time) {
func (t *Team) CountTries(e *Exercice) (int64, *time.Time) {
table := "exercice_tries"
if CountOnlyNotGoodTries {
table += "_notgood"
@ -257,22 +257,22 @@ func (t Team) CountTries(e Exercice) (int64, time.Time) {
if DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM "+table+" WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &tm); tm == nil {
if CountOnlyNotGoodTries {
if DBQueryRow("SELECT COUNT(id_exercice), MAX(time) FROM exercice_tries WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&nb, &tm); tm == nil {
return 0, time.Unix(0, 0)
return 0, nil
} else {
return 0, *tm
return 0, tm
}
}
return 0, time.Unix(0, 0)
return 0, nil
} else if nb == nil {
return 0, *tm
return 0, tm
} else {
return *nb, *tm
return *nb, tm
}
}
// LastTryDist retrieves the distance to the correct answers, for the given challenge.
// The distance is the number of bad responses given in differents MCQs.
func (t Team) LastTryDist(e Exercice) int64 {
func (t *Team) LastTryDist(e *Exercice) int64 {
var nb *int64
if DBQueryRow("SELECT nbdiff FROM exercice_tries WHERE id_team = ? AND id_exercice = ? ORDER BY time DESC LIMIT 1", t.Id, e.Id).Scan(&nb); nb == nil {
return 0
@ -283,17 +283,13 @@ func (t Team) LastTryDist(e Exercice) int64 {
// HasSolved checks if the Team already has validated the given challenge.
// Note that the function also returns the effective validation timestamp.
func (t Team) HasSolved(e Exercice) (bool, time.Time) {
var tm *time.Time
if DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm); tm == nil {
return false, time.Unix(0, 0)
} else {
return true, *tm
}
func (t *Team) HasSolved(e *Exercice) (tm *time.Time) {
DBQueryRow("SELECT MIN(time) FROM exercice_solved WHERE id_team = ? AND id_exercice = ?", t.Id, e.Id).Scan(&tm)
return
}
// GetSolvedRank returns the number of teams that solved the challenge before the Team.
func (t Team) GetSolvedRank(e Exercice) (nb int64, err error) {
func (t *Team) GetSolvedRank(e *Exercice) (nb int64, err error) {
if rows, errr := DBQuery("SELECT id_team FROM exercice_solved WHERE id_exercice = ? ORDER BY time ASC", e.Id); errr != nil {
return nb, errr
} else {
@ -315,10 +311,10 @@ func (t Team) GetSolvedRank(e Exercice) (nb int64, err error) {
}
// HasPartiallySolved checks if the Team already has unlocked the given flag and returns the validation's timestamp.
func (t Team) HasPartiallySolved(f Flag) (tm *time.Time) {
if k, ok := f.(FlagKey); ok {
func (t *Team) HasPartiallySolved(f Flag) (tm *time.Time) {
if k, ok := f.(*FlagKey); ok {
DBQueryRow("SELECT MIN(time) FROM flag_found WHERE id_team = ? AND id_flag = ?", t.Id, k.Id).Scan(&tm)
} else if m, ok := f.(MCQ); ok {
} else if m, ok := f.(*MCQ); ok {
DBQueryRow("SELECT MIN(time) FROM mcq_found WHERE id_team = ? AND id_mcq = ?", t.Id, m.Id).Scan(&tm)
} else {
log.Fatal("Unknown flag type")
@ -327,7 +323,7 @@ func (t Team) HasPartiallySolved(f Flag) (tm *time.Time) {
}
// HashedPassword compute a bcrypt version of the team's password.
func (t Team) HashedPassword() (string, error) {
func (t *Team) HashedPassword() (string, error) {
if t.Password == nil {
if passwd, err := GeneratePassword(); err != nil {
return "", err