Skip to content

Commit

Permalink
Merge pull request #3069 from eugene-doobu/feature/fix-synthesize-pool
Browse files Browse the repository at this point in the history
Refactor Synthesize
  • Loading branch information
eugene-doobu authored Dec 10, 2024
2 parents e0fb1c7 + c7434f8 commit 4b5d212
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 201 deletions.
46 changes: 22 additions & 24 deletions .Lib9c.Tests/Action/SynthesizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ public void ExecuteSingle(Grade grade, ItemSubType itemSubType)
(state, var items) = UpdateItemsFromSubType(grade, itemSubTypes, state, avatarAddress);
state = state.SetActionPoint(avatarAddress, 120);

var previousAvatarState = state.GetAvatarState(avatarAddress);
var action = new Synthesize()
{
AvatarAddress = avatarAddress,
MaterialIds = SynthesizeSimulator.GetItemGuids(items),
ChargeAp = false,
MaterialGradeId = (int)grade,
MaterialItemSubTypeId = (int)itemSubType,
};

var ctx = new ActionContext
Expand Down Expand Up @@ -153,17 +155,11 @@ public void ExecuteSingle(Grade grade, ItemSubType itemSubType)
break;
}

var gradeDict = SynthesizeSimulator.GetGradeDict(
action.MaterialIds,
previousAvatarState,
blockIndex,
avatarAddress.ToHex(),
out _,
out _
);

var inputData = new SynthesizeSimulator.InputData()
{
Grade = grade,
ItemSubType = itemSubType,
MaterialCount = itemSubTypes.Length,
SynthesizeSheet = TableSheets.SynthesizeSheet,
SynthesizeWeightSheet = TableSheets.SynthesizeWeightSheet,
CostumeItemSheet = TableSheets.CostumeItemSheet,
Expand All @@ -173,7 +169,6 @@ out _
EquipmentItemOptionSheet = TableSheets.EquipmentItemOptionSheet,
SkillSheet = TableSheets.SkillSheet,
RandomObject = new TestRandom(),
GradeDict = gradeDict,
};

var result = SynthesizeSimulator.Simulate(inputData)[0];
Expand Down Expand Up @@ -220,11 +215,13 @@ public void ExecuteMultiple(Grade grade, ItemSubType itemSubType)
(state, var items) = UpdateItemsFromSubType(grade, itemSubTypes, state, avatarAddress);
state = state.SetActionPoint(avatarAddress, 120);

var previousAvatarState = state.GetAvatarState(avatarAddress);
var action = new Synthesize()
{
AvatarAddress = avatarAddress,
MaterialIds = SynthesizeSimulator.GetItemGuids(items),
ChargeAp = false,
MaterialGradeId = (int)grade,
MaterialItemSubTypeId = (int)itemSubType,
};

var ctx = new ActionContext
Expand All @@ -235,19 +232,12 @@ public void ExecuteMultiple(Grade grade, ItemSubType itemSubType)
Signer = agentAddress,
};

state = action.Execute(ctx);

var gradeDict = SynthesizeSimulator.GetGradeDict(
action.MaterialIds,
previousAvatarState,
blockIndex,
avatarAddress.ToHex(),
out _,
out _
);

action.Execute(ctx);
var inputData = new SynthesizeSimulator.InputData()
{
Grade = grade,
ItemSubType = itemSubType,
MaterialCount = itemSubTypes.Length,
SynthesizeSheet = TableSheets.SynthesizeSheet,
SynthesizeWeightSheet = TableSheets.SynthesizeWeightSheet,
CostumeItemSheet = TableSheets.CostumeItemSheet,
Expand All @@ -257,7 +247,6 @@ out _
EquipmentItemOptionSheet = TableSheets.EquipmentItemOptionSheet,
SkillSheet = TableSheets.SkillSheet,
RandomObject = new TestRandom(),
GradeDict = gradeDict,
};

var resultList = SynthesizeSimulator.Simulate(inputData);
Expand Down Expand Up @@ -299,6 +288,9 @@ public void ExecuteNotEnoughActionPoint()
{
AvatarAddress = avatarAddress,
MaterialIds = SynthesizeSimulator.GetItemGuids(items),
ChargeAp = false,
MaterialGradeId = (int)grade,
MaterialItemSubTypeId = (int)itemSubType,
};

var ctx = new ActionContext
Expand Down Expand Up @@ -336,6 +328,9 @@ public void ExecuteMultipleSameType(int testCount)
{
AvatarAddress = avatarAddress,
MaterialIds = SynthesizeSimulator.GetItemGuids(items),
ChargeAp = false,
MaterialGradeId = (int)grade,
MaterialItemSubTypeId = (int)itemSubType,
};

var ctx = new ActionContext
Expand Down Expand Up @@ -380,6 +375,9 @@ public void ExecuteInvalidMaterial(Grade grade, ItemSubType[] itemSubTypes)
{
AvatarAddress = avatarAddress,
MaterialIds = SynthesizeSimulator.GetItemGuids(items),
ChargeAp = false,
MaterialGradeId = (int)grade,
MaterialItemSubTypeId = (int)itemSubTypes[0],
};

