Loot fix
This commit is contained in:
+25
-12
@@ -37,55 +37,68 @@ public class LootService
|
||||
_log = log;
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
if (_initialized) return;
|
||||
|
||||
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
|
||||
// Load all loot tables with their entries
|
||||
var tables = await db.Set<DungeonLootTable>()
|
||||
.Include(t => t.Entries)
|
||||
.Where(t => t.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
// Seed defaults if empty
|
||||
if (tables.Count == 0)
|
||||
{
|
||||
tables = await SeedDefaultLootTablesAsync(db);
|
||||
}
|
||||
|
||||
|
||||
_lootTables = tables.ToDictionary(t => t.Id);
|
||||
|
||||
|
||||
// Load mob -> loot table mappings
|
||||
var mappings = await db.Set<DungeonMobLootTable>()
|
||||
.Include(m => m.LootTable)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
// Seed mob mappings if missing (can happen if tables exist but mappings were cleared)
|
||||
if (mappings.Count == 0 && tables.Count > 0)
|
||||
{
|
||||
_log.LogInformation("No mob loot table mappings found, seeding defaults...");
|
||||
var defaultMappings = GetDefaultMobLootTableMappings(tables);
|
||||
db.Set<DungeonMobLootTable>().AddRange(defaultMappings);
|
||||
await db.SaveChangesAsync();
|
||||
mappings = await db.Set<DungeonMobLootTable>()
|
||||
.Include(m => m.LootTable)
|
||||
.ToListAsync();
|
||||
_log.LogInformation("Seeded {Count} mob loot table mappings", mappings.Count);
|
||||
}
|
||||
|
||||
_mobLootTableMappings = mappings
|
||||
.GroupBy(m => m.MobDefinitionId)
|
||||
.ToDictionary(g => g.Key, g => g.OrderBy(m => m.Priority).ToList());
|
||||
|
||||
|
||||
// Load item definitions
|
||||
var items = await db.Set<DungeonItemDefinition>()
|
||||
.Where(i => i.IsEnabled)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
// Seed default items if empty
|
||||
if (items.Count == 0)
|
||||
{
|
||||
items = await SeedDefaultItemsAsync(db);
|
||||
}
|
||||
|
||||
|
||||
_itemDefinitions = items.ToDictionary(i => i.Id);
|
||||
|
||||
|
||||
_initialized = true;
|
||||
_log.LogInformation("LootService initialized: {Tables} tables, {Items} items, {Mappings} mob mappings",
|
||||
_lootTables.Count, _itemDefinitions.Count, mappings.Count);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Generate loot for a mob kill
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user