remote-challenge-sync-airbus: WIP
This commit is contained in:
parent
6d9fd1ff12
commit
367e686e8a
5 changed files with 270 additions and 51 deletions
112
remote/challenge-sync-airbus/treat.go
Normal file
112
remote/challenge-sync-airbus/treat.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"srs.epita.fr/fic-server/libfic"
|
||||
)
|
||||
|
||||
type Walker struct {
|
||||
LastSync time.Time
|
||||
Exercices AirbusExercicesBindings
|
||||
Teams map[string]fic.ExportedTeam
|
||||
API AirbusAPI
|
||||
Coeff float64
|
||||
}
|
||||
|
||||
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
|
||||
if filepath.Base(path) == "scores.json" {
|
||||
mypath := filepath.Join(filepath.Dir(path), "my.json")
|
||||
if _, err := os.Stat(mypath); !os.IsNotExist(err) {
|
||||
// Read team ID
|
||||
fdmy, err := os.Open(mypath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fdmy.Close()
|
||||
|
||||
teammy, err := fic.ReadMyJSON(fdmy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
|
||||
|
||||
// Treat score grid
|
||||
err = w.TreatScoreGrid(path, airbusTeamId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Balance scores
|
||||
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Walker) TreatScoreGrid(path string, airbusTeamId AirbusUserId) error {
|
||||
// Read score grid
|
||||
fdscores, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fdscores.Close()
|
||||
|
||||
teamscores, err := fic.ReadScoreGrid(fdscores)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Found all new entries
|
||||
for _, row := range teamscores {
|
||||
if row.Time.After(w.LastSync) {
|
||||
if row.Reason == "Validation" {
|
||||
err = w.API.ValidateChallengeFromUser(airbusTeamId, w.Exercices[row.IdExercice])
|
||||
} else {
|
||||
err = w.API.AwardUser(airbusTeamId, int64(row.Points*row.Coeff*w.Coeff), row.Reason)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Walker) BalanceScore(score int64, airbusTeamId AirbusUserId) error {
|
||||
// Read current score on other platform
|
||||
stats, err := w.API.GetCurrentStats()
|
||||
if err != nil {
|
||||
fmt.Errorf("unable to retrieve current stats: %w", err)
|
||||
}
|
||||
|
||||
my_session := stats.Data.GetSession(AirbusUUID(w.API.SessionID))
|
||||
if my_session == nil {
|
||||
return fmt.Errorf("session not found")
|
||||
}
|
||||
|
||||
other_team := my_session.GetTeam(AirbusUUID(airbusTeamId))
|
||||
if other_team == nil {
|
||||
return fmt.Errorf("team %q not found", airbusTeamId)
|
||||
}
|
||||
|
||||
other_score := other_team.Score
|
||||
|
||||
// Send diff to the platform
|
||||
if other_score != score {
|
||||
diff := score - other_score
|
||||
return w.API.AwardUser(airbusTeamId, diff, "Équilibrage")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in a new issue