Skip to content

Commit

Permalink
Refactor ship definitions.
Browse files Browse the repository at this point in the history
Refactor to remove unnecessary ship `EliteID` property. Prefer to use ship model in all cases.
Refactor ShipManufacturer into its own class.
  • Loading branch information
Tkael committed May 5, 2024
1 parent b987646 commit 2e0e021
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 146 deletions.
14 changes: 4 additions & 10 deletions DataDefinitions/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,6 @@ public decimal optimalmass

// Admin

// The ID in Elite: Dangerous' database
[JsonIgnore]
public long EDID { get; set; }

// The name in Elite: Dangerous' database
public string EDName { get; set; }

Expand All @@ -625,11 +621,10 @@ public Ship()
cargohatch = new Module();
}

public Ship(long EDID, string EDName, string Manufacturer, string Model, string possessiveYour, List<Translation> PhoneticModel, LandingPadSize Size, int? MilitarySize, decimal reservoirFuelTankSize)
public Ship( string EDName, ShipManufacturer Manufacturer, string Model, string possessiveYour, List<Translation> PhoneticModel, LandingPadSize Size, int? MilitarySize, decimal reservoirFuelTankSize)
{
this.EDID = EDID;
this.EDName = EDName;
manufacturer = Manufacturer;
manufacturer = Manufacturer.name;
model = Model;
this.possessiveYour = possessiveYour;
phoneticModel = PhoneticModel;
Expand Down Expand Up @@ -693,7 +688,7 @@ public string SpokenModel()
return result;
}

public string SpokenManufacturer() => ShipDefinitions.SpokenManufacturer(manufacturer) ?? manufacturer;
public string SpokenManufacturer() => ShipManufacturer.SpokenManufacturer(manufacturer) ?? manufacturer;

