Skip to content

Commit

Permalink
Merge branch 'development' into bugfix/issue-2501
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae authored Apr 4, 2024
2 parents 34ef59f + 2695341 commit 8497a80
Show file tree
Hide file tree
Showing 43 changed files with 500 additions and 1,070 deletions.
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

0 comments on commit 8497a80

Please sign in to comment.