|
| 1 | +package encyclopedia |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/dharmab/collections/sets" |
| 9 | + "github.com/martinlindhe/unit" |
| 10 | + "github.com/rs/zerolog/log" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// serializedAircraft is the on-disk representation of an Aircraft. It exists only to translate |
| 15 | +// between the human-friendly string schema and the Aircraft type's unexported enum fields; it is |
| 16 | +// never exposed outside this package. |
| 17 | +type serializedAircraft struct { |
| 18 | + ACMIShortName string `yaml:"acmi_short_name"` |
| 19 | + Tags []string `yaml:"tags"` |
| 20 | + PlatformDesignation string `yaml:"platform_designation"` |
| 21 | + TypeDesignation string `yaml:"type_designation"` |
| 22 | + NATOReportingName string `yaml:"nato_reporting_name"` |
| 23 | + OfficialName string `yaml:"official_name"` |
| 24 | + Nickname string `yaml:"nickname"` |
| 25 | + ThreatRadiusNM float64 `yaml:"threat_radius_nm"` |
| 26 | + FuelProvider string `yaml:"fuel_provider"` |
| 27 | + FuelReceiver string `yaml:"fuel_receiver"` |
| 28 | +} |
| 29 | + |
| 30 | +// tagsByName maps the tag names accepted in custom aircraft files to their AircraftTag values. |
| 31 | +var tagsByName = map[string]AircraftTag{ |
| 32 | + "fixed-wing": FixedWing, |
| 33 | + "rotary-wing": RotaryWing, |
| 34 | + "unarmed": Unarmed, |
| 35 | + "fighter": Fighter, |
| 36 | + "attack": Attack, |
| 37 | +} |
| 38 | + |
| 39 | +// refuelingByName maps the refueling method names accepted in custom aircraft files to their |
| 40 | +// AirRefuelingMethod values. An empty string or "none" maps to NoAirRefueling. |
| 41 | +var refuelingByName = map[string]AirRefuelingMethod{ |
| 42 | + "": NoAirRefueling, |
| 43 | + "none": NoAirRefueling, |
| 44 | + "boom": FlyingBoom, |
| 45 | + "probe-and-drogue": ProbeAndDrogue, |
| 46 | +} |
| 47 | + |
| 48 | +// toAircraft validates the string schema and converts it into an Aircraft. |
| 49 | +func (s serializedAircraft) toAircraft() (Aircraft, error) { |
| 50 | + if strings.TrimSpace(s.ACMIShortName) == "" { |
| 51 | + return Aircraft{}, errors.New("aircraft must have a non-empty acmi_short_name") |
| 52 | + } |
| 53 | + |
| 54 | + // SkyEye derives an aircraft's reporting name from these fields, so at least one is required or |
| 55 | + // the GCI would have nothing to call the contact. |
| 56 | + if strings.TrimSpace(s.NATOReportingName) == "" && |
| 57 | + strings.TrimSpace(s.Nickname) == "" && |
| 58 | + strings.TrimSpace(s.OfficialName) == "" && |
| 59 | + strings.TrimSpace(s.PlatformDesignation) == "" { |
| 60 | + return Aircraft{}, fmt.Errorf("aircraft %q must have at least one of nato_reporting_name, nickname, official_name, or platform_designation", s.ACMIShortName) |
| 61 | + } |
| 62 | + |
| 63 | + if len(s.Tags) == 0 { |
| 64 | + return Aircraft{}, fmt.Errorf("aircraft %q must have at least one tag", s.ACMIShortName) |
| 65 | + } |
| 66 | + tags := sets.New[AircraftTag]() |
| 67 | + wingCount := 0 |
| 68 | + for _, name := range s.Tags { |
| 69 | + tag, ok := tagsByName[strings.ToLower(strings.TrimSpace(name))] |
| 70 | + if !ok { |
| 71 | + return Aircraft{}, fmt.Errorf("aircraft %q has unrecognized tag %q", s.ACMIShortName, name) |
| 72 | + } |
| 73 | + if tag == FixedWing || tag == RotaryWing { |
| 74 | + wingCount++ |
| 75 | + } |
| 76 | + sets.Add(tags, tag) |
| 77 | + } |
| 78 | + if wingCount != 1 { |
| 79 | + return Aircraft{}, fmt.Errorf("aircraft %q must have exactly one of the tags fixed-wing or rotary-wing", s.ACMIShortName) |
| 80 | + } |
| 81 | + |
| 82 | + if s.ThreatRadiusNM < 0 { |
| 83 | + return Aircraft{}, fmt.Errorf("aircraft %q has negative threat_radius_nm %v", s.ACMIShortName, s.ThreatRadiusNM) |
| 84 | + } |
| 85 | + |
| 86 | + fuelProvider, ok := refuelingByName[strings.ToLower(strings.TrimSpace(s.FuelProvider))] |
| 87 | + if !ok { |
| 88 | + return Aircraft{}, fmt.Errorf("aircraft %q has unrecognized fuel_provider %q", s.ACMIShortName, s.FuelProvider) |
| 89 | + } |
| 90 | + fuelReceiver, ok := refuelingByName[strings.ToLower(strings.TrimSpace(s.FuelReceiver))] |
| 91 | + if !ok { |
| 92 | + return Aircraft{}, fmt.Errorf("aircraft %q has unrecognized fuel_receiver %q", s.ACMIShortName, s.FuelReceiver) |
| 93 | + } |
| 94 | + |
| 95 | + return Aircraft{ |
| 96 | + ACMIShortName: s.ACMIShortName, |
| 97 | + tags: tags, |
| 98 | + PlatformDesignation: s.PlatformDesignation, |
| 99 | + TypeDesignation: s.TypeDesignation, |
| 100 | + NATOReportingName: s.NATOReportingName, |
| 101 | + OfficialName: s.OfficialName, |
| 102 | + Nickname: s.Nickname, |
| 103 | + threatRadius: unit.Length(s.ThreatRadiusNM) * unit.NauticalMile, |
| 104 | + fuelProvider: fuelProvider, |
| 105 | + fuelReceiver: fuelReceiver, |
| 106 | + }, nil |
| 107 | +} |
| 108 | + |
| 109 | +// LoadCustomAircraft parses custom aircraft data. YAML is a superset of JSON, so the data may be in |
| 110 | +// either format. Each entry is validated during conversion. |
| 111 | +func LoadCustomAircraft(data []byte) ([]Aircraft, error) { |
| 112 | + var serialized []serializedAircraft |
| 113 | + if err := yaml.Unmarshal(data, &serialized); err != nil { |
| 114 | + return nil, fmt.Errorf("failed to parse custom aircraft file: %w", err) |
| 115 | + } |
| 116 | + aircraft := make([]Aircraft, 0, len(serialized)) |
| 117 | + for _, s := range serialized { |
| 118 | + a, err := s.toAircraft() |
| 119 | + if err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + aircraft = append(aircraft, a) |
| 123 | + } |
| 124 | + return aircraft, nil |
| 125 | +} |
| 126 | + |
| 127 | +// AddCustomAircraft registers custom aircraft into the encyclopedia, keyed by ACMI short name. |
| 128 | +// An entry whose ACMI short name matches a built-in or previously added entry overrides it. |
| 129 | +func AddCustomAircraft(aircraft []Aircraft) { |
| 130 | + for _, data := range aircraft { |
| 131 | + event := log.Info().Str("aircraft", data.ACMIShortName) |
| 132 | + if _, exists := aircraftDataLUT[data.ACMIShortName]; exists { |
| 133 | + event = event.Bool("override", true) |
| 134 | + } |
| 135 | + aircraftDataLUT[data.ACMIShortName] = data |
| 136 | + event.Msg("loaded custom aircraft into encyclopedia") |
| 137 | + } |
| 138 | +} |
0 commit comments