60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Refresh mob definitions cache. Call this after mobs are modified in admin.
|
|
/// </summary>
|
|
public async Task RefreshMobCacheAsync()
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
|
|
|
var mobs = await db.Set<DungeonMobDefinition>()
|
|
.Where(m => m.IsEnabled)
|
|
.Include(m => m.AbilityAssignments)
|
|
.ThenInclude(a => a.AbilityDefinition)
|
|
.ToListAsync();
|
|
|
|
if (mobs.Count == 0)
|
|
{
|
|
_log.LogCritical("Dungeon Mobs Table EMPTY");
|
|
}
|
|
|
|
_mobDefinitions = mobs.ToDictionary(m => m.Id);
|
|
|
|
var abilities = await db.Set<MobAbilityDefinition>()
|
|
.Where(a => a.IsEnabled)
|
|
.ToListAsync();
|
|
|
|
_abilityDefinitions = abilities.ToDictionary(a => a.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<MobAnimationAssignment>().CountAsync();
|
|
var abilityAssignmentsCount = mobs.Sum(m => m.AbilityAssignments?.Count ?? 0);
|
|
_log.LogInformation(
|
|
"Refreshed mob cache: {MobCount} mobs, {AbilityCount} abilities, {AbilityAssignments} ability assignments, {AssignmentCount} animation assignments",
|
|
mobs.Count,
|
|
_abilityDefinitions.Count,
|
|
abilityAssignmentsCount,
|
|
assignmentCount);
|
|
}
|
|
}
|