Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Market.json processing and general event cleanup #298

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions EliteAPI.Events.Generator/EliteAPI.Events.Generator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
110 changes: 110 additions & 0 deletions EliteAPI.Events.Generator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using EliteAPI.Events.Generator;
using Newtonsoft.Json.Linq;

var schemas = Schemas.GetSchemas();

foreach (var rawSchema in schemas)
{
var schema = JObject.Parse(rawSchema.schema);

var description = schema["description"]?.Value<string>();

var properties = new List<(string type, string name)>();
var subClasses = new Dictionary<string, List<(string type, string name)>>();

if(schema["properties"] == null)
{
continue;
}

foreach (var property in schema["properties"]!)
{
var propertyName = ((JProperty)property).Name;

if(propertyName.EndsWith("_Localised"))
continue;

var propertyType = (string)property.First!["type"]!;

if (propertyName.EndsWith("ID") || propertyName.EndsWith("Address"))
propertyType = "string";

var propertyTypeName = GetCSharpType(propertyType);
properties.Add((propertyTypeName, propertyName));
}

var file = new FileInfo($"../../../../EliteAPI.Events/{rawSchema.name}Event.cs");

if (file.Exists)
{
var content = File.ReadAllText(file.FullName);

foreach (var property in properties)
{
var jsonPropertyAttribute = $"[JsonProperty(\"{property.name}\")]";
var propertyAttribute = $"public {property.type} {property.name} {{ get; init; }}";

if(property.type.StartsWith("SUBCLASS"))
continue;

if (!content.Contains(jsonPropertyAttribute))
{
Console.WriteLine($"Property {property.name} not found in {rawSchema.name}Event.cs");

var propertyLine = $"\n {jsonPropertyAttribute}\n {propertyAttribute}\n";

// Add the property to the class file, just before the last bracket

var lastBracket = content.LastIndexOf('}');

content = content.Insert(lastBracket, propertyLine);

// Format the file
content = content.Replace("\n\n\n", "\n\n");

// Indent based on brackets
var lines = content.Split('\n');
var indent = 0;
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i].Trim();
indent -= line.Count(c => c == '}');
indent += line.Count(c => c == '{');

if (line.StartsWith('{'))
{
lines[i] = new string('\t', indent - 1) + line;
}
else
{
lines[i] = new string('\t', indent) + line;
}


}

content = string.Join('\n', lines);

File.WriteAllText(file.FullName, content);
}
}
}
else
{
Console.WriteLine($"File {rawSchema.name}Event.cs not found");
}
}

string GetCSharpType(string jsonType)
{
return jsonType switch
{
"string" => "string",
"number" => "double",
"integer" => "int",
"boolean" => "bool",
"object" => "SUBCLASS:OBJECT",
"array" => "SUBCLASS:ARRAY",
_ => jsonType
};
}
28 changes: 28 additions & 0 deletions EliteAPI.Events.Generator/Schemas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Diagnostics;

namespace EliteAPI.Events.Generator;

public class Schemas
{
public static IEnumerable<(string name, string schema)> GetSchemas()
{
Process.Start("git", "clone https://github.com/Somfic/journal-schemas.git schemas-repo").WaitForExit();

var files = Directory.GetFiles("schemas-repo", "*.json", SearchOption.AllDirectories);
var schemas = new List<(string, string)>();

foreach (var file in files)
{
var name = Path.GetFileNameWithoutExtension(file);

if (name is "_Event" or "ShipLockerBackpack" or "ShipLockerMaterials")
continue;

var schema = File.ReadAllText(file);

schemas.Add((name, schema));
}

return schemas;
}
}
61 changes: 35 additions & 26 deletions EliteAPI.Events/ApproachSettlementEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,39 @@ namespace EliteAPI.Events;

public readonly struct ApproachSettlementEvent : IEvent
{
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Name")]
public string Name { get; init; }

[JsonProperty("MarketID")]
public string MarketId { get; init; }

[JsonProperty("SystemAddress")]
public string SystemAddress { get; init; }

[JsonProperty("BodyID")]
public string BodyId { get; init; }

[JsonProperty("BodyName")]
public string BodyName { get; init; }

[JsonProperty("Latitude")]
public double Latitude { get; init; }

[JsonProperty("Longitude")]
public double Longitude { get; init; }
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Name")]
public string Name { get; init; }

[JsonProperty("MarketID")]
public string MarketId { get; init; }

[JsonProperty("SystemAddress")]
public string SystemAddress { get; init; }

[JsonProperty("BodyID")]
public string BodyId { get; init; }

[JsonProperty("BodyName")]
public string BodyName { get; init; }

[JsonProperty("Latitude")]
public double Latitude { get; init; }

[JsonProperty("Longitude")]
public double Longitude { get; init; }

[JsonProperty("StationGovernment")]
public string StationGovernment { get; init; }

[JsonProperty("StationAllegiance")]
public string StationAllegiance { get; init; }

[JsonProperty("StationEconomy")]
public string StationEconomy { get; init; }
}
75 changes: 39 additions & 36 deletions EliteAPI.Events/BountyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@ namespace EliteAPI.Events;

public readonly struct BountyEvent : IEvent
{
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Rewards")]
public IReadOnlyCollection<BountyRewardInfo> Rewards { get; init; }

[JsonProperty("Target")]
public Localised Target { get; init; }

[JsonProperty("TotalReward")]
public long TotalReward { get; init; }

[JsonProperty("VictimFaction")]
public string VictimFaction { get; init; }

[JsonProperty("SharedWithOthers")]
public long SharedWithOthers { get; init; }

[JsonProperty("Faction")]
public string Faction { get; init; }


[JsonProperty("Reward")]
public long Reward { get; init; }

public readonly struct BountyRewardInfo
{
[JsonProperty("Faction")]
public string Faction { get; init; }

[JsonProperty("Reward")]
public long Reward { get; init; }
}
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Rewards")]
public IReadOnlyCollection<BountyRewardInfo> Rewards { get; init; }

[JsonProperty("Target")]
public Localised Target { get; init; }

[JsonProperty("TotalReward")]
public long TotalReward { get; init; }

[JsonProperty("VictimFaction")]
public string VictimFaction { get; init; }

[JsonProperty("SharedWithOthers")]
public long SharedWithOthers { get; init; }

[JsonProperty("Faction")]
public string Faction { get; init; }


[JsonProperty("Reward")]
public long Reward { get; init; }

public readonly struct BountyRewardInfo
{
[JsonProperty("Faction")]
public string Faction { get; init; }

[JsonProperty("Reward")]
public long Reward { get; init; }
}

[JsonProperty("PilotName")]
public string PilotName { get; init; }
}
33 changes: 18 additions & 15 deletions EliteAPI.Events/BuySuitEvent.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
using EliteAPI.Abstractions.Events;
using EliteAPI.Abstractions.Events;
using Newtonsoft.Json;

namespace EliteAPI.Events;

public readonly struct BuySuitEvent : IEvent
{
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Name")]
public Localised Name { get; init; }

[JsonProperty("Price")]
public long Price { get; init; }

[JsonProperty("SuitId")]
public string SuitId { get; init; }
[JsonProperty("timestamp")]
public DateTime Timestamp { get; init; }

[JsonProperty("event")]
public string Event { get; init; }

[JsonProperty("Name")]
public Localised Name { get; init; }

[JsonProperty("Price")]
public long Price { get; init; }

[JsonProperty("SuitId")]
public string SuitId { get; init; }

[JsonProperty("SuitID")]
public string SuitID { get; init; }
}
Loading
Loading