Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge improve/action-execute #2509

Merged
merged 39 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7184e92
Use modern account in DailyReward
ipdae Mar 28, 2024
a22500f
Use const value instead of GameConfigState
ipdae Mar 28, 2024
9c0ad06
Apply review suggestions
ipdae Mar 29, 2024
848d559
Merge pull request #2495 from planetarium/refactor/issue-2478
ipdae Mar 29, 2024
82c37f5
remove unnecessary action point validating at ItemEnhancement
sonohoshi Mar 29, 2024
b7933df
remove abnormal GetRequiredAp invoking
sonohoshi Mar 29, 2024
ea5897f
use ActionPointModule at ChargeActionPoint
sonohoshi Mar 29, 2024
884a9d4
reflect ChargeActionPoint hardfork to unit test
sonohoshi Mar 29, 2024
10a0651
use ActionPointModule at CombinationEquipment
sonohoshi Mar 29, 2024
ff2465b
reflect CombinationEquipment hardfork to unit test
sonohoshi Mar 29, 2024
62c6b62
use ActionPointModule at CombinationConsumable
sonohoshi Mar 29, 2024
77aaa3c
reflect CombinationConsumable hardfork to unit test
sonohoshi Mar 29, 2024
d7b6f51
use ActionPointModule at EventConsumableItemCrafts
sonohoshi Mar 29, 2024
cba4854
reflect EventConsumableItemCrafts hardfork to unit test
sonohoshi Mar 29, 2024
a50cf1a
use ActionPointModule at Grinding
sonohoshi Mar 29, 2024
bb77215
reflect Grinding hardfork to unit test
sonohoshi Mar 29, 2024
55822a3
use ActionPointModule at HackAndSlash
sonohoshi Apr 1, 2024
98642c6
reflect HackAndSlash hardfork to unit test
sonohoshi Apr 1, 2024
3ae2140
use ActionPointModule at HackAndSlashSweep
sonohoshi Apr 1, 2024
e101c0a
reflect HackAndSlashSweep hardfork to unit test
sonohoshi Apr 1, 2024
3b170dd
move AvatarState.UseAp to InventoryExtensions.UseActionPoint
sonohoshi Apr 1, 2024
6a5c4f3
use Inventory.UseActionPoint instead AvatarState.UseAp
sonohoshi Apr 1, 2024
0f1b5a0
remove unit test about AvatarState.UseAp()
sonohoshi Apr 1, 2024
491eaee
add unit test about InventoryExtensions.UseActionPoint
sonohoshi Apr 1, 2024
705eb2b
change GameConfigState.ActionPointMax to DailyReward.ActionPointMax
sonohoshi Apr 1, 2024
3b7b0c9
remove obsoleted action ReRegisterProduct0
sonohoshi Apr 1, 2024
f377b88
revoke RegisterProduct0
sonohoshi Apr 1, 2024
ec37aad
fix broken unit tests about ActionPoint hard-forking
sonohoshi Apr 1, 2024
0a1f649
add SetActionPoint() to CreateAvatar
sonohoshi Apr 1, 2024
1a1eccb
add Assert.Equal about ActionPointModule with CreateAvatar
sonohoshi Apr 1, 2024
9add03b
remove unnecessary code
sonohoshi Apr 1, 2024
031cdb5
reflect on review about SetDailyRewardReceivedBlockIndex
sonohoshi Apr 2, 2024
92d1db5
add ActionPointModule.TryGetActionPoint()
sonohoshi Apr 2, 2024
b07fa6a
fix InventoryExtensions.UseActionPoint()
sonohoshi Apr 2, 2024
cfd092b
fix InventoryExtensionsTest for changed interface
sonohoshi Apr 2, 2024
129faf3
fix many actions for passive-state migration
sonohoshi Apr 2, 2024
9f98111
fix unit tests, use TryGetActionPoint
sonohoshi Apr 2, 2024
1dcb552
Merge pull request #2502 from planetarium/migrate/for-action-point
sonohoshi Apr 2, 2024
f7c4236
Merge branch 'development' into improve/action-execute
ipdae Apr 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Action/CancelProductRegistrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public CancelProductRegistrationTest(ITestOutputHelper outputHelper)
.SetLegacyState(GoldCurrencyState.Address, _goldCurrencyState.Serialize())
.SetAgentState(_agentAddress, agentState)
.SetLegacyState(Addresses.Shop, new ShopState().Serialize())
.SetAvatarState(_avatarAddress, avatarState);
.SetAvatarState(_avatarAddress, avatarState)
.SetActionPoint(_avatarAddress, DailyReward.ActionPointMax);
}

