sync: Extract function that import flags from importer

This commit is contained in:
nemunaire 2019-07-05 22:28:56 +02:00
commit 4039a394b5
8 changed files with 438 additions and 294 deletions

View file

@ -20,35 +20,35 @@ var ExerciceCurrentCoefficient = 1.0
// Exercice represents a challenge inside a Theme.
type Exercice struct {
Id int64 `json:"id"`
IdTheme int64 `json:"id_theme"`
Title string `json:"title"`
Id int64 `json:"id"`
IdTheme int64 `json:"id_theme"`
Title string `json:"title"`
// URLid is used to reference the challenge from the URL path
URLId string `json:"urlid"`
URLId string `json:"urlid"`
// Path is the relative import location where find challenge data
Path string `json:"path"`
Path string `json:"path"`
// Statement is the challenge description shown to players
Statement string `json:"statement"`
Statement string `json:"statement"`
// Overview is the challenge description shown to public
Overview string `json:"overview"`
Overview string `json:"overview"`
// Headline is the challenge headline to fill in small part
Headline string `json:"headline"`
Headline string `json:"headline"`
// Finished is the text shown when the exercice is solved
Finished string `json:"finished"`
Finished string `json:"finished"`
// Issue is an optional text describing an issue with the exercice
Issue string `json:"issue"`
Issue string `json:"issue"`
// IssueKind is the criticity level of the previous issue
IssueKind string `json:"issuekind"`
Depend *int64 `json:"depend"`
IssueKind string `json:"issuekind"`
Depend *int64 `json:"depend"`
// Gain is the basis amount of points player will earn if it solve the challenge
// If this value fluctuate during the challenge, players' scores will follow changes.
// Use Coefficient value to create temporary bonus/malus.
Gain int64 `json:"gain"`
Gain int64 `json:"gain"`
// Coefficient is a temporary bonus/malus applied on Gain when the challenge is solved.
// This value is saved along with solved informations: changing it doesn't affect Team that already solved the challenge.
Coefficient float64 `json:"coefficient"`
// VideoURI is the link to the resolution video
VideoURI string `json:"videoURI"`
VideoURI string `json:"videoURI"`
}
// GetExercice retrieves the challenge with the given id.
@ -163,7 +163,7 @@ func (t Theme) addExercice(e *Exercice) (err error) {
cc = fmt.Sprintf("%f", e.Coefficient)
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, " + ik + ", " + cc + ")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI); err != nil {
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, finished, headline, issue, depend, gain, video_uri, issue_kind, coefficient_cur) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, "+ik+", "+cc+")", t.Id, e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Finished, e.Headline, e.Issue, e.Depend, e.Gain, e.VideoURI); err != nil {
return err
} else if eid, err := res.LastInsertId(); err != nil {
return err
@ -182,16 +182,16 @@ func (t Theme) AddExercice(title string, urlId string, path string, statement st
}
e = Exercice{
Title: title,
URLId: urlId,
Path: path,
Title: title,
URLId: urlId,
Path: path,
Statement: statement,
Overview: overview,
Headline: headline,
Depend: dpd,
Finished: finished,
Gain: gain,
VideoURI: videoURI,
Overview: overview,
Headline: headline,
Depend: dpd,
Finished: finished,
Gain: gain,
VideoURI: videoURI,
}
err = t.addExercice(&e)
@ -313,7 +313,7 @@ func (e Exercice) UpdateTry(t Team, nbdiff int) error {
// Solved registers that the given Team solves the challenge.
func (e Exercice) Solved(t Team) error {
if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time, coefficient) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), e.Coefficient * ExerciceCurrentCoefficient); err != nil {
if _, err := DBExec("INSERT INTO exercice_solved (id_exercice, id_team, time, coefficient) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), e.Coefficient*ExerciceCurrentCoefficient); err != nil {
return err
} else {
return nil
@ -338,7 +338,7 @@ func (e Exercice) TriedTeamCount() int64 {
}
var nb int64
if err := DBQueryRow("SELECT COUNT(DISTINCT id_team) FROM " + tries_table + " WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
if err := DBQueryRow("SELECT COUNT(DISTINCT id_team) FROM "+tries_table+" WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
} else {
return nb
@ -353,7 +353,7 @@ func (e Exercice) TriedCount() int64 {
}
var nb int64
if err := DBQueryRow("SELECT COUNT(id_team) FROM " + tries_table + " WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
if err := DBQueryRow("SELECT COUNT(id_team) FROM "+tries_table+" WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
} else {
return nb

View file

@ -3,9 +3,11 @@ package fic
import ()
type Flag interface {
GetId() int64
Create(e Exercice) (Flag, error)
Update() (int64, error)
Delete() (int64, error)
AddDepend(d Flag) (error)
AddDepend(d Flag) error
GetDepends() ([]Flag, error)
Check(val interface{}) int
FoundBy(t Team)
@ -34,6 +36,11 @@ func (e Exercice) GetFlags() ([]Flag, error) {
return flags, nil
}
// AddFlag add the given flag and eventually its entries (from MCQ).
func (e Exercice) AddFlag(flag Flag) (f Flag, err error) {
return flag.Create(e)
}
// WipeFlags deletes flags coming with the challenge.
func (e Exercice) WipeFlags() (int64, error) {
if _, err := DBExec("DELETE FROM exercice_files_deps WHERE id_flag IN (SELECT id_flag FROM exercice_flags WHERE id_exercice = ?)", e.Id); err != nil {

View file

@ -4,13 +4,13 @@ import ()
// FlagChoice represents a choice a respond to a classic flag
type FlagChoice struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// IdFlag is the identifier of the underlying flag
IdFlag int64 `json:"idFlag"`
IdFlag int64 `json:"idFlag"`
// Label is the title of the choice as displayed to players
Label string `json:"label"`
Label string `json:"label"`
// Value is the raw content that'll be written as response if this choice is selected
Value string `json:"value"`
Value string `json:"value"`
}
// GetChoices returns a list of choices for the given Flag.
@ -48,13 +48,12 @@ func (f FlagKey) GetChoice(id int64) (c FlagChoice, err error) {
}
// AddChoice creates and fills a new struct FlagChoice, from a label and a value.
func (f FlagKey) AddChoice(label string, value string) (FlagChoice, error) {
if res, err := DBExec("INSERT INTO flag_choices (id_flag, label, response) VALUES (?, ?, ?)", f.Id, label, value); err != nil {
return FlagChoice{}, err
} else if cid, err := res.LastInsertId(); err != nil {
return FlagChoice{}, err
func (f FlagKey) AddChoice(c FlagChoice) (FlagChoice, error) {
if res, err := DBExec("INSERT INTO flag_choices (id_flag, label, response) VALUES (?, ?, ?)", f.Id, c.Label, c.Value); err != nil {
return c, err
} else {
return FlagChoice{cid, f.Id, label, value}, nil
c.Id, err = res.LastInsertId()
return c, err
}
}

View file

@ -12,21 +12,21 @@ import (
// FlagKey represents a flag's challenge, stored as hash.
type FlagKey struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
IdExercice int64 `json:"idExercice"`
// Label is the title of the flag as displayed to players
Label string `json:"label"`
Label string `json:"label"`
// Help is a small piece of text that aims to add useful information like flag format, ...
Help string `json:"help"`
Help string `json:"help"`
// IgnoreCase indicates if the case is sensitive to case or not
IgnoreCase bool `json:"ignorecase"`
IgnoreCase bool `json:"ignorecase"`
// ValidatorRegexp extracts a subset of the player's answer, that will be checked.
ValidatorRegexp *string `json:"validator_regexp"`
// Checksum is the expected hashed flag
Checksum []byte `json:"value"`
Checksum []byte `json:"value"`
// ChoicesCost is the number of points lost to display choices.
ChoicesCost int64 `json:"choices_cost"`
ChoicesCost int64 `json:"choices_cost"`
}
// GetFlagKeys returns a list of key's flags comming with the challenge.
@ -67,14 +67,14 @@ func (e Exercice) GetFlagKeyByLabel(label string) (k FlagKey, err error) {
return
}
// getHashedFlag calculates the expected checksum for the given raw_value.
func getHashedFlag(raw_value []byte) [blake2b.Size]byte {
// ComputeHashedFlag calculates the expected checksum for the given raw_value.
func ComputeHashedFlag(raw_value []byte) [blake2b.Size]byte {
hash := blake2b.Sum512(raw_value)
return hash
}
func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error) {
if (ignorecase) {
if ignorecase {
vre = "(?i)" + vre
}
if re, err := regexp.Compile(vre); err != nil {
@ -87,38 +87,55 @@ func ExecValidatorRegexp(vre string, val []byte, ignorecase bool) ([]byte, error
}
// AddRawFlagKey creates and fills a new struct FlagKey, from a non-hashed flag, and registers it into the database.
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (FlagKey, error) {
func (e Exercice) AddRawFlagKey(name string, help string, ignorecase bool, validator_regexp *string, raw_value []byte, choicescost int64) (f FlagKey, err error) {
if ignorecase {
raw_value = bytes.ToLower(raw_value)
}
// Check that raw value passes through the regexp
if validator_regexp != nil {
var err error
if raw_value, err = ExecValidatorRegexp(*validator_regexp, raw_value, ignorecase); err != nil {
return FlagKey{}, err
return
}
}
hash := getHashedFlag(raw_value)
return e.AddFlagKey(name, help, ignorecase, validator_regexp, hash[:], choicescost)
hash := ComputeHashedFlag(raw_value)
f = FlagKey{
Label: name,
Help: help,
IgnoreCase: ignorecase,
ValidatorRegexp: validator_regexp,
Checksum: hash[:],
ChoicesCost: choicescost,
}
_, err = f.Create(e)
return
}
// GetId returns the Flag identifier.
func (k FlagKey) GetId() int64 {
return k.Id
}
// AddFlagKey creates and fills a new struct Flag, from a hashed flag, and registers it into the database.
func (e Exercice) AddFlagKey(name string, help string, ignorecase bool, validator_regexp *string, checksum []byte, choicescost int64) (FlagKey, error) {
func (k FlagKey) Create(e Exercice) (Flag, error) {
// Check the regexp compile
if validator_regexp != nil {
if _, err := regexp.Compile(*validator_regexp); err != nil {
return FlagKey{}, err
if k.ValidatorRegexp != nil {
if _, err := regexp.Compile(*k.ValidatorRegexp); err != nil {
return k, err
}
}
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost); err != nil {
return FlagKey{}, err
if res, err := DBExec("INSERT INTO exercice_flags (id_exercice, type, help, ignorecase, validator_regexp, cksum, choices_cost) VALUES (?, ?, ?, ?, ?, ?, ?)", e.Id, k.Label, k.Help, k.IgnoreCase, k.ValidatorRegexp, k.Checksum, k.ChoicesCost); err != nil {
return k, err
} else if kid, err := res.LastInsertId(); err != nil {
return FlagKey{}, err
return k, err
} else {
return FlagKey{kid, e.Id, name, help, ignorecase, validator_regexp, checksum, choicescost}, nil
k.Id = kid
k.IdExercice = e.Id
return k, nil
}
}
@ -140,7 +157,7 @@ func (k FlagKey) ComputeChecksum(val []byte) (cksum []byte, err error) {
err = errors.New("Empty flag after applying filters")
}
hash := getHashedFlag(val)
hash := ComputeHashedFlag(val)
return hash[:], err
}

View file

@ -8,22 +8,22 @@ import (
// MCQ represents a flag's challenge, in the form of checkbox.
type MCQ struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// IdExercice is the identifier of the underlying challenge
IdExercice int64 `json:"idExercice"`
IdExercice int64 `json:"idExercice"`
// Title is the label of the question
Title string `json:"title"`
Title string `json:"title"`
// Entries stores the set of proposed answers
Entries []MCQ_entry `json:"entries"`
Entries []MCQ_entry `json:"entries"`
}
// MCQ_entry represents a proposed response for a given MCQ.
type MCQ_entry struct {
Id int64 `json:"id"`
Id int64 `json:"id"`
// Label is the text displayed to players as proposed answer
Label string `json:"label"`
Label string `json:"label"`
// Response stores if expected checked state.
Response bool `json:"response"`
Response bool `json:"response"`
}
// GetMCQ returns the MCQs coming with the challenge.
@ -97,14 +97,31 @@ func GetMCQbyChoice(cid int64) (m MCQ, c MCQ_entry, err error) {
return
}
// AddMCQ creates and fills a new struct MCQ and registers it into the database.
func (e Exercice) AddMCQ(title string) (MCQ, error) {
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title) VALUES (?, ?)", e.Id, title); err != nil {
return MCQ{}, err
// GetId returns the MCQ identifier.
func (m MCQ) GetId() int64 {
return m.Id
}
// Create registers a MCQ into the database and recursively add its entries.
func (m MCQ) Create(e Exercice) (Flag, error) {
if res, err := DBExec("INSERT INTO exercice_mcq (id_exercice, title) VALUES (?, ?)", e.Id, m.Title); err != nil {
return m, err
} else if qid, err := res.LastInsertId(); err != nil {
return MCQ{}, err
return m, err
} else {
return MCQ{qid, e.Id, title, []MCQ_entry{}}, nil
m.Id = qid
m.IdExercice = e.Id
// Add entries
for k, entry := range m.Entries {
if entry, err = m.AddEntry(entry); err != nil {
return m, err
} else {
m.Entries[k] = entry
}
}
return m, nil
}
}
@ -135,13 +152,14 @@ func (m MCQ) Delete() (int64, error) {
}
// AddEntry creates and fills a new struct MCQ_entry and registers it into the database.
func (m MCQ) AddEntry(label string, response bool) (MCQ_entry, error) {
if res, err := DBExec("INSERT INTO mcq_entries (id_mcq, label, response) VALUES (?, ?, ?)", m.Id, label, response); err != nil {
return MCQ_entry{}, err
func (m MCQ) AddEntry(e MCQ_entry) (MCQ_entry, error) {
if res, err := DBExec("INSERT INTO mcq_entries (id_mcq, label, response) VALUES (?, ?, ?)", m.Id, e.Label, e.Response); err != nil {
return e, err
} else if nid, err := res.LastInsertId(); err != nil {
return MCQ_entry{}, err
return e, err
} else {
return MCQ_entry{nid, label, response}, nil
e.Id = nid
return e, nil
}
}