Skip to content

Commit

Permalink
Merge pull request #2490 from planetarium/release/1.10.0
Browse files Browse the repository at this point in the history
Backmerge 1.10.0
  • Loading branch information
U-lis authored Mar 28, 2024
2 parents d3d5357 + 08eec57 commit e0b0ced
Show file tree
Hide file tree
Showing 63 changed files with 938 additions and 1,180 deletions.
2 changes: 2 additions & 0 deletions .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public ActionEvaluationTest()
[InlineData(typeof(TransferAssets))]
[InlineData(typeof(RuneSummon))]
[InlineData(typeof(ActivateCollection))]
[InlineData(typeof(RetrieveAvatarAssets))]
public void Serialize_With_MessagePack(Type actionType)
{
var action = GetAction(actionType);
Expand Down Expand Up @@ -482,6 +483,7 @@ private ActionBase GetAction(Type type)
),
},
},
RetrieveAvatarAssets _ => new RetrieveAvatarAssets(avatarAddress: new PrivateKey().Address),
_ => throw new InvalidCastException(),
};
}
Expand Down
6 changes: 6 additions & 0 deletions .Lib9c.Tests/Action/ActivateCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ public void Execute()

var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
Assert.Empty(nextAvatarState.inventory.Items);

Assert.Throws<AlreadyActivatedException>(() => activateCollection.Execute(new ActionContext
{
PreviousState = nextState,
Signer = _agentAddress,
}));
}
}
}
8 changes: 6 additions & 2 deletions .Lib9c.Tests/Action/RaidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ int runeId2
null,
raidSimulatorSheets,
_tableSheets.CostumeStatSheet,
new List<StatModifier>());
new List<StatModifier>(),
_tableSheets.DeBuffLimitSheet
);
simulator.Simulate();
var score = simulator.DamageDealt;

Expand Down Expand Up @@ -498,7 +500,9 @@ public void Execute_With_Reward()
null,
_tableSheets.GetRaidSimulatorSheets(),
_tableSheets.CostumeStatSheet,
new List<StatModifier>());
new List<StatModifier>(),
_tableSheets.DeBuffLimitSheet
);
simulator.Simulate();

Dictionary<Currency, FungibleAssetValue> rewardMap
Expand Down
164 changes: 164 additions & 0 deletions .Lib9c.Tests/Action/RetrieveAvatarAssetsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
namespace Lib9c.Tests.Action
{
using System;
using System.Collections.Generic;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Xunit;

public class RetrieveAvatarAssetsTest
{
private static readonly Address _minter = new Address(
new byte[]
{
0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
}
);

#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
private static readonly Currency _currency = Currency.Legacy("NCG", 2, _minter);
#pragma warning restore CS0618

private static readonly Dictionary<string, string>
_csv = TableSheetsImporter.ImportSheets();

private readonly Address _signer;
private readonly IWorld _state;

public RetrieveAvatarAssetsTest()
{
var ca = new CreateAvatar
{
index = 0,
hair = 2,
lens = 3,
ear = 4,
tail = 5,
name = "JohnDoe",
};
_signer = new PrivateKey().Address;
IWorld state = new World(MockWorldState.CreateModern());
foreach (var (key, value) in _csv)
{
state = state.SetLegacyState(Addresses.GetSheetAddress(key), (Text)value);
}

state = state
.SetLegacyState(
Addresses.GameConfig,
new GameConfigState(_csv[nameof(GameConfigSheet)]).Serialize())
.SetLegacyState(Addresses.GoldCurrency, new GoldCurrencyState(_currency).Serialize());

_state = ca.Execute(new ActionContext
{
PreviousState = state,
BlockIndex = 0,
Signer = _signer,
RandomSeed = 0,
});
}

[Fact]
public void PlainValue()
{
var avatarAddress = new PrivateKey().Address;
var action = new RetrieveAvatarAssets(avatarAddress);
var plainValue = (Dictionary)action.PlainValue;
var values = (Dictionary)plainValue["values"];
Assert.Equal((Text)RetrieveAvatarAssets.TypeIdentifier, plainValue["type_id"]);
Assert.Equal(avatarAddress, values["a"].ToAddress());
}

[Fact]
public void LoadPlainValue()
{
var avatarAddress = new PrivateKey().Address;
var expectedValue = Dictionary.Empty
.Add("type_id", RetrieveAvatarAssets.TypeIdentifier)
.Add("values", Dictionary.Empty.Add("a", avatarAddress.Serialize()));

// Let's assume that serializedAction is the serialized representation of the action, which might be obtained through some other part of your tests
var action = new RetrieveAvatarAssets();
action.LoadPlainValue(expectedValue);

var plainValue = (Dictionary)action.PlainValue;
var values = (Dictionary)plainValue["values"];

Assert.Equal((Text)RetrieveAvatarAssets.TypeIdentifier, plainValue["type_id"]);
Assert.Equal(avatarAddress, values["a"].ToAddress());
}

[Fact]
public void Execute()
{
var agentState = _state.GetAgentState(_signer);
Assert.NotNull(agentState);
var avatarAddress = agentState.avatarAddresses[0];
var prevState = _state.MintAsset(
new ActionContext
{
Signer = _minter,
},
avatarAddress,
1 * _currency
);
Assert.Equal(1 * _currency, prevState.GetBalance(avatarAddress, _currency));

var action = new RetrieveAvatarAssets(avatarAddress);
var nextState = action.Execute(new ActionContext
{
PreviousState = prevState,
BlockIndex = 1L,
Signer = _signer,
RandomSeed = 0,
});
Assert.Equal(0 * _currency, nextState.GetBalance(avatarAddress, _currency));
Assert.Equal(1 * _currency, nextState.GetBalance(_signer, _currency));
}

[Fact]
public void Execute_Throw_FailedLoadStateException()
{
var avatarAddress = new PrivateKey().Address;
var action = new RetrieveAvatarAssets(avatarAddress);

var context = new ActionContext()
{
BlockIndex = 0,
PreviousState = new World(MockWorldState.CreateModern()),
RandomSeed = 0,
Signer = avatarAddress,
};

Assert.Throws<FailedLoadStateException>(() => action.Execute(context));
}

[Fact]
public void Execute_Throw_ArgumentOutOfRangeException()
{
var agentState = _state.GetAgentState(_signer);
Assert.NotNull(agentState);
var avatarAddress = agentState.avatarAddresses[0];
Assert.Equal(0 * _currency, _state.GetBalance(avatarAddress, _currency));

var action = new RetrieveAvatarAssets(avatarAddress);
Assert.Throws<ArgumentOutOfRangeException>(() => action.Execute(new ActionContext
{
PreviousState = _state,
BlockIndex = 1L,
Signer = _signer,
RandomSeed = 0,
}));
}
}
}
3 changes: 2 additions & 1 deletion .Lib9c.Tests/Action/Scenario/AuraScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ public void Arena()
enemyArenaPlayerDigest,
_tableSheets.GetArenaSimulatorSheets(),
new List<StatModifier>(),
new List<StatModifier>());
new List<StatModifier>(),
_tableSheets.DeBuffLimitSheet);
// Check player, enemy equip aura
foreach (var spawn in log.OfType<ArenaSpawnCharacter>())
{
Expand Down
13 changes: 7 additions & 6 deletions .Lib9c.Tests/Model/ArenaSimulatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public void Simulate()
{
new (StatType.DEF, StatModifier.OperationType.Add, 1),
new (StatType.HP, StatModifier.OperationType.Add, 100),
}
},
_tableSheets.DeBuffLimitSheet
);
CharacterSheet.Row row =
_tableSheets.CharacterSheet[GameConfig.DefaultAvatarCharacterId];
Expand Down Expand Up @@ -133,7 +134,7 @@ public void HpIncreasingModifier(int? modifier)
var myDigest = new ArenaPlayerDigest(_avatarState1, _arenaAvatarState1);
var enemyDigest = new ArenaPlayerDigest(_avatarState2, _arenaAvatarState2);
var arenaSheets = _tableSheets.GetArenaSimulatorSheets();
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>());
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>(), _tableSheets.DeBuffLimitSheet);
var expectedHpModifier = modifier ?? 2;

