From 9a5e45c35bd6c176f7a11a0d8b82e55c7b515486 Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 25 Jan 2026 15:36:54 -0500 Subject: [PATCH] Dungeon Changes --- Game/Dungeon/DungeonHandler.cs | 82 ++++++++++++++++++++++----- Networking/Messages/DungeonOpCodes.cs | 1 + 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index dfcb6b3..c95a9dd 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -357,6 +357,9 @@ public sealed partial class DungeonHandler : IClientMessageHandler case DungeonOpCodes.MOVE_ITEM: await HandleMoveItem(connection, session, payload, ct); break; + case DungeonOpCodes.AUTO_STACK_INVENTORY: + await HandleAutoStackInventory(connection, session, ct); + break; case DungeonOpCodes.CHAT_SEND: await HandleChatSend(connection, session, payload, ct); break; @@ -3643,9 +3646,9 @@ public sealed partial class DungeonHandler : IClientMessageHandler if (sourceItem?.ItemDefinition == null || targetItem?.ItemDefinition == null) return; - // Can only stack consumables with the same name - if (sourceItem.ItemDefinition.Type != ItemType.Consumable || targetItem.ItemDefinition.Type != ItemType.Consumable) return; - if (sourceItem.ItemDefinition.Name != targetItem.ItemDefinition.Name) return; + // Can only stack items with the same ID and that are stackable + if (sourceItem.ItemDefinitionId != targetItem.ItemDefinitionId) return; + if (!sourceItem.ItemDefinition.Stackable || !targetItem.ItemDefinition.Stackable) return; // Check max stack var maxStack = targetItem.ItemDefinition.MaxStack; @@ -3702,6 +3705,64 @@ public sealed partial class DungeonHandler : IClientMessageHandler await HandleInventoryRequest(connection, session, ct); } + private async Task HandleAutoStackInventory(IConnection connection, DungeonPlayerSession session, CancellationToken ct) + { + using var scope = _scopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + + // Get all inventory items + var items = await db.Set() + .Include(i => i.ItemDefinition) + .Where(i => i.CharacterId == session.CharacterId && !i.IsEquipped) + .OrderBy(i => i.SlotIndex) + .ToListAsync(ct); + + // Group stackable items by ItemDefinitionId + var stackableGroups = items + .Where(i => i.ItemDefinition.Stackable) + .GroupBy(i => i.ItemDefinitionId) + .Where(g => g.Count() > 1) // Only process if there are multiple stacks + .ToList(); + + foreach (var group in stackableGroups) + { + var maxStack = group.First().ItemDefinition.MaxStack; + var stacks = group.OrderBy(i => i.Quantity).ToList(); + + // Consolidate into as few stacks as possible + for (int i = 0; i < stacks.Count; i++) + { + if (stacks[i].Quantity >= maxStack) continue; + + for (int j = i + 1; j < stacks.Count; j++) + { + if (stacks[j].Quantity == 0) continue; + + var spaceAvailable = maxStack - stacks[i].Quantity; + var amountToMove = Math.Min(stacks[j].Quantity, spaceAvailable); + + if (amountToMove > 0) + { + stacks[i].Quantity += amountToMove; + stacks[j].Quantity -= amountToMove; + + if (stacks[j].Quantity <= 0) + { + db.Set().Remove(stacks[j]); + } + } + + if (stacks[i].Quantity >= maxStack) break; + } + } + } + + await db.SaveChangesAsync(ct); + + // Send updated inventory + await HandleInventoryRequest(connection, session, ct); + } + private DungeonEquipmentSlot? GetEquipSlotForItem(DungeonItemDefinition item) { return item.Type switch @@ -3868,23 +3929,16 @@ public sealed partial class DungeonHandler : IClientMessageHandler /// private async Task HandleDungeonClearedAsync(DungeonPlayerSession session) { - _log.LogInformation("Dungeon cleared! Returning player {Name} to hub", session.Character.Name); + _log.LogInformation("Dungeon cleared! Player {Name} can now exit via portal", session.Character.Name); - // Send dungeon cleared notification + // Send dungeon cleared notification (but don't force exit) await SendAsync(session.Connection, DungeonOpCodes.DUNGEON_STATE, new { phase = "cleared", - message = "Dungeon cleared! All enemies defeated." + message = "Dungeon cleared! Use the portal to exit." }, CancellationToken.None); - // Small delay to let client see the message - await Task.Delay(2000); - - // Remove from dungeon (extracted = true, so they keep XP and loot) - await RemoveFromDungeonAsync(session, true, CancellationToken.None); - - // Send hub state - await SendAsync(session.Connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), CancellationToken.None); + // Don't automatically remove from dungeon - let them use the portal } private async Task HandlePlayerDeathAsync(DungeonPlayerSession session) diff --git a/Networking/Messages/DungeonOpCodes.cs b/Networking/Messages/DungeonOpCodes.cs index 8b65f03..e880018 100644 --- a/Networking/Messages/DungeonOpCodes.cs +++ b/Networking/Messages/DungeonOpCodes.cs @@ -63,6 +63,7 @@ public static class DungeonOpCodes public const int HOTBAR_ASSIGN = 256; public const int STACK_ITEMS = 257; // Stack consumables from source to target slot public const int MOVE_ITEM = 258; // Move/swap item between inventory slots + public const int AUTO_STACK_INVENTORY = 259; // Auto-stack all stackable items in inventory // Chat (260-269) public const int CHAT_SEND = 260;