New setting: introduce a decote/discount to exercice's gain

This commit is contained in:
nemunaire 2023-04-01 17:11:40 +02:00
commit 4451e41285
11 changed files with 123 additions and 24 deletions

View file

@ -3,6 +3,7 @@ package fic
import (
"errors"
"fmt"
"math"
"time"
)
@ -69,11 +70,13 @@ func (e *Exercice) AnalyzeTitle() {
}
}
func getExercice(condition string, args ...interface{}) (*Exercice, error) {
func getExercice(table, condition string, args ...interface{}) (*Exercice, error) {
var e Exercice
if err := DBQueryRow("SELECT id_exercice, id_theme, title, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices "+condition, args...).Scan(&e.Id, &e.IdTheme, &e.Title, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
var tmpgain float64
if err := DBQueryRow("SELECT id_exercice, id_theme, title, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM "+table+" "+condition, args...).Scan(&e.Id, &e.IdTheme, &e.Title, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err
}
e.Gain = int64(math.Trunc(tmpgain))
e.AnalyzeTitle()
return &e, nil
@ -81,27 +84,36 @@ func getExercice(condition string, args ...interface{}) (*Exercice, error) {
// GetExercice retrieves the challenge with the given id.
func GetExercice(id int64) (*Exercice, error) {
return getExercice("WHERE id_exercice = ?", id)
return getExercice("exercices", "WHERE id_exercice = ?", id)
}
// GetExercice retrieves the challenge with the given id.
func (t *Theme) GetExercice(id int) (*Exercice, error) {
return getExercice("WHERE id_theme = ? AND id_exercice = ?", t.Id, id)
return getExercice("exercices", "WHERE id_theme = ? AND id_exercice = ?", t.Id, id)
}
// GetExerciceByTitle retrieves the challenge with the given title.
func (t *Theme) GetExerciceByTitle(title string) (*Exercice, error) {
return getExercice("WHERE id_theme = ? AND title = ?", t.Id, title)
return getExercice("exercices", "WHERE id_theme = ? AND title = ?", t.Id, title)
}
// GetExerciceByPath retrieves the challenge with the given path.
func (t *Theme) GetExerciceByPath(epath string) (*Exercice, error) {
return getExercice("WHERE id_theme = ? AND path = ?", t.Id, epath)
return getExercice("exercices", "WHERE id_theme = ? AND path = ?", t.Id, epath)
}
// GetExercices returns the list of all challenges present in the database.
func GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices ORDER BY path ASC"); err != nil {
// GetDiscountedExercice retrieves the challenge with the given id.
func GetDiscountedExercice(id int64) (*Exercice, error) {
table := "exercices"
if DiscountedFactor > 0 {
table = "exercices_discounted"
}
return getExercice(table, "WHERE id_exercice = ?", id)
}
// getExercices returns the list of all challenges present in the database.
func getExercices(table string) ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM " + table + " ORDER BY path ASC"); err != nil {
return nil, err
} else {
defer rows.Close()
@ -109,9 +121,11 @@ func GetExercices() ([]*Exercice, error) {
exos := []*Exercice{}
for rows.Next() {
e := &Exercice{}
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &e.Gain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
var tmpgain float64
if err := rows.Scan(&e.Id, &e.IdTheme, &e.Title, &e.Disabled, &e.URLId, &e.Path, &e.Statement, &e.Overview, &e.Headline, &e.Issue, &e.IssueKind, &e.Depend, &tmpgain, &e.Coefficient, &e.VideoURI, &e.Resolution, &e.SeeAlso, &e.Finished); err != nil {
return nil, err
}
e.Gain = int64(math.Trunc(tmpgain))
e.AnalyzeTitle()
exos = append(exos, e)
}
@ -123,6 +137,18 @@ func GetExercices() ([]*Exercice, error) {
}
}
func GetExercices() ([]*Exercice, error) {
return getExercices("exercices")
}
func GetDiscountedExercices() ([]*Exercice, error) {
table := "exercices"
if DiscountedFactor > 0 {
table = "exercices_discounted"
}
return getExercices(table)
}
// GetExercices returns the list of all challenges in the Theme.
func (t *Theme) GetExercices() ([]*Exercice, error) {
if rows, err := DBQuery("SELECT id_exercice, id_theme, title, disabled, url_id, path, statement, overview, headline, issue, issue_kind, depend, gain, coefficient_cur, video_uri, resolution, seealso, finished FROM exercices WHERE id_theme = ? ORDER BY path ASC", t.Id); err != nil {