sync: implement hint dependency on flags

This commit is contained in:
nemunaire 2019-11-25 16:18:59 +01:00
parent 6ad11e49d5
commit f3a34c00db
5 changed files with 176 additions and 117 deletions

View file

@ -17,7 +17,13 @@ import (
"srs.epita.fr/fic-server/libfic"
)
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, errs []string) {
type importHint struct {
Line int
Hint fic.EHint
FlagsDeps []int64
}
func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []importHint, errs []string) {
params, _, err := parseExerciceParams(i, exercice.Path)
if err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", path.Base(exercice.Path), err))
@ -80,31 +86,52 @@ func buildExerciceHints(i Importer, exercice fic.Exercice) (hints []fic.EHint, e
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): error during markdown formating: %s", path.Base(exercice.Path), hint.Title, n+1, err))
}
hints = append(hints, h)
newHint := importHint{
Line: n + 1,
Hint: h,
}
// Read dependency to flag
for _, nf := range hint.NeedFlag {
newHint.FlagsDeps = append(newHint.FlagsDeps, nf.Id)
}
hints = append(hints, newHint)
}
return
}
// CheckExerciceHints checks if all hints are corrects..
func CheckExerciceHints(i Importer, exercice fic.Exercice) ([]fic.EHint, []string) {
func CheckExerciceHints(i Importer, exercice fic.Exercice) ([]importHint, []string) {
return buildExerciceHints(i, exercice)
}
// SyncExerciceHints reads the content of hints/ directories and import it as EHint for the given challenge.
func SyncExerciceHints(i Importer, exercice fic.Exercice) (hintsBindings map[int]fic.EHint, errs []string) {
func SyncExerciceHints(i Importer, exercice fic.Exercice, flagsBindings map[int64]fic.Flag) (hintsBindings map[int]fic.EHint, errs []string) {
if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error())
} else {
hints, berrs := buildExerciceHints(i, exercice)
errs = append(errs, berrs...)
for n, hint := range hints {
hintsBindings = map[int]fic.EHint{}
for _, hint := range hints {
// Import hint
if h, err := exercice.AddHint(hint.Title, hint.Content, hint.Cost); err != nil {
errs = append(errs, fmt.Sprintf("%q: hint #%d %s: %s", path.Base(exercice.Path), n+1, hint.Title, err))
if h, err := exercice.AddHint(hint.Hint.Title, hint.Hint.Content, hint.Hint.Cost); err != nil {
errs = append(errs, fmt.Sprintf("%q: hint #%d %s: %s", path.Base(exercice.Path), hint.Line, hint.Hint.Title, err))
} else {
hintsBindings[n+1] = h
hintsBindings[hint.Line] = h
// Handle hints dependencies on flags
for _, nf := range hint.FlagsDeps {
if f, ok := flagsBindings[nf]; ok {
h.AddDepend(f)
} else {
errs = append(errs, fmt.Sprintf("%q: error hint #%d dependency to flag #%d: Unexistant flag", path.Base(exercice.Path), hint.Line, nf))
}
}
}
}
}