Assert.Equal(_random, simulator.Random);
Expand Down Expand Up @@ -178,7 +179,7 @@ public void TestSpeedModifierBySkill()
var myDigest = new ArenaPlayerDigest(_avatarState1, arenaAvatarState1);
var enemyDigest = new ArenaPlayerDigest(_avatarState2, arenaAvatarState2);
var arenaSheets = _tableSheets.GetArenaSimulatorSheets();
var unskilledLog = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>());
var unskilledLog = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>(), _tableSheets.DeBuffLimitSheet);
// foreach (var log in unskilledLog)
// {
// _testOutputHelper.WriteLine($"{log.Character.Id} :: {log}");
Expand Down Expand Up @@ -231,7 +232,7 @@ public void TestSpeedModifierBySkill()
myDigest = new ArenaPlayerDigest(_avatarState1, arenaAvatarState1);
enemyDigest = new ArenaPlayerDigest(_avatarState2, arenaAvatarState2);
arenaSheets = _tableSheets.GetArenaSimulatorSheets();
var skilledLog = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>());
var skilledLog = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>(), _tableSheets.DeBuffLimitSheet);
// foreach (var log in skilledLog)
// {
// _testOutputHelper.WriteLine($"{log.Character.Id} :: {log}");
Expand Down Expand Up @@ -273,7 +274,7 @@ public void Thorns()
var myDigest = new ArenaPlayerDigest(avatarState1, arenaAvatarState1);
var enemyDigest = new ArenaPlayerDigest(avatarState2, arenaAvatarState2);
var arenaSheets = _tableSheets.GetArenaSimulatorSheets();
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>(), true);
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, new List<StatModifier>(), new List<StatModifier>(), _tableSheets.DeBuffLimitSheet, true);
var ticks = log.Events
.OfType<ArenaTickDamage>()
.ToList();
Expand Down Expand Up @@ -335,7 +336,7 @@ public void Bleed()
var enemyDigest = new ArenaPlayerDigest(avatarState2, arenaAvatarState2);
enemyDigest.Runes.Add(rune);
var arenaSheets = _tableSheets.GetArenaSimulatorSheets();
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, modifiers, modifiers, true);
var log = simulator.Simulate(myDigest, enemyDigest, arenaSheets, modifiers, modifiers, _tableSheets.DeBuffLimitSheet, true);
var spawns = log.Events.OfType<ArenaSpawnCharacter>().ToList();
Assert.All(spawns, spawn => Assert.Equal(totalAtk, spawn.Character.ATK));
var ticks = log.Events
Expand Down
Loading

0 comments on commit e0b0ced

Please sign in to comment.