implement choices_cost

This commit is contained in:
nemunaire 2018-12-02 23:18:32 +01:00
parent f9abdd23c6
commit 476f0f553c
18 changed files with 180 additions and 24 deletions

View file

@ -24,11 +24,13 @@ type Flag struct {
ValidatorRegexp *string `json:"validator_regexp"`
// Checksum is the expected hashed flag
Checksum []byte `json:"value"`
// ChoicesCost is the number of points lost to display choices.
ChoicesCost int64 `json:"choices_cost"`
}
// GetFlags returns a list of flags comming with the challenge.
func (e Exercice) GetFlags() ([]Flag, error) {
if rows, err := DBQuery("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
if rows, err := DBQuery("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
return nil, err
} else {
defer rows.Close()
@ -38,7 +40,7 @@ func (e Exercice) GetFlags() ([]Flag, error) {
var k Flag
k.IdExercice = e.Id
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum); err != nil {
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum, &k.ChoicesCost); err != nil {
return nil, err
}
@ -52,9 +54,15 @@ func (e Exercice) GetFlags() ([]Flag, error) {
}
}
// GetFlag returns a list of flags comming with the challenge.
func GetFlag(id int64) (k Flag, err error) {
err = DBQueryRow("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost FROM exercice_flags WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum, &k.ChoicesCost)
return
}
// GetFlagByLabel returns a flag matching the given label.
func (e Exercice) GetFlagByLabel(label string) (k Flag, err error) {
err = DBQueryRow("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum FROM exercice_flags WHERE type LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum)
err = DBQueryRow("SELECT id_flag, id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost FROM exercice_flags WHERE type LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.IgnoreCase, &k.ValidatorRegexp, &k.Checksum, &k.ChoicesCost)
return
}
@ -65,7 +73,7 @@ func getHashedFlag(raw_value []byte) [blake2b.Size]byte {
}
// AddRawFlag creates and fills a new struct Flag, from a non-hashed flag, and registers it into the database.
func (e Exercice) AddRawFlag(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte) (Flag, error) {
func (e Exercice) AddRawFlag(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (Flag, error) {
if ignorecase {
raw_value = bytes.ToLower(raw_value)
}
@ -82,11 +90,11 @@ func (e Exercice) AddRawFlag(name string, help string, ignorecase bool, validato
}
hash := getHashedFlag(raw_value)
return e.AddFlag(name, help, ignorecase, validator_regexp, hash[:])
return e.AddFlag(name, help, ignorecase, validator_regexp, hash[:], choicescost)
}
// AddFlag creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (e Exercice) AddFlag(name string, help string, ignorecase bool, validator_regexp *string, checksum []byte) (Flag, error) {
func (e Exercice) AddFlag(name string, help string, ignorecase bool, validator_regexp *string, checksum []byte, choicescost int64) (Flag, error) {
// Check the regexp compile
if validator_regexp != nil {
if _, err := regexp.Compile(*validator_regexp); err != nil {
@ -94,12 +102,12 @@ func (e Exercice) AddFlag(name string, help string, ignorecase bool, validator_r
}
}
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum) VALUES (?, ?, ?, ?, ?, ?)", e.Id, name, help, ignorecase, validator_regexp, checksum); err != nil {
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost); err != nil {
return Flag{}, err
} else if kid, err := res.LastInsertId(); err != nil {
return Flag{}, err
} else {
return Flag{kid, e.Id, name, help, ignorecase, validator_regexp, checksum}, nil
return Flag{kid, e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost}, nil
}
}
@ -111,7 +119,7 @@ func (k Flag) Update() (int64, error) {
}
}
if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, type = ?, help = ?, ignorecase = ?, validator_regexp = ?, cksum = ? WHERE id_flag = ?", k.IdExercice, k.Label, k.Help, k.IgnoreCase, k.ValidatorRegexp, k.Checksum, k.Id); err != nil {
if res, err := DBExec("UPDATE exercice_flags SET id_exercice = ?, type = ?, help = ?, ignorecase = ?, validator_regexp = ?, cksum = ?, choices_cost = ? WHERE id_flag = ?", k.IdExercice, k.Label, k.Help, k.IgnoreCase, k.ValidatorRegexp, k.Checksum, k.ChoicesCost, k.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
@ -143,6 +151,8 @@ func (e Exercice) WipeFlags() (int64, error) {
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 = ?)", e.Id); err != nil {
return 0, err
} 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
} 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
} else if res, err := DBExec("DELETE FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
@ -173,7 +183,7 @@ func (k Flag) GetDepends() ([]Flag, error) {
if err := rows.Scan(&d); err != nil {
return nil, err
}
deps = append(deps, Flag{d, k.IdExercice, "", "", false, nil, []byte{}})
deps = append(deps, Flag{d, k.IdExercice, "", "", false, nil, []byte{}, 0})
}
if err := rows.Err(); err != nil {
return nil, err