server/libfic/exercice.go

326 lines
11 KiB
Go

package fic
import (
"errors"
"time"
)
// PartialValidation allows a Team to validate an Exercice at a given point if
// it has ever found all classical flags, even if the last validation is not
// complete or contains mistakes on previously validated flags.
var PartialValidation bool
// PartialMCQValidation allows a Team to validate each MCQ independently.
// Otherwise, all MCQ has to be correct for being validated.
var PartialMCQValidation bool
// Exercice represents a challenge inside a Theme.
type Exercice struct {
Id int64 `json:"id"`
Title string `json:"title"`
// URLid is used to reference the challenge from the URL path
URLId string `json:"urlid"`
// Path is the relative import location where find challenge data
Path string `json:"path"`
// Statement is the challenge description shown to players
Statement string `json:"statement"`
// Overview is the challenge description shown to public
Overview string `json:"overview"`
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"`
// 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"`
}
// GetExercice retrieves the challenge with the given id.
func GetExercice(id int64) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_exercice = ?", id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
return e, nil
}
// GetExercice retrieves the challenge with the given id.
func (t Theme) GetExercice(id int) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND id_exercice = ?", t.Id, id).Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
return e, nil
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t Theme) GetExerciceByTitle(title string) (Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ? AND title = ?", t.Id, title).Scan(&e.Id, &e.Title, &t.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return Exercice{}, err
}
return e, nil
}
// GetExercices returns the list of all challenges present in the database.
func GetExercices() ([]Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices"); err != nil {
return nil, err
} else {
defer rows.Close()
var exos = make([]Exercice, 0)
for rows.Next() {
var e Exercice
if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return nil, err
}
exos = append(exos, e)
}
if err := rows.Err(); err != nil {
return nil, err
}
return exos, nil
}
}
// GetExercices returns the list of all challenges in the Theme.
func (t Theme) GetExercices() ([]Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, title, url_id, path, statement, overview, depend, gain, coefficient_cur, video_uri FROM exercices WHERE id_theme = ?", t.Id); err != nil {
return nil, err
} else {
defer rows.Close()
var exos = make([]Exercice, 0)
for rows.Next() {
var e Exercice
if err := rows.Scan(&e.Id, &e.Title, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI); err != nil {
return nil, err
}
exos = append(exos, e)
}
if err := rows.Err(); err != nil {
return nil, err
}
return exos, nil
}
}
// AddExercice creates and fills a new struct Exercice and registers it into the database.
func (t Theme) AddExercice(title string, urlId string, path string, statement string, overview string, depend *Exercice, gain int64, videoURI string) (Exercice, error) {
var dpd interface{}
if depend == nil {
dpd = nil
} else {
dpd = depend.Id
}
if res, err := DBExec("INSERT INTO exercices (id_theme, title, url_id, path, statement, overview, depend, gain, video_uri) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", t.Id, title, urlId, path, statement, overview, dpd, gain, videoURI); err != nil {
return Exercice{}, err
} else if eid, err := res.LastInsertId(); err != nil {
return Exercice{}, err
} else {
if depend == nil {
return Exercice{eid, title, urlId, path, statement, overview, nil, gain, 1.0, videoURI}, nil
} else {
return Exercice{eid, title, urlId, path, statement, overview, &depend.Id, gain, 1.0, videoURI}, nil
}
}
}
// Update applies modifications back to the database.
func (e Exercice) Update() (int64, error) {
if res, err := DBExec("UPDATE exercices SET title = ?, url_id = ?, path = ?, statement = ?, overview = ?, depend = ?, gain = ?, coefficient_cur = ?, video_uri = ? WHERE id_exercice = ?", e.Title, e.URLId, e.Path, e.Statement, e.Overview, e.Depend, e.Gain, e.Coefficient, e.VideoURI, e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// FixURLId generates a valid URLid from the challenge Title.
// It returns true if something has been generated.
func (e *Exercice) FixURLId() bool {
if e.URLId == "" {
e.URLId = ToURLid(e.Title)
return true
}
return false
}
// GetThemeId returns the theme's id for the Exercice.
func (e Exercice) GetThemeId() (int, error) {
var tid int
if err := DBQueryRow("SELECT id_theme FROM exercices WHERE id_exercice=?", e.Id).Scan(&tid); err != nil {
return 0, err
}
return tid, nil
}
// GetTheme returns the parent Theme where the Exercice lives.
func (e Exercice) GetTheme() (Theme, error) {
if tid, err := e.GetThemeId(); err != nil {
return Theme{}, err
} else {
return GetTheme(tid)
}
}
// Delete the challenge from the database.
func (e Exercice) Delete() (int64, error) {
if res, err := DBExec("DELETE FROM exercices WHERE id_exercice = ?", e.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err
} else {
return nb, err
}
}
// GetLevel returns the number of dependancy challenges.
func (e Exercice) GetLevel() (int, error) {
dep := e.Depend
nb := 1
for dep != nil {
nb += 1
if edep, err := GetExercice(*dep); err != nil {
return nb, err
} else {
dep = edep.Depend
}
}
return nb, nil
}
// NewTry registers a solving attempt for the given Team.
func (e Exercice) NewTry(t Team) error {
if _, err := DBExec("INSERT INTO exercice_tries (id_exercice, id_team, time) VALUES (?, ?, ?)", e.Id, t.Id, time.Now()); err != nil {
return err
} else {
return nil
}
}
// UpdateTry applies modifications to the latest try registered for the given Team.
// Updated values are time and the given nbdiff.
func (e Exercice) UpdateTry(t Team, nbdiff int) error {
if _, err := DBExec("UPDATE exercice_tries SET nbdiff = ?, time = ? WHERE id_exercice = ? AND id_team = ? ORDER BY time DESC LIMIT 1", nbdiff, time.Now(), e.Id, t.Id); err != nil {
return err
} else {
return nil
}
}
// 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); err != nil {
return err
} else {
return nil
}
}
// SolvedCount returns the number of Team that already have solved the challenge.
func (e Exercice) SolvedCount() int64 {
var nb int64
if err := DBQueryRow("SELECT COUNT(id_exercice) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
} else {
return nb
}
}
// TriedTeamCount returns the number of Team that attempted to solve the exercice.
func (e Exercice) TriedTeamCount() int64 {
var nb int64
if err := DBQueryRow("SELECT COUNT(DISTINCT id_team) FROM exercice_tries WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
} else {
return nb
}
}
// TriedCount returns the number of cumulative attempts, all Team combined, for the exercice.
func (e Exercice) TriedCount() int64 {
var nb int64
if err := DBQueryRow("SELECT COUNT(id_team) FROM exercice_tries WHERE id_exercice = ?", e.Id).Scan(&nb); err != nil {
return 0
} else {
return nb
}
}
// CheckResponse, given both flags and MCQ responses, figures out if thoses are correct (or if they are previously solved).
// In the meanwhile, CheckResponse registers good answers given (but it does not mark the challenge as solved at the end).
func (e Exercice) CheckResponse(respkeys map[string]string, respmcq map[int64]bool, t Team) (bool, error) {
if err := e.NewTry(t); err != nil {
return false, err
} else if keys, err := e.GetKeys(); err != nil {
return false, err
} else if mcqs, err := e.GetMCQ(); err != nil {
return false, err
} else if len(keys) < 1 && len(mcqs) < 1 {
return true, errors.New("Exercice with no key registered")
} else {
valid := true
diff := 0
// Check MCQs
for _, mcq := range mcqs {
if d := mcq.Check(respmcq); d > 0 {
if !PartialValidation || t.HasPartiallyRespond(mcq) == nil {
valid = false
diff += d
}
} else if !PartialMCQValidation {
mcq.FoundBy(t)
}
}
// Validate MCQs if no error
if valid {
for _, mcq := range mcqs {
mcq.FoundBy(t)
}
}
// Check keys
for _, key := range keys {
if res, ok := respkeys[key.Label]; !ok {
valid = false
} else if !key.Check(res) {
if !PartialValidation || t.HasPartiallySolved(key) == nil {
valid = false
}
} else {
key.FoundBy(t)
}
}
if diff > 0 {
e.UpdateTry(t, diff)
}
return valid, nil
}
}
// IsSolved returns the number of time this challenge has been solved and the time of the first solve occurence.
func (e Exercice) IsSolved() (int, time.Time) {
var nb *int
var tm *time.Time
if DBQueryRow("SELECT COUNT(id_exercice), MIN(time) FROM exercice_solved WHERE id_exercice = ?", e.Id).Scan(&nb, &tm); nb == nil || tm == nil {
return 0, time.Time{}
} else {
return *nb, *tm
}
}