Crafting2
This commit is contained in:
@@ -40,18 +40,24 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
|
||||
//LootSystem
|
||||
private LootService _lootService = null!;
|
||||
|
||||
// Crafting
|
||||
private CraftingHandler _craftingHandler = null!;
|
||||
|
||||
public DungeonHandler(
|
||||
ILogger<DungeonHandler> log,
|
||||
ISessionRegistry sessions,
|
||||
IMessageSerializer ser,
|
||||
IServiceScopeFactory scopeFactory, LootService lootService)
|
||||
IServiceScopeFactory scopeFactory,
|
||||
LootService lootService,
|
||||
CraftingHandler craftingHandler)
|
||||
{
|
||||
_log = log;
|
||||
_sessions = sessions;
|
||||
_ser = ser;
|
||||
_scopeFactory = scopeFactory;
|
||||
_lootService = lootService;
|
||||
_craftingHandler = craftingHandler;
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
||||
@@ -82,7 +88,11 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
DungeonOpCodes.DUNGEON_CREATE,
|
||||
DungeonOpCodes.DUNGEON_JOIN,
|
||||
// Asset management
|
||||
DungeonOpCodes.ASSET_MANIFEST_REQUEST
|
||||
DungeonOpCodes.ASSET_MANIFEST_REQUEST,
|
||||
// Crafting
|
||||
DungeonOpCodes.CRAFTING_SKILLS_REQUEST,
|
||||
DungeonOpCodes.CRAFTING_RECIPES_REQUEST,
|
||||
DungeonOpCodes.CRAFTING_ATTEMPT
|
||||
};
|
||||
|
||||
public async Task InitializeAsync()
|
||||
@@ -249,6 +259,17 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
case DungeonOpCodes.ASSET_MANIFEST_REQUEST:
|
||||
await HandleAssetManifestRequest(connection, ct);
|
||||
break;
|
||||
|
||||
// Crafting
|
||||
case DungeonOpCodes.CRAFTING_SKILLS_REQUEST:
|
||||
await HandleCraftingSkillsRequest(connection, session, ct);
|
||||
break;
|
||||
case DungeonOpCodes.CRAFTING_RECIPES_REQUEST:
|
||||
await HandleCraftingRecipesRequest(connection, session, ct);
|
||||
break;
|
||||
case DungeonOpCodes.CRAFTING_ATTEMPT:
|
||||
await HandleCraftingAttempt(connection, session, payload, ct);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -2858,6 +2879,162 @@ public sealed class DungeonHandler : IClientMessageHandler
|
||||
}, ct);
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
var ingredients = request.Ingredients.Select(i => (i.ItemDefinitionId, i.Quantity)).ToList();
|
||||
var (recipe, alreadyKnown) = await _craftingHandler.TryDiscoverRecipeAsync(db, session.CharacterId, ingredients);
|
||||
|
||||
if (recipe == null)
|
||||
{
|
||||
await SendAsync(connection, DungeonOpCodes.CRAFTING_RESULT, new {
|
||||
success = false,
|
||||
error = "No recipe found for these ingredients"
|
||||
}, 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
|
||||
}
|
||||
|
||||
// Request/Input DTOs
|
||||
@@ -2949,3 +3126,16 @@ public class DungeonJoinRequest
|
||||
{
|
||||
public string DungeonId { get; set; } = "";
|
||||
}
|
||||
|
||||
public class CraftingAttemptRequest
|
||||
{
|
||||
public int RecipeId { get; set; }
|
||||
public List<CraftingIngredientInput>? Ingredients { get; set; }
|
||||
public bool Discover { get; set; }
|
||||
}
|
||||
|
||||
public class CraftingIngredientInput
|
||||
{
|
||||
public int ItemDefinitionId { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user