libfic: New function to analyze number flag

This commit is contained in:
nemunaire 2022-08-21 21:29:22 +02:00
commit 64feef8b95
2 changed files with 42 additions and 29 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"time"
@ -47,6 +48,43 @@ type FlagKey struct {
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.
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 {