Files

86 lines
2.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using GameServer.Dungeon;
namespace GameServices.Admin.Pages;
public class ApiModel : AdminPageModel
{
private readonly CraftingHandler _craftingHandler;
private readonly DungeonHandler? _dungeonHandler;
private readonly LootService? _lootService;
public ApiModel(CraftingHandler craftingHandler, DungeonHandler? dungeonHandler = null, LootService? lootService = null)
{
_craftingHandler = craftingHandler;
_dungeonHandler = dungeonHandler;
_lootService = lootService;
}
public IActionResult OnGet()
{
return new JsonResult(new { error = "Method not allowed" }) { StatusCode = 405 };
}
public async Task<IActionResult> OnPostRefreshCacheAsync([FromQuery] string? type)
{
var refreshed = new List<string>();
var errors = new List<string>();
try
{
// 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
});
}
catch (Exception ex)
{
return new JsonResult(new
{
success = false,
error = ex.Message
}) { StatusCode = 500 };
}
}
}