challenge-sync-airbus: Ready for 2024
This commit is contained in:
parent
5a6d9047c2
commit
ac966f9023
5 changed files with 275 additions and 117 deletions
|
@ -1,22 +1,21 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type AirbusAPI struct {
|
type AirbusAPI struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
Token string
|
Token string
|
||||||
SessionID int64
|
SessionID int64
|
||||||
SessionUUID string
|
InsecureSkipVerify bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
|
func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interface{}) error {
|
||||||
|
@ -25,7 +24,7 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: a.InsecureSkipVerify},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +38,8 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Add("Authorization", "Bearer "+a.Token)
|
req.Header.Add("Authorization", "Bearer "+a.Token)
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Accept", "application/json")
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -179,68 +179,21 @@ func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (er
|
||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("AwardUser: %v", awards)
|
var marshalled []byte
|
||||||
|
marshalled, err = json.Marshal(awards)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("AwardUser: %s", marshalled)
|
||||||
if dryRun {
|
if dryRun {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := url.Values{}
|
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), bytes.NewReader(marshalled), nil)
|
||||||
data.Set("gaming_user_id", fmt.Sprintf("%d", awards.UserId))
|
|
||||||
data.Set("name", awards.Message)
|
|
||||||
data.Set("value", fmt.Sprintf("%d", awards.Value))
|
|
||||||
|
|
||||||
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), strings.NewReader(data.Encode()), nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type AirbusStats struct {
|
|
||||||
Data AirbusStatsData `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AirbusStatsData struct {
|
|
||||||
BySessions []AirbusStatsSession `json:"by_sessions"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s AirbusStatsData) GetSession(sessionId AirbusUUID) *AirbusStatsSession {
|
|
||||||
for _, session := range s.BySessions {
|
|
||||||
if session.UUID == sessionId {
|
|
||||||
return &session
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type AirbusUUID string
|
|
||||||
|
|
||||||
type AirbusStatsSession struct {
|
|
||||||
UUID AirbusUUID `json:"uuid"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Duration string `json:"duration"`
|
|
||||||
TeamStats []AirbusTeamStats `json:"team_stats"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s AirbusStatsSession) GetTeam(teamId AirbusUUID) *AirbusTeamStats {
|
|
||||||
for _, team := range s.TeamStats {
|
|
||||||
if team.UUID == teamId {
|
|
||||||
return &team
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type AirbusTeamStats struct {
|
|
||||||
UUID AirbusUUID `json:"uuid"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Score int64 `json:"score"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AirbusAPI) GetCurrentStats() (stats AirbusStats, err error) {
|
|
||||||
err = a.request("GET", "/stats", nil, &stats)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,13 +2,16 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -27,6 +30,8 @@ func main() {
|
||||||
flag.BoolVar(&dryRun, "dry-run", dryRun, "Don't perform any write action, just display")
|
flag.BoolVar(&dryRun, "dry-run", dryRun, "Don't perform any write action, just display")
|
||||||
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
|
flag.BoolVar(&skipInitialSync, "skipinitialsync", skipInitialSync, "Skip the initial synchronization")
|
||||||
flag.BoolVar(&noValidateChallenge, "no-validate-challenge", noValidateChallenge, "Consider challenge validation as a standard award (if each exercice hasn't been imported on their side)")
|
flag.BoolVar(&noValidateChallenge, "no-validate-challenge", noValidateChallenge, "Consider challenge validation as a standard award (if each exercice hasn't been imported on their side)")
|
||||||
|
skipVerify := flag.Bool("skip-tls-verify", false, "Allow not verified certificates (INSECURE!)")
|
||||||
|
tsFromFile := flag.Bool("timestamp-from-file", false, "Load timestamp matching for score from filesystem instead of the API")
|
||||||
daemon := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
|
daemon := flag.Bool("watch", false, "Enable daemon mode by watching the directory")
|
||||||
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
|
tspath := flag.String("timestamp-file", "./REMOTE/timestamp", "Path to the file storing the last timestamp")
|
||||||
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
|
exercicespath := flag.String("exercices-file", "./REMOTE/exercices-bindings.json", "Path to the file containing the ID bindings")
|
||||||
|
@ -34,12 +39,20 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
api := AirbusAPI{
|
api := AirbusAPI{
|
||||||
BaseURL: "https://portal.european-cybercup.lan/api",
|
BaseURL: "https://portal.european-cybercup.lan/api",
|
||||||
|
InsecureSkipVerify: *skipVerify,
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
if v, exists := os.LookupEnv("AIRBUS_BASEURL"); exists {
|
||||||
api.BaseURL = v
|
api.BaseURL = v
|
||||||
}
|
}
|
||||||
|
if v, exists := os.LookupEnv("AIRBUS_SKIP_TLS_VERIFY"); exists {
|
||||||
|
var err error
|
||||||
|
api.InsecureSkipVerify, err = strconv.ParseBool(v)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Unable to parse boolean value in AIRBUS_SKIP_TLS_VERIFY:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
|
if v, exists := os.LookupEnv("AIRBUS_TOKEN"); exists {
|
||||||
api.Token = v
|
api.Token = v
|
||||||
}
|
}
|
||||||
|
@ -68,39 +81,156 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
log.Println("Session ID discovered: ", api.SessionID)
|
log.Println("Session ID discovered: ", api.SessionID)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
sessions, err := api.GetSessions()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Unable to retrieve session (check your credentials!): ", err)
|
||||||
|
}
|
||||||
|
|
||||||
if v, exists := os.LookupEnv("AIRBUS_SESSIONUUID"); exists {
|
log.Println("Please define your AIRBUS_SESSIONID or AIRBUS_SESSION_NAME.")
|
||||||
api.SessionUUID = v
|
log.Println("Existing sessions are:")
|
||||||
|
for _, session := range sessions {
|
||||||
|
log.Printf(" - %d: %q", session.ID, session.Name)
|
||||||
|
}
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetPrefix("[challenge-sync-airbus] ")
|
log.SetPrefix("[challenge-sync-airbus] ")
|
||||||
|
|
||||||
|
if flag.NArg() > 0 {
|
||||||
|
args := flag.Args()
|
||||||
|
switch args[0] {
|
||||||
|
case "list":
|
||||||
|
teams, err := api.GetTeams()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to retrieve teams:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("## Airbus' registered teams:")
|
||||||
|
fmt.Println("-------------------------------------------")
|
||||||
|
fmt.Println(" ID | Name | Nb. | Score | Rank")
|
||||||
|
fmt.Println("-------------------------------------------")
|
||||||
|
for _, team := range teams {
|
||||||
|
fmt.Printf("% 2d | % 15s | % 3d | % 5d | % 3d\n", team.ID, team.Name, len(team.Members), team.Score, team.Rank)
|
||||||
|
}
|
||||||
|
case "rank":
|
||||||
|
teams, err := api.GetTeams()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to retrieve teams:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
ranking := []*AirbusTeam{}
|
||||||
|
for _, team := range teams {
|
||||||
|
tmp := team
|
||||||
|
ranking = append(ranking, &tmp)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(sort.Reverse(ByScore(ranking)))
|
||||||
|
|
||||||
|
fmt.Println("## Airbus' ranking:")
|
||||||
|
fmt.Println("-------------------------------------")
|
||||||
|
fmt.Println(" Rank | Name | ID | Score")
|
||||||
|
fmt.Println("-------------------------------------")
|
||||||
|
for _, team := range ranking {
|
||||||
|
fmt.Printf("% 5d | % 15s |% 3d | % 5d\n", team.Rank, team.Name, team.ID, team.Score)
|
||||||
|
}
|
||||||
|
case "get":
|
||||||
|
teams, err := api.GetTeams()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to retrieve teams:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
teamid, err := strconv.ParseInt(args[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, team := range teams {
|
||||||
|
if team.ID == teamid {
|
||||||
|
fmt.Printf("## Airbus' registered team %d:\n\nID: %d\nName: %s\nScore: %d\nRank: %d\nMembers:\n", teamid, team.ID, team.Name, team.Score, team.Rank)
|
||||||
|
for _, member := range team.Members {
|
||||||
|
fmt.Printf(" - ID: %d\n Name: %s\n Nickname: %s\n E-mail: %s\n", member.ID, member.Name, member.Nickname, member.EMail)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
|
||||||
|
case "award":
|
||||||
|
if len(args) < 3 {
|
||||||
|
fmt.Println("award <TEAM_ID> <VALUE> <MESSAGE>")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
teams, err := api.GetTeams()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to retrieve teams:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
teamid, err := strconv.ParseInt(args[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Invalid team id", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
value, err := strconv.ParseInt(args[2], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Invalid award value:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, team := range teams {
|
||||||
|
if team.ID == teamid {
|
||||||
|
err = api.AwardUser(&team, value, strings.Join(args[3:], " "))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to award team:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Team awarded")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
TeamsDir = path.Clean(TeamsDir)
|
TeamsDir = path.Clean(TeamsDir)
|
||||||
|
|
||||||
// Load the timestamp
|
w := Walker{
|
||||||
ts, err := loadTS(*tspath)
|
API: api,
|
||||||
if err != nil {
|
Coeff: *coeff,
|
||||||
log.Fatal("Unable to open timestamp file: ", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load teams.json
|
// Load teams.json
|
||||||
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
w.Teams, err = getTeams(filepath.Join(TeamsDir, "teams.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to open teams bindings file: ", err.Error())
|
log.Fatal("Unable to open teams bindings file: ", err.Error())
|
||||||
}
|
}
|
||||||
log.Println("Team bindings loaded: ", len(teamsbindings))
|
log.Println("Team bindings loaded: ", len(w.Teams))
|
||||||
|
|
||||||
w := Walker{
|
// Fetch teams from Airbus API
|
||||||
LastSync: ts,
|
|
||||||
Teams: teamsbindings,
|
|
||||||
API: api,
|
|
||||||
Coeff: *coeff,
|
|
||||||
}
|
|
||||||
if err = w.fetchTeams(); err != nil {
|
if err = w.fetchTeams(); err != nil {
|
||||||
log.Fatal("Unable to fetch Airbus teams: ", err.Error())
|
log.Fatal("Unable to fetch Airbus teams: ", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load the timestamp
|
||||||
|
if *tsFromFile {
|
||||||
|
w.LastSync, err = loadTS(*tspath)
|
||||||
|
} else {
|
||||||
|
w.LastSync, err = loadTSFromAPI(w.TeamBindings)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Unable to open timestamp file: ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
if !noValidateChallenge {
|
if !noValidateChallenge {
|
||||||
// Load exercices bindings
|
// Load exercices bindings
|
||||||
w.Exercices, err = ReadExercicesBindings(*exercicespath)
|
w.Exercices, err = ReadExercicesBindings(*exercicespath)
|
||||||
|
@ -110,6 +240,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !skipInitialSync {
|
if !skipInitialSync {
|
||||||
|
log.Println("Doing initial score balance")
|
||||||
err = w.BalanceScores()
|
err = w.BalanceScores()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Something goes wrong during score balance: ", err.Error())
|
log.Println("Something goes wrong during score balance: ", err.Error())
|
||||||
|
@ -120,9 +251,9 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to save timestamp file: ", err.Error())
|
log.Fatal("Unable to save timestamp file: ", err.Error())
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("initial sync done")
|
log.Println("initial sync done")
|
||||||
|
}
|
||||||
|
|
||||||
if daemon != nil && *daemon {
|
if daemon != nil && *daemon {
|
||||||
// Watch teams.json and scores.json
|
// Watch teams.json and scores.json
|
||||||
|
@ -145,7 +276,12 @@ func main() {
|
||||||
interrupt3 := make(chan os.Signal, 1)
|
interrupt3 := make(chan os.Signal, 1)
|
||||||
signal.Notify(interrupt3, syscall.SIGUSR2)
|
signal.Notify(interrupt3, syscall.SIGUSR2)
|
||||||
|
|
||||||
ticker := time.NewTicker(5 * time.Second)
|
var ticker *time.Ticker
|
||||||
|
if *tsFromFile {
|
||||||
|
ticker = time.NewTicker(5 * time.Second)
|
||||||
|
} else {
|
||||||
|
ticker = time.NewTicker(5 * time.Minute)
|
||||||
|
}
|
||||||
|
|
||||||
watchedNotify := fsnotify.Create
|
watchedNotify := fsnotify.Create
|
||||||
|
|
||||||
|
@ -156,17 +292,27 @@ func main() {
|
||||||
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to open teams bindings file: ", err.Error())
|
log.Println("Unable to open teams bindings file: ", err.Error())
|
||||||
return
|
} else {
|
||||||
|
w.Teams = teamsbindings
|
||||||
}
|
}
|
||||||
w.Teams = teamsbindings
|
|
||||||
if err = w.fetchTeams(); err != nil {
|
if err = w.fetchTeams(); err != nil {
|
||||||
log.Fatal("Unable to fetch teams: ", err.Error())
|
log.Println("Unable to fetch teams: ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !*tsFromFile {
|
||||||
|
ts, err := loadTSFromAPI(w.TeamBindings)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to refresh timestamp: ", err.Error())
|
||||||
|
} else {
|
||||||
|
w.LastSync = ts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// save current timestamp for teams
|
// save current timestamp for teams
|
||||||
err = saveTS(*tspath, w.LastSync)
|
err = saveTS(*tspath, w.LastSync)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to save timestamp file: ", err.Error())
|
log.Println("Unable to save timestamp file: ", err.Error())
|
||||||
}
|
}
|
||||||
log.Println("SIGHUP treated.")
|
log.Println("SIGHUP treated.")
|
||||||
case <-interrupt2:
|
case <-interrupt2:
|
||||||
|
@ -180,7 +326,7 @@ func main() {
|
||||||
// save current timestamp for teams
|
// save current timestamp for teams
|
||||||
err = saveTS(*tspath, w.LastSync)
|
err = saveTS(*tspath, w.LastSync)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to save timestamp file: ", err.Error())
|
log.Println("Unable to save timestamp file: ", err.Error())
|
||||||
}
|
}
|
||||||
log.Println("SIGUSR1 treated.")
|
log.Println("SIGUSR1 treated.")
|
||||||
case <-interrupt3:
|
case <-interrupt3:
|
||||||
|
@ -189,17 +335,40 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to open teams bindings file: ", err.Error())
|
log.Println("Unable to open teams bindings file: ", err.Error())
|
||||||
return
|
return
|
||||||
}
|
} else {
|
||||||
w.Teams = teamsbindings
|
w.Teams = teamsbindings
|
||||||
if err = w.fetchTeams(); err != nil {
|
|
||||||
log.Fatal("Unable to fetch teams: ", err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME
|
if err = w.fetchTeams(); err != nil {
|
||||||
|
log.Println("Unable to fetch teams: ", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !*tsFromFile {
|
||||||
|
ts, err := loadTSFromAPI(w.TeamBindings)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to refresh timestamp: ", err.Error())
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
w.LastSync = ts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = w.BalanceScores()
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Unable to balance scores: ", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
log.Println("SIGUSR2 treated.")
|
log.Println("SIGUSR2 treated.")
|
||||||
case ev := <-watcher.Events:
|
case ev := <-watcher.Events:
|
||||||
if d, err := os.Lstat(ev.Name); err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
|
d, err := os.Lstat(ev.Name)
|
||||||
|
|
||||||
|
if err == nil && watchedNotify == fsnotify.Create && ev.Op&fsnotify.Write == fsnotify.Write {
|
||||||
|
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
||||||
|
watchedNotify = fsnotify.Write
|
||||||
|
}
|
||||||
|
|
||||||
|
if err == nil && ev.Op&fsnotify.Create == fsnotify.Create && d.Mode().IsDir() && d.Mode()&os.ModeSymlink == 0 && d.Name() != ".tmp" {
|
||||||
// Register new subdirectory
|
// Register new subdirectory
|
||||||
if err := watchsubdir(watcher, ev.Name); err != nil {
|
if err := watchsubdir(watcher, ev.Name); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -208,22 +377,24 @@ func main() {
|
||||||
if *debugINotify {
|
if *debugINotify {
|
||||||
log.Println("Treating event:", ev, "for", ev.Name)
|
log.Println("Treating event:", ev, "for", ev.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if watchedNotify == fsnotify.Write {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
if filepath.Base(ev.Name) == "scores.json" {
|
if filepath.Base(ev.Name) == "scores.json" {
|
||||||
go w.treat(ev.Name)
|
go w.treat(ev.Name)
|
||||||
} else if filepath.Base(ev.Name) == "teams.json" {
|
} else if filepath.Base(ev.Name) == "teams.json" {
|
||||||
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
teamsbindings, err := getTeams(filepath.Join(TeamsDir, "teams.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to open teams bindings file: ", err.Error())
|
log.Println("Unable to open teams bindings file: ", err.Error())
|
||||||
return
|
} else {
|
||||||
|
w.Teams = teamsbindings
|
||||||
}
|
}
|
||||||
w.Teams = teamsbindings
|
|
||||||
if err = w.fetchTeams(); err != nil {
|
if err = w.fetchTeams(); err != nil {
|
||||||
log.Fatal("Unable to fetch teams: ", err.Error())
|
log.Println("Unable to fetch teams: ", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if err == nil && ev.Op&fsnotify.Write == fsnotify.Write {
|
|
||||||
log.Println("FSNOTIFY WRITE SEEN. Prefer looking at them, as it appears files are not atomically moved.")
|
|
||||||
watchedNotify = fsnotify.Write
|
|
||||||
} else if err == nil && *debugINotify {
|
} else if err == nil && *debugINotify {
|
||||||
log.Println("Skipped event:", ev, "for", ev.Name)
|
log.Println("Skipped event:", ev, "for", ev.Name)
|
||||||
}
|
}
|
||||||
|
@ -233,7 +404,7 @@ func main() {
|
||||||
// save current timestamp for teams
|
// save current timestamp for teams
|
||||||
err = saveTS(*tspath, w.LastSync)
|
err = saveTS(*tspath, w.LastSync)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Unable to save timestamp file: ", err.Error())
|
log.Println("Unable to save timestamp file: ", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ type AirbusTeam struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Members []TeamMember `json:"members"`
|
Members []TeamMember `json:"members"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
Score int64 `json:"score"`
|
||||||
|
Rank int `json:"rank"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type TeamMember struct {
|
type TeamMember struct {
|
||||||
|
@ -30,3 +32,15 @@ func (a *AirbusAPI) GetTeams() ([]AirbusTeam, error) {
|
||||||
return data.Data, nil
|
return data.Data, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ByRank []*AirbusTeam
|
||||||
|
|
||||||
|
func (a ByRank) Len() int { return len(a) }
|
||||||
|
func (a ByRank) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByRank) Less(i, j int) bool { return a[i].Rank < a[j].Rank }
|
||||||
|
|
||||||
|
type ByScore []*AirbusTeam
|
||||||
|
|
||||||
|
func (a ByScore) Len() int { return len(a) }
|
||||||
|
func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||||
|
func (a ByScore) Less(i, j int) bool { return a[i].Score < a[j].Score }
|
||||||
|
|
|
@ -32,6 +32,20 @@ func loadTS(tspath string) (timestamp map[string]*TSValue, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadTSFromAPI(teams map[string]*AirbusTeam) (timestamp map[string]*TSValue, err error) {
|
||||||
|
now := time.Now()
|
||||||
|
timestamp = map[string]*TSValue{}
|
||||||
|
|
||||||
|
for _, team := range teams {
|
||||||
|
timestamp[team.Name] = &TSValue{
|
||||||
|
Time: now,
|
||||||
|
Score: team.Score,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func saveTS(tspath string, ts map[string]*TSValue) error {
|
func saveTS(tspath string, ts map[string]*TSValue) error {
|
||||||
if dryRun {
|
if dryRun {
|
||||||
tmp := map[string]TSValue{}
|
tmp := map[string]TSValue{}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
@ -154,17 +155,17 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Found all new entries
|
// Found all new entries
|
||||||
maxts := &TSValue{
|
maxts := TSValue{
|
||||||
Time: time.Time{},
|
Time: time.Time{},
|
||||||
}
|
}
|
||||||
if ts, ok := w.LastSync[airbusTeam.Name]; ok {
|
ts, ok := w.LastSync[airbusTeam.Name]
|
||||||
maxts = ts
|
if ok {
|
||||||
|
maxts = *ts
|
||||||
|
} else {
|
||||||
|
ts = &TSValue{Time: time.Time{}}
|
||||||
}
|
}
|
||||||
for _, row := range teamscores {
|
for _, row := range teamscores {
|
||||||
if row.Time.After(maxts.Time) {
|
if row.Time.After(ts.Time) {
|
||||||
maxts.Time = row.Time
|
|
||||||
}
|
|
||||||
if ts, ok := w.LastSync[airbusTeam.Name]; !ok || row.Time.After(ts.Time) {
|
|
||||||
if !noValidateChallenge && row.Reason == "Validation" {
|
if !noValidateChallenge && row.Reason == "Validation" {
|
||||||
err = w.API.ValidateChallengeFromUser(airbusTeam, w.Exercices[row.IdExercice])
|
err = w.API.ValidateChallengeFromUser(airbusTeam, w.Exercices[row.IdExercice])
|
||||||
} else {
|
} else {
|
||||||
|
@ -177,20 +178,18 @@ func (w *Walker) TreatScoreGrid(path string, airbusTeam *AirbusTeam) error {
|
||||||
|
|
||||||
maxts.Score += int64(row.Points * row.Coeff * w.Coeff)
|
maxts.Score += int64(row.Points * row.Coeff * w.Coeff)
|
||||||
}
|
}
|
||||||
|
if row.Time.After(maxts.Time) {
|
||||||
|
maxts.Time = row.Time
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
w.LastSync[airbusTeam.Name] = maxts
|
w.LastSync[airbusTeam.Name] = &maxts
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Walker) BalanceScores() error {
|
func (w *Walker) BalanceScores() error {
|
||||||
for team, ts := range w.LastSync {
|
for team_id, team := range w.Teams {
|
||||||
team_id, ok := w.RevTeams[team]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
myteam, err := w.loadMyFile(filepath.Join(TeamsDir, team_id, "my.json"))
|
myteam, err := w.loadMyFile(filepath.Join(TeamsDir, team_id, "my.json"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to open %s/my.json: %w", team_id, err)
|
return fmt.Errorf("Unable to open %s/my.json: %w", team_id, err)
|
||||||
|
@ -198,13 +197,20 @@ func (w *Walker) BalanceScores() error {
|
||||||
|
|
||||||
airbusTeam := w.TeamBindings[fmt.Sprintf("%d", myteam.Id)]
|
airbusTeam := w.TeamBindings[fmt.Sprintf("%d", myteam.Id)]
|
||||||
|
|
||||||
if ts.Score != myteam.Points*int64(w.Coeff) {
|
expected_score := int64(math.Floor(float64(myteam.Points) * w.Coeff))
|
||||||
err := w.API.AwardUser(airbusTeam, myteam.Points*int64(w.Coeff)-ts.Score, "Équilibrage")
|
if airbusTeam == nil {
|
||||||
|
log.Printf("Skip team %q (tid=%d): no binding found", myteam.Name, myteam.Id)
|
||||||
|
} else if airbusTeam.Score != expected_score {
|
||||||
|
err := w.API.AwardUser(airbusTeam, expected_score-airbusTeam.Score, "Équilibrage")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Unable to open %s/my.json: %w", team, err)
|
return fmt.Errorf("Unable to open %s/my.json: %w", team, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
w.LastSync[airbusTeam.Name].Score = myteam.Points * int64(w.Coeff)
|
if _, ok := w.LastSync[airbusTeam.Name]; !ok {
|
||||||
|
w.LastSync[airbusTeam.Name] = &TSValue{}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.LastSync[airbusTeam.Name].Score = expected_score
|
||||||
w.LastSync[airbusTeam.Name].Time = time.Now()
|
w.LastSync[airbusTeam.Name].Time = time.Now()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue