sync: implement hint dependency on flags
This commit is contained in:
parent
6ad11e49d5
commit
f3a34c00db
5 changed files with 176 additions and 117 deletions
|
@ -65,13 +65,14 @@ func init() {
|
|||
})))
|
||||
router.POST("/api/sync/exercices/:eid/hints", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
_, errs := sync.SyncExerciceHints(sync.GlobalImporter, exercice)
|
||||
_, errs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
|
||||
return errs, nil
|
||||
})))
|
||||
router.POST("/api/sync/exercices/:eid/flags", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
_, errs := sync.SyncExerciceFlags(sync.GlobalImporter, exercice)
|
||||
return errs, nil
|
||||
_, herrs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
|
||||
return append(errs, herrs...), nil
|
||||
})))
|
||||
|
||||
router.POST("/api/sync/exercices/:eid/fixurlid", apiHandler(exerciceHandler(
|
||||
|
|
|
@ -70,13 +70,14 @@ func init() {
|
|||
})))
|
||||
router.POST("/api/sync/themes/:thid/exercices/:eid/hints", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
_, errs := sync.SyncExerciceHints(sync.GlobalImporter, exercice)
|
||||
_, errs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
|
||||
return errs, nil
|
||||
})))
|
||||
router.POST("/api/sync/themes/:thid/exercices/:eid/keys", apiHandler(exerciceHandler(
|
||||
func(exercice fic.Exercice, _ []byte) (interface{}, error) {
|
||||
_, errs := sync.SyncExerciceFlags(sync.GlobalImporter, exercice)
|
||||
return errs, nil
|
||||
_, herrs := sync.SyncExerciceHints(sync.GlobalImporter, exercice, sync.ExerciceFlagsMap(sync.GlobalImporter, exercice))
|
||||
return append(errs, herrs...), nil
|
||||
})))
|
||||
|
||||
router.POST("/api/sync/themes/:thid/fixurlid", apiHandler(themeHandler(
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -175,26 +175,7 @@ type importFlag struct {
|
|||
}
|
||||
|
||||
// buildExerciceFlags read challenge.txt and extract all flags.
|
||||
func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]importFlag, flagids []int64, errs []string) {
|
||||
params, gerrs := getExerciceParams(i, exercice)
|
||||
if len(gerrs) > 0 {
|
||||
return flags, flagids, gerrs
|
||||
}
|
||||
|
||||
flags = map[int64]importFlag{}
|
||||
|
||||
for nline, flag := range params.Flags {
|
||||
if flag.Id == 0 {
|
||||
// TODO: should be more smart than that. Perhaps search to increment if possible.
|
||||
flag.Id = rand.Int63()
|
||||
}
|
||||
|
||||
// Ensure flag ID is unique
|
||||
for _, ok := flags[flag.Id]; ok; _, ok = flags[flag.Id] {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: identifier already used (%d), using a random one.", path.Base(exercice.Path), nline+1, flag.Id))
|
||||
flag.Id = rand.Int63()
|
||||
}
|
||||
|
||||
func buildExerciceFlag(i Importer, exercice fic.Exercice, flag ExerciceFlag, nline int) (ret []importFlag, errs []string) {
|
||||
switch strings.ToLower(flag.Type) {
|
||||
case "":
|
||||
flag.Type = "key"
|
||||
|
@ -208,7 +189,7 @@ func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]impo
|
|||
flag.Type = "mcq"
|
||||
default:
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: invalid type of flag: should be 'key', 'mcq', 'ucq' or 'vector'.", path.Base(exercice.Path), nline+1))
|
||||
continue
|
||||
return
|
||||
}
|
||||
|
||||
if flag.Type == "key" || flag.Type == "ucq" || flag.Type == "vector" {
|
||||
|
@ -217,11 +198,11 @@ func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]impo
|
|||
errs = append(errs, berrs...)
|
||||
}
|
||||
if addedFlag != nil {
|
||||
flags[flag.Id] = importFlag{
|
||||
ret = append(ret, importFlag{
|
||||
Line: nline + 1,
|
||||
Flag: *addedFlag,
|
||||
Choices: choices,
|
||||
}
|
||||
})
|
||||
}
|
||||
} else if flag.Type == "mcq" {
|
||||
addedFlag := fic.MCQ{
|
||||
|
@ -274,36 +255,67 @@ func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]impo
|
|||
errs = append(errs, berrs...)
|
||||
}
|
||||
if addedFlag != nil {
|
||||
flags[flag.Id] = importFlag{
|
||||
Line: nline,
|
||||
ret = append(ret, importFlag{
|
||||
Line: nline + 1,
|
||||
Flag: *addedFlag,
|
||||
Choices: choices,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flags[flag.Id] = importFlag{
|
||||
ret = append(ret, importFlag{
|
||||
Line: nline + 1,
|
||||
Flag: addedFlag,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
flagids = append(flagids, flag.Id)
|
||||
// buildExerciceFlags read challenge.txt and extract all flags.
|
||||
func buildExerciceFlags(i Importer, exercice fic.Exercice) (flags map[int64]importFlag, flagids []int64, errs []string) {
|
||||
params, gerrs := getExerciceParams(i, exercice)
|
||||
if len(gerrs) > 0 {
|
||||
return flags, flagids, gerrs
|
||||
}
|
||||
|
||||
flags = map[int64]importFlag{}
|
||||
|
||||
for nline, flag := range params.Flags {
|
||||
if flag.Id == 0 {
|
||||
// TODO: should be more smart than that. Perhaps search to increment if possible.
|
||||
flag.Id = rand.Int63()
|
||||
}
|
||||
|
||||
// Ensure flag ID is unique
|
||||
for _, ok := flags[flag.Id]; ok; _, ok = flags[flag.Id] {
|
||||
errs = append(errs, fmt.Sprintf("%q: flag #%d: identifier already used (%d), using a random one.", path.Base(exercice.Path), nline+1, flag.Id))
|
||||
flag.Id = rand.Int63()
|
||||
}
|
||||
|
||||
newFlags, ferrs := buildExerciceFlag(i, exercice, flag, nline)
|
||||
if len(ferrs) > 0 {
|
||||
errs = append(errs, ferrs...)
|
||||
}
|
||||
if len(newFlags) > 0 {
|
||||
for _, newFlag := range newFlags {
|
||||
fId := flag.Id
|
||||
for _, ok := flags[fId]; ok; _, ok = flags[fId] {
|
||||
fId = rand.Int63()
|
||||
}
|
||||
|
||||
// Read dependency to flag
|
||||
for _, nf := range flag.NeedFlag {
|
||||
if v, ok := flags[flag.Id]; ok {
|
||||
v.FlagsDeps = append(v.FlagsDeps, nf.Id)
|
||||
flags[flag.Id] = v
|
||||
}
|
||||
newFlag.FlagsDeps = append(newFlag.FlagsDeps, nf.Id)
|
||||
}
|
||||
|
||||
// Read dependency to file
|
||||
for _, lf := range flag.LockedFile {
|
||||
if v, ok := flags[flag.Id]; ok {
|
||||
v.FilesDeps = append(v.FilesDeps, lf.Filename)
|
||||
flags[flag.Id] = v
|
||||
newFlag.FilesDeps = append(newFlag.FilesDeps, lf.Filename)
|
||||
}
|
||||
|
||||
flags[fId] = newFlag
|
||||
flagids = append(flagids, fId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -346,6 +358,24 @@ func CheckExerciceFlags(i Importer, exercice fic.Exercice, files []fic.EFile) (r
|
|||
return
|
||||
}
|
||||
|
||||
// ExerciceFlagsMap builds the flags bindings between challenge.txt and DB.
|
||||
func ExerciceFlagsMap(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Flag) {
|
||||
flags, flagids, _ := buildExerciceFlags(i, exercice)
|
||||
|
||||
kmap = map[int64]fic.Flag{}
|
||||
|
||||
for _, flagid := range flagids {
|
||||
if flag, ok := flags[flagid]; ok {
|
||||
if addedFlag, err := flag.Flag.RecoverId(); err == nil {
|
||||
kmap[flagid] = addedFlag
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
// SyncExerciceFlags imports all kind of flags for the given challenge.
|
||||
func SyncExerciceFlags(i Importer, exercice fic.Exercice) (kmap map[int64]fic.Flag, errs []string) {
|
||||
if _, err := exercice.WipeFlags(); err != nil {
|
||||
|
|
|
@ -55,11 +55,11 @@ func SyncDeep(i Importer) (errs map[string][]string) {
|
|||
errs[theme.Name] = append(errs[theme.Name], SyncExerciceFiles(i, exercice)...)
|
||||
|
||||
DeepSyncProgress += exerciceStep / 3
|
||||
_, ferrs := SyncExerciceFlags(i, exercice)
|
||||
flagsBindings, ferrs := SyncExerciceFlags(i, exercice)
|
||||
errs[theme.Name] = append(errs[theme.Name], ferrs...)
|
||||
|
||||
DeepSyncProgress += exerciceStep / 3
|
||||
_, herrs := SyncExerciceHints(i, exercice)
|
||||
_, herrs := SyncExerciceHints(i, exercice, flagsBindings)
|
||||
errs[theme.Name] = append(errs[theme.Name], herrs...)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue