249 lines
8.1 KiB
C#
249 lines
8.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using GameServer.Dungeon;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Entities;
|
|
using System.Text.Json;
|
|
|
|
namespace GameServices.Admin.Pages.Dungeons;
|
|
|
|
public class ViewModel : AdminPageModel
|
|
{
|
|
private readonly DungeonHandler _dungeonHandler;
|
|
private readonly GameDbContext _db;
|
|
|
|
public ViewModel(DungeonHandler dungeonHandler, GameDbContext db)
|
|
{
|
|
_dungeonHandler = dungeonHandler;
|
|
_db = db;
|
|
}
|
|
|
|
public GameServer.Dungeon.DungeonInstance? Dungeon { get; set; }
|
|
public string Duration { get; set; } = "";
|
|
public int RoomCount { get; set; }
|
|
public int MapWidth { get; set; }
|
|
public int MapHeight { get; set; }
|
|
public string WalkableJson { get; set; } = "[]";
|
|
public string PlayersJson { get; set; } = "[]";
|
|
public string MobsJson { get; set; } = "[]";
|
|
public List<DungeonMobDefinition> MobDefinitions { get; set; } = new();
|
|
|
|
public async Task OnGetAsync(Guid id)
|
|
{
|
|
Dungeon = _dungeonHandler.GetDungeon(id);
|
|
|
|
// Load mob definitions for spawn dropdown
|
|
MobDefinitions = await _db.DungeonMobDefinitions
|
|
.Where(m => m.IsEnabled)
|
|
.OrderBy(m => m.Name)
|
|
.ToListAsync();
|
|
|
|
if (Dungeon == null) return;
|
|
|
|
Duration = FormatDuration(DateTime.UtcNow - Dungeon.CreatedAt);
|
|
RoomCount = Dungeon.LayoutV2?.Rooms?.Count ?? Dungeon.Simulation.Layout.Rooms.Count;
|
|
|
|
// Get map dimensions
|
|
var layout = Dungeon.LayoutV2;
|
|
if (layout != null)
|
|
{
|
|
MapWidth = layout.Width;
|
|
MapHeight = layout.Height;
|
|
|
|
// Flatten walkable array
|
|
var walkable = new List<bool>();
|
|
for (int y = 0; y < layout.Height; y++)
|
|
{
|
|
for (int x = 0; x < layout.Width; x++)
|
|
{
|
|
walkable.Add(layout.Walkable[x, y]);
|
|
}
|
|
}
|
|
WalkableJson = JsonSerializer.Serialize(walkable);
|
|
}
|
|
else
|
|
{
|
|
// Fallback for V1 layouts
|
|
MapWidth = 100;
|
|
MapHeight = 100;
|
|
WalkableJson = "[]";
|
|
}
|
|
|
|
// Serialize entity positions
|
|
PlayersJson = JsonSerializer.Serialize(
|
|
Dungeon.Simulation.Players.Values.Select(p => new
|
|
{
|
|
name = p.CharacterName,
|
|
x = p.X,
|
|
y = p.Y,
|
|
health = p.Health,
|
|
maxHealth = p.MaxHealth
|
|
})
|
|
);
|
|
|
|
MobsJson = JsonSerializer.Serialize(
|
|
Dungeon.Simulation.Mobs.Values.Select(m => new
|
|
{
|
|
id = m.Id.ToString(),
|
|
name = m.Name,
|
|
x = m.X,
|
|
y = m.Y,
|
|
health = m.Health,
|
|
maxHealth = m.MaxHealth,
|
|
alive = m.IsAlive,
|
|
isBoss = m.Type == DataServer.Data.Enums.MobType.Boss,
|
|
type = m.Type.ToString(),
|
|
state = m.State.ToString() ?? "Idle",
|
|
definitionId = m.DefinitionId
|
|
})
|
|
);
|
|
}
|
|
|
|
public IActionResult OnGetStatus(Guid id)
|
|
{
|
|
var dungeon = _dungeonHandler.GetDungeon(id);
|
|
if (dungeon == null)
|
|
return new JsonResult(new { error = "Not found" }) { StatusCode = 404 };
|
|
|
|
return new JsonResult(new
|
|
{
|
|
playerCount = dungeon.Simulation.Players.Count,
|
|
mobsAlive = dungeon.Simulation.Mobs.Values.Count(m => m.IsAlive),
|
|
duration = FormatDuration(DateTime.UtcNow - dungeon.CreatedAt),
|
|
players = dungeon.Simulation.Players.Values.Select(p => new
|
|
{
|
|
name = p.CharacterName,
|
|
x = p.X,
|
|
y = p.Y
|
|
}),
|
|
mobs = dungeon.Simulation.Mobs.Values.Select(m => new
|
|
{
|
|
id = m.Id.ToString(),
|
|
name = m.Name,
|
|
x = m.X,
|
|
y = m.Y,
|
|
health = m.Health,
|
|
maxHealth = m.MaxHealth,
|
|
alive = m.IsAlive,
|
|
isBoss = m.Type == DataServer.Data.Enums.MobType.Boss,
|
|
type = m.Type.ToString(),
|
|
state = m.State.ToString() ?? "Idle",
|
|
definitionId = m.DefinitionId
|
|
})
|
|
});
|
|
}
|
|
|
|
public IActionResult OnPostKillMob(Guid id, string mobId)
|
|
{
|
|
var dungeon = _dungeonHandler.GetDungeon(id);
|
|
if (dungeon == null)
|
|
return new JsonResult(new { error = "Dungeon not found" }) { StatusCode = 404 };
|
|
|
|
if (!int.TryParse(mobId, out var mobInt))
|
|
return new JsonResult(new { error = "Invalid mob ID" }) { StatusCode = 400 };
|
|
|
|
if (dungeon.Simulation.Mobs.TryGetValue(mobInt, out var mob))
|
|
{
|
|
mob.Health = 0;
|
|
mob.IsAlive = false;
|
|
return new JsonResult(new { success = true });
|
|
}
|
|
|
|
return new JsonResult(new { error = "Mob not found" }) { StatusCode = 404 };
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostSpawnMob(Guid id, int mobDefId, float x, float y)
|
|
{
|
|
var dungeon = _dungeonHandler.GetDungeon(id);
|
|
if (dungeon == null)
|
|
return new JsonResult(new { error = "Dungeon not found" }) { StatusCode = 404 };
|
|
|
|
var mobDef = await _db.DungeonMobDefinitions.FindAsync(mobDefId);
|
|
if (mobDef == null)
|
|
return new JsonResult(new { error = "Mob definition not found" }) { StatusCode = 404 };
|
|
int mobId = dungeon.Simulation.Mobs.Count + 1;
|
|
var stats = mobDef.GetStatsForBracket(dungeon.LevelBracketMin);
|
|
// Create a new mob instance
|
|
var mob = new MobInstance
|
|
{
|
|
Id = mobId,
|
|
DefinitionId = mobDef.Id,
|
|
Name = mobDef.Name,
|
|
Type = mobDef.Type,
|
|
X = x,
|
|
Y = y,
|
|
Health = stats.MaxHealth,
|
|
MaxHealth = stats.MaxHealth,
|
|
Mana = mobDef.BaseMana,
|
|
MaxMana = mobDef.BaseMana,
|
|
Stats = stats,
|
|
State = MobState.Idle,
|
|
SpriteConfigJson = mobDef.SpriteConfigJson,
|
|
TintColor = mobDef.TintColor,
|
|
Scale = mobDef.Scale,
|
|
Behavior = mobDef.Behavior,
|
|
PreferredRange = mobDef.PreferredRange,
|
|
FleeThreshold = mobDef.FleeThreshold,
|
|
Phases = mobDef.Phases,
|
|
CurrentAnimation = "idle_down",
|
|
FacingDirection = "down"
|
|
};
|
|
|
|
dungeon.Simulation.Mobs[mob.Id] = mob;
|
|
|
|
return new JsonResult(new { success = true, mobId = mob.Id });
|
|
}
|
|
|
|
public IActionResult OnPostKillAllMobs(Guid id)
|
|
{
|
|
var dungeon = _dungeonHandler.GetDungeon(id);
|
|
if (dungeon != null)
|
|
{
|
|
foreach (var mob in dungeon.Simulation.Mobs.Values)
|
|
{
|
|
mob.Health = 0;
|
|
mob.IsAlive = false;
|
|
}
|
|
TempData["Message"] = "All mobs killed.";
|
|
}
|
|
|
|
return RedirectToPage(new { id });
|
|
}
|
|
|
|
public IActionResult OnPostCompleteDungeon(Guid id)
|
|
{
|
|
var dungeon = _dungeonHandler.GetDungeon(id);
|
|
if (dungeon != null)
|
|
{
|
|
dungeon.Simulation.ForceComplete();
|
|
TempData["Message"] = "Dungeon marked as completed.";
|
|
}
|
|
|
|
return RedirectToPage(new { id });
|
|
}
|
|
|
|
public IActionResult OnPostDelete(Guid id)
|
|
{
|
|
var result = _dungeonHandler.ForceCloseDungeon(id);
|
|
if (result)
|
|
{
|
|
TempData["Message"] = "Dungeon deleted.";
|
|
}
|
|
else
|
|
{
|
|
TempData["Error"] = "Failed to delete dungeon.";
|
|
}
|
|
|
|
return RedirectToPage("/Dungeons/Index");
|
|
}
|
|
|
|
private static string FormatDuration(TimeSpan duration)
|
|
{
|
|
if (duration.TotalHours >= 1)
|
|
return $"{(int)duration.TotalHours}h {duration.Minutes}m";
|
|
if (duration.TotalMinutes >= 1)
|
|
return $"{(int)duration.TotalMinutes}m {duration.Seconds}s";
|
|
return $"{(int)duration.TotalSeconds}s";
|
|
}
|
|
}
|