continuity/Server/Services/ItemObjectPoolService.cs

52 lines
1.5 KiB
C#
Raw Normal View History

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();
}
}