[Theory]
Expand Down
32 changes: 13 additions & 19 deletions .Lib9c.Tests/Action/ChargeActionPointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ public ChargeActionPointTest()
_tableSheets.GetAvatarSheets(),
gameConfigState,
default
)
{
actionPoint = 0,
};
);
agent.avatarAddresses.Add(0, _avatarAddress);

_initialState = new World(MockUtil.MockModernWorldState)
Expand Down Expand Up @@ -76,11 +73,10 @@ public void Execute(bool useTradable)
avatarState.inventory.AddItem(apStone);
}

Assert.Equal(0, avatarState.actionPoint);

IWorld state;
state = _initialState.SetAvatarState(_avatarAddress, avatarState);
Assert.False(_initialState.TryGetActionPoint(_avatarAddress, out var actionPoint));
Assert.Equal(0L, actionPoint);

var state = _initialState.SetAvatarState(_avatarAddress, avatarState);
foreach (var (key, value) in _sheets)
{
state = state.SetLegacyState(Addresses.TableSheet.Derive(key), value.Serialize());
Expand All @@ -98,9 +94,8 @@ public void Execute(bool useTradable)
RandomSeed = 0,
});

var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
var gameConfigState = nextState.GetGameConfigState();
Assert.Equal(gameConfigState.ActionPointMax, nextAvatarState.actionPoint);
Assert.True(nextState.TryGetActionPoint(_avatarAddress, out var nextActionPoint));
Assert.Equal(DailyReward.ActionPointMax, nextActionPoint);
}

