Skip to content

Commit

Permalink
Merge pull request #2795 from planetarium/hotfix/sweep-negative-potion
Browse files Browse the repository at this point in the history
Fix with code formatting
  • Loading branch information
ipdae authored Aug 29, 2024
2 parents 2767d12 + f7f14b3 commit 74f745c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
59 changes: 59 additions & 0 deletions .Lib9c.Tests/Action/HackAndSlashSweepTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,5 +705,64 @@ public void ExecuteDuplicatedException(int slotIndex, int runeId, int slotIndex2
throw new SheetRowNotFoundException(nameof(StageSheet), stageId);
}
}

[Theory]
[InlineData(0, -1)]
[InlineData(0, int.MinValue + 1)]
[InlineData(-1, 0)]
[InlineData(int.MinValue + 1, 0)]
public void Execute_ArgumentOutOfRangeException(int ap, int apPotion)
{
var avatarState = new AvatarState(
_avatarAddress,
_agentAddress,
0,
_initialState.GetAvatarSheets(),
_rankingMapAddress)
{
worldInformation =
new WorldInformation(0, _initialState.GetSheet<WorldSheet>(), 25),
level = 400,
};

IWorld state = _initialState.SetAvatarState(_avatarAddress, avatarState)
.SetActionPoint(_avatarAddress, 0);
var actionPoint = _initialState.GetActionPoint(_avatarAddress);

var stageSheet = _initialState.GetSheet<StageSheet>();
var (expectedLevel, expectedExp) = (0, 0L);
if (stageSheet.TryGetValue(2, out var stageRow))
{
var itemPlayCount =
DailyReward.ActionPointMax / stageRow.CostAP * 1;
var apPlayCount = actionPoint / stageRow.CostAP;
var playCount = apPlayCount + itemPlayCount;
(expectedLevel, expectedExp) = avatarState.GetLevelAndExp(
_tableSheets.CharacterLevelSheet,
2,
(int)playCount);

var (equipments, costumes) = GetDummyItems(avatarState);
var action = new HackAndSlashSweep
{
runeInfos = new List<RuneSlotInfo>(),
costumes = costumes,
equipments = equipments,
avatarAddress = _avatarAddress,
actionPoint = ap,
apStoneCount = apPotion,
worldId = 1,
stageId = 2,
};

Assert.Throws<ArgumentOutOfRangeException>(() =>
action.Execute(new ActionContext()
{
PreviousState = state,
Signer = _agentAddress,
RandomSeed = 0,
}));
}
}
}
}
47 changes: 33 additions & 14 deletions Lib9c/Action/HackAndSlashSweep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Nekoyume.TableData;
using Nekoyume.TableData.Rune;
using Serilog;
using static Lib9c.SerializeKeys;

namespace Nekoyume.Action
{
Expand All @@ -42,8 +41,10 @@ public class HackAndSlashSweep : GameAction, IHackAndSlashSweepV3

IEnumerable<Guid> IHackAndSlashSweepV3.Costumes => costumes;
IEnumerable<Guid> IHackAndSlashSweepV3.Equipments => equipments;

IEnumerable<IValue> IHackAndSlashSweepV3.RuneSlotInfos =>
runeInfos.Select(x => x.Serialize());

Address IHackAndSlashSweepV3.AvatarAddress => avatarAddress;
int IHackAndSlashSweepV3.ApStoneCount => apStoneCount;
int IHackAndSlashSweepV3.ActionPoint => actionPoint;
Expand All @@ -55,7 +56,8 @@ public class HackAndSlashSweep : GameAction, IHackAndSlashSweepV3
{
["costumes"] = new List(costumes.OrderBy(i => i).Select(e => e.Serialize())),
["equipments"] = new List(equipments.OrderBy(i => i).Select(e => e.Serialize())),
["runeInfos"] = runeInfos.OrderBy(x => x.SlotIndex).Select(x=> x.Serialize()).Serialize(),
["runeInfos"] = runeInfos.OrderBy(x => x.SlotIndex)
.Select(x => x.Serialize()).Serialize(),
["avatarAddress"] = avatarAddress.Serialize(),
["apStoneCount"] = apStoneCount.Serialize(),
["actionPoint"] = actionPoint.Serialize(),
Expand Down Expand Up @@ -91,6 +93,13 @@ public override IWorld Execute(IActionContext context)
$"apStoneCount : {apStoneCount} > UsableApStoneCount : {UsableApStoneCount}");
}

