QOL and Inventory

This commit is contained in:
dingo
2026-01-21 13:34:05 -05:00
parent a1c6140143
commit 0863fe1a9f
4 changed files with 146 additions and 7 deletions
+116 -6
View File
@@ -351,6 +351,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler
case DungeonOpCodes.USE_ITEM:
await HandleUseItem(connection, session, payload, ct);
break;
case DungeonOpCodes.STACK_ITEMS:
await HandleStackItems(connection, session, payload, ct);
break;
case DungeonOpCodes.MOVE_ITEM:
await HandleMoveItem(connection, session, payload, ct);
break;
case DungeonOpCodes.CHAT_SEND:
await HandleChatSend(connection, session, payload, ct);
break;
@@ -1981,13 +1987,36 @@ public sealed partial class DungeonHandler : IClientMessageHandler
int remaining = drop.Quantity - toAdd;
if (remaining > 0)
{
// Overflow the rest
overflow.Add(new LootDrop
// Try to create a new stack for remaining items
int nextSlot = FindNextSlot();
if (nextSlot >= 0)
{
ItemDefinitionId = drop.ItemDefinitionId,
Item = drop.Item,
Quantity = remaining
});
var newItem = new DungeonCharacterItem
{
CharacterId = session.CharacterId,
ItemDefinitionId = drop.ItemDefinitionId,
Quantity = remaining,
SlotIndex = nextSlot
};
db.Set<DungeonCharacterItem>().Add(newItem);
existingItems.Add(newItem);
added.Add(new LootDrop
{
ItemDefinitionId = drop.ItemDefinitionId,
Item = drop.Item,
Quantity = remaining
});
}
else
{
// Inventory full - overflow
overflow.Add(new LootDrop
{
ItemDefinitionId = drop.ItemDefinitionId,
Item = drop.Item,
Quantity = remaining
});
}
}
}
else
@@ -3593,6 +3622,85 @@ public sealed partial class DungeonHandler : IClientMessageHandler
await HandleInventoryRequest(connection, session, ct);
}
private async Task HandleStackItems(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
var request = System.Text.Json.JsonSerializer.Deserialize<StackItemsRequest>(json);
if (request == null) return;
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
// Get both items
var sourceItem = await db.Set<DungeonCharacterItem>()
.Include(i => i.ItemDefinition)
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.SourceSlot && !i.IsEquipped, ct);
var targetItem = await db.Set<DungeonCharacterItem>()
.Include(i => i.ItemDefinition)
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.TargetSlot && !i.IsEquipped, ct);
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;
// Check max stack
var maxStack = targetItem.ItemDefinition.MaxStack;
if (targetItem.Quantity >= maxStack) return;
// Stack as much as possible
var spaceAvailable = maxStack - targetItem.Quantity;
var amountToMove = Math.Min(sourceItem.Quantity, spaceAvailable);
targetItem.Quantity += amountToMove;
sourceItem.Quantity -= amountToMove;
// Remove source if empty
if (sourceItem.Quantity <= 0)
{
db.Set<DungeonCharacterItem>().Remove(sourceItem);
}
await db.SaveChangesAsync(ct);
// Send updated inventory
await HandleInventoryRequest(connection, session, ct);
}
private async Task HandleMoveItem(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
{
var json = System.Text.Encoding.UTF8.GetString(payload.Span);
var request = System.Text.Json.JsonSerializer.Deserialize<MoveItemRequest>(json);
if (request == null) return;
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
// Get both items (one or both might be null)
var sourceItem = await db.Set<DungeonCharacterItem>()
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.SourceSlot && !i.IsEquipped, ct);
var targetItem = await db.Set<DungeonCharacterItem>()
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == request.TargetSlot && !i.IsEquipped, ct);
if (sourceItem == null) return; // Nothing to move
// Simple swap of slot indices
if (targetItem != null)
{
// Swap the slots
targetItem.SlotIndex = request.SourceSlot;
}
sourceItem.SlotIndex = request.TargetSlot;
await db.SaveChangesAsync(ct);
// Send updated inventory
await HandleInventoryRequest(connection, session, ct);
}
private DungeonEquipmentSlot? GetEquipSlotForItem(DungeonItemDefinition item)
{
return item.Type switch
@@ -3608,6 +3716,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
private class EquipItemRequest { public int SlotIndex { get; set; } }
private class UnequipItemRequest { public string Slot { get; set; } = ""; }
private class UseItemRequest { public int SlotIndex { get; set; } }
private class StackItemsRequest { public int SourceSlot { get; set; } public int TargetSlot { get; set; } }
private class MoveItemRequest { public int SourceSlot { get; set; } public int TargetSlot { get; set; } }
private async Task SendDungeonStateAsync(DungeonPlayerSession session, TickResult? tickResult)
{