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

@ -52,45 +52,45 @@ type Exercice struct {
}
// GetExercice retrieves the challenge with the given id.
func GetExercice(id int64) (Exercice, error) {
func GetExercice(id int64) (*Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
return Exercice{}, err
return nil, err
}
return e, nil
return &e, nil
}
// GetExercice retrieves the challenge with the given id.
func (t Theme) GetExercice(id int) (Exercice, error) {
var e Exercice
func (t *Theme) GetExercice(id int) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
return Exercice{}, err
return nil, err
}
return e, nil
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t Theme) GetExerciceByTitle(title string) (Exercice, error) {
var e Exercice
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
e := &Exercice{}
if err := DBQueryRow("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.IdTheme, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
return Exercice{}, err
return nil, err
}
return e, nil
}
// GetExercices returns the list of all challenges present in the database.
func GetExercices() ([]Exercice, error) {
func GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices"); err != nil {
return nil, err
} else {
defer rows.Close()
var exos = make([]Exercice, 0)
exos := []*Exercice{}
for rows.Next() {
var e Exercice
e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
return nil, err
}
@ -105,15 +105,15 @@ func GetExercices() ([]Exercice, error) {
}
// GetExercices returns the list of all challenges in the Theme.
func (t Theme) GetExercices() ([]Exercice, error) {
func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, finished FROM exercices WHERE id_theme = ?", t.Id); err != nil {
return nil, err
} else {
defer rows.Close()
var exos = make([]Exercice, 0)
exos := []*Exercice{}
for rows.Next() {
var e Exercice
e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Finished); err != nil {
return nil, err
}
@ -128,8 +128,8 @@ func (t Theme) GetExercices() ([]Exercice, error) {
}
// SaveNamedExercice looks for an exercice with the same title to update it, or create it if it doesn't exists yet.
func (t Theme) SaveNamedExercice(e *Exercice) (err error) {
var search Exercice
func (t *Theme) SaveNamedExercice(e *Exercice) (err error) {
var search *Exercice
if search, err = t.GetExerciceByTitle(e.Title); err == nil {
// Force ID
e.Id = search.Id
@ -152,7 +152,7 @@ func (t Theme) SaveNamedExercice(e *Exercice) (err error) {
return
}
func (t Theme) addExercice(e *Exercice) (err error) {
func (t *Theme) addExercice(e *Exercice) (err error) {
var ik = "DEFAULT"
if len(e.IssueKind) > 0 {
ik = fmt.Sprintf("%q", e.IssueKind)
@ -175,13 +175,13 @@ func (t Theme) addExercice(e *Exercice) (err error) {
}
// AddExercice creates and fills a new struct Exercice and registers it into the database.
func (t Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (e Exercice, err error) {
func (t *Theme) AddExercice(title string, urlId string, path string, statement string, overview string, headline string, depend *Exercice, gain int64, videoURI string, finished string) (e *Exercice, err error) {
var dpd *int64 = nil
if depend != nil {
dpd = &depend.Id
}
e = Exercice{
e = &Exercice{
Title: title,
URLId: urlId,
Path: path,
@ -194,13 +194,13 @@ func (t Theme) AddExercice(title string, urlId string, path string, statement st
VideoURI: videoURI,
}
err = t.addExercice(&e)
err = t.addExercice(e)
return
}
// Update applies modifications back to the database.
func (e Exercice) Update() (int64, error) {
func (e *Exercice) Update() (int64, error) {
if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, headline = ?, issue = ?, issue_kind = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ?, finished = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Headline, e.Issue, e.IssueKind, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Finished, e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -221,7 +221,7 @@ func (e *Exercice) FixURLId() bool {
}
// Delete the challenge from the database.
func (e Exercice) Delete() (int64, error) {
func (e *Exercice) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM exercices WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
@ -232,7 +232,7 @@ func (e Exercice) Delete() (int64, error) {
}
// DeleteCascade the challenge from the database, including inner content but not player content.
func (e Exercice) DeleteCascade() (int64, error) {
func (e *Exercice) DeleteCascade() (int64, error) {
if _, err := DBExec("UPDATE exercices SET depend = NULL WHERE depend = ?", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_files_okey_deps WHERE id_file IN (SELECT id_file FROM exercice_files WHERE id_exercice = ?)", e.Id); err != nil {
@ -269,7 +269,7 @@ func (e Exercice) DeleteCascade() (int64, error) {
}
// DeleteCascadePlayer delete player content related to this challenge.
func (e Exercice) DeleteCascadePlayer() (int64, error) {
func (e *Exercice) DeleteCascadePlayer() (int64, error) {
if _, err := DBExec("DELETE FROM mcq_found WHERE id_mcq IN (SELECT id_mcq FROM exercice_mcq WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM flag_found WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
@ -288,7 +288,7 @@ func (e Exercice) DeleteCascadePlayer() (int64, error) {
}
// DeleteDeep the challenge from the database, including player content.
func (e Exercice) DeleteDeep() (int64, error) {
func (e *Exercice) DeleteDeep() (int64, error) {
if _, err := e.DeleteCascadePlayer(); err != nil {
return 0, err
} else {
@ -297,13 +297,13 @@ func (e Exercice) DeleteDeep() (int64, error) {
}
// GetLevel returns the number of dependancy challenges.
func (e Exercice) GetLevel() (int, error) {
func (e *Exercice) GetLevel() (int, error) {
dep := e.Depend
nb := 1
for dep != nil {
nb += 1
if nb > 10 || *dep == e.Id {
return nb, errors.New("Exceed number of levels")
return nb, errors.New("exceed number of levels")
} else if edep, err := GetExercice(*dep); err != nil {
return nb, err
} else {
@ -314,7 +314,7 @@ func (e Exercice) GetLevel() (int, error) {
}
// NewTry registers a solving attempt for the given Team.
func (e Exercice) NewTry(t Team, cksum []byte) error {
func (e *Exercice) NewTry(t *Team, cksum []byte) error {
if _, err := DBExec("INSERT INTO exercice_tries (id_exercice, id_team, time, cksum) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), cksum); err != nil {
return err
} else {
@ -324,7 +324,7 @@ func (e Exercice) NewTry(t Team, cksum []byte) error {
// UpdateTry applies modifications to the latest try registered for the given Team.
// Updated values are time and the given nbdiff.
func (e Exercice) UpdateTry(t Team, nbdiff int, oneGood bool) error {
func (e *Exercice) UpdateTry(t *Team, nbdiff int, oneGood bool) error {
if _, err := DBExec("UPDATE exercice_tries SET nbdiff = ?, onegood = ?, time = ? WHERE id_exercice = ? AND id_team = ? ORDER BY time DESC LIMIT 1", nbdiff, oneGood, time.Now(), e.Id, t.Id); err != nil {
return err
} else {
@ -333,7 +333,7 @@ func (e Exercice) UpdateTry(t Team, nbdiff int, oneGood bool) error {
}
// Solved registers that the given Team solves the challenge.
func (e Exercice) Solved(t Team) error {
func (e *Exercice) Solved(t *Team) error {
if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time, coefficient) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), e.Coefficient*ExerciceCurrentCoefficient); err != nil {
return err
} else {
@ -342,7 +342,7 @@ func (e Exercice) Solved(t Team) error {
}
// SolvedCount returns the number of Team that already have solved the challenge.
func (e Exercice) SolvedCount() int64 {
func (e *Exercice) SolvedCount() int64 {
var nb int64
if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
@ -352,7 +352,7 @@ func (e Exercice) SolvedCount() int64 {
}
// TriedTeamCount returns the number of Team that attempted to solve the exercice.
func (e Exercice) TriedTeamCount() int64 {
func (e *Exercice) TriedTeamCount() int64 {
tries_table := "exercice_tries"
if SubmissionUniqueness {
tries_table = "exercice_distinct_tries"
@ -367,7 +367,7 @@ func (e Exercice) TriedTeamCount() int64 {
}
// TriedCount returns the number of cumulative attempts, all Team combined, for the exercice.
func (e Exercice) TriedCount() int64 {
func (e *Exercice) TriedCount() int64 {
tries_table := "exercice_tries"
if SubmissionUniqueness {
tries_table = "exercice_distinct_tries"
@ -382,7 +382,7 @@ func (e Exercice) TriedCount() int64 {
}
// FlagSolved returns the list of flags solved.
func (e Exercice) FlagSolved() (res []int64) {
func (e *Exercice) FlagSolved() (res []int64) {
if rows, err := DBQuery("SELECT F.id_flag FROM flag_found F INNER JOIN exercice_flags E ON E.id_flag = F.id_flag WHERE E.id_exercice = ? GROUP BY id_flag", e.Id); err != nil {
return
} else {
@ -400,7 +400,7 @@ func (e Exercice) FlagSolved() (res []int64) {
}
// MCQSolved returns the list of mcqs solved.
func (e Exercice) MCQSolved() (res []int64) {
func (e *Exercice) MCQSolved() (res []int64) {
if rows, err := DBQuery("SELECT F.id_mcq FROM mcq_found F INNER JOIN exercice_mcq E ON E.id_mcq = F.id_mcq WHERE E.id_exercice = ? GROUP BY id_mcq", e.Id); err != nil {
return
} else {
@ -419,7 +419,7 @@ func (e Exercice) MCQSolved() (res []int64) {
// CheckResponse, given both flags and MCQ responses, figures out if thoses are correct (or if they are previously solved).
// In the meanwhile, CheckResponse registers good answers given (but it does not mark the challenge as solved at the end).
func (e Exercice) CheckResponse(cksum []byte, respflags map[int]string, respmcq map[int]bool, t Team) (bool, error) {
func (e *Exercice) CheckResponse(cksum []byte, respflags map[int]string, respmcq map[int]bool, t *Team) (bool, error) {
if err := e.NewTry(t, cksum); err != nil {
return false, err
} else if flags, err := e.GetFlagKeys(); err != nil {
@ -477,12 +477,12 @@ func (e Exercice) CheckResponse(cksum []byte, respflags map[int]string, respmcq
}
// IsSolved returns the number of time this challenge has been solved and the time of the first solve occurence.
func (e Exercice) IsSolved() (int, time.Time) {
func (e *Exercice) IsSolved() (int, *time.Time) {
var nb *int
var tm *time.Time
if DBQueryRow("SELECT COUNT(id_exercice), MIN(time) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb, &tm); nb == nil || tm == nil {
return 0, time.Time{}
return 0, nil
} else {
return *nb, *tm
return *nb, tm
}
}