Implement label only flag
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
b98e23d060
commit
01b05aaed0
9 changed files with 311 additions and 11 deletions
32
libfic/db.go
32
libfic/db.go
|
@ -198,6 +198,18 @@ CREATE TABLE IF NOT EXISTS exercice_flags(
|
|||
choices_cost INTEGER NOT NULL,
|
||||
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS exercice_flag_labels(
|
||||
id_label INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
id_exercice INTEGER NOT NULL,
|
||||
ordre TINYINT NOT NULL,
|
||||
label VARCHAR(255) NOT NULL,
|
||||
variant VARCHAR(255) NOT NULL,
|
||||
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -218,6 +230,16 @@ CREATE TABLE IF NOT EXISTS exercice_flags_deps(
|
|||
FOREIGN KEY(id_flag) REFERENCES exercice_flags(id_flag),
|
||||
FOREIGN KEY(id_flag_dep) REFERENCES exercice_flags(id_flag)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS exercice_flag_labels_deps(
|
||||
id_label INTEGER NOT NULL,
|
||||
id_flag_dep INTEGER NOT NULL,
|
||||
FOREIGN KEY(id_label) REFERENCES exercice_flag_labels(id_label),
|
||||
FOREIGN KEY(id_flag_dep) REFERENCES exercice_flags(id_flag)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -290,6 +312,16 @@ CREATE TABLE IF NOT EXISTS exercice_flags_omcq_deps(
|
|||
FOREIGN KEY(id_flag) REFERENCES exercice_flags(id_flag),
|
||||
FOREIGN KEY(id_mcq_dep) REFERENCES exercice_mcq(id_mcq)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS exercice_flag_labels_omcq_deps(
|
||||
id_label INTEGER NOT NULL,
|
||||
id_mcq_dep INTEGER NOT NULL,
|
||||
FOREIGN KEY(id_label) REFERENCES exercice_flag_labels(id_label),
|
||||
FOREIGN KEY(id_mcq_dep) REFERENCES exercice_mcq(id_mcq)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -27,6 +27,14 @@ func (e *Exercice) GetFlags() ([]Flag, error) {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
} else {
|
||||
|
@ -53,6 +61,10 @@ func (e *Exercice) WipeFlags() (int64, error) {
|
|||
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
|
||||
} 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
|
||||
} 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 {
|
||||
|
@ -63,6 +75,8 @@ func (e *Exercice) WipeFlags() (int64, error) {
|
|||
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 _, err := DBExec("DELETE FROM exercice_flag_labels 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 {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
|
|
187
libfic/flag_label.go
Normal file
187
libfic/flag_label.go
Normal file
|
@ -0,0 +1,187 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// FlagLabel represents a flag's challenge, stored as hash.
|
||||
type FlagLabel struct {
|
||||
Id int `json:"id"`
|
||||
// IdExercice is the identifier of the underlying challenge
|
||||
IdExercice int64 `json:"idExercice"`
|
||||
// Order is used to sort the flag between them
|
||||
Order int8 `json:"order"`
|
||||
// Label is the title of the flag as displayed to players
|
||||
Label string `json:"label"`
|
||||
// Variant stores the label color.
|
||||
Variant string `json:"variant"`
|
||||
}
|
||||
|
||||
// GetFlagLabels returns a list of key's flags comming with the challenge.
|
||||
func (e *Exercice) GetFlagLabels() ([]*FlagLabel, error) {
|
||||
if rows, err := DBQuery("SELECT id_label, id_exercice, ordre, label, variant FROM exercice_flag_labels WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
flags := []*FlagLabel{}
|
||||
for rows.Next() {
|
||||
k := &FlagLabel{}
|
||||
k.IdExercice = e.Id
|
||||
|
||||
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Variant); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
flags = append(flags, k)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return flags, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetFlagLabel returns a list of flags comming with the challenge.
|
||||
func GetFlagLabel(id int) (k *FlagLabel, err error) {
|
||||
k = &FlagLabel{}
|
||||
err = DBQueryRow("SELECT id_label, id_exercice, ordre, label, variant FROM exercice_flag_labels WHERE id_flag = ?", id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Variant)
|
||||
return
|
||||
}
|
||||
|
||||
// GetFlagLabelByLabel returns a flag matching the given label.
|
||||
func (e *Exercice) GetFlagLabelByLabel(label string) (k *FlagLabel, err error) {
|
||||
k = &FlagLabel{}
|
||||
err = DBQueryRow("SELECT id_label, id_exercice, ordre, label, variant FROM exercice_flags WHERE type LIKE ? AND id_exercice = ?", label, e.Id).Scan(&k.Id, &k.IdExercice, &k.Order, &k.Label, &k.Variant)
|
||||
return
|
||||
}
|
||||
|
||||
// GetId returns the Flag identifier.
|
||||
func (k *FlagLabel) GetId() int {
|
||||
return k.Id
|
||||
}
|
||||
|
||||
// RecoverId returns the Flag identifier as register in DB.
|
||||
func (k *FlagLabel) RecoverId() (Flag, error) {
|
||||
if err := DBQueryRow("SELECT id_label FROM exercice_flag_labels WHERE label LIKE ? AND id_exercice = ?", k.Label, k.IdExercice).Scan(&k.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return k, err
|
||||
}
|
||||
}
|
||||
|
||||
// AddFlagLabel creates and fills a new struct Flag and registers it into the database.
|
||||
func (k *FlagLabel) Create(e *Exercice) (Flag, error) {
|
||||
if res, err := DBExec("INSERT INTO exercice_flag_labels (id_exercice, ordre, label, variant) VALUES (?, ?, ?, ?)", e.Id, k.Order, k.Label, k.Variant); err != nil {
|
||||
return k, err
|
||||
} else if kid, err := res.LastInsertId(); err != nil {
|
||||
return k, err
|
||||
} else {
|
||||
k.Id = int(kid)
|
||||
k.IdExercice = e.Id
|
||||
return k, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (k *FlagLabel) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercice_flag_labels SET id_exercice = ?, ordre = ?, label = ?, variant = ? WHERE id_label = ?", k.IdExercice, k.Order, k.Label, k.Variant, k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the flag from the database.
|
||||
func (k *FlagLabel) Delete() (int64, error) {
|
||||
if _, err := DBExec("DELETE FROM exercice_flag_labels_deps WHERE id_label = ?", k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if _, err := DBExec("DELETE FROM exercice_flag_labels_omcq_deps WHERE id_label = ?", k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if res, err := DBExec("DELETE FROM exercice_flag_labels WHERE id_label = ?", k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (k *FlagLabel) GetOrder() int8 {
|
||||
return k.Order
|
||||
}
|
||||
|
||||
// AddDepend insert a new dependency to a given flag.
|
||||
func (k *FlagLabel) AddDepend(j Flag) (err error) {
|
||||
if d, ok := j.(*FlagKey); ok {
|
||||
_, err = DBExec("INSERT INTO exercice_flag_labels_deps (id_label, id_flag_dep) VALUES (?, ?)", k.Id, d.Id)
|
||||
} else if d, ok := j.(*MCQ); ok {
|
||||
_, err = DBExec("INSERT INTO exercice_flag_labels_omcq_deps (id_label, id_mcq_dep) VALUES (?, ?)", k.Id, d.Id)
|
||||
} else {
|
||||
err = fmt.Errorf("dependancy type for key (%T) not implemented for this flag", j)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetDepends retrieve the flag's dependency list.
|
||||
func (k *FlagLabel) GetDepends() ([]Flag, error) {
|
||||
var deps []Flag
|
||||
|
||||
if rows, err := DBQuery("SELECT id_flag_dep FROM exercice_flag_labels_deps WHERE id_label = ?", k.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var d int
|
||||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, &FlagKey{Id: d, IdExercice: k.IdExercice})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if rows, err := DBQuery("SELECT id_mcq_dep FROM exercice_flag_labels_omcq_deps WHERE id_label = ?", k.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var d int
|
||||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, &MCQ{Id: d, IdExercice: k.IdExercice})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return deps, nil
|
||||
}
|
||||
|
||||
// Check if the given val is the expected one for this flag.
|
||||
func (k *FlagLabel) Check(v interface{}) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// FoundBy registers in the database that the given Team solved the flag.
|
||||
func (k *FlagLabel) FoundBy(t *Team) {
|
||||
}
|
||||
|
||||
// GetExercice returns the parent Exercice where this flag can be found.
|
||||
func (k *FlagLabel) GetExercice() (*Exercice, error) {
|
||||
var eid int64
|
||||
if err := DBQueryRow("SELECT id_exercice FROM exercice_flag_labels WHERE id_label = ?", k.Id).Scan(&eid); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return GetExercice(eid)
|
||||
}
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
type FlagLabel struct {
|
||||
type FlagMCQLabel struct {
|
||||
Label string
|
||||
IdChoice int
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ func (k FlagKey) IsMCQJustification() bool {
|
|||
}
|
||||
|
||||
// GetMCQJustification returns the structure corresponding to the given flag.
|
||||
func (k FlagKey) GetMCQJustification() (fl FlagLabel, err error) {
|
||||
func (k FlagKey) GetMCQJustification() (fl FlagMCQLabel, err error) {
|
||||
spl := strings.Split(k.Label, "%")
|
||||
if len(spl) >= 3 && len(spl[0]) == 0 {
|
||||
var idChoice int64
|
||||
|
|
|
@ -312,7 +312,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 {
|
||||
if _, ok := f.(*FlagLabel); ok {
|
||||
now := time.Now()
|
||||
return &now
|
||||
} else 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 {
|
||||
DBQueryRow("SELECT MIN(time) FROM mcq_found WHERE id_team = ? AND id_mcq = ?", t.Id, m.Id).Scan(&tm)
|
||||
|
|
|
@ -31,7 +31,7 @@ type myTeamHint struct {
|
|||
Cost int64 `json:"cost"`
|
||||
}
|
||||
type myTeamFlag struct {
|
||||
Id int `json:"id"`
|
||||
Id int `json:"id,omitempty"`
|
||||
order int8 `json:"order"`
|
||||
Label string `json:"label"`
|
||||
Type string `json:"type,omitempty"`
|
||||
|
@ -50,6 +50,7 @@ type myTeamFlag struct {
|
|||
Justify bool `json:"justify,omitempty"`
|
||||
Choices map[string]interface{} `json:"choices,omitempty"`
|
||||
ChoicesCost int64 `json:"choices_cost,omitempty"`
|
||||
Variant string `json:"variant,omitempty"`
|
||||
Min *float64 `json:"min,omitempty"`
|
||||
Max *float64 `json:"max,omitempty"`
|
||||
Step *float64 `json:"step,omitempty"`
|
||||
|
@ -142,10 +143,8 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
|
|||
} else {
|
||||
exercice.Tries, stime = t.CountTries(e)
|
||||
exercice.SolvedTime = stime
|
||||
if exercice.Tries > 0 {
|
||||
if DisplayMCQBadCount {
|
||||
exercice.SolveDist = t.LastTryDist(e)
|
||||
}
|
||||
if DisplayMCQBadCount && exercice.Tries > 0 {
|
||||
exercice.SolveDist = t.LastTryDist(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -194,6 +193,23 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
|
|||
|
||||
justifiedMCQ := map[int]myTeamFlag{}
|
||||
|
||||
if labels, err := e.GetFlagLabels(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
for _, l := range labels {
|
||||
if !DisplayAllFlags && t != nil && !t.CanSeeFlag(l) {
|
||||
// Dependancy missing, skip the flag for now
|
||||
continue
|
||||
}
|
||||
|
||||
exercice.Flags = append(exercice.Flags, myTeamFlag{
|
||||
order: l.Order,
|
||||
Label: l.Label,
|
||||
Variant: l.Variant,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if flags, err := e.GetFlagKeys(); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
@ -257,7 +273,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
|
|||
|
||||
flag.Multiline = k.Multiline
|
||||
|
||||
var fl FlagLabel
|
||||
var fl FlagMCQLabel
|
||||
if fl, err = k.GetMCQJustification(); err == nil {
|
||||
k.Label = fl.Label
|
||||
}
|
||||
|
|
Reference in a new issue