Crafting2
This commit is contained in:
@@ -261,6 +261,36 @@ public class CraftingHandler
|
||||
return new CraftingResult { Success = false, ErrorMessage = "Invalid recipe output" };
|
||||
}
|
||||
|
||||
// Check for bonus output (upgraded item at higher levels)
|
||||
bool gotBonusOutput = false;
|
||||
int finalOutputItemId = recipe.OutputItemDefinitionId;
|
||||
int finalOutputQuantity = recipe.OutputQuantity;
|
||||
|
||||
if (recipe.BonusOutputItemDefinitionId.HasValue &&
|
||||
skill.Level >= recipe.BonusOutputMinLevel)
|
||||
{
|
||||
// Calculate bonus chance based on level
|
||||
// At BonusOutputMinLevel: BonusOutputChanceAtMinLevel% chance
|
||||
// At BonusOutputMaxLevel: 100% chance
|
||||
// Linear interpolation between
|
||||
float levelRange = recipe.BonusOutputMaxLevel - recipe.BonusOutputMinLevel;
|
||||
float levelProgress = Math.Min(1f, (skill.Level - recipe.BonusOutputMinLevel) / Math.Max(1f, levelRange));
|
||||
float bonusChance = recipe.BonusOutputChanceAtMinLevel +
|
||||
(100f - recipe.BonusOutputChanceAtMinLevel) * levelProgress;
|
||||
|
||||
var rng = new Random();
|
||||
if (rng.NextDouble() * 100 < bonusChance)
|
||||
{
|
||||
gotBonusOutput = true;
|
||||
finalOutputItemId = recipe.BonusOutputItemDefinitionId.Value;
|
||||
finalOutputQuantity = recipe.BonusOutputQuantity;
|
||||
outputItemDef = await db.DungeonItemDefinitions.FindAsync(finalOutputItemId);
|
||||
|
||||
_log.LogInformation("Character {CharId} got bonus output! {BonusChance:F1}% chance",
|
||||
characterId, bonusChance);
|
||||
}
|
||||
}
|
||||
|
||||
DungeonCraftedItem? craftedItem = null;
|
||||
var rollResults = new List<(string Stat, int Value)>();
|
||||
|
||||
@@ -270,7 +300,7 @@ public class CraftingHandler
|
||||
var rng = new Random();
|
||||
craftedItem = new DungeonCraftedItem
|
||||
{
|
||||
BaseItemDefinitionId = recipe.OutputItemDefinitionId,
|
||||
BaseItemDefinitionId = finalOutputItemId,
|
||||
CrafterId = characterId,
|
||||
CrafterName = character.Name,
|
||||
RecipeId = recipeId,
|
||||
@@ -327,8 +357,8 @@ public class CraftingHandler
|
||||
var newItem = new DungeonCharacterItem
|
||||
{
|
||||
CharacterId = characterId,
|
||||
ItemDefinitionId = recipe.OutputItemDefinitionId,
|
||||
Quantity = recipe.OutputQuantity,
|
||||
ItemDefinitionId = finalOutputItemId,
|
||||
Quantity = finalOutputQuantity,
|
||||
SlotIndex = emptySlot,
|
||||
IsEquipped = false,
|
||||
AcquiredAt = DateTime.UtcNow
|
||||
@@ -382,7 +412,9 @@ public class CraftingHandler
|
||||
XpGained = xpGained,
|
||||
NewSkillLevel = skill.Level,
|
||||
LeveledUp = leveledUp,
|
||||
SlotIndex = emptySlot
|
||||
SlotIndex = emptySlot,
|
||||
GotBonusOutput = gotBonusOutput,
|
||||
OutputQuantity = finalOutputQuantity
|
||||
};
|
||||
}
|
||||
|
||||
@@ -436,4 +468,6 @@ public class CraftingResult
|
||||
public int NewSkillLevel { get; set; }
|
||||
public bool LeveledUp { get; set; }
|
||||
public int SlotIndex { get; set; }
|
||||
public bool GotBonusOutput { get; set; }
|
||||
public int OutputQuantity { get; set; }
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
+2293
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,72 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace SystemRevivalServer.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Crafting2 : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<float>(
|
||||
name: "BonusOutputChanceAtMinLevel",
|
||||
table: "dungeon_crafting_recipes",
|
||||
type: "float",
|
||||
nullable: false,
|
||||
defaultValue: 0f);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "BonusOutputItemDefinitionId",
|
||||
table: "dungeon_crafting_recipes",
|
||||
type: "int",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "BonusOutputMaxLevel",
|
||||
table: "dungeon_crafting_recipes",
|
||||
type: "int",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "BonusOutputMinLevel",
|
||||
table: "dungeon_crafting_recipes",
|
||||
type: "int",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "BonusOutputQuantity",
|
||||
table: "dungeon_crafting_recipes",
|
||||
type: "int",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BonusOutputChanceAtMinLevel",
|
||||
table: "dungeon_crafting_recipes");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BonusOutputItemDefinitionId",
|
||||
table: "dungeon_crafting_recipes");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BonusOutputMaxLevel",
|
||||
table: "dungeon_crafting_recipes");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BonusOutputMinLevel",
|
||||
table: "dungeon_crafting_recipes");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "BonusOutputQuantity",
|
||||
table: "dungeon_crafting_recipes");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -562,6 +562,21 @@ namespace SystemRevivalServer.Migrations
|
||||
b.Property<int>("BaseXp")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<float>("BonusOutputChanceAtMinLevel")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<int?>("BonusOutputItemDefinitionId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("BonusOutputMaxLevel")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("BonusOutputMinLevel")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("BonusOutputQuantity")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.IsRequired()
|
||||
.HasMaxLength(256)
|
||||
|
||||
@@ -60,6 +60,7 @@ builder.Services.AddSingleton<IClientMessageHandler, GameHandler>();
|
||||
builder.Services.AddSingleton<DungeonHandler>();
|
||||
builder.Services.AddSingleton<IClientMessageHandler>(sp => sp.GetRequiredService<DungeonHandler>());
|
||||
builder.Services.AddSingleton<LootService>();
|
||||
builder.Services.AddSingleton<CraftingHandler>();
|
||||
builder.Services.AddSingleton<MessageRouter>();
|
||||
|
||||
builder.Services.AddSingleton<IItemService, CachedItemService>();
|
||||
@@ -86,6 +87,9 @@ var app = builder.Build();
|
||||
var dungeonHandler = app.Services.GetRequiredService<DungeonHandler>();
|
||||
await dungeonHandler.InitializeAsync();
|
||||
|
||||
var craftingHandler = app.Services.GetRequiredService<CraftingHandler>();
|
||||
await craftingHandler.InitializeAsync();
|
||||
|
||||
// DEBUG: Directly try to resolve each service
|
||||
app.Logger.LogInformation("=== DI Debug Start ===");
|
||||
|
||||
|
||||
@@ -74,6 +74,32 @@ public class DungeonCraftingRecipe
|
||||
[MaxLength(1024)]
|
||||
public string? PossibleStatsJson { get; set; }
|
||||
|
||||
// Bonus output system - at higher levels, chance to get upgraded item
|
||||
/// <summary>
|
||||
/// Alternative item to produce at higher levels (null = no bonus output)
|
||||
/// </summary>
|
||||
public int? BonusOutputItemDefinitionId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Minimum level to start getting bonus output chance (e.g., 10)
|
||||
/// </summary>
|
||||
public int BonusOutputMinLevel { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Chance percentage at min level (e.g., 10 for 10%)
|
||||
/// </summary>
|
||||
public float BonusOutputChanceAtMinLevel { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Level at which bonus chance reaches 100% (e.g., 100)
|
||||
/// </summary>
|
||||
public int BonusOutputMaxLevel { get; set; } = 100;
|
||||
|
||||
/// <summary>
|
||||
/// Quantity of bonus item to give
|
||||
/// </summary>
|
||||
public int BonusOutputQuantity { get; set; } = 1;
|
||||
|
||||
public bool IsEnabled { get; set; } = true;
|
||||
|
||||
// Navigation
|
||||
|
||||
Reference in New Issue
Block a user