libfic: New function to analyze number flag
This commit is contained in:
parent
6aead541d6
commit
64feef8b95
2 changed files with 42 additions and 29 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -47,6 +48,43 @@ type FlagKey struct {
|
||||||
BonusGain int32 `json:"bonus_gain"`
|
BonusGain int32 `json:"bonus_gain"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AnalyzeNumberFlag(t string) (min, max, step *float64, err error) {
|
||||||
|
fields := strings.Split(t, ",")
|
||||||
|
|
||||||
|
if len(fields) != 4 || fields[0] != "number" {
|
||||||
|
err = errors.New("this is not a flag of type 'number'")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields[1]) > 0 {
|
||||||
|
min = new(float64)
|
||||||
|
*min, err = strconv.ParseFloat(fields[1], 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields[2]) > 0 {
|
||||||
|
max = new(float64)
|
||||||
|
*max, err = strconv.ParseFloat(fields[2], 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields[3]) > 0 {
|
||||||
|
step = new(float64)
|
||||||
|
*step, err = strconv.ParseFloat(fields[3], 64)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*step = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetFlagKeys returns a list of key's flags comming with the challenge.
|
// GetFlagKeys returns a list of key's flags comming with the challenge.
|
||||||
func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
|
func (e *Exercice) GetFlagKeys() ([]*FlagKey, error) {
|
||||||
if rows, err := DBQuery("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
|
if rows, err := DBQuery("SELECT id_flag, id_exercice, ordre, label, type, placeholder, help, unit, ignorecase, notrim, multiline, validator_regexp, sort_re_grps, cksum, choices_cost, bonus_gain FROM exercice_flags WHERE id_exercice = ?", e.Id); err != nil {
|
||||||
|
|
|
@ -236,35 +236,10 @@ func MyJSONTeam(t *Team, started bool) (interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(flag.Type, "number") {
|
if strings.HasPrefix(flag.Type, "number") {
|
||||||
fields := strings.Split(flag.Type, ",")
|
flag.Type = "number"
|
||||||
if len(fields) == 4 {
|
flag.Min, flag.Max, flag.Step, err = AnalyzeNumberFlag(flag.Type)
|
||||||
flag.Type = fields[0]
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
var tmp_min, tmp_max, tmp_step float64
|
|
||||||
|
|
||||||
if len(fields[1]) > 0 {
|
|
||||||
tmp_min, err = strconv.ParseFloat(fields[1], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
flag.Min = &tmp_min
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(fields[2]) > 0 {
|
|
||||||
tmp_max, err = strconv.ParseFloat(fields[2], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
flag.Max = &tmp_max
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(fields[3]) > 0 {
|
|
||||||
tmp_step, err = strconv.ParseFloat(fields[3], 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
flag.Step = &tmp_step
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue