Dungeon viewer changes

This commit is contained in:
dingo
2026-01-11 08:04:20 -05:00
parent 13ee594e29
commit fb2b339121
5 changed files with 720 additions and 726 deletions
+79 -144
View File
@@ -20,12 +20,14 @@ public class ViewModel : AdminPageModel
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 string VisualTilesJson { get; set; } = "[]";
public string TileSheetsJson { get; set; } = "[]";
public int VisualTileCount { get; set; }
public List<DungeonMobDefinition> MobDefinitions { get; set; } = new();
public async Task OnGetAsync(Guid id)
@@ -33,14 +35,11 @@ public class ViewModel : AdminPageModel
Dungeon = _dungeonHandler.GetDungeon(id);
MobDefinitions = await _db.DungeonMobDefinitions
.Where(m => m.IsEnabled)
.OrderBy(m => m.Name)
.ToListAsync();
.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;
var layout = Dungeon.LayoutV2;
if (layout != null)
@@ -53,203 +52,139 @@ public class ViewModel : AdminPageModel
for (int x = 0; x < layout.Width; x++)
walkable.Add(layout.Walkable[x, y]);
WalkableJson = JsonSerializer.Serialize(walkable);
VisualTileCount = layout.TileDetails.Count;
VisualTilesJson = JsonSerializer.Serialize(layout.TileDetails.Select(t => new {
x = t.X, y = t.Y, sid = t.SheetId, frame = t.FrameIndex,
l = t.Layer, r = t.Rotation, fx = t.FlipX, fy = t.FlipY
}));
TileSheetsJson = JsonSerializer.Serialize(layout.TileMapSheets.Select(s => new {
id = s.Id, name = s.CdnPath?.Split('/').LastOrDefault() ?? $"Sheet {s.Id}",
path = s.CdnPath, tileW = s.TileWidth, tileH = s.TileHeight,
sheetW = s.SheetWidth, sheetH = s.SheetHeight, cols = s.Columns
}));
}
else
{
MapWidth = 100;
MapHeight = 100;
WalkableJson = "[]";
}
else { MapWidth = 100; MapHeight = 100; }
PlayersJson = JsonSerializer.Serialize(
Dungeon.Simulation.Players.Values.Select(p => new
{
id = p.CharacterId.ToString(),
name = p.CharacterName,
x = p.X,
y = p.Y,
health = p.Health,
maxHealth = p.MaxHealth,
mana = p.Mana,
maxMana = p.MaxMana,
isAlive = p.IsAlive,
isInCombat = p.IsInCombat
})
);
PlayersJson = JsonSerializer.Serialize(Dungeon.Simulation.Players.Values.Select(p => new {
id = p.CharacterId.ToString(), name = p.CharacterName, x = p.X, y = p.Y,
health = p.Health, maxHealth = p.MaxHealth, isAlive = p.IsAlive
}));
MobsJson = JsonSerializer.Serialize(
Dungeon.Simulation.Mobs.Values.Select(m => new
{
id = m.Id,
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(),
definitionId = m.DefinitionId
})
);
MobsJson = JsonSerializer.Serialize(Dungeon.Simulation.Mobs.Values.Select(m => new {
id = m.Id, 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, state = m.State.ToString()
}));
}
public IActionResult OnGetStatus(Guid id)
{
var dungeon = _dungeonHandler.GetDungeon(id);
if (dungeon == null)
return new JsonResult(new { error = "Not found" }) { StatusCode = 404 };
if (dungeon == null) return new JsonResult(new { error = "Not found" }) { StatusCode = 404 };
return new JsonResult(new
{
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
{
id = p.CharacterId.ToString(),
name = p.CharacterName,
x = p.X,
y = p.Y,
health = p.Health,
maxHealth = p.MaxHealth,
mana = p.Mana,
maxMana = p.MaxMana,
isAlive = p.IsAlive,
isInCombat = p.IsInCombat
players = dungeon.Simulation.Players.Values.Select(p => new {
id = p.CharacterId.ToString(), name = p.CharacterName, x = p.X, y = p.Y,
health = p.Health, maxHealth = p.MaxHealth
}),
mobs = dungeon.Simulation.Mobs.Values.Select(m => new
{
id = m.Id,
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(),
definitionId = m.DefinitionId
mobs = dungeon.Simulation.Mobs.Values.Select(m => new {
id = m.Id, 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, state = m.State.ToString()
})
});
}
// ==================== MOB HANDLERS ====================
public IActionResult OnPostSetTile(Guid id, int x, int y, bool walkable)
{
return new JsonResult(new { success = _dungeonHandler.SetTileWalkable(id, x, y, walkable) });
}
public IActionResult OnPostPlaceVisualTile(Guid id, int x, int y, int sheetId, int frameIndex, int layer = 0, bool walkable = true)
{
return new JsonResult(new { success = _dungeonHandler.PlaceVisualTile(id, x, y, sheetId, frameIndex, layer, walkable) });
}
public IActionResult OnPostRemoveTile(Guid id, int x, int y, int? layer = null)
{
return new JsonResult(new { success = _dungeonHandler.RemoveTile(id, x, y, layer) });
}
public IActionResult OnPostFillRect(Guid id, int x1, int y1, int x2, int y2, bool walkable)
{
return new JsonResult(new { success = _dungeonHandler.FillTileRect(id, x1, y1, x2, y2, walkable) > 0 });
}
public IActionResult OnPostBroadcastTiles(Guid id)
{
var dungeon = _dungeonHandler.GetDungeon(id);
if (dungeon == null) return new JsonResult(new { error = "Not found" }) { StatusCode = 404 };
dungeon.HasPendingTileUpdates = true;
return new JsonResult(new { success = true });
}
public IActionResult OnPostKillMob(Guid id, int mobId)
{
var dungeon = _dungeonHandler.GetDungeon(id);
if (dungeon == null)
return new JsonResult(new { error = "Dungeon not found" }) { StatusCode = 404 };
if (dungeon.Simulation.Mobs.TryGetValue(mobId, out var mob))
if (dungeon?.Simulation.Mobs.TryGetValue(mobId, out var mob) == true)
{
mob.Health = 0;
mob.IsAlive = false;
mob.Health = 0; mob.IsAlive = false;
return new JsonResult(new { success = true });
}
return new JsonResult(new { error = "Mob not found" }) { StatusCode = 404 };
return new JsonResult(new { error = "Not found" }) { StatusCode = 404 };
}
public IActionResult OnPostSpawnMob(Guid id, int mobDefId, float x, float y)
{
var mob = _dungeonHandler.SpawnMobInDungeon(id, mobDefId, x, y);
if (mob == null)
return new JsonResult(new { error = "Failed to spawn mob" }) { StatusCode = 400 };
return new JsonResult(new { success = true, mobId = mob.Id });
return mob != null
? new JsonResult(new { success = true, mobId = mob.Id })
: new JsonResult(new { error = "Failed" }) { StatusCode = 400 };
}
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;
}
}
foreach (var mob in dungeon.Simulation.Mobs.Values) { mob.Health = 0; mob.IsAlive = false; }
return RedirectToPage(new { id });
}
// ==================== TILE EDITING HANDLERS ====================
public IActionResult OnPostSetTile(Guid id, int x, int y, bool walkable)
{
var result = _dungeonHandler.SetTileWalkable(id, x, y, walkable);
return new JsonResult(new { success = result });
}
public IActionResult OnPostFillRect(Guid id, int x1, int y1, int x2, int y2, bool walkable)
{
var count = _dungeonHandler.FillTileRect(id, x1, y1, x2, y2, walkable);
return new JsonResult(new { success = count > 0, count });
}
// ==================== PLAYER HANDLERS ====================
public IActionResult OnPostHealPlayer(Guid id, string playerId)
{
if (!ulong.TryParse(playerId, out var charId))
return new JsonResult(new { error = "Invalid player ID" }) { StatusCode = 400 };
var result = _dungeonHandler.HealPlayer(id, charId);
return new JsonResult(new { success = result });
if (!ulong.TryParse(playerId, out var charId)) return new JsonResult(new { error = "Invalid ID" }) { StatusCode = 400 };
return new JsonResult(new { success = _dungeonHandler.HealPlayer(id, charId) });
}
public IActionResult OnPostTeleportPlayer(Guid id, string playerId, float x, float y)
{
if (!ulong.TryParse(playerId, out var charId))
return new JsonResult(new { error = "Invalid player ID" }) { StatusCode = 400 };
var result = _dungeonHandler.TeleportPlayer(id, charId, x, y);
return new JsonResult(new { success = result });
if (!ulong.TryParse(playerId, out var charId)) return new JsonResult(new { error = "Invalid ID" }) { StatusCode = 400 };
return new JsonResult(new { success = _dungeonHandler.TeleportPlayer(id, charId, x, y) });
}
public IActionResult OnPostKillPlayer(Guid id, string playerId)
{
if (!ulong.TryParse(playerId, out var charId))
return new JsonResult(new { error = "Invalid player ID" }) { StatusCode = 400 };
var result = _dungeonHandler.KillPlayer(id, charId);
return new JsonResult(new { success = result });
if (!ulong.TryParse(playerId, out var charId)) return new JsonResult(new { error = "Invalid ID" }) { StatusCode = 400 };
return new JsonResult(new { success = _dungeonHandler.KillPlayer(id, charId) });
}
public IActionResult OnPostSetGodMode(Guid id, string playerId, bool enabled)
{
if (!ulong.TryParse(playerId, out var charId))
return new JsonResult(new { error = "Invalid player ID" }) { StatusCode = 400 };
var result = _dungeonHandler.SetPlayerInvulnerable(id, charId, enabled);
return new JsonResult(new { success = result });
if (!ulong.TryParse(playerId, out var charId)) return new JsonResult(new { error = "Invalid ID" }) { StatusCode = 400 };
return new JsonResult(new { success = _dungeonHandler.SetPlayerInvulnerable(id, charId, enabled) });
}
// ==================== DUNGEON HANDLERS ====================
public IActionResult OnPostCompleteDungeon(Guid id)
{
var dungeon = _dungeonHandler.GetDungeon(id);
if (dungeon != null)
dungeon.Simulation.ForceComplete();
dungeon?.Simulation.ForceComplete();
return RedirectToPage(new { id });
}
public IActionResult OnPostDelete(Guid id)
{
_dungeonHandler.ForceCloseDungeon(id);
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";
}
private static string FormatDuration(TimeSpan d) =>
d.TotalHours >= 1 ? $"{(int)d.TotalHours}h {d.Minutes}m" :
d.TotalMinutes >= 1 ? $"{(int)d.TotalMinutes}m {d.Seconds}s" : $"{(int)d.TotalSeconds}s";
}