server/libfic/zqsd.go

101 lines
2.8 KiB
Go
Raw Permalink Normal View History

package fic
import (
2023-05-12 12:53:15 +00:00
"fmt"
"srs.epita.fr/fic-server/settings"
)
2024-03-02 23:23:28 +00:00
type WorkZoneRoleZQDS struct {
Name string `json:"name"`
Accesses []interface{} `json:"acceses"`
}
type WorkZoneZQDS struct {
Name string `json:"name"`
Duplicable bool `json:"duplicable"`
Roles []WorkZoneRoleZQDS `json:"roles"`
}
type ChallengeRequirementsZQDS struct {
Challenges []string `json:"challenges"`
}
type ChallengeZQDS struct {
2024-03-02 23:23:28 +00:00
Name string `json:"name"`
Type string `json:"type,omitempty"`
Category string `json:"category"`
Points int64 `json:"points"`
FirstBloodBonus []float64 `json:"first_blood_bonus,omitempty"`
Description string `json:"description"`
Flags []string `json:"flags,omitempty"`
Requirements *ChallengeRequirementsZQDS `json:"requirements,omitempty"`
}
type SessionZQDS struct {
2024-03-02 23:23:28 +00:00
Name string `json:"name"`
BackgroundURL string `json:"background_url,omitempty"`
Description string `json:"description"`
Scenario string `json:"scenario,omitempty"`
YourMission string `json:"your_mission"`
Rules string `json:"rules"`
Duration string `json:"duration"`
WorkZones []WorkZoneZQDS `json:"work_zones,omitempty"`
Challenges []*ChallengeZQDS `json:"challenges"`
}
2022-05-01 20:33:59 +00:00
func GenZQDSSessionFile(c *settings.ChallengeInfo, s *settings.Settings) (*SessionZQDS, error) {
themes, err := GetThemes()
if err != nil {
return nil, err
}
2023-05-12 12:53:15 +00:00
if s.End == nil {
return nil, fmt.Errorf("Please define an end date")
}
var challenges []*ChallengeZQDS
for _, th := range themes {
exos, err := th.GetExercices()
if err != nil {
return nil, err
}
for _, ex := range exos {
2024-03-02 23:23:28 +00:00
var requirements *ChallengeRequirementsZQDS
2022-05-02 10:08:05 +00:00
if ex.Depend != nil {
2024-03-02 23:23:28 +00:00
exdep, err := GetExercice(*ex.Depend)
if err != nil {
return nil, fmt.Errorf("unable to find dependancy: %w", err)
}
requirements = &ChallengeRequirementsZQDS{}
requirements.Challenges = append(requirements.Challenges, exdep.Title)
2022-05-02 10:08:05 +00:00
}
challenges = append(challenges, &ChallengeZQDS{
Name: ex.Title,
Type: "first_blood",
Category: th.Name,
Points: ex.Gain,
FirstBloodBonus: []float64{
float64(ex.Gain) * (1 + s.FirstBlood),
},
2024-03-02 23:23:28 +00:00
Description: ex.Overview,
Requirements: requirements,
})
}
}
return &SessionZQDS{
2022-05-01 20:33:59 +00:00
Name: c.Title,
Description: c.Description,
Rules: c.Rules,
YourMission: c.YourMission,
2024-03-02 23:23:28 +00:00
Duration: fmt.Sprintf("%d:%d:00", c.ExpectedDuration/60, c.ExpectedDuration%60),
Challenges: challenges,
}, nil
}