132 lines
4.4 KiB
C#
132 lines
4.4 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.Drops;
|
|
using WingsEmu.Game._enum;
|
|
using WingsEmu.Game.Managers.ServerData;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects.Drops;
|
|
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.Persistence;
|
|
|
|
namespace WingsEmu.Plugins.BasicImplementations.ServerConfigs;
|
|
|
|
public class DropManager : IDropManager
|
|
{
|
|
private static readonly List<DropDTO> EmptyList = new();
|
|
private readonly IKeyValueCache<List<DropDTO>> _dropCache;
|
|
private readonly IEnumerable<DropImportFile> _dropConfigurations;
|
|
|
|
private readonly List<DropDTO> _generalDrops = new();
|
|
|
|
public DropManager(IEnumerable<DropImportFile> dropConfigurations, IKeyValueCache<List<DropDTO>> dropCache)
|
|
{
|
|
_dropConfigurations = dropConfigurations;
|
|
_dropCache = dropCache;
|
|
}
|
|
|
|
public async Task InitializeAsync()
|
|
{
|
|
List<DropDTO> drops = null;
|
|
bool dbFirst = ParserDataPostgresReader.DbFirstEnabled;
|
|
bool strictDbOnly = ParserDataPostgresReader.StrictDbOnlyEnabled;
|
|
|
|
if (dbFirst)
|
|
{
|
|
try
|
|
{
|
|
drops = ParserDataPostgresReader.LoadDrops();
|
|
Log.Info($"[DB_FIRST] Loaded {drops.Count} drops from database");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (strictDbOnly)
|
|
{
|
|
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but failed to load drops from database.", e);
|
|
}
|
|
|
|
Log.Error("[DB_FIRST] Could not load drops from database", e);
|
|
}
|
|
|
|
if (strictDbOnly && (drops == null || drops.Count == 0))
|
|
{
|
|
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but no drops were loaded from database.");
|
|
}
|
|
}
|
|
|
|
if (drops == null || drops.Count == 0)
|
|
{
|
|
drops = new List<DropDTO>();
|
|
foreach (DropImportFile dropImportExportFile in _dropConfigurations)
|
|
{
|
|
drops.AddRange(dropImportExportFile.Drops.SelectMany(s => s.ToDto()));
|
|
}
|
|
|
|
ParserDataPostgresSync.SyncDrops(drops);
|
|
}
|
|
|
|
foreach (DropDTO drop in drops)
|
|
{
|
|
if (drop.MonsterVNum != null)
|
|
{
|
|
string key = $"monsterVnum-{drop.MonsterVNum.Value}";
|
|
List<DropDTO> list = _dropCache.Get(key);
|
|
if (list == null)
|
|
{
|
|
list = new List<DropDTO> { drop };
|
|
_dropCache.Set(key, list);
|
|
continue;
|
|
}
|
|
|
|
list.Add(drop);
|
|
continue;
|
|
}
|
|
|
|
if (drop.MapId != null)
|
|
{
|
|
string key = $"mapId-{drop.MapId.Value}";
|
|
List<DropDTO> list = _dropCache.Get(key);
|
|
if (list == null)
|
|
{
|
|
list = new List<DropDTO> { drop };
|
|
_dropCache.Set(key, list);
|
|
continue;
|
|
}
|
|
|
|
list.Add(drop);
|
|
continue;
|
|
}
|
|
|
|
if (drop.RaceType != null && drop.RaceSubType != null)
|
|
{
|
|
string key = $"race-{drop.RaceType.Value}.{drop.RaceSubType.Value}";
|
|
List<DropDTO> list = _dropCache.Get(key);
|
|
if (list == null)
|
|
{
|
|
list = new List<DropDTO> { drop };
|
|
_dropCache.Set(key, list);
|
|
continue;
|
|
}
|
|
|
|
list.Add(drop);
|
|
continue;
|
|
}
|
|
|
|
_generalDrops.Add(drop);
|
|
}
|
|
}
|
|
|
|
public IEnumerable<DropDTO> GetGeneralDrops() => _generalDrops;
|
|
|
|
public IReadOnlyList<DropDTO> GetDropsByMapId(int mapId) => _dropCache.Get($"mapId-{mapId}") ?? EmptyList;
|
|
|
|
public IReadOnlyList<DropDTO> GetDropsByMonsterVnum(int monsterVnum) => _dropCache.Get($"monsterVnum-{monsterVnum}") ?? EmptyList;
|
|
|
|
public IReadOnlyList<DropDTO> GetDropsByMonsterRace(MonsterRaceType monsterRaceType, byte monsterSubRaceType) => _dropCache.Get($"race-{(byte)monsterRaceType}.{monsterSubRaceType}") ?? EmptyList;
|
|
}
|