Crafting System v2
This commit is contained in:
@@ -135,8 +135,9 @@ public class CraftingHandler
|
|||||||
/// Try to discover a recipe by combining ingredients
|
/// Try to discover a recipe by combining ingredients
|
||||||
/// Returns the recipe if discovered, null otherwise
|
/// Returns the recipe if discovered, null otherwise
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<(DungeonCraftingRecipe? Recipe, bool AlreadyKnown)> TryDiscoverRecipeAsync(
|
public async Task<(DungeonCraftingRecipe? Recipe, bool AlreadyKnown, string? Error)> TryDiscoverRecipeAsync(
|
||||||
GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients)
|
GameDbContext db, ulong characterId, List<(int ItemDefinitionId, int Quantity)> ingredients,
|
||||||
|
CraftingSkillType? skillTypeFilter = null)
|
||||||
{
|
{
|
||||||
// Build hash from ingredients
|
// Build hash from ingredients
|
||||||
var sortedIngredients = ingredients.OrderBy(i => i.ItemDefinitionId).ThenBy(i => i.Quantity).ToList();
|
var sortedIngredients = ingredients.OrderBy(i => i.ItemDefinitionId).ThenBy(i => i.Quantity).ToList();
|
||||||
@@ -144,18 +145,24 @@ public class CraftingHandler
|
|||||||
|
|
||||||
if (!_ingredientHashToRecipeId.TryGetValue(hash, out var recipeId))
|
if (!_ingredientHashToRecipeId.TryGetValue(hash, out var recipeId))
|
||||||
{
|
{
|
||||||
return (null, false);
|
return (null, false, "No recipe found for these ingredients");
|
||||||
}
|
}
|
||||||
|
|
||||||
var recipe = _recipes[recipeId];
|
var recipe = _recipes[recipeId];
|
||||||
|
|
||||||
|
// Check skill type filter
|
||||||
|
if (skillTypeFilter.HasValue && recipe.SkillType != skillTypeFilter.Value)
|
||||||
|
{
|
||||||
|
return (null, false, $"This combination creates something in {recipe.SkillType}, not {skillTypeFilter.Value}");
|
||||||
|
}
|
||||||
|
|
||||||
// Check if already known
|
// Check if already known
|
||||||
var alreadyKnown = await db.DungeonCharacterKnownRecipes
|
var alreadyKnown = await db.DungeonCharacterKnownRecipes
|
||||||
.AnyAsync(kr => kr.CharacterId == characterId && kr.RecipeId == recipeId);
|
.AnyAsync(kr => kr.CharacterId == characterId && kr.RecipeId == recipeId);
|
||||||
|
|
||||||
if (alreadyKnown)
|
if (alreadyKnown)
|
||||||
{
|
{
|
||||||
return (recipe, true);
|
return (recipe, true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Learn the recipe
|
// Learn the recipe
|
||||||
@@ -169,7 +176,7 @@ public class CraftingHandler
|
|||||||
|
|
||||||
_log.LogInformation("Character {CharId} discovered recipe: {Recipe}", characterId, recipe.Name);
|
_log.LogInformation("Character {CharId} discovered recipe: {Recipe}", characterId, recipe.Name);
|
||||||
|
|
||||||
return (recipe, false);
|
return (recipe, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -336,46 +343,87 @@ public class CraftingHandler
|
|||||||
db.DungeonCraftedItems.Add(craftedItem);
|
db.DungeonCraftedItems.Add(craftedItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find empty inventory slot
|
// Check if we can stack with existing items (only for non-crafted items without stat rolls)
|
||||||
var usedSlots = inventory.Where(i => i.CharacterId == characterId && !i.IsEquipped).Select(i => i.SlotIndex).ToHashSet();
|
int finalSlotIndex = -1;
|
||||||
int emptySlot = -1;
|
bool addedToStack = false;
|
||||||
for (int i = 0; i < 30; i++)
|
|
||||||
|
if (!recipe.HasStatRolls && outputItemDef != null && outputItemDef.Stackable)
|
||||||
{
|
{
|
||||||
if (!usedSlots.Contains(i))
|
// Find existing stack of same item
|
||||||
|
var existingStack = inventory.FirstOrDefault(i =>
|
||||||
|
i.ItemDefinitionId == finalOutputItemId &&
|
||||||
|
!i.IsEquipped &&
|
||||||
|
i.Quantity < (outputItemDef.MaxStack > 0 ? outputItemDef.MaxStack : 99));
|
||||||
|
|
||||||
|
if (existingStack != null)
|
||||||
{
|
{
|
||||||
emptySlot = i;
|
int canAdd = (outputItemDef.MaxStack > 0 ? outputItemDef.MaxStack : 99) - existingStack.Quantity;
|
||||||
break;
|
int toAdd = Math.Min(canAdd, finalOutputQuantity);
|
||||||
|
existingStack.Quantity += toAdd;
|
||||||
|
finalSlotIndex = existingStack.SlotIndex;
|
||||||
|
addedToStack = true;
|
||||||
|
|
||||||
|
// If we couldn't add all, we need a new slot for the rest
|
||||||
|
if (toAdd < finalOutputQuantity)
|
||||||
|
{
|
||||||
|
finalOutputQuantity -= toAdd;
|
||||||
|
addedToStack = false; // Need to create new item for remainder
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
finalOutputQuantity = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (emptySlot == -1)
|
// If not stacked (or remainder), find empty slot and create new item
|
||||||
|
if (!addedToStack || finalOutputQuantity > 0)
|
||||||
{
|
{
|
||||||
return new CraftingResult { Success = false, ErrorMessage = "Inventory full" };
|
var usedSlots = inventory.Where(i => i.CharacterId == characterId && !i.IsEquipped).Select(i => i.SlotIndex).ToHashSet();
|
||||||
}
|
int emptySlot = -1;
|
||||||
|
for (int i = 0; i < 30; i++)
|
||||||
// Create the inventory item
|
|
||||||
var newItem = new DungeonCharacterItem
|
|
||||||
{
|
|
||||||
CharacterId = characterId,
|
|
||||||
ItemDefinitionId = finalOutputItemId,
|
|
||||||
Quantity = finalOutputQuantity,
|
|
||||||
SlotIndex = emptySlot,
|
|
||||||
IsEquipped = false,
|
|
||||||
AcquiredAt = DateTime.UtcNow
|
|
||||||
};
|
|
||||||
db.DungeonCharacterItems.Add(newItem);
|
|
||||||
await db.SaveChangesAsync();
|
|
||||||
|
|
||||||
// Link crafted item if applicable
|
|
||||||
if (craftedItem != null)
|
|
||||||
{
|
|
||||||
db.DungeonCharacterItemCraftedLinks.Add(new DungeonCharacterItemCraftedLink
|
|
||||||
{
|
{
|
||||||
CharacterItemId = newItem.Id,
|
if (!usedSlots.Contains(i))
|
||||||
CraftedItemId = craftedItem.Id
|
{
|
||||||
});
|
emptySlot = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (emptySlot == -1 && finalOutputQuantity > 0)
|
||||||
|
{
|
||||||
|
return new CraftingResult { Success = false, ErrorMessage = "Inventory full" };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (finalOutputQuantity > 0)
|
||||||
|
{
|
||||||
|
var newItem = new DungeonCharacterItem
|
||||||
|
{
|
||||||
|
CharacterId = characterId,
|
||||||
|
ItemDefinitionId = finalOutputItemId,
|
||||||
|
Quantity = finalOutputQuantity,
|
||||||
|
SlotIndex = emptySlot,
|
||||||
|
IsEquipped = false,
|
||||||
|
AcquiredAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
db.DungeonCharacterItems.Add(newItem);
|
||||||
|
finalSlotIndex = emptySlot;
|
||||||
|
|
||||||
|
// Link crafted item if applicable
|
||||||
|
if (craftedItem != null)
|
||||||
|
{
|
||||||
|
await db.SaveChangesAsync(); // Save to get newItem.Id
|
||||||
|
db.DungeonCharacterItemCraftedLinks.Add(new DungeonCharacterItemCraftedLink
|
||||||
|
{
|
||||||
|
CharacterItemId = newItem.Id,
|
||||||
|
CraftedItemId = craftedItem.Id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
// Award XP
|
// Award XP
|
||||||
int xpGained = DungeonCharacterCraftingSkill.GetCraftingXp(recipe.Tier, skill.Level);
|
int xpGained = DungeonCharacterCraftingSkill.GetCraftingXp(recipe.Tier, skill.Level);
|
||||||
skill.Experience += xpGained;
|
skill.Experience += xpGained;
|
||||||
@@ -412,9 +460,10 @@ public class CraftingHandler
|
|||||||
XpGained = xpGained,
|
XpGained = xpGained,
|
||||||
NewSkillLevel = skill.Level,
|
NewSkillLevel = skill.Level,
|
||||||
LeveledUp = leveledUp,
|
LeveledUp = leveledUp,
|
||||||
SlotIndex = emptySlot,
|
SlotIndex = finalSlotIndex,
|
||||||
GotBonusOutput = gotBonusOutput,
|
GotBonusOutput = gotBonusOutput,
|
||||||
OutputQuantity = finalOutputQuantity
|
OutputQuantity = recipe.OutputQuantity,
|
||||||
|
AddedToStack = addedToStack
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -470,4 +519,5 @@ public class CraftingResult
|
|||||||
public int SlotIndex { get; set; }
|
public int SlotIndex { get; set; }
|
||||||
public bool GotBonusOutput { get; set; }
|
public bool GotBonusOutput { get; set; }
|
||||||
public int OutputQuantity { get; set; }
|
public int OutputQuantity { get; set; }
|
||||||
|
public bool AddedToStack { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2961,15 +2961,20 @@ public sealed class DungeonHandler : IClientMessageHandler
|
|||||||
|
|
||||||
if (request.Discover && request.Ingredients != null && request.Ingredients.Count > 0)
|
if (request.Discover && request.Ingredients != null && request.Ingredients.Count > 0)
|
||||||
{
|
{
|
||||||
// Recipe discovery attempt
|
// Recipe discovery attempt with optional skill type filter
|
||||||
var ingredients = request.Ingredients.Select(i => (i.ItemDefinitionId, i.Quantity)).ToList();
|
var ingredients = request.Ingredients.Select(i => (i.ItemDefinitionId, i.Quantity)).ToList();
|
||||||
var (recipe, alreadyKnown) = await _craftingHandler.TryDiscoverRecipeAsync(db, session.CharacterId, ingredients);
|
CraftingSkillType? skillFilter = request.SkillType.HasValue
|
||||||
|
? (CraftingSkillType)request.SkillType.Value
|
||||||
|
: null;
|
||||||
|
|
||||||
|
var (recipe, alreadyKnown, error) = await _craftingHandler.TryDiscoverRecipeAsync(
|
||||||
|
db, session.CharacterId, ingredients, skillFilter);
|
||||||
|
|
||||||
if (recipe == null)
|
if (recipe == null)
|
||||||
{
|
{
|
||||||
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
|
||||||
success = false,
|
success = false,
|
||||||
error = "No recipe found for these ingredients"
|
error = error ?? "No recipe found for these ingredients"
|
||||||
}, ct);
|
}, ct);
|
||||||
}
|
}
|
||||||
else if (alreadyKnown)
|
else if (alreadyKnown)
|
||||||
@@ -3132,6 +3137,7 @@ public class CraftingAttemptRequest
|
|||||||
public int RecipeId { get; set; }
|
public int RecipeId { get; set; }
|
||||||
public List<CraftingIngredientInput>? Ingredients { get; set; }
|
public List<CraftingIngredientInput>? Ingredients { get; set; }
|
||||||
public bool Discover { get; set; }
|
public bool Discover { get; set; }
|
||||||
|
public int? SkillType { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CraftingIngredientInput
|
public class CraftingIngredientInput
|
||||||
|
|||||||
Reference in New Issue
Block a user