Skip to content

Commit

Permalink
Only give role nickname to the POC, not to assistants / team members
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonhasacat committed Feb 23, 2024
1 parent 307f2c1 commit 9a8253c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 14 deletions.
2 changes: 1 addition & 1 deletion internal/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
50 changes: 50 additions & 0 deletions internal/api/facility.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions internal/bot/facility.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 30 additions & 13 deletions internal/bot/nickname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 9a8253c

Please sign in to comment.