206 lines
8.0 KiB
C#
206 lines
8.0 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using DataServer.Data;
|
|
using DataServer.Data.Enums;
|
|
using Server.Networking;
|
|
using GameServer.Networking.DTOs;
|
|
using Server.Networking.Messages;
|
|
using DataServer.Data.Entities;
|
|
|
|
namespace GameServer.Dungeon;
|
|
|
|
public sealed partial class DungeonHandler
|
|
{
|
|
#region Crafting
|
|
|
|
private async Task HandleCraftingSkillsRequest(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
|
|
|
var skills = await _craftingHandler.GetAllCraftingSkillsAsync(db, session.CharacterId);
|
|
|
|
var skillsData = skills.ToDictionary(
|
|
kvp => (int)kvp.Key,
|
|
kvp => new
|
|
{
|
|
level = kvp.Value.Level,
|
|
xp = kvp.Value.Experience,
|
|
xpForNext = kvp.Value.ExperienceForNextLevel,
|
|
totalCrafted = kvp.Value.TotalItemsCrafted
|
|
}
|
|
);
|
|
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_SKILLS_STATE, new { skills = skillsData }, ct);
|
|
}
|
|
|
|
private async Task HandleCraftingRecipesRequest(IConnection connection, DungeonPlayerSession session, CancellationToken ct)
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
|
|
|
var recipes = await _craftingHandler.GetKnownRecipesAsync(db, session.CharacterId);
|
|
|
|
// Get item definitions for names
|
|
var itemDefIds = recipes.SelectMany(r => r.Ingredients.Select(i => i.ItemDefinitionId))
|
|
.Concat(recipes.Select(r => r.OutputItemDefinitionId))
|
|
.Concat(recipes.Where(r => r.BonusOutputItemDefinitionId.HasValue).Select(r => r.BonusOutputItemDefinitionId!.Value))
|
|
.Distinct()
|
|
.ToList();
|
|
|
|
var itemDefs = await db.DungeonItemDefinitions
|
|
.Where(d => itemDefIds.Contains(d.Id))
|
|
.ToDictionaryAsync(d => d.Id, ct);
|
|
|
|
var recipesData = recipes.Select(r => new
|
|
{
|
|
id = r.Id,
|
|
name = r.Name,
|
|
description = r.Description,
|
|
skillType = (int)r.SkillType,
|
|
requiredLevel = r.RequiredLevel,
|
|
tier = r.Tier,
|
|
outputItemDefinitionId = r.OutputItemDefinitionId,
|
|
outputName = itemDefs.TryGetValue(r.OutputItemDefinitionId, out var outDef) ? outDef.Name : "Unknown",
|
|
outputQuantity = r.OutputQuantity,
|
|
hasStatRolls = r.HasStatRolls,
|
|
bonusOutputName = r.BonusOutputItemDefinitionId.HasValue && itemDefs.TryGetValue(r.BonusOutputItemDefinitionId.Value, out var bonusDef)
|
|
? bonusDef.Name
|
|
: null,
|
|
bonusOutputMinLevel = r.BonusOutputMinLevel,
|
|
ingredients = r.Ingredients.Select(i => new
|
|
{
|
|
itemDefinitionId = i.ItemDefinitionId,
|
|
itemName = itemDefs.TryGetValue(i.ItemDefinitionId, out var ingDef) ? ingDef.Name : "Unknown",
|
|
quantity = i.Quantity
|
|
}).ToList()
|
|
});
|
|
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RECIPES_STATE, new { recipes = recipesData }, ct);
|
|
}
|
|
|
|
private async Task HandleCraftingAttempt(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory<byte> payload, CancellationToken ct)
|
|
{
|
|
var request = _ser.Deserialize<CraftingAttemptRequest>(payload);
|
|
if (request == null) return;
|
|
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
|
|
|
// Load character
|
|
var character = await db.DungeonCharacters.FindAsync(new object[] { session.CharacterId }, ct);
|
|
if (character == null)
|
|
{
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new { success = false, error = "Character not found" }, ct);
|
|
return;
|
|
}
|
|
|
|
if (request.Discover && request.Ingredients != null && request.Ingredients.Count > 0)
|
|
{
|
|
// Recipe discovery attempt with optional skill type filter
|
|
var ingredients = request.Ingredients.Select(i => (i.ItemDefinitionId, i.Quantity)).ToList();
|
|
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)
|
|
{
|
|
// 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
|
|
{
|
|
success = false,
|
|
error = errorMsg,
|
|
materialsLost = materialsLost,
|
|
lostItems = lostItems
|
|
}, ct);
|
|
}
|
|
else if (alreadyKnown)
|
|
{
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new
|
|
{
|
|
success = false,
|
|
error = "You already know this recipe!",
|
|
recipeName = recipe.Name
|
|
}, ct);
|
|
}
|
|
else
|
|
{
|
|
// New recipe discovered!
|
|
await SendAsync(connection, DungeonOpCodes.RECIPE_DISCOVERED, new
|
|
{
|
|
id = recipe.Id,
|
|
name = recipe.Name,
|
|
skillType = (int)recipe.SkillType
|
|
}, ct);
|
|
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new
|
|
{
|
|
success = true,
|
|
discovered = true,
|
|
recipeName = recipe.Name
|
|
}, ct);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Normal crafting from known recipe
|
|
var result = await _craftingHandler.CraftItemAsync(db, session.CharacterId, request.RecipeId, character);
|
|
|
|
if (result.Success)
|
|
{
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new
|
|
{
|
|
success = true,
|
|
outputName = result.OutputItem?.Name ?? "Unknown",
|
|
quantity = result.OutputQuantity,
|
|
xpGained = result.XpGained,
|
|
newSkillLevel = result.NewSkillLevel,
|
|
leveledUp = result.LeveledUp,
|
|
gotBonusOutput = result.GotBonusOutput,
|
|
slotIndex = result.SlotIndex,
|
|
rollResults = result.RollResults.Select(r => new { stat = r.Stat, value = r.Value }),
|
|
qualityRating = result.CraftedItem?.QualityRating
|
|
}, ct);
|
|
|
|
if (result.LeveledUp)
|
|
{
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_SKILL_LEVELUP, new
|
|
{
|
|
skillType = (int)result.OutputItem?.Type, // We'd need to get the skill type from recipe
|
|
newLevel = result.NewSkillLevel
|
|
}, ct);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new
|
|
{
|
|
success = false,
|
|
error = result.ErrorMessage
|
|
}, ct);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|