143 lines
5 KiB
C#
143 lines
5 KiB
C#
// WingsEmu
|
|
//
|
|
// Developed by NosWings Team
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using PhoenixLib.Caching;
|
|
using PhoenixLib.Logging;
|
|
using WingsAPI.Data.Shops;
|
|
using WingsEmu.DTOs.Shops;
|
|
using WingsEmu.Game.Entities;
|
|
using WingsEmu.Game.Managers.ServerData;
|
|
using WingsEmu.Game.Shops;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects.Npcs;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.Persistence;
|
|
|
|
namespace WingsEmu.Plugins.BasicImplementations.ServerConfigs;
|
|
|
|
public class ShopManager : IShopManager
|
|
{
|
|
private readonly IEnumerable<MapNpcImportFile> _importFile;
|
|
private readonly IShopFactory _shopFactory;
|
|
|
|
private readonly ILongKeyCachedRepository<ShopNpc> _shopsByNpcId;
|
|
|
|
public ShopManager(IEnumerable<MapNpcImportFile> importFile, ILongKeyCachedRepository<ShopNpc> shopsByNpcId, IShopFactory shopFactory)
|
|
{
|
|
_importFile = importFile;
|
|
_shopsByNpcId = shopsByNpcId;
|
|
_shopFactory = shopFactory;
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
List<ShopDTO> allShops = null;
|
|
bool dbFirst = ParserDataPostgresReader.DbFirstEnabled;
|
|
bool strictDbOnly = ParserDataPostgresReader.StrictDbOnlyEnabled;
|
|
|
|
if (dbFirst)
|
|
{
|
|
try
|
|
{
|
|
allShops = ParserDataPostgresReader.LoadShops();
|
|
Log.Info($"[DB_FIRST] Loaded {allShops.Count} shops from database");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (strictDbOnly)
|
|
{
|
|
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but failed to load shops from database.", e);
|
|
}
|
|
|
|
Log.Error("[DB_FIRST] Could not load shops from database", e);
|
|
}
|
|
|
|
if (strictDbOnly && (allShops == null || allShops.Count == 0))
|
|
{
|
|
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but no shops were loaded from database.");
|
|
}
|
|
}
|
|
|
|
if (allShops == null || allShops.Count == 0)
|
|
{
|
|
IEnumerable<MapNpcObject> importedNpcs = _importFile.SelectMany(x => x.Npcs.Select(s =>
|
|
{
|
|
s.MapId = x.MapId;
|
|
return s;
|
|
})).ToList();
|
|
|
|
allShops = new List<ShopDTO>();
|
|
|
|
foreach (MapNpcObject npc in importedNpcs)
|
|
{
|
|
try
|
|
{
|
|
if (npc.ItemShop == null && npc.SkillShop == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
ShopDTO shop = npc.SkillShop?.ToDto() ?? npc.ItemShop.ToDto();
|
|
|
|
shop.MapNpcId = npc.MapNpcId;
|
|
|
|
if (shop.MenuType == 1)
|
|
{
|
|
shop.Skills = new List<ShopSkillDTO>();
|
|
foreach (MapNpcShopTabObject<MapNpcShopSkillObject> tabs in npc.SkillShop.ShopTabs.Where(x => x.Items != null))
|
|
{
|
|
short index = 0;
|
|
shop.Skills.AddRange(tabs.Items.Select(x =>
|
|
{
|
|
ShopSkillDTO tpp = x.ToDto((byte)tabs.ShopTabId, index);
|
|
index++;
|
|
return tpp;
|
|
}));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
short i = 0;
|
|
shop.Items = new List<ShopItemDTO>();
|
|
foreach (MapNpcShopTabObject<MapNpcShopItemObject> tabs in npc.ItemShop.ShopTabs.Where(tabs => tabs.Items != null))
|
|
{
|
|
shop.Items.AddRange(tabs.Items.Select(s =>
|
|
{
|
|
ShopItemDTO tpp = s.ToDto((byte)tabs.ShopTabId, i);
|
|
i++;
|
|
return tpp;
|
|
}));
|
|
}
|
|
}
|
|
|
|
allShops.Add(shop);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error("[MAPNPC_IMPORT] ERROR", e);
|
|
}
|
|
}
|
|
|
|
ParserDataPostgresSync.SyncShops(allShops);
|
|
}
|
|
|
|
int shopItemsCount = 0;
|
|
int shopSkillsCount = 0;
|
|
foreach (ShopDTO shop in allShops)
|
|
{
|
|
_shopsByNpcId.Set(shop.MapNpcId, _shopFactory.CreateShop(shop));
|
|
shopItemsCount += shop.Items?.Count ?? 0;
|
|
shopSkillsCount += shop.Skills?.Count ?? 0;
|
|
}
|
|
|
|
Log.Info($"[SHOP_MANAGER] Loaded {allShops.Count.ToString()} shops.");
|
|
Log.Info($"[SHOP_MANAGER] Loaded {shopItemsCount.ToString()} shops items.");
|
|
Log.Info($"[SHOP_MANAGER] Loaded {shopSkillsCount.ToString()} shops skills.");
|
|
}
|
|
|
|
public ShopNpc GetShopByNpcId(int npcId) => _shopsByNpcId.Get(npcId);
|
|
}
|