server/libfic/flag.go

89 lines
3.4 KiB
Go
Raw Normal View History

2018-09-24 08:00:17 +00:00
package fic
import ()
2018-09-24 08:00:17 +00:00
type Flag interface {
GetId() int
RecoverId() (Flag, error)
2021-11-22 14:35:07 +00:00
Create(e *Exercice) (Flag, error)
Update() (int64, error)
Delete() (int64, error)
AddDepend(d Flag) error
GetDepends() ([]Flag, error)
GetOrder() int8
Check(val interface{}) int
2022-05-31 20:03:51 +00:00
IsOptionnal() bool
FoundBy(t *Team) error
2018-09-24 08:00:17 +00:00
}
// GetFlag returns a list of flags comming with the challenge.
2021-11-22 14:35:07 +00:00
func (e *Exercice) GetFlags() ([]Flag, error) {
var flags []Flag
if ks, err := e.GetFlagKeys(); err != nil {
2018-09-24 08:00:17 +00:00
return nil, err
} else {
for _, k := range ks {
2018-09-24 08:00:17 +00:00
flags = append(flags, k)
}
}
2022-01-21 12:06:37 +00:00
if ls, err := e.GetFlagLabels(); err != nil {
return nil, err
} else {
for _, l := range ls {
flags = append(flags, l)
}
}
if ms, err := e.GetMCQ(); err != nil {
return nil, err
2018-09-24 08:00:17 +00:00
} else {
for _, m := range ms {
flags = append(flags, m)
2018-11-16 19:46:19 +00:00
}
}
return flags, nil
2018-09-24 08:00:17 +00:00
}
// AddFlag add the given flag and eventually its entries (from MCQ).
2021-11-22 14:35:07 +00:00
func (e *Exercice) AddFlag(flag Flag) (Flag, error) {
return flag.Create(e)
}
2018-09-24 08:00:17 +00:00
// WipeFlags deletes flags coming with the challenge.
2021-11-22 14:35:07 +00:00
func (e *Exercice) WipeFlags() (int64, error) {
2020-01-20 16:28:37 +00:00
if _, err := DBExec("DELETE FROM exercice_files_okey_deps WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_files_omcq_deps WHERE id_mcq IN (SELECT id_mcq FROM exercice_mcq WHERE id_exercice = ?)", e.Id); err != nil {
2018-09-24 08:00:17 +00:00
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_flags_deps WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?) OR id_flag_dep IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id, e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_flags_omcq_deps WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
2022-01-21 12:06:37 +00:00
} else if _, err := DBExec("DELETE FROM exercice_flag_labels_omcq_deps WHERE id_label IN (SELECT id_label FROM exercice_flag_labels WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_flag_labels_deps WHERE id_label IN (SELECT id_label FROM exercice_flag_labels WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
2020-01-23 11:39:53 +00:00
} else if _, err := DBExec("DELETE FROM exercice_hints_okey_deps WHERE id_flag_dep IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_hints_omcq_deps WHERE id_mcq_dep IN (SELECT id_mcq FROM exercice_mcq WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
} else if _, err := DBExec("DELETE FROM exercice_mcq_okey_deps WHERE id_flag_dep IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
2018-12-02 18:21:07 +00:00
return 0, err
2018-12-02 22:18:32 +00:00
} else if _, err := DBExec("DELETE FROM team_wchoices WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
2018-11-21 03:10:22 +00:00
} else if _, err := DBExec("DELETE FROM flag_choices WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {
return 0, err
2022-01-21 12:06:37 +00:00
} else if _, err := DBExec("DELETE FROM exercice_flag_labels WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
2018-09-24 08:00:17 +00:00
} else if res, err := DBExec("DELETE FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}