using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using DataServer.Data;
using DataServer.Data.Entities;
namespace GameServer.Dungeon;
public sealed partial class DungeonHandler
{
private async Task LoadMobDefinitionsAsync()
{
await RefreshMobCacheAsync();
}
///
/// Refresh mob definitions cache. Call this after mobs are modified in admin.
///
public async Task RefreshMobCacheAsync()
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService();
var mobs = await db.Set()
.Where(m => m.IsEnabled)
.ToListAsync();
if (mobs.Count == 0)
{
_log.LogCritical("Dungeon Mobs Table EMPTY");
}
_mobDefinitions = mobs.ToDictionary(m => m.Id);
// Build sprite configs from animation assignments
var spriteConfigBuilder = new MobSpriteConfigBuilder(db);
_mobSpriteConfigs = await spriteConfigBuilder.BuildSpriteConfigsAsync(mobs.Select(m => m.Id));
// Count animation assignments for debugging
var assignmentCount = await db.Set().CountAsync();
_log.LogInformation("Refreshed mob cache: {MobCount} mobs, {AssignmentCount} animation assignments", mobs.Count, assignmentCount);
}
}