Craftingv3
This commit is contained in:
@@ -179,6 +179,63 @@ public class CraftingHandler
|
|||||||
return (recipe, false, null);
|
return (recipe, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Consume materials on failed discovery attempt (65% chance to lose materials)
|
||||||
|
/// </summary>
|
||||||
|
public async Task<(bool MaterialsLost, List<string> LostItems)> TryConsumeMaterialsOnFailedDiscoveryAsync(
|
||||||
|
GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients)
|
||||||
|
{
|
||||||
|
const float MATERIAL_LOSS_CHANCE = 0.65f;
|
||||||
|
var rng = new Random();
|
||||||
|
|
||||||
|
if (rng.NextDouble() >= MATERIAL_LOSS_CHANCE)
|
||||||
|
{
|
||||||
|
// Lucky! Materials saved
|
||||||
|
return (false, new List<string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load inventory
|
||||||
|
var inventory = await db.DungeonCharacterItems
|
||||||
|
.Where(i => i.CharacterId == characterId && !i.IsEquipped)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
var lostItems = new List<string>();
|
||||||
|
|
||||||
|
foreach (var (itemDefId, qty) in ingredients)
|
||||||
|
{
|
||||||
|
int remaining = qty;
|
||||||
|
var stacks = inventory.Where(i => i.ItemDefinitionId == itemDefId).ToList();
|
||||||
|
|
||||||
|
foreach (var stack in stacks)
|
||||||
|
{
|
||||||
|
if (remaining <= 0) break;
|
||||||
|
|
||||||
|
int toRemove = Math.Min(remaining, stack.Quantity);
|
||||||
|
stack.Quantity -= toRemove;
|
||||||
|
remaining -= toRemove;
|
||||||
|
|
||||||
|
if (stack.Quantity <= 0)
|
||||||
|
{
|
||||||
|
db.DungeonCharacterItems.Remove(stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get item name for message
|
||||||
|
var itemDef = await db.DungeonItemDefinitions.FindAsync(itemDefId);
|
||||||
|
if (itemDef != null)
|
||||||
|
{
|
||||||
|
lostItems.Add($"{qty}x {itemDef.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
|
_log.LogInformation("Character {CharId} lost materials on failed discovery: {Items}",
|
||||||
|
characterId, string.Join(", ", lostItems));
|
||||||
|
|
||||||
|
return (true, lostItems);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Craft an item from a known recipe
|
/// Craft an item from a known recipe
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -2972,9 +2972,25 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
|
|
||||||
if (recipe == null)
|
if (recipe == null)
|
||||||
{
|
{
|
||||||
|
// Failed discovery - 65% chance to lose materials
|
||||||
|
var (materialsLost, lostItems) = await _craftingHandler.TryConsumeMaterialsOnFailedDiscoveryAsync(
|
||||||
|
db, session.CharacterId, ingredients);
|
||||||
|
|
||||||
|
var errorMsg = error ?? "No recipe found for these ingredients";
|
||||||
|
if (materialsLost)
|
||||||
|
{
|
||||||
|
errorMsg += $"\nMaterials lost: {string.Join(", ", lostItems)}";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
errorMsg += "\nMaterials saved!";
|
||||||
|
}
|
||||||
|
|
||||||
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
|
||||||
success = false,
|
success = false,
|
||||||
error = error ?? "No recipe found for these ingredients"
|
error = errorMsg,
|
||||||
|
materialsLost = materialsLost,
|
||||||
|
lostItems = lostItems
|
||||||
}, ct);
|
}, ct);
|
||||||
}
|
}
|
||||||
else if (alreadyKnown)
|
else if (alreadyKnown)
|
||||||
|
|||||||
Reference in New Issue
Block a user