Skip to content

Commit

Permalink
add action point logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-doobu committed Nov 27, 2024
1 parent 4d11831 commit bf33ba9
Showing 1 changed file with 54 additions and 8 deletions.
62 changes: 54 additions & 8 deletions Lib9c/Action/Synthesize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,12 @@ public override IWorld Execute(IActionContext context)
typeof(EquipmentItemSheet),
typeof(SynthesizeSheet),
typeof(SynthesizeWeightSheet),
typeof(MaterialItemSheet),
});

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

// Initialize variables
var materialEquipments = new List<Equipment>();
var materialCostumes = new List<Costume>();
Expand Down Expand Up @@ -158,18 +162,18 @@ public override IWorld Execute(IActionContext context)
var gradeId = gradeItem.Key;
var subTypeDict = gradeItem.Value;

if (!synthesizeSheet.TryGetValue(gradeId, out var synthesizeRow))
{
throw new SheetRowNotFoundException(
$"Aborted as the synthesize row for grade ({gradeId}) was failed to load in {nameof(SynthesizeSheet)}", gradeId
);
}

foreach (var subTypeItem in subTypeDict)
{
var itemSubType = subTypeItem.Key;
var materialCount = subTypeItem.Value;

if (!synthesizeSheet.TryGetValue(gradeId, out var synthesizeRow))
{
throw new SheetRowNotFoundException(
$"Aborted as the synthesize row for grade ({gradeId}) was failed to load in {nameof(SynthesizeSheet)}", gradeId
);
}

// TODO: subType별로 필요한 아이템 개수가 다를 수 있음
var requiredCount = synthesizeRow.RequiredCount;
var succeedRate = Math.Clamp(synthesizeRow.SucceedRate, 0, 1);
Expand Down Expand Up @@ -206,7 +210,9 @@ public override IWorld Execute(IActionContext context)
avatarState.inventory.AddNonFungibleItem(item);
}

return states.SetAvatarState(AvatarAddress, avatarState, true, true, false, false);
return states
.SetActionPoint(AvatarAddress, actionPoint)
.SetAvatarState(AvatarAddress, avatarState, true, true, false, false);
}

private ItemBase GetSynthesizedItem(Grade grade, Sheets sheets, IRandom random, ItemSubType itemSubTypeValue)
Expand Down Expand Up @@ -409,6 +415,46 @@ private void SetGradeDict(ref GradeDict gradeDict, int grade, ItemSubType itemSu
}
}

private long CalculateActionPoint(IWorld states, AvatarState avatarState, Sheets sheets, IActionContext context)
{
if (!states.TryGetActionPoint(AvatarAddress, out var actionPoint))
{
actionPoint = avatarState.actionPoint;
}

if (actionPoint < GameConfig.ActionCostAP)
{
switch (ChargeAp)
{
case false:
throw new NotEnoughActionPointException("Action point is not enough. for synthesize.");
case true:
{
var row = sheets.GetSheet<MaterialItemSheet>()
.OrderedList?
.First(r => r.ItemSubType == ItemSubType.ApStone);
if (row == null)
{
throw new SheetRowNotFoundException(
nameof(MaterialItemSheet),
ItemSubType.ApStone.ToString()
);
}

if (!avatarState.inventory.RemoveFungibleItem(row.ItemId, context.BlockIndex))
{
throw new NotEnoughMaterialException("not enough ap stone.");
}
actionPoint = DailyReward.ActionPointMax;
break;
}
}
}

actionPoint -= GameConfig.ActionCostAP;
return actionPoint;
}

#region Serialize
protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>
Expand Down

0 comments on commit bf33ba9

Please sign in to comment.