fic: Sort mixed flags by order before packing them

This commit is contained in:
nemunaire 2021-08-30 20:03:17 +02:00
parent 102a0878ac
commit ef899ee99b

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"path" "path"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -27,7 +28,8 @@ type myTeamHint struct {
Cost int64 `json:"cost"` Cost int64 `json:"cost"`
} }
type myTeamFlag struct { type myTeamFlag struct {
Order int8 `json:"order"` Id int `json:"id"`
order int8 `json:"order"`
Label string `json:"label"` Label string `json:"label"`
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Placeholder string `json:"placeholder,omitempty"` Placeholder string `json:"placeholder,omitempty"`
@ -51,22 +53,22 @@ type myTeamMCQJustifiedChoice struct {
Justification myTeamFlag `json:"justification,omitempty"` Justification myTeamFlag `json:"justification,omitempty"`
} }
type myTeamExercice struct { type myTeamExercice struct {
ThemeId int64 `json:"theme_id"` ThemeId int64 `json:"theme_id"`
Statement string `json:"statement"` Statement string `json:"statement"`
Overview string `json:"overview,omitempty"` Overview string `json:"overview,omitempty"`
Finished string `json:"finished,omitempty"` Finished string `json:"finished,omitempty"`
Hints []myTeamHint `json:"hints,omitempty"` Hints []myTeamHint `json:"hints,omitempty"`
Gain int `json:"gain"` Gain int `json:"gain"`
Files []myTeamFile `json:"files,omitempty"` Files []myTeamFile `json:"files,omitempty"`
Flags map[int]myTeamFlag `json:"flags,omitempty"` Flags []myTeamFlag `json:"flags,omitempty"`
SolveDist int64 `json:"solve_dist,omitempty"` SolveDist int64 `json:"solve_dist,omitempty"`
SolvedTime *time.Time `json:"solved_time,omitempty"` SolvedTime *time.Time `json:"solved_time,omitempty"`
SolvedRank int64 `json:"solved_rank,omitempty"` SolvedRank int64 `json:"solved_rank,omitempty"`
Tries int64 `json:"tries,omitempty"` Tries int64 `json:"tries,omitempty"`
TotalTries int64 `json:"total_tries,omitempty"` TotalTries int64 `json:"total_tries,omitempty"`
VideoURI string `json:"video_uri,omitempty"` VideoURI string `json:"video_uri,omitempty"`
Issue string `json:"issue,omitempty"` Issue string `json:"issue,omitempty"`
IssueKind string `json:"issuekind,omitempty"` IssueKind string `json:"issuekind,omitempty"`
} }
type myTeam struct { type myTeam struct {
Id int64 `json:"team_id"` Id int64 `json:"team_id"`
@ -76,6 +78,12 @@ type myTeam struct {
Exercices map[string]myTeamExercice `json:"exercices"` Exercices map[string]myTeamExercice `json:"exercices"`
} }
type ByOrder []myTeamFlag
func (a ByOrder) Len() int { return len(a) }
func (a ByOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByOrder) Less(i, j int) bool { return a[i].order < a[j].order }
func MyJSONTeam(t *Team, started bool) (interface{}, error) { func MyJSONTeam(t *Team, started bool) (interface{}, error) {
ret := myTeam{} ret := myTeam{}
@ -176,15 +184,17 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
// Expose exercice flags // Expose exercice flags
justifiedMCQ := map[int]myTeamFlag{} justifiedMCQ := map[int]myTeamFlag{}
exercice.Flags = map[int]myTeamFlag{}
if flags, err := e.GetFlagKeys(); err != nil { if flags, err := e.GetFlagKeys(); err != nil {
return nil, err return nil, err
} else { } else {
for _, k := range flags { for _, k := range flags {
var flag myTeamFlag flag := myTeamFlag{
Id: k.Id,
flag.Order = k.Order Type: k.Type,
order: k.Order,
Help: k.Help,
}
if !DisplayAllFlags && t != nil && !t.CanSeeFlag(k) { if !DisplayAllFlags && t != nil && !t.CanSeeFlag(k) {
// Dependancy missing, skip the flag for now // Dependancy missing, skip the flag for now
@ -236,7 +246,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
if fl.IdChoice != 0 { if fl.IdChoice != 0 {
justifiedMCQ[fl.IdChoice] = flag justifiedMCQ[fl.IdChoice] = flag
} else { } else {
exercice.Flags[k.Id] = flag exercice.Flags = append(exercice.Flags, flag)
} }
} }
} }
@ -251,9 +261,10 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
} }
m := myTeamFlag{ m := myTeamFlag{
Id: mcq.Id,
Type: "mcq", Type: "mcq",
order: mcq.Order,
Label: mcq.Title, Label: mcq.Title,
Order: mcq.Order,
Choices: map[string]interface{}{}, Choices: map[string]interface{}{},
} }
@ -307,10 +318,13 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
m.PSolved = nil m.PSolved = nil
} }
exercice.Flags[mcq.Id] = m exercice.Flags = append(exercice.Flags, m)
} }
} }
// Sort flags by order
sort.Sort(ByOrder(exercice.Flags))
// Hash table ordered by exercice Id // Hash table ordered by exercice Id
ret.Exercices[fmt.Sprintf("%d", e.Id)] = exercice ret.Exercices[fmt.Sprintf("%d", e.Id)] = exercice
} }