You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.2 KiB
79 lines
2.2 KiB
using System.Text; |
|
using System.Text.Json; |
|
using System.Text.Json.Serialization; |
|
using Model.BuildOrders; |
|
using Model.Entity; |
|
using Model.Entity.Data; |
|
using YamlDotNet.Serialization; |
|
|
|
namespace Services.Immortal; |
|
|
|
public class BuildComparisionService : IBuildComparisonService { |
|
private event Action OnChange = default!; |
|
|
|
private BuildComparisonModel buildComparison = new(); |
|
|
|
public void Subscribe(Action action) { |
|
OnChange += action; |
|
} |
|
|
|
public void Unsubscribe(Action action) { |
|
OnChange -= action; |
|
} |
|
|
|
private void NotifyDataChanged() { |
|
OnChange?.Invoke(); |
|
} |
|
|
|
public void SetBuilds(BuildComparisonModel buildComparisonModel) { |
|
buildComparison = buildComparisonModel; |
|
NotifyDataChanged(); |
|
} |
|
|
|
public BuildComparisonModel Get() { |
|
return buildComparison; |
|
} |
|
|
|
public string AsJson() { |
|
var options = new JsonSerializerOptions { |
|
WriteIndented = true |
|
}; |
|
options.Converters.Add(new JsonStringEnumConverter()); |
|
return JsonSerializer.Serialize(buildComparison, options); |
|
} |
|
|
|
public bool LoadJson(string data) { |
|
try { |
|
var options = new JsonSerializerOptions { |
|
WriteIndented = true |
|
}; |
|
options.Converters.Add(new JsonStringEnumConverter()); |
|
buildComparison = JsonSerializer.Deserialize<BuildComparisonModel>(data, options)!; |
|
|
|
// Must Hydrate because not loaded with Parts |
|
HydratedLoadedJson(); |
|
|
|
NotifyDataChanged(); |
|
return true; |
|
} |
|
catch { |
|
return false; |
|
} |
|
} |
|
|
|
public string BuildOrderAsYaml() { |
|
var stringBuilder = new StringBuilder(); |
|
var serializer = new Serializer(); |
|
stringBuilder.AppendLine(serializer.Serialize(buildComparison)); |
|
var buildOrderText = stringBuilder.ToString(); |
|
return buildOrderText; |
|
} |
|
|
|
|
|
public void HydratedLoadedJson() { |
|
foreach (var build in buildComparison.Builds) |
|
foreach (var orders in build.StartedOrders.Values) |
|
foreach (var order in orders) |
|
order.Copy(EntityModel.Get(order.DataType)); |
|
} |
|
} |