admin/sync: defines.txt can also contains informations about hints
This commit is contained in:
parent
332aa90931
commit
ca41e4edee
2 changed files with 30 additions and 1 deletions
|
@ -6,8 +6,15 @@ import (
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ExerciceHintParams struct {
|
||||||
|
Filename string
|
||||||
|
Cost int64
|
||||||
|
Title string
|
||||||
|
}
|
||||||
|
|
||||||
type ExerciceParams struct {
|
type ExerciceParams struct {
|
||||||
Gain int64
|
Gain int64
|
||||||
|
Hints []ExerciceHintParams `toml:"hint"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, err error) {
|
func parseExerciceParams(i Importer, exPath string) (p ExerciceParams, err error) {
|
||||||
|
|
|
@ -15,6 +15,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
||||||
|
params, err := parseExerciceParams(i, exercice.Path)
|
||||||
|
if err != nil {
|
||||||
|
errs = append(errs, fmt.Sprintf("%q: defines.txt: %s", path.Base(exercice.Path), err))
|
||||||
|
}
|
||||||
|
|
||||||
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")) {
|
} else if ! i.exists(path.Join(exercice.Path, "hints")) {
|
||||||
|
@ -23,6 +28,22 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
||||||
errs = append(errs, err.Error())
|
errs = append(errs, err.Error())
|
||||||
} else {
|
} else {
|
||||||
for n, hfile := range hints {
|
for n, hfile := range hints {
|
||||||
|
// Find corresponding parameters
|
||||||
|
var 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract hint content
|
||||||
var hint_cnt string
|
var hint_cnt string
|
||||||
|
|
||||||
if fi, err := i.stat(path.Join(exercice.Path, "hints", hfile)); err != nil {
|
if fi, err := i.stat(path.Join(exercice.Path, "hints", hfile)); err != nil {
|
||||||
|
@ -59,7 +80,8 @@ func SyncExerciceHints(i Importer, exercice fic.Exercice) (errs []string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := exercice.AddHint(fmt.Sprintf("Astuce #%d", n + 1), hint_cnt, 1); err != nil {
|
// Import hint
|
||||||
|
if _, err := exercice.AddHint(hint_title, hint_cnt, 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: unable to add hint %q: %s", path.Base(exercice.Path), hfile, err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue