sync: Perfers content from challenge.txt to import hints

This commit is contained in:
nemunaire 2018-08-17 21:13:13 +02:00 committed by Pierre-Olivier Mercier
parent baf12f87a3
commit be7a159815

View file

@ -24,35 +24,25 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
if _, err := exercice.WipeHints(); err != nil { if _, err := exercice.WipeHints(); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
} else if ! i.exists(path.Join(exercice.Path, "hints")) {
return
} else if hints, err := i.listDir(path.Join(exercice.Path, "hints")); err != nil {
errs = append(errs, err.Error())
} else { } else {
for n, hfile := range hints { for n, hint := range params.Hints {
// Find corresponding parameters if hint.Title != "" {
var hint_title = fmt.Sprintf("Astuce #%d", n + 1) hint.Title = fmt.Sprintf("Astuce #%d", n+1)
var hint_cost = exercice.Gain / 4
for _, hp := range params.Hints {
if hp.Filename == hfile {
if hp.Cost > 0 {
hint_cost = hp.Cost
}
if hp.Title != "" {
hint_title = hp.Title
}
break
} }
if hint.Cost <= 0 {
hint.Cost = exercice.Gain / 4
} }
// Extract hint content if hint.Filename != "" {
var hint_cnt string if hint.Content != "" {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): content and filename can't be filled at the same time", path.Base(exercice.Path), hint.Title, n+1))
if fi, err := i.stat(path.Join(exercice.Path, "hints", hfile)); err != nil { continue
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err)) } else if !i.exists(path.Join(exercice.Path, "hints", hint.Filename)) {
} else if ! fi.IsDir() && fi.Size() > 512 { errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): %s: File not found", path.Base(exercice.Path), hint.Title, n+1, hint.Filename))
// Handle big files as downloadable content continue
if res, err := i.importFile(path.Join(exercice.Path, "hints", hfile), func(filePath string, origin string) (interface{}, error) { } else {
// Handle files as downloadable content
if res, err := i.importFile(path.Join(exercice.Path, "hints", hint.Filename), func(filePath string, origin string) (interface{}, error) {
// Calculate hash // Calculate hash
hash512 := crypto.BLAKE2b_512.New() hash512 := crypto.BLAKE2b_512.New()
if fd, err := os.Open(filePath); err != nil { if fd, err := os.Open(filePath); err != nil {
@ -67,24 +57,29 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
} }
result512 := hash512.Sum(nil) result512 := hash512.Sum(nil)
// Special format for downloadable hints: $FILES + hexhash + path from FILES/
return "$FILES" + hex.EncodeToString(result512) + strings.TrimPrefix(filePath, fic.FilesDir), nil return "$FILES" + hex.EncodeToString(result512) + strings.TrimPrefix(filePath, fic.FilesDir), nil
}); err != nil { }); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: %s", path.Base(exercice.Path), hfile, err)) errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: %s", path.Base(exercice.Path), hint.Filename, err))
continue continue
} else if s, ok := res.(string); !ok { } else if s, ok := res.(string); !ok {
errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hfile)) errs = append(errs, fmt.Sprintf("%q: unable to import hint file %q: invalid string returned as filename", path.Base(exercice.Path), hint.Filename))
continue continue
} else { } else {
hint_cnt = s hint.Content = s
} }
} else if hint_cnt, err = getFileContent(i, path.Join(exercice.Path, "hints", hfile)); err != nil { }
errs = append(errs, fmt.Sprintf("%q: unable to read hint file %q: %s", path.Base(exercice.Path), hfile, err)) }
// At this point, filename should have been replaced by Content if filled, so let's ensure that
if hint.Content == "" {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): content and filename can't be empty at the same time", path.Base(exercice.Path), hint.Title, n+1))
continue continue
} }
// Import hint // Import hint
if _, err := exercice.AddHint(hint_title, hint_cnt, hint_cost); err != nil { if _, err := exercice.AddHint(hint.Title, hint.Content, hint.Cost); err != nil {
errs = append(errs, fmt.Sprintf("%q: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err)) errs = append(errs, fmt.Sprintf("%q: challenge.txt: hint %s (%d): %s", path.Base(exercice.Path), hint.Title, n+1, err))
} }
} }
} }