2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
pulsar/Pulsar/Features/Market/MarketService.cs
Ben Parsons 579b2b115d Fix issues with Journal handling
Implement basic database
Handle startup events
only send events after the most recent LoadGame
2024-05-25 16:19:07 +10:00

46 lines
1.5 KiB
C#

namespace Pulsar.Features.Market;
using Observatory.Framework.Files;
public interface IMarketService : IJournalHandler<MarketFile>;
public class MarketService(IOptions<PulsarConfiguration> options, IEventHubContext hub, ILogger<MarketService> logger) : IMarketService
{
public async Task<MarketFile> Get()
{
var filePath = Path.Combine(options.Value.JournalDirectory, FileName);
if (!FileHelper.ValidateFile(filePath))
{
return new MarketFile();
}
await using var file = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var market = await JsonSerializer.DeserializeAsync<MarketFile>(file);
if (market != null) return market;
logger.LogWarning("Failed to deserialize market file {File}", filePath);
return new MarketFile();
}
public async Task HandleFile(string path, CancellationToken token = new ())
{
if (!FileHelper.ValidateFile(path))
{
return;
}
var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var market = await JsonSerializer.DeserializeAsync<MarketFile>(file, cancellationToken: token);
if (market == null)
{
logger.LogWarning("Failed to deserialize market File {FilePath}", file);
return;
}
await hub.Clients.All.MarketUpdated(market);
}
public string FileName => FileHandlerService.MarketFileName;
}