/// <summary> Calculates the distance from the specified coordinates to the ship's recorded x, y, and z coordinates </summary>
public decimal? Distance(decimal? fromX, decimal? fromY, decimal? fromZ)
Expand Down Expand Up @@ -765,7 +760,6 @@ public void Augment()
Ship template = ShipDefinitions.FromModel(model);
if (template != null)
{
EDID = template.EDID;
EDName = template.EDName;
manufacturer = template.manufacturer;
possessiveYour = template.possessiveYour;
Expand Down Expand Up @@ -909,7 +903,7 @@ public static Ship FromShipyardInfo(ShipyardInfoItem item)
try
{
Logging.Debug($"Converting ShipyardInfoItem to Ship: ", item);
var ship = ShipDefinitions.FromEliteID(item.EliteID) ?? ShipDefinitions.FromEDModel(item.edModel, false);
var ship = ShipDefinitions.FromEDModel(item.edModel, false);
if (ship == null)
{
// Unknown ship; report the full object so that we can update the definitions
Expand Down
151 changes: 48 additions & 103 deletions DataDefinitions/ShipDefinitions.cs

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions DataDefinitions/ShipManufacturer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using System.Linq;
using Utilities;

namespace EddiDataDefinitions
{
public class ShipManufacturer
{
public static readonly ShipManufacturer CoreDynamics = new ShipManufacturer ( "Core Dynamics" );
public static readonly ShipManufacturer FaulconDeLacy = new ShipManufacturer ( "Faulcon DeLacy" );
public static readonly ShipManufacturer Gutamaya = new ShipManufacturer ( "Gutamaya", new List<Translation> {new Translation("Gutamaya", "guːtəˈmaɪə") } );
public static readonly ShipManufacturer LakonSpaceways = new ShipManufacturer ( "Lakon Spaceways", new List<Translation> {new Translation("Lakon", "leɪkɒn"), new Translation("Spaceways", "speɪsweɪz") } );
public static readonly ShipManufacturer SaudKruger = new ShipManufacturer ( "Saud Kruger", new List<Translation> {new Translation("Saud", "saʊd"), new Translation("Kruger", "ˈkruːɡə") } );
public static readonly ShipManufacturer ZorgonPeterson = new ShipManufacturer ( "Zorgon Peterson" );

public static readonly List<ShipManufacturer> AllOfThem = new List<ShipManufacturer>()
{
CoreDynamics,
FaulconDeLacy,
Gutamaya,
LakonSpaceways,
SaudKruger,
ZorgonPeterson
};

public string name { get; }

public List<Translation> phoneticName { get; }

private ShipManufacturer ( string name, List<Translation> phoneticName = null )
{
this.name = name;
this.phoneticName = phoneticName;
}

public static string SpokenManufacturer(string manufacturer)
{
var phoneticmanufacturer = AllOfThem.FirstOrDefault(m => m.name == manufacturer)?.phoneticName;
if (phoneticmanufacturer != null)
{
var result = "";
foreach (var item in phoneticmanufacturer)
{
result += "<phoneme alphabet=\"ipa\" ph=\"" + item.to + "\">" + item.from + "</phoneme> ";
}
return result;
}
// Model isn't in the dictionary
return null;
}
}
}
8 changes: 2 additions & 6 deletions DataDefinitions/ShipyardInfoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ namespace EddiDataDefinitions
/// <summary> This class is designed to be deserialized from either shipyard.json or the Frontier API. </summary>
public class ShipyardInfoItem
{
[JsonProperty("id")]
public long EliteID { get; set; }

[JsonProperty("ShipType")]
public string edModel { get; set; }

Expand All @@ -27,16 +24,15 @@ public class ShipyardInfoItem
public ShipyardInfoItem()
{ }

public ShipyardInfoItem(long eliteId, string ShipType, long ShipPrice)
public ShipyardInfoItem( string ShipType, long ShipPrice )
{
this.EliteID = eliteId;
this.edModel = ShipType;
this.shipPrice = ShipPrice;
}

public Ship ToShip()
{
Ship ship = ShipDefinitions.FromEliteID(EliteID) ?? ShipDefinitions.FromEDModel(edModel, false);
Ship ship = ShipDefinitions.FromEDModel( edModel, false );
if (ship == null)
{
// Unknown ship; report the full object so that we can update the definitions
Expand Down
2 changes: 1 addition & 1 deletion SpeechResponder/CustomFunctions/ShipCallsign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static string phoneticCallsign(Ship ship, string id)
if (string.IsNullOrEmpty(ship?.manufacturer) || string.IsNullOrEmpty(id)) { return string.Empty; }

// First, obtain the phonetic manufacturer. This may be the complete name or only a partial name, depending on the manufacturer.
var phoneticmanufacturer = ShipDefinitions.ManufacturerPhoneticNames.FirstOrDefault(m => m.Key == ship.manufacturer).Value;
var phoneticmanufacturer = ShipManufacturer.AllOfThem.FirstOrDefault(m => m.name == ship.manufacturer)?.phoneticName;

var sb = new StringBuilder();
switch (ship.manufacturer)
Expand Down
5 changes: 3 additions & 2 deletions SpeechService/PhoneticShipModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ public static partial class Translations
{
public static string getPhoneticShipModel(string val)
{
Ship ship = ShipDefinitions.FromModel(val);
if (ship != null && ship.EDID > 0)
var ship = ShipDefinitions.FromModel( val );
if ( ship != null && !string.IsNullOrEmpty( ship.model ) )
{
return ship.SpokenModel().Trim();
}

return val;
}
}
Expand Down
22 changes: 11 additions & 11 deletions Tests/CapiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ public void TestShipyardShips()
{
// Test shipyard data
var expectedShips = new List<ShipyardInfoItem>()
{
new ShipyardInfoItem(128049255, "Eagle", 44800),
new ShipyardInfoItem(128672276, "Asp_Scout", 3961154),
new ShipyardInfoItem(128049249, "SideWinder", 32000),
new ShipyardInfoItem(128049309, "Vulture", 4925615),
new ShipyardInfoItem(128049363, "Anaconda", 146969451),
new ShipyardInfoItem(128049321, "Federation_Dropship", 14314205),
new ShipyardInfoItem(128672152, "Federation_Gunship", 35814205),
new ShipyardInfoItem(128672145, "Federation_Dropship_MkII", 19814205)
{
new ShipyardInfoItem( "Eagle", 44800 ),
new ShipyardInfoItem( "Asp_Scout", 3961154 ),
new ShipyardInfoItem( "SideWinder", 32000 ),
new ShipyardInfoItem( "Vulture", 4925615 ),
new ShipyardInfoItem( "Anaconda", 146969451 ),
new ShipyardInfoItem( "Federation_Dropship", 14314205 ),
new ShipyardInfoItem( "Federation_Gunship", 35814205 ),
new ShipyardInfoItem( "Federation_Dropship_MkII", 19814205 )
};

JObject json = DeserializeJsonResource<JObject>(Resources.capi_shipyard_Abasheli_Barracks)?.ToObject<JObject>();
var json = DeserializeJsonResource<JObject>(Resources.capi_shipyard_Abasheli_Barracks)?.ToObject<JObject>();
Assert.IsNotNull(json);
json["timestamp"] = DateTime.UtcNow; // We add a timestamp to the json returned from the Frontier API, do the same here.
var station = FrontierApiStation.FromJson(null, json);
Expand All @@ -72,7 +72,7 @@ public void TestShipyardShips()
{
foreach (var actualShip in actualShips)
{
if (expectedShip.EliteID == actualShip.EliteID)
if (expectedShip.edModel == actualShip.edModel)
{
Assert.IsTrue(expectedShip.DeepEquals(actualShip));
}
Expand Down
26 changes: 13 additions & 13 deletions Tests/SpeechTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,32 @@ public void TestSagAStar()
{
string SagI = "Sagittarius A*";
string translated = Translations.GetTranslation(SagI);
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), translated);
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), translated);
}

[TestMethod, TestCategory("Speech")]
public void TestSsml1()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), @"<break time=""100ms""/>Fred's ship.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), @"<break time=""100ms""/>Fred's ship.");
}

[TestMethod, TestCategory("Speech")]
public void TestSsml2()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), @"<break time=""100ms""/>7 < 10.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), @"<break time=""100ms""/>7 < 10.");
}

[TestMethod, TestCategory("Speech")]
public void TestSsml3()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), @"<break time=""100ms""/>He said ""Foo"".");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), @"<break time=""100ms""/>He said ""Foo"".");
}