var ctx = new ActionContext
Expand Down
61 changes: 52 additions & 9 deletions Lib9c/Action/Synthesize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Helper;
using Nekoyume.Model.EnumType;
using Nekoyume.Module;
using Nekoyume.TableData;

Expand All @@ -34,11 +35,30 @@ public class Synthesize : GameAction
private const string MaterialsKey = "m";
private const string ChargeApKey = "c";
private const string AvatarAddressKey = "a";
private const string GradeKey = "g";
private const string ItemSubTypeKey = "i";

#region Fields
/// <summary>
/// Id list of items as material.
/// </summary>
public List<Guid> MaterialIds = new();
/// <summary>
/// Whether to charge action points with action execution.
/// </summary>
public bool ChargeAp;
/// <summary>
/// AvatarAddress of the signer.
/// </summary>
public Address AvatarAddress;
/// <summary>
/// MaterialGrade of the material item.
/// </summary>
public int MaterialGradeId;
/// <summary>
/// ItemSubType of the material item.
/// </summary>
public int MaterialItemSubTypeId;
#endregion Fields

/// <summary>
Expand All @@ -50,6 +70,8 @@ public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
var states = context.PreviousState;
var materialGrade = (Grade)MaterialGradeId;
var materialItemSubType = (ItemSubType)MaterialItemSubTypeId;

// Collect addresses
var addressesHex = GetSignerAndOtherAddressesHex(context, AvatarAddress);
Expand Down Expand Up @@ -85,19 +107,34 @@ public override IWorld Execute(IActionContext context)

// Calculate action point
var actionPoint = CalculateActionPoint(states, avatarState, sheets, context);

// Initialize variables
var gradeDict = SynthesizeSimulator.GetGradeDict(MaterialIds, avatarState, context.BlockIndex,
addressesHex, out var materialEquipments, out var materialCostumes);
var materialItems = SynthesizeSimulator.GetMaterialList(
MaterialIds,
avatarState,
context.BlockIndex,
materialGrade,
materialItemSubType,
addressesHex
);

// Unequip items (if necessary)
foreach (var materialEquipment in materialEquipments)
foreach (var materialItem in materialItems)
{
materialEquipment.Unequip();
switch (materialItem)
{
case Equipment equipment:
equipment.Unequip();
break;
case Costume costume:
costume.Unequip();
break;
}
}
foreach (var materialCostume in materialCostumes)

if (materialItems.Count == 0 || materialItems.Count != MaterialIds.Count)
{
materialCostume.Unequip();
throw new InvalidMaterialException(
$"{addressesHex} Aborted as the material item is not valid."
);
}

// Remove materials from inventory
Expand All @@ -113,6 +150,9 @@ public override IWorld Execute(IActionContext context)

var synthesizedItems = SynthesizeSimulator.Simulate(new SynthesizeSimulator.InputData()
{
Grade = materialGrade,
ItemSubType = materialItemSubType,
MaterialCount = materialItems.Count,
SynthesizeSheet = sheets.GetSheet<SynthesizeSheet>(),
SynthesizeWeightSheet = sheets.GetSheet<SynthesizeWeightSheet>(),
CostumeItemSheet = sheets.GetSheet<CostumeItemSheet>(),
Expand All @@ -123,7 +163,6 @@ public override IWorld Execute(IActionContext context)
SkillSheet = sheets.GetSheet<SkillSheet>(),
BlockIndex = context.BlockIndex,
RandomObject = context.GetRandom(),
GradeDict = gradeDict,
});

// Add synthesized items to inventory
Expand Down Expand Up @@ -184,6 +223,8 @@ private long CalculateActionPoint(IWorld states, AvatarState avatarState, Sheets
[MaterialsKey] = new List(MaterialIds.OrderBy(i => i).Select(i => i.Serialize())),
[ChargeApKey] = ChargeAp.Serialize(),
[AvatarAddressKey] = AvatarAddress.Serialize(),
[GradeKey] = (Integer)MaterialGradeId,
[ItemSubTypeKey] = (Integer)MaterialItemSubTypeId,
}
.ToImmutableDictionary();

Expand All @@ -192,6 +233,8 @@ protected override void LoadPlainValueInternal(IImmutableDictionary<string, IVal
MaterialIds = plainValue[MaterialsKey].ToList(StateExtensions.ToGuid);
ChargeAp = plainValue[ChargeApKey].ToBoolean();
AvatarAddress = plainValue[AvatarAddressKey].ToAddress();
MaterialGradeId = (Integer)plainValue[GradeKey];
MaterialItemSubTypeId = (Integer)plainValue[ItemSubTypeKey];
}
#endregion Serialize
}
Expand Down
Loading

0 comments on commit 4b5d212

Please sign in to comment.