From 0e36a850cfadc9318821629b4c41ac0fd47f9561 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Tue, 4 Dec 2018 19:09:58 +0100 Subject: [PATCH] Array flags can be non-ordered --- admin/sync/README.md | 1 + admin/sync/exercice_defines.go | 1 + admin/sync/exercice_keys.go | 14 ++++++++++++-- frontend/static/js/challenge.js | 5 ++++- libfic/team_my.go | 6 ++++-- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/admin/sync/README.md b/admin/sync/README.md index cc598c99..1a342516 100644 --- a/admin/sync/README.md +++ b/admin/sync/README.md @@ -22,6 +22,7 @@ Tous les textes doivent utiliser l'encodage UTF8. * `label = "Intitulé"` : (facultatif, par défaut : `Flag`) intitulé du drapeau ; * `raw = 'MieH2athxuPhai6u'` ou `raw = ['part1', 'part2']` : drapeau exact à trouver ; sous forme de tableau, le participant n'aura pas connaissaance du nombre d'éléments ; * `validator_regexp = "^(?:sudo +)?(.*)$"` : (facultatif) expression rationnelle dont les groupes capturés serviront comme chaîne à valider (notez que `?:` au début d'un groupe ne le capturera pas) ; + * `ordered = false` : (facultatif, par défaut : `false`) ignore l'ordre dans lequels les éléments du tableau sont passés ; * `ignorecase = true` : (facultatif, par défaut : `false`) ignore la case de ce drapeau ; * `help = "Indication"` : (facultatif) chaîne de caractères placée sous le champ du formulaire, idéale pour donner une indication de format ; * `[[flag.unlock_file]]` : bloque l'accès à un fichier tant que le flag n'est pas obtenu : diff --git a/admin/sync/exercice_defines.go b/admin/sync/exercice_defines.go index 006bc5d6..483c30df 100644 --- a/admin/sync/exercice_defines.go +++ b/admin/sync/exercice_defines.go @@ -31,6 +31,7 @@ type ExerciceFlag struct { Label string `toml:",omitempty"` Raw interface{} Separator string `toml:",omitempty"` + Ordered bool `toml:",omitempty"` IgnoreCase bool `toml:",omitempty"` ValidatorRe string `toml:"validator_regexp,omitempty"` Help string `toml:",omitempty"` diff --git a/admin/sync/exercice_keys.go b/admin/sync/exercice_keys.go index 8458489c..a5a3bae4 100644 --- a/admin/sync/exercice_keys.go +++ b/admin/sync/exercice_keys.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "path" + "sort" "strings" "unicode" @@ -70,13 +71,14 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { errs = append(errs, fmt.Sprintf("%q: flag #%d: separator truncated to %q", path.Base(exercice.Path), nline + 1, flag.Separator)) } + var fitems []string for i, v := range f { if g, ok := v.(string); ok { if strings.Index(g, flag.Separator) != -1 { errs = append(errs, fmt.Sprintf("%q: flag #%d: flag items cannot contain %q character as it is used as separator. Change the separator attribute for this flag.", path.Base(exercice.Path), nline + 1, flag.Separator)) continue flags } else { - raw += g + flag.Separator + fitems = append(fitems, g) } } else { errs = append(errs, fmt.Sprintf("%q: flag #%d, item %d has an invalid type: can only be string, is %T.", path.Base(exercice.Path), nline + 1, i, v)) @@ -84,7 +86,15 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) { } } - flag.Label = "`" + flag.Separator + flag.Label + ignord := "t" + if flag.Ordered { + sort.Strings(fitems) + ignord = "f" + } + + raw = strings.Join(fitems, flag.Separator) + flag.Separator + + flag.Label = "`" + flag.Separator + ignord + flag.Label } else if f, ok := flag.Raw.(int64); ok { raw = fmt.Sprintf("%d", f) } else if f, ok := flag.Raw.(string); !ok { diff --git a/frontend/static/js/challenge.js b/frontend/static/js/challenge.js index 7affdcde..766a6e27 100644 --- a/frontend/static/js/challenge.js +++ b/frontend/static/js/challenge.js @@ -294,7 +294,10 @@ angular.module("FICApp", ["ngRoute", "ngSanitize"]) flag.values.splice(i, 1); } - flag.value = flag.values.join(flag.separator) + flag.separator; + if (flag.ignoreorder) + flag.value = flag.values.slice().sort().join(flag.separator) + flag.separator; + else + flag.value = flag.values.join(flag.separator) + flag.separator; if (flag.values.length == 0) flag.values = [""]; diff --git a/libfic/team_my.go b/libfic/team_my.go index 30382c25..eb6c9bff 100644 --- a/libfic/team_my.go +++ b/libfic/team_my.go @@ -40,6 +40,7 @@ type myTeamFlag struct { Label string `json:"label"` Help string `json:"help,omitempty"` Separator string `json:"separator,omitempty"` + IgnoreOrder bool `json:"ignore_order,omitempty"` Solved *time.Time `json:"found,omitempty"` Soluce string `json:"soluce,omitempty"` Choices map[string]string `json:"choices,omitempty"` @@ -195,9 +196,10 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) { flag.Solved = t.HasPartiallySolved(k) } - if k.Label[0] == '`' && len(k.Label) > 2 { - flag.Label = k.Label[2:] + if k.Label[0] == '`' && len(k.Label) > 3 { + flag.Label = k.Label[3:] flag.Separator = string(k.Label[1]) + flag.IgnoreOrder = k.Label[2] == 't' } else { flag.Label = k.Label }