Apply standalone exercices settings

This commit is contained in:
nemunaire 2024-03-16 11:28:59 +01:00
commit 398de21793
6 changed files with 63 additions and 1 deletions

View file

@ -420,6 +420,27 @@ func (e *Exercice) GetLevel() (int, error) {
return nb, nil
}
// GetOrdinal returns the position of the exercice in list (usefull for standalone exercices).
func (e *Exercice) GetOrdinal() (int, error) {
theme := &Theme{Id: 0}
if e.IdTheme != nil {
theme.Id = *e.IdTheme
}
exercices, err := theme.GetExercices()
if err != nil {
return 0, err
}
for n, exercice := range exercices {
if exercice.Id == e.Id {
return n, nil
}
}
return 0, fmt.Errorf("exercice not found in theme")
}
// NewTry registers a solving attempt for the given Team.
func (e *Exercice) NewTry(t *Team, cksum []byte) error {
if _, err := DBExec("INSERT INTO exercice_tries (id_exercice, id_team, time, cksum) VALUES (?, ?, ?, ?)", e.Id, t.Id, time.Now(), cksum); err != nil {

View file

@ -116,6 +116,29 @@ func (t *Team) Delete() (int64, error) {
// HasAccess checks if the Team has access to the given challenge.
func (t *Team) HasAccess(e *Exercice) bool {
// Case of standalone exercices
if e.IdTheme == nil || *e.IdTheme == 0 {
ord, err := e.GetOrdinal()
if err != nil {
return false
}
if ord < UnlockedStandaloneExercices {
return true
}
sc, err := t.SolvedCount()
if sc == nil || err != nil {
return false
}
if ord < UnlockedStandaloneExercices+int(UnlockedStandaloneExercicesByValidation*float64(*sc)) {
return true
}
return false
}
if UnlockedChallengeDepth < 0 {
return true
}
@ -297,6 +320,12 @@ func (t *Team) LastTryDist(e *Exercice) int64 {
}
}
// SolvedCount returns the number of solved exercices.
func (t *Team) SolvedCount() (nb *int64, err error) {
err = DBQueryRow("SELECT COUNT(*) FROM exercice_solved WHERE id_team = ?", t.Id).Scan(&nb)
return
}
// HasSolved checks if the Team already has validated the given challenge.
// Note that the function also returns the effective validation timestamp.
func (t *Team) HasSolved(e *Exercice) (tm *time.Time) {

View file

@ -22,6 +22,12 @@ var DisplayMCQBadCount bool
// HideCaseSensitivity never tells the user if the flag is case sensitive or not.
var HideCaseSensitivity bool
// UnlockedStandaloneExercices unlock this number of standalone exercice.
var UnlockedStandaloneExercices int
// UnlockedStandaloneExercicesByValidation unlock this number of standalone exercice for each exercice validated.
var UnlockedStandaloneExercicesByValidation float64
type myTeamFile struct {
Path string `json:"path"`
Name string `json:"name"`