From 9a8253c994f9081d0b60235303c087b49aae5308 Mon Sep 17 00:00:00 2001 From: Jon Date: Thu, 22 Feb 2024 18:42:16 -0800 Subject: [PATCH] Only give role nickname to the POC, not to assistants / team members --- internal/api/controller.go | 2 +- internal/api/facility.go | 50 ++++++++++++++++++++++++++++++++++++++ internal/bot/facility.go | 32 ++++++++++++++++++++++++ internal/bot/nickname.go | 43 ++++++++++++++++++++++---------- 4 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 internal/api/facility.go create mode 100644 internal/bot/facility.go diff --git a/internal/api/controller.go b/internal/api/controller.go index 99ea8c3..a355aa2 100644 --- a/internal/api/controller.go +++ b/internal/api/controller.go @@ -12,7 +12,7 @@ type ControllerDataWrapper struct { } type ControllerData struct { - CID uint `json:"cid"` + CID uint64 `json:"cid"` FirstName string `json:"fname"` LastName string `json:"lname"` Email *string `json:"email"` diff --git a/internal/api/facility.go b/internal/api/facility.go new file mode 100644 index 0000000..b0484e5 --- /dev/null +++ b/internal/api/facility.go @@ -0,0 +1,50 @@ +package api + +import ( + "encoding/json" + "errors" + "io/ioutil" +) + +type FacilityData struct { + Id string `json:"id"` + Name string `json:"name"` + AirTrafficManagerCID uint64 `json:"atm"` + DeputyAirTrafficManagerCID uint64 `json:"datm"` + TrainingAdministratorCID uint64 `json:"ta"` + EventCoordinatorCID uint64 `json:"ec"` + FacilityEngineerCID uint64 `json:"fe"` + WebMasterCID uint64 `json:"wm"` +} + +type FacilityListWrapper struct { + Data []FacilityData `json:"data"` +} + +func GetFacilities() ([]FacilityData, error) { + response, err := Get("/facility/") + if err != nil { + return nil, err + } + if response.StatusCode == 404 { + return nil, nil + } + if response.StatusCode != 200 { + return nil, errors.New("HTTP Error when fetching facility data") + } + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, err + } + var wrapper FacilityListWrapper + err = json.Unmarshal(responseData, &wrapper) + + if err != nil { + return nil, err + } + out := make([]FacilityData, len(wrapper.Data)) + for _, value := range wrapper.Data { + out = append(out, value) + } + return out, nil +} diff --git a/internal/bot/facility.go b/internal/bot/facility.go new file mode 100644 index 0000000..668a72e --- /dev/null +++ b/internal/bot/facility.go @@ -0,0 +1,32 @@ +package bot + +import ( + "github.com/VATUSA/discord-bot-v3/internal/api" + "log" + "strings" + "time" +) + +var FacilityDataByIdMap = make(map[string]api.FacilityData) +var LastFacilityDataLoad *time.Time = nil + +const FacilityDataCacheDuration = 5 * time.Minute + +func LoadFacilityData() { + facilityData, err := api.GetFacilities() + if err != nil { + log.Printf("Error fetching facility data: %v", err) + return + } + for _, facility := range facilityData { + FacilityDataByIdMap[strings.ToUpper(facility.Id)] = facility + } +} + +func GetFacilityData(facilityId string) api.FacilityData { + if LastFacilityDataLoad == nil || time.Now().After(LastFacilityDataLoad.Add(FacilityDataCacheDuration)) { + LoadFacilityData() + } + facility := FacilityDataByIdMap[facilityId] + return facility +} diff --git a/internal/bot/nickname.go b/internal/bot/nickname.go index 46e1979..1f27db3 100644 --- a/internal/bot/nickname.go +++ b/internal/bot/nickname.go @@ -94,6 +94,30 @@ func CalculateTitle(c *api.ControllerData, cfg *ServerConfig) (string, error) { } } +func facilityStaffTitle(c *api.ControllerData) (string, bool) { + facility := GetFacilityData(c.Facility) + if facility.AirTrafficManagerCID == c.CID { + return "ATM", true + } + if facility.DeputyAirTrafficManagerCID == c.CID { + return "DATM", true + } + if facility.TrainingAdministratorCID == c.CID { + return "TA", true + } + if facility.EventCoordinatorCID == c.CID { + return "EC", true + } + if facility.FacilityEngineerCID == c.CID { + return "FE", true + } + if facility.WebMasterCID == c.CID { + return "WM", true + } + + return "", false +} + func CalculateDivisionTitle(c *api.ControllerData, cfg *ServerConfig) string { for _, r := range c.Roles { if strings.HasPrefix(r.Role, "US") { @@ -104,11 +128,9 @@ func CalculateDivisionTitle(c *api.ControllerData, cfg *ServerConfig) string { } } } - for _, r := range c.Roles { - re := regexp.MustCompile("ATM|DATM|TA|FE|EC|WM") - if re.MatchString(r.Role) { - return fmt.Sprintf("%s %s", r.Facility, r.Role) - } + roleTitle, hasRoleTitle := facilityStaffTitle(c) + if hasRoleTitle { + return fmt.Sprintf("%s %s", c.Facility, roleTitle) } if c.Facility == "ZZN" { return fmt.Sprintf("%s", c.RatingShort) @@ -131,14 +153,9 @@ func CalculateLocalTitle(c *api.ControllerData, cfg *ServerConfig) string { } } } - for _, r := range c.Roles { - re := regexp.MustCompile("ATM|DATM|TA|FE|EC|WM") - if re.MatchString(r.Role) { - if r.Facility == cfg.Facility { - return fmt.Sprintf("%s", r.Role) - } - return fmt.Sprintf("%s %s", r.Facility, r.Role) - } + roleTitle, hasRoleTitle := facilityStaffTitle(c) + if hasRoleTitle { + return fmt.Sprintf("%s %s", c.Facility, roleTitle) } if c.Facility == "ZZN" { return fmt.Sprintf("%s", c.RatingShort)