[TestMethod, TestCategory("Speech")]
public void TestSsml4()
{
Logging.Verbose = true;
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), @"<break time=""100ms""/>We're on our way to " + Translations.GetTranslation("i Bootis") + ".");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), @"<break time=""100ms""/>We're on our way to " + Translations.GetTranslation("i Bootis") + ".");
}

[TestMethod, TestCategory("Speech")]
Expand Down Expand Up @@ -117,19 +117,19 @@ public void TestAudio()
[TestMethod, TestCategory("Speech")]
public void TestCallsign()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), Translations.ICAO("GAB-1655"));
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), Translations.ICAO("GAB-1655"));
}

[TestMethod, TestCategory("Speech")]
public void TestSsml()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049363), "You are travelling to the " + Translations.GetTranslation("Hotas") + " system.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Anaconda" ), "You are travelling to the " + Translations.GetTranslation("Hotas") + " system.");
}

[TestMethod, TestCategory("Speech")]
public void TestPowerplay()
{
var ship = ShipDefinitions.FromEliteID(128049363);
var ship = ShipDefinitions.FromEDModel( "Anaconda" );
var speaker = SpeechService.Instance;
string[] powerNames = {
"Aisling Duval",
Expand Down Expand Up @@ -178,7 +178,7 @@ public void TestExtendedSource()
[TestMethod, TestCategory("Speech")]
public void TestDamage()
{
Ship ship = ShipDefinitions.FromEliteID(128049363);
Ship ship = ShipDefinitions.FromEDModel( "Anaconda" );
var origHealth = ship.health;
ship.health = 100;
SpeechService.Instance.Say(ship, "Systems fully operational.");
Expand All @@ -198,9 +198,9 @@ public void TestDamage()
[TestMethod, TestCategory("Speech")]
public void TestVariants()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049309), "Welcome to your Vulture. Weapons online.");
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049339), "Welcome to your Python. Scanning at full range.");
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049363), "Welcome to your Anaconda. All systems operational.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Vulture" ), "Welcome to your Vulture. Weapons online.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Python" ), "Welcome to your Python. Scanning at full range.");
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Anaconda" ), "Welcome to your Anaconda. All systems operational.");
}

[TestMethod, TestCategory("Speech")]
Expand All @@ -217,7 +217,7 @@ public void TestChorus()
[TestMethod, TestCategory("Speech")]
public void TestSendAndReceive()
{
SpeechService.Instance.Say(ShipDefinitions.FromEliteID(128049339), "Anaconda golf foxtrot lima one niner six eight returning from orbit.", 3, null, true);
SpeechService.Instance.Say(ShipDefinitions.FromEDModel( "Python" ), "Anaconda golf foxtrot lima one niner six eight returning from orbit.", 3, null, true);
}

[TestMethod, TestCategory("Speech")]
Expand Down

0 comments on commit 2e0e021

Please sign in to comment.