Write docs!
This commit is contained in:
parent
c460bb7bf5
commit
bcc598ebd5
37 changed files with 478 additions and 188 deletions
|
|
@ -5,22 +5,40 @@ import (
|
|||
"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 {
|
||||
|
|
@ -30,6 +48,7 @@ func GetExercice(id int64) (Exercice, error) {
|
|||
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 {
|
||||
|
|
@ -39,6 +58,7 @@ func (t Theme) GetExercice(id int) (Exercice, error) {
|
|||
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 {
|
||||
|
|
@ -48,6 +68,7 @@ func (t Theme) GetExerciceByTitle(title string) (Exercice, error) {
|
|||
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
|
||||
|
|
@ -70,6 +91,7 @@ func GetExercices() ([]Exercice, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -92,6 +114,7 @@ func (t Theme) GetExercices() ([]Exercice, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
@ -112,6 +135,7 @@ func (t Theme) AddExercice(title string, urlId string, path string, statement st
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -122,6 +146,8 @@ func (e Exercice) Update() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
|
|
@ -130,6 +156,7 @@ func (e *Exercice) FixURLId() bool {
|
|||
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 {
|
||||
|
|
@ -138,6 +165,7 @@ func (e Exercice) GetThemeId() (int, error) {
|
|||
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
|
||||
|
|
@ -146,6 +174,7 @@ func (e Exercice) GetTheme() (Theme, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -156,6 +185,7 @@ func (e Exercice) Delete() (int64, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// GetLevel returns the number of dependancy challenges.
|
||||
func (e Exercice) GetLevel() (int, error) {
|
||||
dep := e.Depend
|
||||
nb := 1
|
||||
|
|
@ -170,6 +200,7 @@ func (e Exercice) GetLevel() (int, error) {
|
|||
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
|
||||
|
|
@ -178,6 +209,8 @@ func (e Exercice) NewTry(t Team) error {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -186,6 +219,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); err != nil {
|
||||
return err
|
||||
|
|
@ -194,6 +228,7 @@ func (e Exercice) Solved(t Team) error {
|
|||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
@ -203,6 +238,7 @@ func (e Exercice) SolvedCount() int64 {
|
|||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
@ -212,6 +248,7 @@ func (e Exercice) TriedTeamCount() int64 {
|
|||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
|
@ -221,6 +258,8 @@ func (e Exercice) TriedCount() int64 {
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
@ -273,3 +312,14 @@ func (e Exercice) CheckResponse(respkeys map[string]string, respmcq map[int64]bo
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue