Compare commits
2 Commits
02e1912593
...
c688db5e62
Author | SHA1 | Date | |
---|---|---|---|
c688db5e62 | |||
7df675346c |
@ -14,10 +14,18 @@ import (
|
|||||||
type AirbusAPI struct {
|
type AirbusAPI struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
Token string
|
Token string
|
||||||
SessionID int64
|
SessionUUID string
|
||||||
InsecureSkipVerify bool
|
InsecureSkipVerify bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AirbusAPIResponse struct {
|
||||||
|
Data interface{}
|
||||||
|
CurrentPage int `json:"current_page"`
|
||||||
|
PerPage int `json:"per_page"`
|
||||||
|
LastPage int `json:"last_page"`
|
||||||
|
Total int `json:"total"`
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
var req *http.Request
|
var req *http.Request
|
||||||
var err error
|
var err error
|
||||||
@ -51,7 +59,7 @@ func (a *AirbusAPI) request(method, endpoint string, data io.Reader, out interfa
|
|||||||
if out != nil {
|
if out != nil {
|
||||||
jdec := json.NewDecoder(resp.Body)
|
jdec := json.NewDecoder(resp.Body)
|
||||||
|
|
||||||
if err := jdec.Decode(out); err != nil {
|
if err := jdec.Decode(&AirbusAPIResponse{Data: out}); err != nil {
|
||||||
return fmt.Errorf("an error occurs when trying to decode response: %w", err)
|
return fmt.Errorf("an error occurs when trying to decode response: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,7 +95,7 @@ type AirbusUser struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) GetUsers() (users AirbusUserData, err error) {
|
func (a *AirbusAPI) GetUsers() (users AirbusUserData, err error) {
|
||||||
err = a.request("GET", fmt.Sprintf("/sessions/%d/users", a.SessionID), nil, &users)
|
err = a.request("GET", fmt.Sprintf("/sessions/%s/users", a.SessionUUID), nil, &users)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,7 +145,7 @@ type AirbusChallenge struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) GetChallenges() (challenges AirbusChallengeData, err error) {
|
func (a *AirbusAPI) GetChallenges() (challenges AirbusChallengeData, err error) {
|
||||||
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/challenges", a.SessionID), nil, &challenges)
|
err = a.request("GET", fmt.Sprintf("/v1/sessions/%s/challenges", a.SessionUUID), nil, &challenges)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,24 +165,22 @@ func (a *AirbusAPI) GetChallengeFromName(name string) (*AirbusChallenge, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) ValidateChallengeFromUser(team *AirbusTeam, challengeId AirbusChallengeId) (err error) {
|
func (a *AirbusAPI) ValidateChallengeFromUser(team *AirbusTeam, challengeId AirbusChallengeId) (err error) {
|
||||||
log.Printf("ValidateChallenge: %d, %s, %d", a.SessionID, challengeId.String(), team.Members[0].ID)
|
log.Printf("ValidateChallenge: %s, %s, %s", a.SessionUUID, challengeId.String(), team.Members[0].UUID)
|
||||||
if dryRun {
|
if dryRun {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = a.request("GET", fmt.Sprintf("/v1/sessions/%d/%s/%d/validate", a.SessionID, challengeId.String(), team.Members[0].ID), nil, nil)
|
err = a.request("GET", fmt.Sprintf("/v1/sessions/%s/%s/%s/validate", a.SessionUUID, challengeId.String(), team.Members[0].UUID), nil, nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type AirbusUserAwards struct {
|
type AirbusUserAwards struct {
|
||||||
UserId int64 `json:"gaming_user_id"`
|
|
||||||
Message string `json:"name"`
|
Message string `json:"name"`
|
||||||
Value int64 `json:"value"`
|
Value int64 `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (err error) {
|
func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (err error) {
|
||||||
awards := AirbusUserAwards{
|
awards := AirbusUserAwards{
|
||||||
UserId: team.Members[0].ID,
|
|
||||||
Message: message,
|
Message: message,
|
||||||
Value: value,
|
Value: value,
|
||||||
}
|
}
|
||||||
@ -190,7 +196,7 @@ func (a *AirbusAPI) AwardUser(team *AirbusTeam, value int64, message string) (er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = a.request("POST", fmt.Sprintf("/v1/sessions/%d/awards", a.SessionID), bytes.NewReader(marshalled), nil)
|
err = a.request("POST", fmt.Sprintf("/v1/sessions/%s/%s/awards", a.SessionUUID, team.Members[0].UUID), bytes.NewReader(marshalled), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -77,12 +77,8 @@ func main() {
|
|||||||
fd.Close()
|
fd.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, exists := os.LookupEnv("AIRBUS_SESSIONID"); exists {
|
if v, exists := os.LookupEnv("AIRBUS_SESSION_UUID"); exists {
|
||||||
var err error
|
api.SessionUUID = v
|
||||||
api.SessionID, err = strconv.ParseInt(v, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("AIRBUS_SESSIONID is invalid: ", err.Error())
|
|
||||||
}
|
|
||||||
} else if v, exists := os.LookupEnv("AIRBUS_SESSION_NAME_FILE"); exists {
|
} else if v, exists := os.LookupEnv("AIRBUS_SESSION_NAME_FILE"); exists {
|
||||||
fd, err := os.Open(v)
|
fd, err := os.Open(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -101,15 +97,15 @@ func main() {
|
|||||||
|
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
if session.Name == v {
|
if session.Name == v {
|
||||||
api.SessionID = session.ID
|
api.SessionUUID = session.UUID
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if api.SessionID == 0 {
|
if api.SessionUUID == "" {
|
||||||
log.Fatal("Session ID not found")
|
log.Fatal("Session ID not found")
|
||||||
} else {
|
} else {
|
||||||
log.Println("Session ID discovered: ", api.SessionID)
|
log.Println("Session ID discovered: ", api.SessionUUID)
|
||||||
}
|
}
|
||||||
} else if v, exists := os.LookupEnv("AIRBUS_SESSION_NAME"); exists {
|
} else if v, exists := os.LookupEnv("AIRBUS_SESSION_NAME"); exists {
|
||||||
sessions, err := api.GetSessions()
|
sessions, err := api.GetSessions()
|
||||||
@ -117,17 +113,19 @@ func main() {
|
|||||||
log.Fatal("Unable to retrieve session: ", err)
|
log.Fatal("Unable to retrieve session: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
if session.Name == v {
|
if session.Name == v {
|
||||||
api.SessionID = session.ID
|
api.SessionUUID = session.UUID
|
||||||
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if api.SessionID == 0 {
|
if !found {
|
||||||
log.Fatal("Session ID not found")
|
log.Fatal("Session ID not found")
|
||||||
} else {
|
} else {
|
||||||
log.Println("Session ID discovered: ", api.SessionID)
|
log.Println("Session ID discovered: ", api.SessionUUID)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sessions, err := api.GetSessions()
|
sessions, err := api.GetSessions()
|
||||||
@ -138,7 +136,7 @@ func main() {
|
|||||||
log.Println("Please define your AIRBUS_SESSIONID or AIRBUS_SESSION_NAME.")
|
log.Println("Please define your AIRBUS_SESSIONID or AIRBUS_SESSION_NAME.")
|
||||||
log.Println("Existing sessions are:")
|
log.Println("Existing sessions are:")
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
log.Printf(" - %d: %q", session.ID, session.Name)
|
log.Printf(" - %s: %q", session.UUID, session.Name)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -154,13 +152,14 @@ func main() {
|
|||||||
log.Println("Unable to retrieve teams:", err)
|
log.Println("Unable to retrieve teams:", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
log.Println(teams)
|
||||||
|
|
||||||
fmt.Println("## Airbus' registered teams:")
|
fmt.Println("## Airbus' registered teams:")
|
||||||
fmt.Println("-------------------------------------------")
|
fmt.Println("----------------------------------------------------------------------------------")
|
||||||
fmt.Println(" ID | Name | Nb. | Score | Rank")
|
fmt.Println(" UUID | Name | Nb. | Score | Rank")
|
||||||
fmt.Println("-------------------------------------------")
|
fmt.Println("----------------------------------------------------------------------------------")
|
||||||
for _, team := range teams {
|
for _, team := range teams {
|
||||||
fmt.Printf("% 2d | % 15s | % 3d | % 5d | % 3d\n", team.ID, team.Name, len(team.Members), team.Score, team.Rank)
|
fmt.Printf(" %s | % 20s | % 3d | % 5d | % 3d\n", team.UUID, team.Name, len(team.Members), team.Score, team.Rank)
|
||||||
}
|
}
|
||||||
case "rank":
|
case "rank":
|
||||||
teams, err := api.GetTeams()
|
teams, err := api.GetTeams()
|
||||||
@ -178,11 +177,11 @@ func main() {
|
|||||||
sort.Sort(sort.Reverse(ByScore(ranking)))
|
sort.Sort(sort.Reverse(ByScore(ranking)))
|
||||||
|
|
||||||
fmt.Println("## Airbus' ranking:")
|
fmt.Println("## Airbus' ranking:")
|
||||||
fmt.Println("-------------------------------------")
|
fmt.Println("----------------------------------------------------------------------------")
|
||||||
fmt.Println(" Rank | Name | ID | Score")
|
fmt.Println(" Rank | Name | UUID | Score")
|
||||||
fmt.Println("-------------------------------------")
|
fmt.Println("----------------------------------------------------------------------------")
|
||||||
for _, team := range ranking {
|
for _, team := range ranking {
|
||||||
fmt.Printf("% 5d | % 15s |% 3d | % 5d\n", team.Rank, team.Name, team.ID, team.Score)
|
fmt.Printf("% 5d | % 20s | %s | % 5d\n", team.Rank, team.Name, team.UUID, team.Score)
|
||||||
}
|
}
|
||||||
case "get":
|
case "get":
|
||||||
teams, err := api.GetTeams()
|
teams, err := api.GetTeams()
|
||||||
@ -191,22 +190,18 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
teamid, err := strconv.ParseInt(args[1], 10, 64)
|
teamid := args[1]
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, team := range teams {
|
for _, team := range teams {
|
||||||
if team.ID == teamid {
|
if team.UUID == 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)
|
fmt.Printf("## Airbus' registered team %s:\n\nUUID: %s\nName: %s\nScore: %d\nRank: %d\nMembers:\n", teamid, team.UUID, team.Name, team.Score, team.Rank)
|
||||||
for _, member := range team.Members {
|
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)
|
fmt.Printf(" - UUID: %s\n Name: %s\n Nickname: %s\n E-mail: %s\n", member.UUID, member.Name, member.Nickname, member.EMail)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
|
fmt.Printf("Team %s not found. Use 'list' to view all existing teams\n", teamid)
|
||||||
case "award":
|
case "award":
|
||||||
if len(args) < 3 {
|
if len(args) < 3 {
|
||||||
fmt.Println("award <TEAM_ID> <VALUE> <MESSAGE>")
|
fmt.Println("award <TEAM_ID> <VALUE> <MESSAGE>")
|
||||||
@ -219,11 +214,7 @@ func main() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
teamid, err := strconv.ParseInt(args[1], 10, 64)
|
teamid := args[1]
|
||||||
if err != nil {
|
|
||||||
log.Println("Invalid team id", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
value, err := strconv.ParseInt(args[2], 10, 64)
|
value, err := strconv.ParseInt(args[2], 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -232,7 +223,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, team := range teams {
|
for _, team := range teams {
|
||||||
if team.ID == teamid {
|
if team.UUID == teamid {
|
||||||
err = api.AwardUser(&team, value, strings.Join(args[3:], " "))
|
err = api.AwardUser(&team, value, strings.Join(args[3:], " "))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to award team:", err)
|
log.Println("Unable to award team:", err)
|
||||||
@ -243,7 +234,7 @@ func main() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("Team %d not found. Use 'list' to view all existing teams\n", teamid)
|
fmt.Printf("Team %s not found. Use 'list' to view all existing teams\n", teamid)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -1,12 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import ()
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Status string `json:"status"`
|
QuestionValidation string `json:"question_validation"`
|
||||||
ID int64 `json:"id"`
|
Status string `json:"status"`
|
||||||
Mode string `json:"mode"`
|
UUID string `json:"uuid"`
|
||||||
|
Mode string `json:"mode"`
|
||||||
|
Difficulty int `json:"difficulty"`
|
||||||
|
StartedAt time.Time `json:"start_at"`
|
||||||
|
FinishAt time.Time `json:"finish_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AirbusAPI) GetSessions() (ret []Session, err error) {
|
func (a *AirbusAPI) GetSessions() (ret []Session, err error) {
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AirbusTeam struct {
|
type AirbusTeam struct {
|
||||||
ID int64 `json:"id"`
|
UUID string `json:"session_uuid"`
|
||||||
Members []TeamMember `json:"members"`
|
Members []TeamMember `json:"members"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Score int64 `json:"score"`
|
Score int64 `json:"score"`
|
||||||
@ -13,23 +13,21 @@ type AirbusTeam struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TeamMember struct {
|
type TeamMember struct {
|
||||||
ID int64 `json:"id"`
|
UUID string `json:"session_uuid"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Nickname string `json:"nickname"`
|
Nickname string `json:"nickname"`
|
||||||
EMail string `json:"email"`
|
EMail string `json:"email"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type airbusDataTeam struct {
|
type airbusDataTeam []AirbusTeam
|
||||||
Data []AirbusTeam `json:"data"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *AirbusAPI) GetTeams() ([]AirbusTeam, error) {
|
func (a *AirbusAPI) GetTeams() ([]AirbusTeam, error) {
|
||||||
var data airbusDataTeam
|
var data airbusDataTeam
|
||||||
err := a.request("GET", fmt.Sprintf("/v1/sessions/%d/teams", a.SessionID), nil, &data)
|
err := a.request("GET", fmt.Sprintf("/v1/sessions/%s/teams", a.SessionUUID), nil, &data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else {
|
} else {
|
||||||
return data.Data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"name": "#D\u00e9fendonsEnsemble",
|
"name": "#D\u00e9fendonsEnsemble",
|
||||||
|
"external_id": "89b35c67-1aa4-43e2-b9da-7f9e7fdfa649",
|
||||||
"score": 0
|
"score": 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -39,7 +39,7 @@ func (w *Walker) fetchTeams() error {
|
|||||||
|
|
||||||
for tid, team := range w.Teams {
|
for tid, team := range w.Teams {
|
||||||
for i, t := range teams {
|
for i, t := range teams {
|
||||||
if team.Name == t.Name || team.ExternalId == t.Name {
|
if team.Name == t.Name || team.ExternalId == t.Name || team.ExternalId == t.UUID {
|
||||||
w.TeamBindings[tid] = &teams[i]
|
w.TeamBindings[tid] = &teams[i]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user