[Theory]
Expand All @@ -109,11 +104,11 @@ public void Execute(bool useTradable)
[InlineData(true, true, false, false, typeof(NotEnoughMaterialException))]
[InlineData(true, false, true, true, typeof(ActionPointExceededException))]
[InlineData(true, true, true, true, typeof(ActionPointExceededException))]
public void Execute_Throw_Exception(bool useAvatarAddress, bool useTradable, bool enough, bool charge, Type exc)
public void Execute_Throw_Exception(bool useAvatarAddress, bool useTradable, bool enoughApStone, bool actionPointIsAlreadyCharged, Type exc)
{
var avatarState = _initialState.GetAvatarState(_avatarAddress);

Assert.Equal(0, avatarState.actionPoint);
_initialState.TryGetActionPoint(_avatarAddress, out var prevActionPoint);
Assert.Equal(0L, prevActionPoint);

var avatarAddress = useAvatarAddress ? _avatarAddress : default;
var state = _initialState;
Expand All @@ -123,22 +118,21 @@ public void Execute_Throw_Exception(bool useAvatarAddress, bool useTradable, boo
: ItemFactory.CreateMaterial(row);
if (apStone is TradableMaterial tradableMaterial)
{
if (!enough)
if (!enoughApStone)
{
tradableMaterial.RequiredBlockIndex = 10;
}
}

if (enough)
if (enoughApStone)
{
avatarState.inventory.AddItem(apStone);
state = state.SetAvatarState(_avatarAddress, avatarState);
}

if (charge)
if (actionPointIsAlreadyCharged)
{
avatarState.actionPoint = state.GetGameConfigState().ActionPointMax;
state = state.SetAvatarState(_avatarAddress, avatarState);
state = state.SetActionPoint(_avatarAddress, DailyReward.ActionPointMax);
}

var action = new ChargeActionPoint()
Expand Down
8 changes: 6 additions & 2 deletions .Lib9c.Tests/Action/CombinationConsumableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Execute()
avatarState.inventory.AddItem(material, materialInfo.Count);
}

var previousActionPoint = avatarState.actionPoint;
_initialState.TryGetActionPoint(_avatarAddress, out var previousActionPoint);
var previousResultConsumableCount =
avatarState.inventory.Equipments.Count(e => e.Id == row.ResultConsumableItemId);
var previousMailCount = avatarState.mailBox.Count;
Expand Down Expand Up @@ -123,7 +123,11 @@ public void Execute()
Assert.NotNull(consumable);

var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
Assert.Equal(previousActionPoint - costActionPoint, nextAvatarState.actionPoint);
if (nextState.TryGetActionPoint(_avatarAddress, out var nextActionPoint))
{
Assert.Equal(previousActionPoint - costActionPoint, nextActionPoint);
}

Assert.Equal(previousMailCount + 1, nextAvatarState.mailBox.Count);
Assert.IsType<CombinationMail>(nextAvatarState.mailBox.First());
Assert.Equal(
Expand Down
11 changes: 10 additions & 1 deletion .Lib9c.Tests/Action/CombinationEquipmentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public CombinationEquipmentTest(ITestOutputHelper outputHelper)

_initialState = new World(MockUtil.MockModernWorldState)
.SetLegacyState(_slotAddress, combinationSlotState.Serialize())
.SetLegacyState(GoldCurrencyState.Address, gold.Serialize());
.SetLegacyState(GoldCurrencyState.Address, gold.Serialize())
.SetActionPoint(_avatarAddress, DailyReward.ActionPointMax);

foreach (var (key, value) in sheets)
{
Expand Down Expand Up @@ -291,6 +292,9 @@ bool previousCostStateExist
Assert.NotNull(slotState.Result.itemUsable);

var equipment = (Equipment)slotState.Result.itemUsable;
var expectedActionPoint = DailyReward.ActionPointMax - _tableSheets
.EquipmentItemRecipeSheet[recipeId]
.RequiredActionPoint;
if (subRecipeId.HasValue)
{
Assert.True(equipment.optionCountFromCombination > 0);
Expand All @@ -302,6 +306,10 @@ bool previousCostStateExist
var feeStoreAddress = Addresses.GetBlacksmithFeeAddress(arenaData.ChampionshipId, arenaData.Round);
Assert.Equal(450 * currency, nextState.GetBalance(feeStoreAddress, currency));
}

expectedActionPoint -= _tableSheets
.EquipmentItemSubRecipeSheetV2[subRecipeId.Value]
.RequiredActionPoint;
}
else
{
Expand All @@ -328,6 +336,7 @@ bool previousCostStateExist
}

Assert.Equal(expectedCrystal * CrystalCalculator.CRYSTAL, nextState.GetBalance(Addresses.MaterialCost, CrystalCalculator.CRYSTAL));
Assert.Equal(expectedActionPoint, nextState.GetActionPoint(_avatarAddress));
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions .Lib9c.Tests/Action/CreateAvatarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void Execute(long blockIndex)
Assert.True(agentState.avatarAddresses.Any());
Assert.Equal("test", nextAvatarState.name);
Assert.Equal(200_000 * CrystalCalculator.CRYSTAL, nextState.GetBalance(_agentAddress, CrystalCalculator.CRYSTAL));
Assert.Equal(DailyReward.ActionPointMax, nextState.GetActionPoint(avatarAddress));
Assert.True(nextState.TryGetDailyRewardReceivedBlockIndex(avatarAddress, out var nextDailyRewardReceivedIndex));
Assert.Equal(0L, nextDailyRewardReceivedIndex);
var avatarItemSheet = nextState.GetSheet<CreateAvatarItemSheet>();
foreach (var row in avatarItemSheet.Values)
{
Expand Down
113 changes: 30 additions & 83 deletions .Lib9c.Tests/Action/DailyRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ namespace Lib9c.Tests.Action
using Nekoyume.Helper;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Serilog;
using Xunit;
using Xunit.Abstractions;
using static Lib9c.SerializeKeys;

public class DailyRewardTest
{
Expand All @@ -35,84 +33,52 @@ public DailyRewardTest(ITestOutputHelper outputHelper)
.SetLegacyState(Addresses.TableSheet.Derive(key), value.Serialize());
}

var tableSheets = new TableSheets(sheets);
var gameConfigState = new GameConfigState();
gameConfigState.Set(tableSheets.GameConfigSheet);
_agentAddress = new PrivateKey().Address;
var agentState = new AgentState(_agentAddress);
_avatarAddress = new PrivateKey().Address;
var rankingMapAddress = new PrivateKey().Address;
var avatarState = new AvatarState(
_avatarAddress,
_agentAddress,
0,
tableSheets.GetAvatarSheets(),
gameConfigState,
rankingMapAddress)
{
actionPoint = 0,
};
agentState.avatarAddresses[0] = _avatarAddress;

_initialState = _initialState
.SetLegacyState(Addresses.GameConfig, gameConfigState.Serialize())
.SetAgentState(_agentAddress, agentState)
.SetAvatarState(_avatarAddress, avatarState);
_avatarAddress = Addresses.GetAvatarAddress(_agentAddress, 0);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void Execute(bool legacy)
public void Execute(bool stateExist)
{
IWorld previousStates = null;
switch (legacy)
IWorld previousStates = stateExist switch
{
case true:
previousStates = _initialState;
break;
case false:
var avatarState = _initialState.GetAvatarState(_avatarAddress);
previousStates = SetAvatarStateAsV2To(_initialState, avatarState);
break;
}
true => _initialState.SetDailyRewardReceivedBlockIndex(_agentAddress, 0L)
.SetActionPoint(_avatarAddress, 0),
false => _initialState
};

var nextState = ExecuteInternal(previousStates, 2448);
var nextGameConfigState = nextState.GetGameConfigState();
nextState.TryGetAvatarState(_agentAddress, _avatarAddress, out var nextAvatarState);
Assert.NotNull(nextAvatarState);
Assert.NotNull(nextAvatarState.inventory);
Assert.NotNull(nextAvatarState.questList);
Assert.NotNull(nextAvatarState.worldInformation);
Assert.Equal(nextGameConfigState.ActionPointMax, nextAvatarState.actionPoint);
var nextState = ExecuteInternal(previousStates, _avatarAddress, DailyReward.DailyRewardInterval);
var receivedBlockIndex = nextState.GetDailyRewardReceivedBlockIndex(_avatarAddress);
Assert.Equal(DailyReward.DailyRewardInterval, receivedBlockIndex);
var actionPoint = nextState.GetActionPoint(_avatarAddress);
Assert.Equal(DailyReward.ActionPointMax, actionPoint);

var avatarRuneAmount = nextState.GetBalance(_avatarAddress, RuneHelper.DailyRewardRune);
var expectedRune = RuneHelper.DailyRewardRune * nextGameConfigState.DailyRuneRewardAmount;
var expectedRune = RuneHelper.DailyRewardRune * DailyReward.DailyRuneRewardAmount;
Assert.Equal(expectedRune, avatarRuneAmount);
}

[Fact]
public void Execute_Throw_FailedLoadStateException() =>
Assert.Throws<FailedLoadStateException>(() => ExecuteInternal(new World(MockUtil.MockModernWorldState)));

[Theory]
[InlineData(0, 0, true)]
[InlineData(0, 2447, true)]
[InlineData(0, 2448, false)]
[InlineData(2448, 2448, true)]
[InlineData(2448, 2448 + 2447, true)]
[InlineData(2448, 2448 + 2448, false)]
[InlineData(0, DailyReward.DailyRewardInterval - 1, true)]
[InlineData(0, DailyReward.DailyRewardInterval, false)]
[InlineData(DailyReward.DailyRewardInterval, DailyReward.DailyRewardInterval, true)]
[InlineData(DailyReward.DailyRewardInterval, DailyReward.DailyRewardInterval * 2 - 1, true)]
[InlineData(DailyReward.DailyRewardInterval, DailyReward.DailyRewardInterval * 2, false)]
public void Execute_Throw_RequiredBlockIndexException(
long dailyRewardReceivedIndex,
long executeBlockIndex,
bool throwsException)
{
var avatarState = _initialState.GetAvatarState(_avatarAddress);
avatarState.dailyRewardReceivedIndex = dailyRewardReceivedIndex;
var previousStates = SetAvatarStateAsV2To(_initialState, avatarState);
var previousStates =
_initialState.SetDailyRewardReceivedBlockIndex(
_avatarAddress,
dailyRewardReceivedIndex);
try
{
ExecuteInternal(previousStates, executeBlockIndex);
ExecuteInternal(previousStates, _avatarAddress, executeBlockIndex);
}
catch (RequiredBlockIndexException)
{
Expand All @@ -121,38 +87,19 @@ public void Execute_Throw_RequiredBlockIndexException(
}

[Fact]
private void Execute_Without_Runereward()
public void Execute_Throw_InvalidAddressException()
{
var gameConfigSheet = new GameConfigSheet();
var csv = @"key,value
hourglass_per_block,3
action_point_max,120
daily_reward_interval,1
daily_arena_interval,5040
weekly_arena_interval,56000
required_appraise_block,10
battle_arena_interval,4
rune_stat_slot_unlock_cost,50
rune_skill_slot_unlock_cost,500";
gameConfigSheet.Set(csv);
var gameConfigState = new GameConfigState();
gameConfigState.Set(gameConfigSheet);

var state = _initialState
.SetLegacyState(Addresses.GameConfig, gameConfigState.Serialize());
var nextState = ExecuteInternal(state, 1800);
var avatarRuneAmount = nextState.GetBalance(_avatarAddress, RuneHelper.DailyRewardRune);
Assert.Equal(0, (int)avatarRuneAmount.MajorUnit);
Assert.Throws<InvalidAddressException>(() =>
ExecuteInternal(
new World(MockUtil.MockModernWorldState),
new PrivateKey().Address));
}

private IWorld SetAvatarStateAsV2To(IWorld state, AvatarState avatarState) =>
state.SetAvatarState(_avatarAddress, avatarState);

private IWorld ExecuteInternal(IWorld previousStates, long blockIndex = 0)
private IWorld ExecuteInternal(IWorld previousStates, Address avatarAddress, long blockIndex = 0)
{
var dailyRewardAction = new DailyReward
{
avatarAddress = _avatarAddress,
avatarAddress = avatarAddress,
};

return dailyRewardAction.Execute(new ActionContext
Expand Down
12 changes: 8 additions & 4 deletions .Lib9c.Tests/Action/EventConsumableItemCraftsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private void Execute(
previousStates = previousStates
.SetAvatarState(_avatarAddress, previousAvatarState);

var previousActionPoint = previousAvatarState.actionPoint;
previousStates.TryGetActionPoint(_avatarAddress, out var previousActionPoint);
var previousResultConsumableCount =
previousAvatarState.inventory.Equipments
.Count(e => e.Id == recipeRow.ResultConsumableItemId);
Expand Down Expand Up @@ -157,9 +157,13 @@ private void Execute(
Assert.NotNull(consumable);

var nextAvatarState = nextStates.GetAvatarState(_avatarAddress);
Assert.Equal(
previousActionPoint - recipeRow.RequiredActionPoint,
nextAvatarState.actionPoint);
if (nextStates.TryGetActionPoint(_avatarAddress, out var nextAp))
{
Assert.Equal(
previousActionPoint - recipeRow.RequiredActionPoint,
nextAp);
}

Assert.Equal(
previousMailCount + 1,
nextAvatarState.mailBox.Count);
Expand Down
4 changes: 2 additions & 2 deletions .Lib9c.Tests/Action/GrindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Type exc

if (avatarExist)
{
_avatarState.actionPoint = ap;
state = state.SetActionPoint(_avatarAddress, ap);

if (equipmentExist)
{
Expand Down Expand Up @@ -229,7 +229,7 @@ Type exc

Assert.Equal(asset, nextState.GetBalance(_agentAddress, _crystalCurrency));
Assert.False(nextAvatarState.inventory.HasNonFungibleItem(default));
Assert.Equal(115, nextAvatarState.actionPoint);
Assert.Equal(115, nextState.GetActionPoint(_avatarAddress));

var mail = nextAvatarState.mailBox.OfType<GrindingMail>().First(i => i.id.Equals(action.Id));

Expand Down
Loading
Loading