Skip to content

Commit

Permalink
Add ICAO matching
Browse files Browse the repository at this point in the history
  • Loading branch information
GewoonJaap committed Jul 5, 2021
1 parent 114683e commit b69a49e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
1 change: 1 addition & 0 deletions Config/ModelMatching.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"Cessna 525B CitationJet CJ3+": "Cessna CJ4 Citation",
"Cessna 650 Citation III": "Cessna CJ4 Citation",
"Cessna 510 Citation Mustang": "Cessna CJ4 Citation",
"Cessna 560XLS Citation Excel": "Cessna CJ4 Citation",
"Pilatus PC-12/47E": "TBM 930",
"Piper PA-46-M600": "TBM 930",
"Piper PA-46-M500": "TBM 930",
Expand Down
1 change: 1 addition & 0 deletions Model/Addon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class Addon
{
public string Title;
public string ModelCode;
public string Icao_Airline;
}
}
1 change: 1 addition & 0 deletions Model/Aircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class Aircraft
public string TailNumber;
public string Model;
public string Airline;
public string IcaoAirline;
public string ModelCode;

#endregion Aircraft
Expand Down
11 changes: 9 additions & 2 deletions Util/AddonScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private static List<Addon> ParseCfg(string cfgPath)

string title = "";
string modelCode = "";
string icaoAirline = "";
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Expand All @@ -97,9 +98,10 @@ private static List<Addon> ParseCfg(string cfgPath)
addons.Add(curentAddon);
curentAddon = null;
title = "";
icaoAirline = "";
}
}
if (!line.ToLower().StartsWith("title") && !line.ToLower().StartsWith("icao_type_designator")) continue;
if (!line.ToLower().StartsWith("title") && !line.ToLower().StartsWith("icao_type_designator") && !line.ToLower().StartsWith("icao_airline")) continue;

string value = line.Split('=')[1].Trim();
if (value.StartsWith("\""))
Expand All @@ -120,10 +122,14 @@ private static List<Addon> ParseCfg(string cfgPath)
{
title = value;
}
else
else if(line.ToLower().StartsWith("icao_type_designator"))
{
modelCode = value;
}
else
{
icaoAirline = value;
}

if (curentAddon == null)
{
Expand All @@ -132,6 +138,7 @@ private static List<Addon> ParseCfg(string cfgPath)

curentAddon.Title = title.Trim();
curentAddon.ModelCode = modelCode.Trim();
curentAddon.Icao_Airline = icaoAirline.Trim();
}

if (curentAddon != null && !curentAddon.Title.Contains("AirTraffic"))
Expand Down
8 changes: 5 additions & 3 deletions Util/LiveTrafficHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ private void ParsePlaneData(JObject planeData)
int speed = (int)property.Value[5];
string callsign = (string)property.Value[16];
bool isGrounded = (bool)property.Value[14];
string icaoAirline = (string)property.Value[18];
string airportOrigin = null;
string airportDestination = null;
string tailNumber = callsign;
Expand All @@ -94,7 +95,7 @@ private void ParsePlaneData(JObject planeData)
{
tailNumber = (string)extraData["identification"]?["number"]?["default"] ?? callsign;
model = (string)extraData["aircraft"]?["model"]?["text"] ?? "Airbus A320 Neo";
modelCode = (string)extraData["aircraft"]?["model"]?["code"] ?? "A320";
modelCode = (string)extraData["aircraft"]?["model"]?["code"] ?? "A32N";
airline = (string)extraData["airline"]?["name"] ?? "Asobo";
airportOrigin = (string)extraData["airport"]?["origin"]?["code"]?["icao"] ?? null;

Expand All @@ -120,9 +121,10 @@ private void ParsePlaneData(JObject planeData)
Airline = airline,
AirportOrigin = airportOrigin,
AirportDestination = airportDestination,
ModelCode = modelCode
ModelCode = modelCode,
IcaoAirline = icaoAirline
};
aircraft.MatchedModel = ModelMatching.MatchModel(aircraft.ModelCode, aircraft.Model, aircraft.Airline, _addons);
aircraft.MatchedModel = ModelMatching.MatchModel(aircraft.ModelCode, aircraft.Model, aircraft.Airline, aircraft.IcaoAirline, _addons);

