From 74fefb3b7de69eca97ab6a7ee28bf8e984ed0d8d Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 11 Jan 2026 14:04:33 -0500 Subject: [PATCH] Admin --- Game/Dungeon/DungeonHandler.cs | 111 +++++++- Game/Dungeon/DungeonHandlerAdmin.cs | 125 +++++++++ Networking/Messages/DungeonOpCodes.cs | 3 +- Services/Admin/Pages/Dungeons/View.cshtml | 12 +- Services/Admin/Pages/Shared/_Layout.cshtml | 312 +++++++-------------- 5 files changed, 342 insertions(+), 221 deletions(-) diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index 244edb7..b3c11f2 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -42,6 +42,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler private Dictionary _mobSpriteConfigs = new(); // MobDefinitionId -> SpriteConfig JSON private bool _initialized = false; + // Tick management private Timer? _tickTimer; private long _currentTick = 0; @@ -1096,7 +1097,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler // Process pending broadcasts ProcessPendingBroadcasts(); - + // Update active connection count for admin display _broadcastService.SetActiveConnectionCount(_playerSessions.Count); @@ -1113,10 +1114,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler { if (death.EntityType == EntityType.Mob && death.KillerType == EntityType.Player) { - // Fire and forget loot generation (async in sync context) _ = ProcessMobDeathLootAsync(instance, death); - - // Check if this was a boss kill + if (instance.Simulation.Mobs.TryGetValue(death.EntityId, out var killedMob)) { if (killedMob.Type == MobType.Boss) @@ -1128,17 +1127,109 @@ public sealed partial class DungeonHandler : IClientMessageHandler } else if (death.EntityType == EntityType.Player) { - // Player died - transfer loot to killer _ = ProcessPlayerDeathLootTransferAsync(instance, death); } } } - // Send state to all players in this dungeon + // Get players in this dungeon var playersInDungeon = _playerSessions.Values .Where(s => s.CurrentDungeonId == dungeonId && s.State == PlayerState.InDungeon) .ToList(); + // ==================== TILE UPDATE BROADCASTING ==================== + // Check for pending tile updates from admin editing + if (instance.HasPendingTileUpdates && instance.LayoutV2 != null && playersInDungeon.Count > 0) + { + instance.HasPendingTileUpdates = false; + + // Get changed tiles if we're tracking them, otherwise send all + object[] tilesToSend; + object[]? walkableUpdates = null; + + if (_changedTiles.TryRemove(dungeonId, out var changes) && changes.Count > 0) + { + // Differential update - only send changed tiles + var changedList = changes.ToList(); + var tileDetails = instance.LayoutV2.TileDetails; + + tilesToSend = changedList + .Select(c => { + var tile = tileDetails.FirstOrDefault(t => t.X == c.x && t.Y == c.y && t.Layer == c.layer); + if (tile != null) + { + return new + { + x = tile.X, + y = tile.Y, + sid = tile.SheetId, + frame = tile.FrameIndex, + l = tile.Layer, + r = tile.Rotation, + fx = tile.FlipX, + fy = tile.FlipY + }; + } + // Tile was removed - send with sid=0 to indicate removal + return new + { + x = c.x, + y = c.y, + sid = 0, + frame = 0, + l = c.layer, + r = 0, + fx = false, + fy = false + }; + }) + .Cast() + .ToArray(); + + // Also send walkable updates for changed positions + var layout = instance.LayoutV2; + walkableUpdates = changedList + .Select(c => c.layer == 0 ? new { x = c.x, y = c.y, w = layout.Walkable[c.x, c.y] } : null) + .Where(w => w != null) + .Distinct() + .Cast() + .ToArray(); + + _log.LogInformation("Broadcasting {Count} tile updates to {PlayerCount} players in dungeon {Id}", + tilesToSend.Length, playersInDungeon.Count, dungeonId); + } + else + { + // Full update - send all tiles (fallback, shouldn't happen often) + tilesToSend = instance.LayoutV2.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 + }).Cast().ToArray(); + + _log.LogInformation("Broadcasting full tile update ({Count} tiles) to {PlayerCount} players in dungeon {Id}", + tilesToSend.Length, playersInDungeon.Count, dungeonId); + } + + // Broadcast to all players in this dungeon + var tileUpdate = new + { + tiles = tilesToSend, + walkable = walkableUpdates + }; + + foreach (var session in playersInDungeon) + { + _ = SendAsync(session.Connection, DungeonOpCodes.DUNGEON_TILE_UPDATE, tileUpdate, CancellationToken.None); + } + } + // ==================== END TILE UPDATE BROADCASTING ==================== + // Check if dungeon was just cleared if (result.Phase == DungeonPhase.Cleared && !instance.ClearedHandled) { @@ -1147,7 +1238,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler { _ = HandleDungeonClearedAsync(session); } - continue; // Skip normal state updates since we're returning everyone to hub + continue; } foreach (var session in playersInDungeon) @@ -1179,8 +1270,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler if (playersInDungeon.Count == 0) { var emptyTime = DateTime.UtcNow - instance.LastPlayerActivity; - - // If boss not killed and empty for 10+ minutes, persist to database + if (!instance.BossKilled && !instance.IsPersisted && emptyTime.TotalMinutes >= 5) { _ = PersistDungeonAsync(instance, CancellationToken.None); @@ -1188,7 +1278,6 @@ public sealed partial class DungeonHandler : IClientMessageHandler _ = TrackDungeonDestroyedAsync(dungeonId); _log.LogInformation("Dungeon {Id} persisted after 10 minutes empty", dungeonId); } - // If boss killed and empty for 5+ minutes, just remove (cleared) else if (instance.BossKilled && emptyTime.TotalMinutes >= 5) { _dungeons.TryRemove(dungeonId, out _); @@ -1203,7 +1292,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler } } } - + /// /// Process pending server broadcasts and send to all connected players /// diff --git a/Game/Dungeon/DungeonHandlerAdmin.cs b/Game/Dungeon/DungeonHandlerAdmin.cs index d9f2349..87f0aa7 100644 --- a/Game/Dungeon/DungeonHandlerAdmin.cs +++ b/Game/Dungeon/DungeonHandlerAdmin.cs @@ -8,6 +8,18 @@ namespace GameServer.Dungeon; /// public partial class DungeonHandler { + // Track changed tiles per dungeon for differential updates + private readonly ConcurrentDictionary> _changedTiles = new(); + + /// + /// Mark a tile as changed for differential updates + /// + public void MarkTileChanged(Guid dungeonId, int x, int y, int layer = 0) + { + var bag = _changedTiles.GetOrAdd(dungeonId, _ => new ConcurrentBag<(int, int, int)>()); + bag.Add((x, y, layer)); + } + /// /// Get all active dungeon instances (for admin viewing) /// @@ -246,6 +258,7 @@ public partial class DungeonHandler } // Mark dungeon as having pending tile updates + MarkTileChanged(dungeonId, x, y, layer); dungeon.HasPendingTileUpdates = true; return true; @@ -300,6 +313,9 @@ public partial class DungeonHandler TileDefinitionId = (int)tile.TileDefId }); } + + // Track change for differential update + MarkTileChanged(dungeonId, tileX, tileY, tile.Layer); } } } @@ -334,6 +350,9 @@ public partial class DungeonHandler TileDefinitionId = tileDef.Id }); } + + // Track change for differential update + MarkTileChanged(dungeonId, x, y, 0); } } @@ -355,12 +374,30 @@ public partial class DungeonHandler if (layer.HasValue) { layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y && t.Layer == layer.Value); + MarkTileChanged(dungeonId, x, y, layer.Value); } else { + // Remove all layers at this position + var removedLayers = layout.TileDetails + .Where(t => t.X == x && t.Y == y) + .Select(t => t.Layer) + .Distinct() + .ToList(); + layout.TileDetails.RemoveAll(t => t.X == x && t.Y == y); layout.Walkable[x, y] = false; layout.Tiles[x, y] = 0; + + // Mark all removed layers as changed + foreach (var l in removedLayers) + { + MarkTileChanged(dungeonId, x, y, l); + } + if (removedLayers.Count == 0) + { + MarkTileChanged(dungeonId, x, y, 0); // At least mark ground layer + } } dungeon.HasPendingTileUpdates = true; @@ -436,6 +473,66 @@ public partial class DungeonHandler player.Armor = invulnerable ? 999999 : 0; return true; } + + /// + /// Get dungeons with pending tile updates (for tick loop) + /// + public IEnumerable GetDungeonsWithPendingTileUpdates() + { + return _dungeons.Values.Where(d => d.HasPendingTileUpdates); + } + + /// + /// Build tile update payload for broadcasting to clients + /// + public object? BuildTileUpdatePayload(Guid dungeonId) + { + if (!_dungeons.TryGetValue(dungeonId, out var dungeon)) + return null; + + if (!dungeon.HasPendingTileUpdates) + return null; + + dungeon.HasPendingTileUpdates = false; + + var layout = dungeon.LayoutV2; + if (layout == null) + return null; + + // Build walkability data + var walkable = new List(); + for (int y = 0; y < layout.Height; y++) + for (int x = 0; x < layout.Width; x++) + walkable.Add(layout.Walkable[x, y]); + + return new + { + width = layout.Width, + height = layout.Height, + walkable = walkable, + tiles = 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 + }), + sheets = layout.TileMapSheets.Select(s => new + { + id = s.Id, + path = s.CdnPath, + tileW = s.TileWidth, + tileH = s.TileHeight, + sheetW = s.SheetWidth, + sheetH = s.SheetHeight, + cols = s.Columns + }) + }; + } } ///// @@ -462,3 +559,31 @@ public class DungeonStats public int CompletedCount { get; set; } public int BossesKilled { get; set; } } + +/// +/// Tile update payload for broadcasting to clients +/// +public class TileUpdatePayload +{ + public List Tiles { get; set; } = new(); + public List Walkable { get; set; } = new(); +} + +public class TileUpdateData +{ + public int X { get; set; } + public int Y { get; set; } + public int SheetId { get; set; } + public int Frame { get; set; } + public int Layer { get; set; } + public int Rotation { get; set; } + public bool FlipX { get; set; } + public bool FlipY { get; set; } +} + +public class WalkableUpdateData +{ + public int X { get; set; } + public int Y { get; set; } + public bool Walkable { get; set; } +} diff --git a/Networking/Messages/DungeonOpCodes.cs b/Networking/Messages/DungeonOpCodes.cs index b5bd242..49f647a 100644 --- a/Networking/Messages/DungeonOpCodes.cs +++ b/Networking/Messages/DungeonOpCodes.cs @@ -15,7 +15,8 @@ public static class DungeonOpCodes public const int DUNGEON_STATE = 302; public const int DUNGEON_LEAVE = 303; public const int DUNGEON_CLEARED = 304; - + public const int DUNGEON_TILE_UPDATE = 305; + // Dungeon Browser (310-319) public const int DUNGEON_LIST_REQUEST = 310; public const int DUNGEON_LIST_RESPONSE = 311; diff --git a/Services/Admin/Pages/Dungeons/View.cshtml b/Services/Admin/Pages/Dungeons/View.cshtml index d9e5b55..28e8331 100644 --- a/Services/Admin/Pages/Dungeons/View.cshtml +++ b/Services/Admin/Pages/Dungeons/View.cshtml @@ -6,7 +6,7 @@
-
- -