Breakdown DungeonHandler Monolith to sealed partials
This commit is contained in:
@@ -0,0 +1,355 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using DataServer.Data;
|
||||
using DataServer.Data.Entities;
|
||||
using DataServer.Data.Enums;
|
||||
using Server.Networking;
|
||||
using GameServer.Networking.DTOs;
|
||||
using Server.Networking.Messages;
|
||||
|
||||
namespace GameServer.Dungeon;
|
||||
|
||||
#region Stash System (VIP Feature)
|
||||
|
||||
public sealed partial class DungeonHandler
|
||||
{
|
||||
private const int STASH_SLOTS = 50; // Number of stash slots for VIP
|
||||
|
||||
/// <summary>
|
||||
/// Handle request to view stash contents
|
||||
/// </summary>
|
||||
private async Task HandleStashRequest(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
// Check VIP status
|
||||
var account = await db.Accounts.FindAsync(new object[] { session.AccountId }, ct);
|
||||
if (account == null || account.Role < AccountRole.Vip)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "vip_required",
|
||||
message = "Stash is a VIP feature. Upgrade your account to access it!"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Get stash items
|
||||
var stashItems = await db.DungeonStashItems
|
||||
.Where(s => s.CharacterId == session.CharacterId)
|
||||
.Include(s => s.Definition)
|
||||
.OrderBy(s => s.SlotIndex)
|
||||
.ToListAsync(ct);
|
||||
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_STATE, new
|
||||
{
|
||||
slots = STASH_SLOTS,
|
||||
items = stashItems.Select(s => new
|
||||
{
|
||||
id = s.Id,
|
||||
slotIndex = s.SlotIndex,
|
||||
itemDefinitionId = s.ItemDefinitionId,
|
||||
quantity = s.Quantity,
|
||||
storedAt = s.StoredAt,
|
||||
definition = s.Definition == null ? null : new
|
||||
{
|
||||
id = s.Definition.Id,
|
||||
name = s.Definition.Name,
|
||||
description = s.Definition.Description,
|
||||
type = s.Definition.Type.ToString(),
|
||||
rarity = s.Definition.Rarity.ToString(),
|
||||
spritePath = s.Definition.SpritePath,
|
||||
stackable = s.Definition.Stackable,
|
||||
maxStack = s.Definition.MaxStack
|
||||
}
|
||||
})
|
||||
}, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle depositing an item from inventory to stash
|
||||
/// </summary>
|
||||
private async Task HandleStashDeposit(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
// Check VIP status
|
||||
var account = await db.Accounts.FindAsync(new object[] { session.AccountId }, ct);
|
||||
if (account == null || account.Role < AccountRole.Vip)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "vip_required",
|
||||
message = "Stash is a VIP feature."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Must be in hub to use stash
|
||||
if (session.State != PlayerState.InHub)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "not_in_hub",
|
||||
message = "You must be in the hub to access your stash."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var request = _ser.Deserialize<StashDepositRequest>(payload);
|
||||
var inventorySlot = request.InventorySlot;
|
||||
var quantity = request.Quantity > 0 ? request.Quantity : 1;
|
||||
var targetSlot = request.TargetSlot;
|
||||
|
||||
// Find the inventory item
|
||||
var invItem = await db.DungeonCharacterItems
|
||||
.Include(i => i.ItemDefinition)
|
||||
.FirstOrDefaultAsync(i => i.CharacterId == session.CharacterId && i.SlotIndex == inventorySlot && !i.IsEquipped, ct);
|
||||
|
||||
if (invItem == null)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "item_not_found",
|
||||
message = "Item not found in inventory."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Find target slot
|
||||
if (targetSlot < 0)
|
||||
{
|
||||
// Auto-find empty slot
|
||||
var usedSlots = await db.DungeonStashItems
|
||||
.Where(s => s.CharacterId == session.CharacterId)
|
||||
.Select(s => s.SlotIndex)
|
||||
.ToListAsync(ct);
|
||||
|
||||
for (int i = 0; i < STASH_SLOTS; i++)
|
||||
{
|
||||
if (!usedSlots.Contains(i))
|
||||
{
|
||||
targetSlot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (targetSlot < 0)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "stash_full",
|
||||
message = "Your stash is full!"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Validate quantity
|
||||
quantity = Math.Min(quantity, invItem.Quantity);
|
||||
|
||||
// Check if stacking with existing stash item
|
||||
DungeonStashItem? existingStash = null;
|
||||
if (invItem.ItemDefinition?.Stackable == true)
|
||||
{
|
||||
existingStash = await db.DungeonStashItems
|
||||
.Include(s => s.Definition)
|
||||
.FirstOrDefaultAsync(s => s.CharacterId == session.CharacterId &&
|
||||
s.ItemDefinitionId == invItem.ItemDefinitionId, ct);
|
||||
}
|
||||
|
||||
if (existingStash != null && invItem.ItemDefinition?.Stackable == true)
|
||||
{
|
||||
// Stack with existing
|
||||
var maxAdd = (invItem.ItemDefinition?.MaxStack ?? 99) - existingStash.Quantity;
|
||||
var toAdd = Math.Min(quantity, maxAdd);
|
||||
|
||||
if (toAdd > 0)
|
||||
{
|
||||
existingStash.Quantity += toAdd;
|
||||
invItem.Quantity -= toAdd;
|
||||
|
||||
if (invItem.Quantity <= 0)
|
||||
db.DungeonCharacterItems.Remove(invItem);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create new stash entry
|
||||
var stashItem = new DungeonStashItem
|
||||
{
|
||||
CharacterId = session.CharacterId,
|
||||
ItemDefinitionId = invItem.ItemDefinitionId,
|
||||
Quantity = quantity,
|
||||
SlotIndex = targetSlot,
|
||||
StoredAt = DateTime.UtcNow
|
||||
};
|
||||
db.DungeonStashItems.Add(stashItem);
|
||||
|
||||
invItem.Quantity -= quantity;
|
||||
if (invItem.Quantity <= 0)
|
||||
db.DungeonCharacterItems.Remove(invItem);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
message = "Item deposited to stash."
|
||||
}, ct);
|
||||
|
||||
// Send updated inventory and stash
|
||||
await HandleInventoryRequest(connection, session, ct);
|
||||
await HandleStashRequest(connection, session, ct);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle withdrawing an item from stash to inventory
|
||||
/// </summary>
|
||||
private async Task HandleStashWithdraw(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
||||
{
|
||||
using var scope = _scopeFactory.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
// Check VIP status
|
||||
var account = await db.Accounts.FindAsync(new object[] { session.AccountId }, ct);
|
||||
if (account == null || account.Role < AccountRole.Vip)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "vip_required",
|
||||
message = "Stash is a VIP feature."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Must be in hub to use stash
|
||||
if (session.State != PlayerState.InHub)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "not_in_hub",
|
||||
message = "You must be in the hub to access your stash."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
var request = _ser.Deserialize<StashWithdrawRequest>(payload);
|
||||
var stashSlot = request.StashSlot;
|
||||
var quantity = request.Quantity > 0 ? request.Quantity : 1;
|
||||
|
||||
// Find the stash item
|
||||
var stashItem = await db.DungeonStashItems
|
||||
.Include(s => s.Definition)
|
||||
.FirstOrDefaultAsync(s => s.CharacterId == session.CharacterId && s.SlotIndex == stashSlot, ct);
|
||||
|
||||
if (stashItem == null)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "item_not_found",
|
||||
message = "Item not found in stash."
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate quantity
|
||||
quantity = Math.Min(quantity, stashItem.Quantity);
|
||||
|
||||
// Find empty inventory slot or stackable slot
|
||||
var invItems = await db.DungeonCharacterItems
|
||||
.Where(i => i.CharacterId == session.CharacterId && !i.IsEquipped)
|
||||
.Include(i => i.ItemDefinition)
|
||||
.ToListAsync(ct);
|
||||
|
||||
// Check for stackable existing
|
||||
DungeonCharacterItem? targetItem = null;
|
||||
if (stashItem.Definition?.Stackable == true)
|
||||
{
|
||||
targetItem = invItems.FirstOrDefault(i =>
|
||||
i.ItemDefinitionId == stashItem.ItemDefinitionId &&
|
||||
i.Quantity < (i.ItemDefinition?.MaxStack ?? 99));
|
||||
}
|
||||
|
||||
if (targetItem != null)
|
||||
{
|
||||
// Stack with existing
|
||||
var maxAdd = (stashItem.Definition?.MaxStack ?? 99) - targetItem.Quantity;
|
||||
var toAdd = Math.Min(quantity, maxAdd);
|
||||
|
||||
targetItem.Quantity += toAdd;
|
||||
stashItem.Quantity -= toAdd;
|
||||
|
||||
if (stashItem.Quantity <= 0)
|
||||
db.DungeonStashItems.Remove(stashItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Find empty slot (starting from slot 0 for inventory)
|
||||
var usedSlots = invItems.Select(i => i.SlotIndex).ToHashSet();
|
||||
int emptySlot = -1;
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
if (!usedSlots.Contains(i))
|
||||
{
|
||||
emptySlot = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (emptySlot < 0)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = false,
|
||||
error = "inventory_full",
|
||||
message = "Your inventory is full!"
|
||||
}, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create new inventory item
|
||||
var newItem = new DungeonCharacterItem
|
||||
{
|
||||
CharacterId = session.CharacterId,
|
||||
ItemDefinitionId = stashItem.ItemDefinitionId,
|
||||
Quantity = quantity,
|
||||
SlotIndex = emptySlot
|
||||
};
|
||||
db.DungeonCharacterItems.Add(newItem);
|
||||
|
||||
stashItem.Quantity -= quantity;
|
||||
if (stashItem.Quantity <= 0)
|
||||
db.DungeonStashItems.Remove(stashItem);
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
await SendAsync(connection, DungeonOpCodes.STASH_RESULT, new
|
||||
{
|
||||
success = true,
|
||||
message = "Item withdrawn from stash."
|
||||
}, ct);
|
||||
|
||||
// Send updated inventory and stash
|
||||
await HandleInventoryRequest(connection, session, ct);
|
||||
await HandleStashRequest(connection, session, ct);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
Reference in New Issue
Block a user