Apply standalone exercices settings

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

View File

@ -317,6 +317,8 @@ func ApplySettings(config *settings.Settings) {
fic.UnlockedChallengeUpTo = config.UnlockedChallengeUpTo
fic.DisplayAllFlags = config.DisplayAllFlags
fic.HideCaseSensitivity = config.HideCaseSensitivity
fic.UnlockedStandaloneExercices = config.UnlockedStandaloneExercices
fic.UnlockedStandaloneExercicesByValidation = config.UnlockedStandaloneExercicesByValidation
fic.DisplayMCQBadCount = config.DisplayMCQBadCount
fic.FirstBlood = config.FirstBlood
fic.SubmissionCostBase = config.SubmissionCostBase

View File

@ -79,6 +79,8 @@ func reloadSettings(config *settings.Settings) {
fic.PartialValidation = config.PartialValidation
fic.UnlockedChallengeDepth = config.UnlockedChallengeDepth
fic.UnlockedChallengeUpTo = config.UnlockedChallengeUpTo
fic.UnlockedStandaloneExercices = config.UnlockedStandaloneExercices
fic.UnlockedStandaloneExercicesByValidation = config.UnlockedStandaloneExercicesByValidation
fic.DisplayAllFlags = config.DisplayAllFlags
fic.FirstBlood = config.FirstBlood

View File

@ -30,12 +30,14 @@ func reloadSettings(config *settings.Settings) {
fic.WChoiceCoefficient = config.WChoiceCurCoefficient
fic.ExerciceCurrentCoefficient = config.ExerciceCurCoefficient
ChStarted = config.Start.Unix() > 0 && time.Since(config.Start) >= 0
if allowRegistration != config.AllowRegistration || fic.PartialValidation != config.PartialValidation || fic.UnlockedChallengeDepth != config.UnlockedChallengeDepth || fic.UnlockedChallengeUpTo != config.UnlockedChallengeUpTo || fic.DisplayAllFlags != config.DisplayAllFlags || fic.FirstBlood != config.FirstBlood || fic.SubmissionCostBase != config.SubmissionCostBase || fic.SubmissionUniqueness != config.SubmissionUniqueness || fic.DiscountedFactor != config.DiscountedFactor || fic.HideCaseSensitivity != config.HideCaseSensitivity {
if allowRegistration != config.AllowRegistration || fic.PartialValidation != config.PartialValidation || fic.UnlockedChallengeDepth != config.UnlockedChallengeDepth || fic.UnlockedStandaloneExercices != config.UnlockedStandaloneExercices || fic.UnlockedStandaloneExercicesByValidation != config.UnlockedStandaloneExercicesByValidation || fic.UnlockedChallengeUpTo != config.UnlockedChallengeUpTo || fic.DisplayAllFlags != config.DisplayAllFlags || fic.FirstBlood != config.FirstBlood || fic.SubmissionCostBase != config.SubmissionCostBase || fic.SubmissionUniqueness != config.SubmissionUniqueness || fic.DiscountedFactor != config.DiscountedFactor || fic.HideCaseSensitivity != config.HideCaseSensitivity {
allowRegistration = config.AllowRegistration
fic.PartialValidation = config.PartialValidation
fic.UnlockedChallengeDepth = config.UnlockedChallengeDepth
fic.UnlockedChallengeUpTo = config.UnlockedChallengeUpTo
fic.UnlockedStandaloneExercices = config.UnlockedStandaloneExercices
fic.UnlockedStandaloneExercicesByValidation = config.UnlockedStandaloneExercicesByValidation
fic.DisplayAllFlags = config.DisplayAllFlags
fic.FirstBlood = config.FirstBlood

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"`