QOL and Inventory
This commit is contained in:
@@ -351,6 +351,12 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
case DungeonOpCodes.USE_ITEM:
|
case DungeonOpCodes.USE_ITEM:
|
||||||
await HandleUseItem(connection, session, payload, ct);
|
await HandleUseItem(connection, session, payload, ct);
|
||||||
break;
|
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:
|
case DungeonOpCodes.CHAT_SEND:
|
||||||
await HandleChatSend(connection, session, payload, ct);
|
await HandleChatSend(connection, session, payload, ct);
|
||||||
break;
|
break;
|
||||||
@@ -1981,13 +1987,36 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
int remaining = drop.Quantity - toAdd;
|
int remaining = drop.Quantity - toAdd;
|
||||||
if (remaining > 0)
|
if (remaining > 0)
|
||||||
{
|
{
|
||||||
// Overflow the rest
|
// Try to create a new stack for remaining items
|
||||||
overflow.Add(new LootDrop
|
int nextSlot = FindNextSlot();
|
||||||
|
if (nextSlot >= 0)
|
||||||
{
|
{
|
||||||
ItemDefinitionId = drop.ItemDefinitionId,
|
var newItem = new DungeonCharacterItem
|
||||||
Item = drop.Item,
|
{
|
||||||
Quantity = remaining
|
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
|
else
|
||||||
@@ -3593,6 +3622,85 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
await HandleInventoryRequest(connection, session, ct);
|
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)
|
private DungeonEquipmentSlot? GetEquipSlotForItem(DungeonItemDefinition item)
|
||||||
{
|
{
|
||||||
return item.Type switch
|
return item.Type switch
|
||||||
@@ -3608,6 +3716,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
|||||||
private class EquipItemRequest { public int SlotIndex { get; set; } }
|
private class EquipItemRequest { public int SlotIndex { get; set; } }
|
||||||
private class UnequipItemRequest { public string Slot { get; set; } = ""; }
|
private class UnequipItemRequest { public string Slot { get; set; } = ""; }
|
||||||
private class UseItemRequest { public int SlotIndex { 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)
|
private async Task SendDungeonStateAsync(DungeonPlayerSession session, TickResult? tickResult)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -368,7 +368,31 @@ public partial class DungeonSimulation
|
|||||||
{
|
{
|
||||||
UpdatePlayer(player);
|
UpdatePlayer(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle corpse cleanup (remove corpses 10 seconds after loot is empty)
|
||||||
|
var corpsesToRemove = new List<int>();
|
||||||
|
foreach (var mob in Mobs.Values.Where(m => !m.IsAlive))
|
||||||
|
{
|
||||||
|
// Check if loot just became empty
|
||||||
|
if (!mob.HasDeathLoot && !mob.LootEmptiedAtTick.HasValue)
|
||||||
|
{
|
||||||
|
mob.LootEmptiedAtTick = CurrentTick;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove corpse 10 seconds after loot emptied (10 ticks per second)
|
||||||
|
if (mob.LootEmptiedAtTick.HasValue &&
|
||||||
|
CurrentTick - mob.LootEmptiedAtTick.Value >= 10 * TicksPerSecond)
|
||||||
|
{
|
||||||
|
corpsesToRemove.Add(mob.Id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove corpses
|
||||||
|
foreach (var mobId in corpsesToRemove)
|
||||||
|
{
|
||||||
|
Mobs.Remove(mobId, out _);
|
||||||
|
}
|
||||||
|
|
||||||
// Check dungeon completion
|
// Check dungeon completion
|
||||||
if (Mobs.Values.All(m => !m.IsAlive))
|
if (Mobs.Values.All(m => !m.IsAlive))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -164,6 +164,9 @@ public class MobInstance
|
|||||||
public List<LootDrop>? DeathLoot { get; set; }
|
public List<LootDrop>? DeathLoot { get; set; }
|
||||||
public long DeathLootGold { get; set; }
|
public long DeathLootGold { get; set; }
|
||||||
|
|
||||||
|
// Corpse cleanup tracking
|
||||||
|
public long? LootEmptiedAtTick { get; set; } // Tick when loot became empty (for corpse removal timer)
|
||||||
|
|
||||||
/// <summary>Check if this mob has loot available</summary>
|
/// <summary>Check if this mob has loot available</summary>
|
||||||
public bool HasDeathLoot => DeathLoot != null && (DeathLoot.Count > 0 || DeathLootGold > 0);
|
public bool HasDeathLoot => DeathLoot != null && (DeathLoot.Count > 0 || DeathLootGold > 0);
|
||||||
|
|
||||||
|
|||||||
@@ -61,6 +61,8 @@ public static class DungeonOpCodes
|
|||||||
public const int USE_ITEM = 254;
|
public const int USE_ITEM = 254;
|
||||||
public const int DROP_ITEM = 255;
|
public const int DROP_ITEM = 255;
|
||||||
public const int HOTBAR_ASSIGN = 256;
|
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
|
||||||
|
|
||||||
// Chat (260-269)
|
// Chat (260-269)
|
||||||
public const int CHAT_SEND = 260;
|
public const int CHAT_SEND = 260;
|
||||||
|
|||||||
Reference in New Issue
Block a user