Refresh fix, Dungeon Builder v2

This commit is contained in:
dingo
2026-01-04 21:35:42 -05:00
parent 89e6684288
commit 7de9bd2761
8 changed files with 1559 additions and 67 deletions
+37 -2
View File
@@ -7,10 +7,14 @@ namespace Server.Services.Admin.Pages;
public class ApiModel : AdminPageModel
{
private readonly CraftingHandler _craftingHandler;
private readonly DungeonHandler? _dungeonHandler;
private readonly LootService? _lootService;
public ApiModel(CraftingHandler craftingHandler)
public ApiModel(CraftingHandler craftingHandler, DungeonHandler? dungeonHandler = null, LootService? lootService = null)
{
_craftingHandler = craftingHandler;
_dungeonHandler = dungeonHandler;
_lootService = lootService;
}
public IActionResult OnGet()
@@ -21,20 +25,51 @@ public class ApiModel : AdminPageModel
public async Task<IActionResult> OnPostRefreshCacheAsync([FromQuery] string? type)
{
var refreshed = new List<string>();
var errors = new List<string>();
try
{
if (type == null || type.Equals("recipes", StringComparison.OrdinalIgnoreCase))
// Refresh recipes
if (type == null || type.Equals("recipes", StringComparison.OrdinalIgnoreCase) || type.Equals("all", StringComparison.OrdinalIgnoreCase))
{
await _craftingHandler.RefreshRecipeCacheAsync();
refreshed.Add("recipes");
}
// Refresh mobs (animations, sprites, definitions)
if (type == null || type.Equals("mobs", StringComparison.OrdinalIgnoreCase) || type.Equals("all", StringComparison.OrdinalIgnoreCase))
{
if (_dungeonHandler != null)
{
await _dungeonHandler.RefreshMobCacheAsync();
refreshed.Add("mobs");
}
else
{
errors.Add("DungeonHandler not available for mob refresh");
}
}
// Refresh loot tables
if (type == null || type.Equals("loot", StringComparison.OrdinalIgnoreCase) || type.Equals("all", StringComparison.OrdinalIgnoreCase))
{
if (_lootService != null)
{
await _lootService.RefreshCacheAsync();
refreshed.Add("loot");
}
else
{
errors.Add("LootService not available for loot refresh");
}
}
return new JsonResult(new
{
success = true,
message = "Cache refreshed",
refreshed,
errors = errors.Count > 0 ? errors : null,
timestamp = DateTime.UtcNow
});
}
+7 -2
View File
@@ -5,6 +5,8 @@
ViewData["Title"] = "Recipes";
}
@Html.AntiForgeryToken()
<div class="space-y-6">
<!-- Header -->
<div class="flex items-center justify-between">
@@ -214,9 +216,12 @@
btn.innerHTML = '<svg class="w-5 h-5 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg> Refreshing...';
try {
const response = await fetch('/admin/api/refresh-cache?type=recipes', {
const response = await fetch('/admin/api?handler=RefreshCache&type=recipes', {
method: 'POST',
credentials: 'same-origin'
credentials: 'same-origin',
headers: {
'RequestVerificationToken': document.querySelector('input[name="__RequestVerificationToken"]')?.value || ''
}
});
const data = await response.json();
+102
View File
@@ -114,6 +114,48 @@
</div>
</div>
</div>
<!-- Cache Management -->
<div class="card p-6">
<h2 class="text-lg font-semibold text-white mb-4">Cache Management</h2>
<p class="text-sm text-gray-400 mb-4">Refresh server caches without restarting. Use after adding/modifying game data.</p>
<div class="grid grid-cols-2 md:grid-cols-4 gap-3" id="cacheButtons">
<button type="button" onclick="refreshCache('recipes')" class="btn btn-secondary flex flex-col items-center gap-1 py-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
</svg>
<span class="text-xs">Recipes</span>
</button>
<button type="button" onclick="refreshCache('mobs')" class="btn btn-secondary flex flex-col items-center gap-1 py-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.828 14.828a4 4 0 01-5.656 0M9 10h.01M15 10h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
</svg>
<span class="text-xs">Mobs</span>
</button>
<button type="button" onclick="refreshCache('loot')" class="btn btn-secondary flex flex-col items-center gap-1 py-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
</svg>
<span class="text-xs">Loot Tables</span>
</button>
<button type="button" onclick="refreshCache('all')" class="btn bg-purple-700 hover:bg-purple-600 text-white flex flex-col items-center gap-1 py-3">
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"/>
</svg>
<span class="text-xs">Refresh All</span>
</button>
</div>
<div id="cacheStatus" class="mt-4 hidden">
<div class="p-3 rounded-lg bg-gray-800 border border-gray-700">
<span id="cacheStatusText" class="text-gray-300"></span>
</div>
</div>
</div>
<!-- Recent Broadcasts -->
<div class="card p-6">
@@ -147,3 +189,63 @@
}
</div>
</div>
@section Scripts {
<script>
async function refreshCache(type) {
const statusDiv = document.getElementById('cacheStatus');
const statusText = document.getElementById('cacheStatusText');
const buttons = document.querySelectorAll('#cacheButtons button');
// Disable all buttons
buttons.forEach(btn => btn.disabled = true);
// Show loading
statusDiv.classList.remove('hidden');
statusText.innerHTML = '<span class="animate-pulse">Refreshing ' + type + ' cache...</span>';
statusDiv.querySelector('div').className = 'p-3 rounded-lg bg-blue-900/50 border border-blue-700';
try {
const token = document.querySelector('input[name="__RequestVerificationToken"]')?.value || '';
const response = await fetch('/admin/api?handler=RefreshCache&type=' + type, {
method: 'POST',
credentials: 'same-origin',
headers: {
'RequestVerificationToken': token
}
});
let data;
const text = await response.text();
try {
data = JSON.parse(text);
} catch (e) {
throw new Error('Invalid response from server: ' + text.substring(0, 100));
}
if (response.ok && data.success) {
statusText.innerHTML = '✓ Cache refreshed: ' + (data.refreshed || []).join(', ');
statusDiv.querySelector('div').className = 'p-3 rounded-lg bg-green-900/50 border border-green-700';
if (data.errors && data.errors.length > 0) {
statusText.innerHTML += '<br><span class="text-yellow-400">Warnings: ' + data.errors.join(', ') + '</span>';
}
} else {
statusText.innerHTML = '✗ Error: ' + (data.error || 'Unknown error');
statusDiv.querySelector('div').className = 'p-3 rounded-lg bg-red-900/50 border border-red-700';
}
} catch (err) {
statusText.innerHTML = '✗ Error: ' + err.message;
statusDiv.querySelector('div').className = 'p-3 rounded-lg bg-red-900/50 border border-red-700';
} finally {
// Re-enable buttons
buttons.forEach(btn => btn.disabled = false);
// Hide status after 5 seconds
setTimeout(() => {
statusDiv.classList.add('hidden');
}, 5000);
}
}
</script>
}