server-master/srcs/_plugins/WingsEmu.Plugins.BasicImplementation/ServerConfigs/MapMonsterManager.cs

84 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PhoenixLib.Caching;
using PhoenixLib.Logging;
using WingsEmu.DTOs.Maps;
using WingsEmu.Game.Managers.ServerData;
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects;
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.ImportObjects.Monsters;
using WingsEmu.Plugins.BasicImplementations.ServerConfigs.Persistence;
namespace WingsEmu.Plugins.BasicImplementations.ServerConfigs;
public class MapMonsterManager : IMapMonsterManager
{
private readonly IEnumerable<MapMonsterImportFile> _files;
private readonly ILongKeyCachedRepository<MapMonsterDTO> _mapMonsterById;
private readonly IKeyValueCache<List<MapMonsterDTO>> _mapMonsters;
public MapMonsterManager(IEnumerable<MapMonsterImportFile> files, ILongKeyCachedRepository<MapMonsterDTO> mapMonsterById, IKeyValueCache<List<MapMonsterDTO>> mapMonsters)
{
_files = files;
_mapMonsterById = mapMonsterById;
_mapMonsters = mapMonsters;
}
public async Task InitializeAsync()
{
List<MapMonsterDTO> monsters = null;
bool dbFirst = ParserDataPostgresReader.DbFirstEnabled;
bool strictDbOnly = ParserDataPostgresReader.StrictDbOnlyEnabled;
if (dbFirst)
{
try
{
monsters = ParserDataPostgresReader.LoadMapMonsters();
Log.Info($"[DB_FIRST] Loaded {monsters.Count} map_monsters from database");
}
catch (Exception e)
{
if (strictDbOnly)
{
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but failed to load map_monsters from database.", e);
}
Log.Error("[DB_FIRST] Could not load map_monsters from database", e);
}
if (strictDbOnly && (monsters == null || monsters.Count == 0))
{
throw new InvalidOperationException("DB_FIRST/STRICT_DB_ONLY enabled but no map_monsters were loaded from database.");
}
}
if (monsters == null || monsters.Count == 0)
{
monsters = _files.SelectMany(x => x.Monsters.Select(s =>
{
s.MapId = x.MapId;
return s.ToDto();
})).ToList();
ParserDataPostgresSync.SyncMapMonsters(monsters);
}
int count = 0;
foreach (MapMonsterDTO npcDto in monsters)
{
_mapMonsterById.Set(npcDto.Id, npcDto);
_mapMonsters.GetOrSet($"by-map-id-{npcDto.MapId.ToString()}", () => new List<MapMonsterDTO>()).Add(npcDto);
_mapMonsters.GetOrSet($"by-monster-vnum-{npcDto.MonsterVNum.ToString()}", () => new List<MapMonsterDTO>()).Add(npcDto);
count++;
}
Log.Info($"[DATABASE] Loaded {count.ToString()} MapMonsters");
}
public MapMonsterDTO GetById(int mapNpcId) => _mapMonsterById.Get(mapNpcId);
public IReadOnlyList<MapMonsterDTO> GetByMapId(int mapId) => _mapMonsters.Get($"by-map-id-{mapId.ToString()}");
public IReadOnlyList<MapMonsterDTO> GetMapMonstersPerVNum(int npcMonsterVnum) => _mapMonsters.Get($"by-monster-vnum-{npcMonsterVnum.ToString()}");
}