if (apStoneCount < 0 || actionPoint < 0)
{
throw new ArgumentOutOfRangeException(
$"{addressesHex} Aborted as the player give negative value ({actionPoint} AP & {apStoneCount} potions)"
);
}

states.ValidateWorldId(avatarAddress, worldId);

if (!states.TryGetAvatarState(
Expand All @@ -102,7 +111,9 @@ public override IWorld Execute(IActionContext context)
$"{addressesHex}Aborted as the avatar state of the signer was failed to load.");
}

var collectionExist = states.TryGetCollectionState(avatarAddress, out var collectionState) && collectionState.Ids.Any();
var collectionExist =
states.TryGetCollectionState(avatarAddress, out var collectionState) &&
collectionState.Ids.Any();
var sheetTypes = new List<Type>
{
typeof(WorldSheet),
Expand All @@ -126,6 +137,7 @@ public override IWorld Execute(IActionContext context)
{
sheetTypes.Add(typeof(CollectionSheet));
}

var sheets = states.GetSheets(
sheetTypes: sheetTypes);

Expand Down Expand Up @@ -206,19 +218,23 @@ public override IWorld Execute(IActionContext context)
}

// update rune slot
var runeSlotStateAddress = RuneSlotState.DeriveAddress(avatarAddress, BattleType.Adventure);
var runeSlotState = states.TryGetLegacyState(runeSlotStateAddress, out List rawRuneSlotState)
? new RuneSlotState(rawRuneSlotState)
: new RuneSlotState(BattleType.Adventure);
var runeSlotStateAddress =
RuneSlotState.DeriveAddress(avatarAddress, BattleType.Adventure);
var runeSlotState =
states.TryGetLegacyState(runeSlotStateAddress, out List rawRuneSlotState)
? new RuneSlotState(rawRuneSlotState)
: new RuneSlotState(BattleType.Adventure);
var runeListSheet = sheets.GetSheet<RuneListSheet>();
runeSlotState.UpdateSlot(runeInfos, runeListSheet);
states = states.SetLegacyState(runeSlotStateAddress, runeSlotState.Serialize());

// update item slot
var itemSlotStateAddress = ItemSlotState.DeriveAddress(avatarAddress, BattleType.Adventure);
var itemSlotState = states.TryGetLegacyState(itemSlotStateAddress, out List rawItemSlotState)
? new ItemSlotState(rawItemSlotState)
: new ItemSlotState(BattleType.Adventure);
var itemSlotStateAddress =
ItemSlotState.DeriveAddress(avatarAddress, BattleType.Adventure);
var itemSlotState =
states.TryGetLegacyState(itemSlotStateAddress, out List rawItemSlotState)
? new ItemSlotState(rawItemSlotState)
: new ItemSlotState(BattleType.Adventure);
itemSlotState.UpdateEquipment(equipments);
itemSlotState.UpdateCostumes(costumes);
states = states.SetLegacyState(itemSlotStateAddress, itemSlotState.Serialize());
Expand All @@ -238,6 +254,7 @@ public override IWorld Execute(IActionContext context)
equippedRune.Add(runeState);
}
}

var runeOptionSheet = sheets.GetSheet<RuneOptionSheet>();
var runeOptions = new List<RuneOptionSheet.Row.RuneOptionInfo>();
foreach (var runeState in equippedRune)
Expand Down Expand Up @@ -354,9 +371,11 @@ public override IWorld Execute(IActionContext context)
avatarState.UpdateExp(level, exp);

var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}HackAndSlashSweep Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states
.SetAvatarState(avatarAddress, avatarState);
Log.Debug(
"{AddressesHex}HackAndSlashSweep Total Executed Time: {Elapsed}",
addressesHex, ended - started
);
return states.SetAvatarState(avatarAddress, avatarState);
}

public static List<ItemBase> GetRewardItems(IRandom random,
Expand Down

0 comments on commit 74f745c

Please sign in to comment.