2023-11-21 20:37:50 +00:00
|
|
|
// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License.
|
2023-11-20 18:58:30 +00:00
|
|
|
|
2023-11-08 18:04:37 +00:00
|
|
|
using System.Collections.Concurrent;
|
2024-01-29 07:39:18 +00:00
|
|
|
using Continuity.AuthServer.DB.Documents;
|
2023-11-08 18:04:37 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2023-11-15 19:00:08 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2023-11-08 18:04:37 +00:00
|
|
|
using Wonderking.Game.Data;
|
|
|
|
using Wonderking.Game.Reader;
|
|
|
|
|
2024-01-29 07:39:18 +00:00
|
|
|
namespace Continuity.AuthServer.Services;
|
2023-11-08 18:04:37 +00:00
|
|
|
|
|
|
|
public class ItemObjectPoolService : IHostedService
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
private readonly ConcurrentDictionary<uint, ItemObject> _itemObjectPool;
|
2023-11-08 18:04:37 +00:00
|
|
|
private readonly ItemReader _itemReader;
|
2023-11-15 19:00:08 +00:00
|
|
|
private readonly ILogger<ItemObjectPoolService> _logger;
|
2023-11-08 18:04:37 +00:00
|
|
|
|
2023-11-15 19:00:08 +00:00
|
|
|
public ItemObjectPoolService(IConfiguration configuration, ILogger<ItemObjectPoolService> logger)
|
2023-11-08 18:04:37 +00:00
|
|
|
{
|
2023-11-15 19:00:08 +00:00
|
|
|
_logger = logger;
|
2023-11-08 18:04:37 +00:00
|
|
|
_itemReader = new ItemReader(configuration.GetSection("Game").GetSection("Data").GetValue<string>("Path") ??
|
|
|
|
string.Empty);
|
2023-11-16 05:47:28 +00:00
|
|
|
|
|
|
|
_itemObjectPool = new ConcurrentDictionary<uint, ItemObject>();
|
2023-11-08 18:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var amountOfEntries = _itemReader.GetAmountOfEntries();
|
2023-11-15 19:00:08 +00:00
|
|
|
Parallel.For(0, (int)amountOfEntries, i =>
|
2023-11-08 18:04:37 +00:00
|
|
|
{
|
|
|
|
var itemObject = _itemReader.GetEntry((uint)i);
|
2023-11-15 19:00:08 +00:00
|
|
|
var result = _itemObjectPool.TryAdd(itemObject.ItemID, itemObject);
|
|
|
|
if (!result)
|
|
|
|
{
|
2023-11-16 05:47:28 +00:00
|
|
|
throw new KeyNotFoundException($"Failed to add item {itemObject.ItemID} to the item object pool");
|
2023-11-15 19:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_logger.LogTrace("Item with {ID} has been added", itemObject.ItemID);
|
2023-11-08 18:04:37 +00:00
|
|
|
});
|
2023-11-15 19:00:08 +00:00
|
|
|
_logger.LogInformation("A total of {AmountOfEntries} items have been added to the item object pool",
|
|
|
|
_itemObjectPool.Count);
|
2023-11-15 21:12:59 +00:00
|
|
|
|
2023-11-08 18:04:37 +00:00
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemObject GetItem(ushort itemId)
|
|
|
|
{
|
2023-11-15 19:00:08 +00:00
|
|
|
_ = _itemObjectPool.TryGetValue(itemId, out var itemObject);
|
|
|
|
return itemObject;
|
2023-11-08 18:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public bool ContainsItem(ushort itemId)
|
|
|
|
{
|
|
|
|
return _itemObjectPool.ContainsKey(itemId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IQueryable<ItemObject> QueryItems()
|
|
|
|
{
|
|
|
|
return _itemObjectPool.AsReadOnly().Values.AsQueryable();
|
|
|
|
}
|
2023-11-15 19:00:08 +00:00
|
|
|
|
|
|
|
public InventoryItem GetBaseInventoryItem(ushort itemId, ushort count = 1, bool isWorn = false)
|
|
|
|
{
|
2023-11-19 16:07:28 +00:00
|
|
|
var item = GetItem(itemId);
|
2023-11-15 19:00:08 +00:00
|
|
|
return new InventoryItem
|
|
|
|
{
|
|
|
|
ItemId = itemId,
|
|
|
|
Count = count,
|
2023-11-19 14:00:56 +00:00
|
|
|
Slot = (byte)item.SlotNo1,
|
|
|
|
InventoryTab =
|
|
|
|
item.ItemType switch
|
|
|
|
{
|
|
|
|
1 => InventoryTab.WornCashEquipment,
|
|
|
|
2 => isWorn ? InventoryTab.WornEquipment : InventoryTab.Equipment,
|
|
|
|
3 => InventoryTab.Etc,
|
|
|
|
4 => isWorn ? InventoryTab.WornCashEquipment : InventoryTab.Cash,
|
|
|
|
5 => InventoryTab.Warehouse,
|
|
|
|
0 => InventoryTab.WornEquipment,
|
2023-11-25 13:29:17 +00:00
|
|
|
_ => InventoryTab.WornEquipment
|
2023-11-19 14:00:56 +00:00
|
|
|
},
|
2023-11-15 19:00:08 +00:00
|
|
|
Level = item.MinimumLevelRequirement,
|
|
|
|
Rarity = 0,
|
|
|
|
AddOption = 0,
|
|
|
|
AddOption2 = 0,
|
|
|
|
AddOption3 = 0,
|
|
|
|
Option = 0,
|
|
|
|
Option2 = 0,
|
|
|
|
Option3 = 0
|
|
|
|
};
|
|
|
|
}
|
2023-11-08 18:04:37 +00:00
|
|
|
}
|