2022-06-07 14:06:36 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-06-07 15:04:07 +00:00
|
|
|
"log"
|
2022-06-07 14:06:36 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"srs.epita.fr/fic-server/libfic"
|
|
|
|
)
|
|
|
|
|
2023-04-03 10:04:48 +00:00
|
|
|
var (
|
|
|
|
noValidateChallenge bool
|
|
|
|
)
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
type Walker struct {
|
2022-06-07 15:04:07 +00:00
|
|
|
LastSync map[AirbusUserId]time.Time
|
2022-06-07 14:06:36 +00:00
|
|
|
Exercices AirbusExercicesBindings
|
|
|
|
Teams map[string]fic.ExportedTeam
|
|
|
|
API AirbusAPI
|
|
|
|
Coeff float64
|
|
|
|
}
|
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
func (w *Walker) treat(path string) {
|
|
|
|
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 {
|
|
|
|
log.Println("Unable to open my.json:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fdmy.Close()
|
2022-06-07 14:06:36 +00:00
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
teammy, err := fic.ReadMyJSON(fdmy)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to parse my.json:", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-07 14:06:36 +00:00
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
airbusTeamId := NewAirbusUserId(w.Teams[fmt.Sprintf("%d", teammy.Id)].ExternalId)
|
2022-06-07 14:06:36 +00:00
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
// Treat score grid
|
2022-06-07 22:09:49 +00:00
|
|
|
/*err = w.TreatScoreGrid(path, airbusTeamId)
|
2022-06-07 15:04:07 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to treat score grid:", err)
|
|
|
|
return
|
2022-06-07 22:09:49 +00:00
|
|
|
}*/
|
2022-06-07 14:06:36 +00:00
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
// Balance scores
|
|
|
|
err = w.BalanceScore(int64(float64(teammy.Points)*w.Coeff), airbusTeamId)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to balance score:", err)
|
|
|
|
return
|
2022-06-07 14:06:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-07 15:04:07 +00:00
|
|
|
}
|
2022-06-07 14:06:36 +00:00
|
|
|
|
2022-06-07 22:09:49 +00:00
|
|
|
func (w *Walker) WalkScoreSync(path string, d os.DirEntry, err error) error {
|
|
|
|
if filepath.Base(path) == "scores.json" {
|
|
|
|
w.treat(path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
func (w *Walker) WalkScore(path string, d os.DirEntry, err error) error {
|
|
|
|
if filepath.Base(path) == "scores.json" {
|
|
|
|
go w.treat(path)
|
|
|
|
}
|
2022-06-07 14:06:36 +00:00
|
|
|
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
|
2022-06-07 15:04:07 +00:00
|
|
|
maxts := w.LastSync[airbusTeamId]
|
2022-06-07 14:06:36 +00:00
|
|
|
for _, row := range teamscores {
|
2022-06-07 15:04:07 +00:00
|
|
|
if row.Time.After(maxts) {
|
|
|
|
maxts = row.Time
|
|
|
|
}
|
|
|
|
if row.Time.After(w.LastSync[airbusTeamId]) {
|
2023-04-03 10:04:48 +00:00
|
|
|
if !noValidateChallenge && row.Reason == "Validation" {
|
2022-06-07 14:06:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 15:04:07 +00:00
|
|
|
w.LastSync[airbusTeamId] = maxts
|
|
|
|
|
2022-06-07 14:06:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:09:49 +00:00
|
|
|
my_session := stats.Data.GetSession(AirbusUUID(w.API.SessionUUID))
|
2022-06-07 14:06:36 +00:00
|
|
|
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
|
|
|
|
}
|