admin: Add ability to append element to exercice history

This commit is contained in:
nemunaire 2024-03-17 10:19:35 +01:00
commit 977caccc1f
6 changed files with 237 additions and 3 deletions

View file

@ -1,7 +1,9 @@
package fic
import (
"encoding/binary"
"fmt"
"math/rand"
"time"
)
@ -56,6 +58,32 @@ func (e *Exercice) GetHistory() ([]map[string]interface{}, error) {
return hist, nil
}
// AppendHistoryItem sets values an entry from the history.
func (e *Exercice) AppendHistoryItem(tId int64, kind string, secondary *int64) error {
team, err := GetTeam(tId)
if err != nil {
return err
}
if kind == "tries" {
bid := make([]byte, 5)
binary.LittleEndian.PutUint32(bid, rand.Uint32())
return (&Exercice{Id: e.Id}).NewTry(team, bid)
} else if kind == "hint" && secondary != nil {
return team.OpenHint(&EHint{Id: *secondary})
} else if kind == "wchoices" && secondary != nil {
return team.DisplayChoices(&FlagKey{Id: int(*secondary)})
} else if kind == "flag_found" && secondary != nil {
return (&FlagKey{Id: int(*secondary)}).FoundBy(team)
} else if kind == "mcq_found" && secondary != nil {
return (&MCQ{Id: int(*secondary)}).FoundBy(team)
} else if kind == "solved" {
return (&Exercice{Id: e.Id}).Solved(team)
} else {
return nil
}
}
// UpdateHistoryItem sets values an entry from the history.
func (e *Exercice) UpdateHistoryItem(coeff float32, tId int64, kind string, h time.Time, secondary *int64) (interface{}, error) {
if kind == "hint" && secondary != nil {