challenge-sync-airbus: Done

This commit is contained in:
nemunaire 2023-04-06 15:37:05 +02:00
commit 0d5b87b3f7
6 changed files with 73 additions and 68 deletions

View file

@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"time"
"srs.epita.fr/fic-server/libfic"
)
@ -51,12 +52,14 @@ func (w *Walker) fetchTeams() error {
return nil
}
func (w *Walker) treat(path string) {
func (w *Walker) treat(path string) error {
teamid := filepath.Base(filepath.Dir(path))
if _, ok := w.TeamBindings[teamid]; ok {
w.TreatScoreGrid(path, w.TeamBindings[teamid])
return w.TreatScoreGrid(path, w.TeamBindings[teamid])
}
return nil
}
func (w *Walker) LoadScoreState(path string) (int64, error) {
@ -107,30 +110,7 @@ func (w *Walker) LoadScoreGrid(path string) ([]fic.ScoreGridRow, error) {
func (w *Walker) WalkScoreSync(path string, d os.DirEntry, err error) error {
if filepath.Base(path) == "scores.json" {
w.treat(path)
}
for team, ts := range w.LastSync {
team_id, ok := w.RevTeams[team]
if !ok {
continue
}
myteam, err := w.loadMyFile(filepath.Join(TeamsDir, team_id, "my.json"))
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team_id, err)
}
airbusTeam := w.TeamBindings[fmt.Sprintf("%d", myteam.Id)]
if ts.Score != myteam.Points*int64(w.Coeff) {
err := w.API.AwardUser(airbusTeam, myteam.Points-ts.Score, "Équilibrage")
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team, err)
}
w.LastSync[airbusTeam.Name].Score = myteam.Points * int64(w.Coeff)
}
return w.treat(path)
}
return nil
@ -174,7 +154,9 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
}
// Found all new entries
maxts := &TSValue{}
maxts := &TSValue{
Time: time.Time{},
}
if ts, ok := w.LastSync[airbusTeam.Name]; ok {
maxts = ts
}
@ -182,7 +164,7 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
if row.Time.After(maxts.Time) {
maxts.Time = row.Time
}
if row.Time.After(w.LastSync[airbusTeam.Name].Time) {
if ts, ok := w.LastSync[airbusTeam.Name]; !ok || row.Time.After(ts.Time) {
if !noValidateChallenge && row.Reason == "Validation" {
err = w.API.ValidateChallengeFromUser(airbusTeam, w.Exercices[row.IdExercice])
} else {
@ -202,29 +184,29 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
return nil
}
func (w *Walker) BalanceScore(score int64, airbusTeam *AirbusTeam) error {
// Read current score on other platform
stats, err := w.API.GetCurrentStats()
if err != nil {
fmt.Errorf("unable to retrieve current stats: %w", err)
}
func (w *Walker) BalanceScores() error {
for team, ts := range w.LastSync {
team_id, ok := w.RevTeams[team]
if !ok {
continue
}
my_session := stats.Data.GetSession(AirbusUUID(w.API.SessionUUID))
if my_session == nil {
return fmt.Errorf("session not found")
}
myteam, err := w.loadMyFile(filepath.Join(TeamsDir, team_id, "my.json"))
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team_id, err)
}
other_team := my_session.GetTeam(AirbusUUID(airbusTeam.Name))
if other_team == nil {
return fmt.Errorf("team %q not found", airbusTeam.Name)
}
airbusTeam := w.TeamBindings[fmt.Sprintf("%d", myteam.Id)]
other_score := other_team.Score
if ts.Score != myteam.Points*int64(w.Coeff) {
err := w.API.AwardUser(airbusTeam, myteam.Points*int64(w.Coeff)-ts.Score, "Équilibrage")
if err != nil {
return fmt.Errorf("Unable to open %s/my.json: %w", team, err)
}
// Send diff to the platform
if other_score != score {
diff := score - other_score
return w.API.AwardUser(airbusTeam, diff, "Équilibrage")
w.LastSync[airbusTeam.Name].Score = myteam.Points * int64(w.Coeff)
w.LastSync[airbusTeam.Name].Time = time.Now()
}
}
return nil