LiveTrafficAircraft.Add(aircraft);
SpawnPlane(aircraft);
Expand Down
39 changes: 22 additions & 17 deletions Util/ModelMatching.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Simvars.Util
{
public static class ModelMatching
{
public static string MatchModel(string modelCode, string model, string airline, List<Addon> addons = null)
public static string MatchModel(string modelCode, string model, string airline, string icaoAirline, List<Addon> addons = null)
{
List<Addon> installedAddons = addons ?? AddonScanner.ScanAddons();

Expand All @@ -18,43 +18,48 @@ public static string MatchModel(string modelCode, string model, string airline,
if (models.GetValue(model) == null) Console.WriteLine($"Failed to model match: {model}");
string matchedModel = (string)models.GetValue(model) ?? (string)models.GetValue(modelCode) ?? installedAddons.FirstOrDefault(addon => addon.ModelCode == modelCode)?.Title?.Replace("Asobo", "")?.Trim() ?? (string)models.GetValue("Default Aircraft") ?? "Airbus A320 Neo";

if (TryFindAircraft(models, installedAddons, $"{matchedModel} {airline}") != null)
if (installedAddons.FirstOrDefault(addon => addon.Title == matchedModel) == null && installedAddons.FirstOrDefault(addon => addon.Title == matchedModel + " Asobo") == null)
{
matchedModel = TryFindAircraft(models, installedAddons, $"{matchedModel} {airline}");
Console.WriteLine($"Failed to model match: {matchedModel} not installed!");
matchedModel = installedAddons.FirstOrDefault(addon => addon.ModelCode == modelCode)?.Title ?? "Airbus A320 Neo Asobo";
}
else if (airline.Contains("(") && TryFindAircraft(models, installedAddons, $"{matchedModel} {airline.Split('(')[0].Trim()}") != null)

if (TryFindAircraft(models, installedAddons, matchedModel, airline, icaoAirline) != null)
{
matchedModel = TryFindAircraft(models, installedAddons, matchedModel, airline, icaoAirline);
}
else if (airline.Contains("(") && TryFindAircraft(models, installedAddons, matchedModel, airline.Split('(')[0].Trim(), icaoAirline ) != null)
{
matchedModel = TryFindAircraft(models, installedAddons, $"{matchedModel} {airline.Split('(')[0].Trim()}");
matchedModel = TryFindAircraft(models, installedAddons, matchedModel, airline.Split('(')[0].Trim(), icaoAirline);
}
else
{
if (models.GetValue(matchedModel + " Default") == null) Console.WriteLine($"Failed to model match: {matchedModel} Default");
matchedModel = (string)models.GetValue($"{matchedModel} Default") ?? installedAddons.FirstOrDefault(addon => addon.ModelCode == modelCode)?.Title ?? "Airbus A320 Neo Asobo";
if ((string) models.GetValue($"{matchedModel} Default") != null && installedAddons.FirstOrDefault(addon => addon.Title == (string)models.GetValue($"{matchedModel} Default")) == null)
{
Console.WriteLine($"Failed to model match: {(string)models.GetValue($"{matchedModel} Default")} not installed!");
matchedModel = installedAddons.FirstOrDefault(addon => addon.ModelCode == modelCode)?.Title ?? "Airbus A320 Neo Asobo";
}
}
Console.WriteLine($"Model matched {model} {airline} with: {matchedModel}");
return matchedModel;
}

private static string TryFindAircraft(JObject models, List<Addon> installedAddons, string fullName)
private static string TryFindAircraft(JObject models, List<Addon> installedAddons, string model, string airline, string icao)
{
string foundAircraft = null;

if (models.GetValue(fullName) != null)
if (models.GetValue($"{model} {airline}") != null)
{
foundAircraft = (string)models.GetValue($"{model} {airline}");
}
else if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith($"{model} {airline} AI")) != null)
{
foundAircraft = (string)models.GetValue(fullName);
foundAircraft = installedAddons.First(addon => addon.Title.StartsWith($"{model} {airline} AI")).Title;
}
else if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith($"{fullName} AI")) != null)
else if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith($"{model} {airline}")) != null)
{
foundAircraft = installedAddons.First(addon => addon.Title.StartsWith($"{fullName} AI")).Title;
foundAircraft = installedAddons.First(addon => addon.Title.StartsWith($"{model} {airline}")).Title;
}
else if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith(fullName)) != null)
else if (installedAddons.FirstOrDefault(addon => addon.Title.StartsWith(model) && addon.Icao_Airline == icao) != null)
{
foundAircraft = installedAddons.First(addon => addon.Title.StartsWith(fullName)).Title;
foundAircraft = installedAddons.First(addon => addon.Title.StartsWith(model) && addon.Icao_Airline == icao).Title;
}

return foundAircraft;
Expand Down

0 comments on commit b69a49e

Please sign in to comment.