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"
"log"
"path"
"sort"
"strconv"
"strings"
"time"
@ -27,7 +28,8 @@ type myTeamHint struct {
Cost int64 `json:"cost"`
}
type myTeamFlag struct {
Order int8 `json:"order"`
Id int `json:"id"`
order int8 `json:"order"`
Label string `json:"label"`
Type string `json:"type,omitempty"`
Placeholder string `json:"placeholder,omitempty"`
@ -58,7 +60,7 @@ type myTeamExercice struct {
Hints []myTeamHint `json:"hints,omitempty"`
Gain int `json:"gain"`
Files []myTeamFile `json:"files,omitempty"`
Flags map[int]myTeamFlag `json:"flags,omitempty"`
Flags []myTeamFlag `json:"flags,omitempty"`
SolveDist int64 `json:"solve_dist,omitempty"`
SolvedTime *time.Time `json:"solved_time,omitempty"`
SolvedRank int64 `json:"solved_rank,omitempty"`
@ -76,6 +78,12 @@ type myTeam struct {
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) {
ret := myTeam{}
@ -176,15 +184,17 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
// Expose exercice flags
justifiedMCQ := map[int]myTeamFlag{}
exercice.Flags = map[int]myTeamFlag{}
if flags, err := e.GetFlagKeys(); err != nil {
return nil, err
} else {
for _, k := range flags {
var flag myTeamFlag
flag.Order = k.Order
flag := myTeamFlag{
Id: k.Id,
Type: k.Type,
order: k.Order,
Help: k.Help,
}
if !DisplayAllFlags && t != nil && !t.CanSeeFlag(k) {
// Dependancy missing, skip the flag for now
@ -236,7 +246,7 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
if fl.IdChoice != 0 {
justifiedMCQ[fl.IdChoice] = flag
} 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{
Id: mcq.Id,
Type: "mcq",
order: mcq.Order,
Label: mcq.Title,
Order: mcq.Order,
Choices: map[string]interface{}{},
}
@ -307,10 +318,13 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
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
ret.Exercices[fmt.Sprintf("%d", e.Id)] = exercice
}