From f1b2749526cc54549372e1988f04112818d24661 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 28 Dec 2025 09:01:40 -0500 Subject: [PATCH] Recipe fix --- Api/AdminController.cs | 35 ++++++++++++++++- Game/Dungeon/CraftingHandler.cs | 11 ++++++ Services/Admin/Pages/Recipes/Index.cshtml | 46 ++++++++++++++++++++--- 3 files changed, 85 insertions(+), 7 deletions(-) diff --git a/Api/AdminController.cs b/Api/AdminController.cs index 133a0c0..c49136a 100644 --- a/Api/AdminController.cs +++ b/Api/AdminController.cs @@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore; using DataServer.Data; using DataServer.Data.Entities; using Server.Services.Auth; +using Server.Game.Dungeon; namespace Server.Api; @@ -15,11 +16,43 @@ public class AdminController : ControllerBase { private readonly GameDbContext _db; private readonly IAdminAuthService _adminAuth; + private readonly CraftingHandler? _craftingHandler; - public AdminController(GameDbContext db, IAdminAuthService adminAuth) + public AdminController(GameDbContext db, IAdminAuthService adminAuth, CraftingHandler? craftingHandler = null) { _db = db; _adminAuth = adminAuth; + _craftingHandler = craftingHandler; + } + + /// + /// Refresh server caches (recipes, items, etc.) + /// + [HttpPost("cache/refresh")] + public async Task RefreshCache([FromQuery] string? type = null) + { + var auth = await ValidateAdminAsync(AccountRole.Admin); + if (!auth.IsAuthorized) return Unauthorized(new { error = auth.Error }); + + var refreshed = new List(); + + // Refresh recipe cache + if (type == null || type.Equals("recipes", StringComparison.OrdinalIgnoreCase)) + { + if (_craftingHandler != null) + { + await _craftingHandler.RefreshRecipeCacheAsync(); + refreshed.Add("recipes"); + } + } + + // Add more cache types here as needed (items, mobs, etc.) + + return Ok(new { + message = "Cache refreshed", + refreshed, + timestamp = DateTime.UtcNow + }); } /// diff --git a/Game/Dungeon/CraftingHandler.cs b/Game/Dungeon/CraftingHandler.cs index c559e03..c65bd40 100644 --- a/Game/Dungeon/CraftingHandler.cs +++ b/Game/Dungeon/CraftingHandler.cs @@ -29,6 +29,14 @@ public class CraftingHandler } public async Task InitializeAsync() + { + await RefreshRecipeCacheAsync(); + } + + /// + /// Refresh the recipe cache from database. Call this after recipes are modified in admin. + /// + public async Task RefreshRecipeCacheAsync() { using var scope = _scopeFactory.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); @@ -40,6 +48,9 @@ public class CraftingHandler _recipes = recipes.ToDictionary(r => r.Id); + // Clear old hash lookup and rebuild + _ingredientHashToRecipeId.Clear(); + // Build ingredient hash lookup for recipe discovery foreach (var recipe in recipes) { diff --git a/Services/Admin/Pages/Recipes/Index.cshtml b/Services/Admin/Pages/Recipes/Index.cshtml index a576d30..7b7259a 100644 --- a/Services/Admin/Pages/Recipes/Index.cshtml +++ b/Services/Admin/Pages/Recipes/Index.cshtml @@ -12,12 +12,20 @@

Crafting Recipes

Manage crafting recipes

- - - - - Create Recipe - +
+ + + + + + Create Recipe + +
@@ -199,6 +207,32 @@ @section Scripts {