admin: Can extract exercices as ZQDS session.yml

This commit is contained in:
nemunaire 2022-05-01 22:05:26 +02:00
parent 15afbb8b87
commit 457cd307dd
2 changed files with 78 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"path"
"strconv"
"strings"
@ -18,6 +19,13 @@ func init() {
router.GET("/api/themes", apiHandler(listThemes))
router.POST("/api/themes", apiHandler(createTheme))
router.GET("/api/themes.json", apiHandler(exportThemes))
router.GET("/api/session-forensic.yaml", apiHandler(func(_ httprouter.Params, _ []byte) (interface{}, error) {
if s, err := settings.ReadSettings(path.Join(settings.SettingsDir, settings.SettingsFile)); err != nil {
return nil, err
} else {
return fic.GenZQDSSessionFile(s)
}
}))
router.GET("/api/files-bindings", apiHandler(bindingFiles))
router.GET("/api/themes/:thid", apiHandler(themeHandler(showTheme)))

70
libfic/zqsd.go Normal file
View File

@ -0,0 +1,70 @@
package fic
import (
"time"
"srs.epita.fr/fic-server/settings"
)
type ChallengeZQDS struct {
Name string `json:"name"`
Type string `json:"type,omitempty"`
Category string `json:"category"`
Points int64 `json:"points"`
State string `json:"state"`
FirstBloodBonus []float64 `json:"first_blood_bonus,omitempty"`
Description string `json:"description"`
Flags []string `json:"flags,omitempty"`
}
type SessionZQDS struct {
Name string `json:"name"`
Description string `json:"description"`
Scenario string `json:"scenario"`
YourMission string `json:"your_mission"`
Rules string `json:"rules"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Challenges []*ChallengeZQDS `json:"challenges"`
}
func GenZQDSSessionFile(s *settings.FICSettings) (*SessionZQDS, error) {
themes, err := GetThemes()
if err != nil {
return nil, err
}
var challenges []*ChallengeZQDS
for _, th := range themes {
exos, err := th.GetExercices()
if err != nil {
return nil, err
}
for _, ex := range exos {
challenges = append(challenges, &ChallengeZQDS{
Name: ex.Title,
Type: "first_blood",
Category: th.Name,
Points: ex.Gain,
State: "visible",
FirstBloodBonus: []float64{
float64(ex.Gain) * (1 + s.FirstBlood),
},
Description: ex.Overview,
})
}
}
return &SessionZQDS{
Name: "Challenge Forensic",
Description: "",
Scenario: "",
YourMission: "",
Rules: "",
Start: s.Start,
End: s.End,
Challenges: challenges,
}, nil
}