This commit is contained in:
dingo
2025-12-15 16:07:17 -05:00
parent 8e197eefb2
commit b2ea3c19c1
8 changed files with 1850 additions and 2 deletions
+88 -2
View File
@@ -71,7 +71,8 @@ public sealed class DungeonHandler : IClientMessageHandler
DungeonOpCodes.UNEQUIP_ITEM,
DungeonOpCodes.USE_ITEM,
DungeonOpCodes.DROP_ITEM,
DungeonOpCodes.CHAT_SEND
DungeonOpCodes.CHAT_SEND,
DungeonOpCodes.HOTBAR_ASSIGN
};
public async Task InitializeAsync()
@@ -195,6 +196,9 @@ public sealed class DungeonHandler : IClientMessageHandler
case DungeonOpCodes.CHAT_SEND:
await HandleChatSend(connection, session, payload, ct);
break;
case DungeonOpCodes.HOTBAR_ASSIGN:
await HandleHotbarAssign(connection, session, payload, ct);
break;
}
}
catch (Exception ex)
@@ -545,6 +549,71 @@ public sealed class DungeonHandler : IClientMessageHandler
{
session.IsEscaping = false;
}
/// <summary>
/// Handle hotbar slot assignment - links a hotbar slot to an inventory slot
/// </summary>
private async Task HandleHotbarAssign(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<HotbarAssignRequest>(json);
if (request == null) return;
// Validate slots
if (request.HotbarSlot < 0 || request.HotbarSlot >= 8) return;
if (request.InventorySlot < 0 || request.InventorySlot >= 30) return;
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
// Check if the inventory slot has a consumable item
var invItem = await db.Set<DungeonCharacterItem>()
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId &&
i.SlotIndex == request.InventorySlot &&
!i.IsEquipped, ct);
if (invItem != null)
{
var itemDef = await db.Set<DungeonItemDefinition>()
.FirstOrDefaultAsync(d => d.Id == invItem.ItemDefinitionId, ct);
// Only allow consumables in hotbar
if (itemDef == null || itemDef.Type != ItemType.Consumable)
{
_log.LogDebug("Cannot assign non-consumable item to hotbar");
return;
}
}
// Find existing hotbar entry for this slot
var existingEntry = await db.Set<DungeonBeltItem>()
.FirstOrDefaultAsync(b => b.CharacterId == session.CharacterId &&
b.SlotIndex == request.HotbarSlot, ct);
if (existingEntry != null)
{
// Update existing
existingEntry.InventorySlotIndex = request.InventorySlot;
}
else
{
// Create new
var newEntry = new DungeonBeltItem
{
CharacterId = session.CharacterId,
SlotIndex = request.HotbarSlot,
InventorySlotIndex = request.InventorySlot
};
db.Set<DungeonBeltItem>().Add(newEntry);
}
await db.SaveChangesAsync(ct);
// Send updated inventory state (includes hotbar)
await HandleInventoryRequest(connection, session, ct);
_log.LogDebug("Player {Name} assigned inventory slot {InvSlot} to hotbar slot {HotbarSlot}",
session.Character.Name, request.InventorySlot, request.HotbarSlot);
}
private void TickAll(object? state)
{
@@ -782,7 +851,23 @@ public sealed class DungeonHandler : IClientMessageHandler
};
}
}
// Load hotbar assignments
var hotbarItems = await db.Set<DungeonBeltItem>()
.Where(b => b.CharacterId == session.CharacterId)
.OrderBy(b => b.SlotIndex)
.ToListAsync(ct);
var hotbar = new object?[8];
foreach (var belt in hotbarItems)
{
if (belt.SlotIndex >= 0 && belt.SlotIndex < 8)
{
hotbar[belt.SlotIndex] = new
{
inventory_slot = belt.InventorySlotIndex
};
}
}
// Build equipment map - using your actual enum names
var equipment = new Dictionary<string, object?>();
foreach (var equippedItem in inventoryItems.Where(i => i.IsEquipped))
@@ -824,7 +909,8 @@ public sealed class DungeonHandler : IClientMessageHandler
await SendAsync(connection, DungeonOpCodes.INVENTORY_STATE, new
{
items,
equipment
equipment,
hotbar,
}, ct);
}