This commit is contained in:
dingo
2026-01-11 14:04:33 -05:00
parent 14c50f0aae
commit 74fefb3b7d
5 changed files with 342 additions and 221 deletions
+100 -11
View File
@@ -42,6 +42,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
private Dictionary<int, string?> _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<object>()
.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<object>()
.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<object>().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
}
}
}
/// <summary>
/// Process pending server broadcasts and send to all connected players
/// </summary>