continuity/Server/Services/ItemObjectPoolService.cs
Timothy Schenk f5cd6c380e
Some checks failed
Build, Package and Push Images / preprocess (push) Successful in 2s
Build, Package and Push Images / build (push) Successful in 27s
Build, Package and Push Images / sonarqube (push) Has been skipped
Build, Package and Push Images / sbom-scan (push) Successful in 33s
Build, Package and Push Images / container-build (push) Failing after 1m10s
Build, Package and Push Images / container-sbom-scan (push) Has been skipped
feat: Itemdata pooling & benchmarks for storage options
2023-11-08 19:04:37 +01:00

51 lines
1.5 KiB
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Wonderking.Game.Data;
using Wonderking.Game.Reader;
namespace Server.Services;
public class ItemObjectPoolService : IHostedService
{
readonly ConcurrentDictionary<uint, ItemObject> _itemObjectPool = new();
private readonly ItemReader _itemReader;
public ItemObjectPoolService(IConfiguration configuration)
{
_itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue<string>("Path") ??
string.Empty);
}
public Task StartAsync(CancellationToken cancellationToken)
{
var amountOfEntries = _itemReader.GetAmountOfEntries();
ParallelEnumerable.Range(0, (int)amountOfEntries).AsParallel().ForAll(i =>
{
var itemObject = _itemReader.GetEntry((uint)i);
_itemObjectPool.TryAdd(itemObject.ItemID, itemObject);
});
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
_itemReader.Dispose();
return Task.CompletedTask;
}
public ItemObject GetItem(ushort itemId)
{
return _itemObjectPool[itemId];
}
public bool ContainsItem(ushort itemId)
{
return _itemObjectPool.ContainsKey(itemId);
}
public IQueryable<ItemObject> QueryItems()
{
return _itemObjectPool.AsReadOnly().Values.AsQueryable();
}
}