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

Migrate AvatarState.actionPoint to use ActionPointModule #2502

Merged
merged 33 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
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
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
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