Skip to content

Commit

Permalink
Merge pull request #2861 from planetarium/release/1.18.0
Browse files Browse the repository at this point in the history
Backmerge 1.18.0
  • Loading branch information
U-lis authored Oct 2, 2024
2 parents ebf3d8a + 5e6cea5 commit 84926d5
Show file tree
Hide file tree
Showing 52 changed files with 1,071 additions and 510 deletions.
27 changes: 21 additions & 6 deletions .Lib9c.Tests/Action/AccountStateDeltaExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
var random = new TestRandom();
var tableSheets = new TableSheets(TableSheetsImporter.ImportSheets());
var runeSheet = tableSheets.RuneSheet;
var materialItemSheet = tableSheets.MaterialItemSheet;
var runeCurrency = RuneHelper.ToCurrency(runeSheet[10001]);
var avatarAddress = new PrivateKey().Address;
var bossState = new WorldBossState(
tableSheets.WorldBossListSheet[1],
tableSheets.WorldBossGlobalHpSheet[1]
Expand All @@ -71,14 +71,27 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
1,{bossId},0,10001,100
");
var killRewardSheet = new WorldBossKillRewardSheet();
killRewardSheet.Set($@"id,boss_id,rank,rune_min,rune_max,crystal
1,{bossId},0,1,1,100
killRewardSheet.Set($@"id,boss_id,rank,rune_min,rune_max,crystal,circle
1,{bossId},0,1,1,100,0
");

if (exc is null)
{
var nextState = states.SetWorldBossKillReward(context, rewardInfoAddress, rewardRecord, 0, bossState, runeWeightSheet, killRewardSheet, runeSheet, random, avatarAddress, _agentAddress);
Assert.Equal(expectedRune * runeCurrency, nextState.GetBalance(avatarAddress, runeCurrency));
var nextState = states.SetWorldBossKillReward(
context,
rewardInfoAddress,
rewardRecord,
0,
bossState,
runeWeightSheet,
killRewardSheet,
runeSheet,
materialItemSheet,
random,
_avatarState.inventory,
_avatarAddress,
_agentAddress);
Assert.Equal(expectedRune * runeCurrency, nextState.GetBalance(_avatarState.address, runeCurrency));
Assert.Equal(expectedCrystal * CrystalCalculator.CRYSTAL, nextState.GetBalance(_agentAddress, CrystalCalculator.CRYSTAL));
var nextRewardInfo = new WorldBossKillRewardRecord((List)nextState.GetLegacyState(rewardInfoAddress));
Assert.All(nextRewardInfo, kv => Assert.True(kv.Value));
Expand All @@ -96,8 +109,10 @@ public void SetWorldBossKillReward(int level, int expectedRune, int expectedCrys
runeWeightSheet,
killRewardSheet,
runeSheet,
materialItemSheet,
random,
avatarAddress,
_avatarState.inventory,
_avatarAddress,
_agentAddress)
);
}
Expand Down
17 changes: 13 additions & 4 deletions .Lib9c.Tests/Action/AdventureBoss/ClaimAdventureBossRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,16 +1055,25 @@ private void Test(IWorld world, AdventureBossGameData.ClaimableReward expectedRe
}

var inventory = world.GetInventoryV2(TesterAvatarAddress);
foreach (var item in expectedReward.ItemReward)
var materialSheet = world.GetSheet<MaterialItemSheet>();
var circleRow = materialSheet.OrderedList.First(i => i.ItemSubType == ItemSubType.Circle);
foreach (var (id, amount) in expectedReward.ItemReward)
{
var itemState = inventory.Items.FirstOrDefault(i => i.item.Id == item.Key);
if (item.Value == 0)
var itemState = inventory.Items.FirstOrDefault(i => i.item.Id == id);
if (amount == 0)
{
Assert.Null(itemState);
}
else if (id == circleRow.Id)
{
var itemCount = inventory.TryGetTradableFungibleItems(circleRow.ItemId, null, 1L, out var items)
? items.Sum(item => item.count)
: 0;
Assert.Equal(amount, itemCount);
}
else
{
Assert.Equal(item.Value, itemState!.count);
Assert.Equal(amount, itemState!.count);
}
}
}
Expand Down
23 changes: 20 additions & 3 deletions .Lib9c.Tests/Action/AdventureBoss/ExploreAdventureBossTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ Type exc
}

// override sheet
state = state.SetLegacyState(Addresses.GetSheetAddress<CollectionSheet>(), CollectionSheetFixture.Default.Serialize());
state = state.SetLegacyState(
Addresses.GetSheetAddress<CollectionSheet>(),
CollectionSheetFixture.Default.Serialize()
);

state = Stake(state, WantedAddress);
var sheets = state.GetSheets(sheetTypes: new[]
Expand Down Expand Up @@ -223,7 +226,8 @@ Type exc
var expectedItemRewards = new List<(int, int)>();
var expectedFavRewards = new List<(int, int)>();
var firstRewardSheet = TableSheets.AdventureBossFloorFirstRewardSheet;
foreach (var row in firstRewardSheet.Values.Where(r => r.FloorId > floor && r.FloorId <= expectedFloor))
foreach (var row in firstRewardSheet.Values.Where(r =>
r.FloorId > floor && r.FloorId <= expectedFloor))
{
foreach (var reward in row.Rewards)
{
Expand Down Expand Up @@ -289,12 +293,24 @@ Type exc
Assert.Equal(expectedFloor, explorer.Floor);

var inventory = state.GetInventoryV2(TesterAvatarAddress);
var circleRow =
materialSheet.OrderedList.First(row => row.ItemSubType == ItemSubType.Circle);
foreach (var (id, amount) in expectedItemRewards)
{
if (amount == 0)
{
Assert.Null(inventory.Items.FirstOrDefault(i => i.item.Id == id));
}
else if (id == circleRow.Id)
{
var itemCount =
inventory.TryGetTradableFungibleItems(
circleRow.ItemId, null, 1L, out var items
)
? items.Sum(item => item.count)
: 0;
Assert.Equal(amount, itemCount);
}
else
{
Assert.True(amount <= inventory.Items.First(i => i.item.Id == id).count);
Expand All @@ -306,7 +322,8 @@ Type exc
{
var ticker = runeSheet.Values.First(rune => rune.Id == id).Ticker;
var currency = Currencies.GetRune(ticker);
Assert.True(amount * currency <= state.GetBalance(TesterAvatarAddress, currency));
Assert.True(
amount * currency <= state.GetBalance(TesterAvatarAddress, currency));
}

itemSlotState =
Expand Down
8 changes: 8 additions & 0 deletions .Lib9c.Tests/Action/AdventureBoss/SweepAdventureBossTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,20 @@ public void Execute(
Assert.Equal(explorer.UsedApPotion, exploreBoard.UsedApPotion);

inventory = state.GetInventoryV2(TesterAvatarAddress);
var circleRow = materialSheet.OrderedList.First(row => row.ItemSubType == ItemSubType.Circle);
foreach (var (id, amount) in expectedRewards)
{
if (amount == 0)
{
Assert.Null(inventory.Items.FirstOrDefault(i => i.item.Id == id));
}
else if (id == circleRow.Id)
{
var itemCount = inventory.TryGetTradableFungibleItems(circleRow.ItemId, null, 1L, out var items)
? items.Sum(item => item.count)
: 0;
Assert.Equal(amount, itemCount);
}
else
{
Assert.Equal(amount, inventory.Items.First(i => i.item.Id == id).count);
Expand Down
40 changes: 40 additions & 0 deletions .Lib9c.Tests/Action/ApprovePledgeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ namespace Lib9c.Tests.Action
{
using System;
using Bencodex.Types;
using Lib9c.Tests.Util;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.Guild;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Nekoyume.TypedAddress;
using Xunit;

public class ApprovePledgeTest
Expand Down Expand Up @@ -41,6 +45,42 @@ public void Execute(int mead)
Assert.Equal(patron, contract[0].ToAddress());
Assert.True(contract[1].ToBoolean());
Assert.Equal(mead, contract[2].ToInteger());
Assert.Null(nextState.GetJoinedGuild(new AgentAddress(address)));
}

[Theory]
[InlineData(RequestPledge.DefaultRefillMead)]
[InlineData(100)]
public void Execute_JoinGuild(int mead)
{
var address = new PrivateKey().Address;
var patron = MeadConfig.PatronAddress;
var contractAddress = address.Derive(nameof(RequestPledge));
var guildAddress = AddressUtil.CreateGuildAddress();
IWorld states = new World(MockUtil.MockModernWorldState)
.SetLegacyState(
contractAddress,
List.Empty.Add(patron.Serialize()).Add(false.Serialize()).Add(mead.Serialize())
)
.MakeGuild(guildAddress, GuildConfig.PlanetariumGuildOwner);

var action = new ApprovePledge
{
PatronAddress = patron,
};
var nextState = action.Execute(new ActionContext
{
Signer = address,
PreviousState = states,
});

var contract = Assert.IsType<List>(nextState.GetLegacyState(contractAddress));
Assert.Equal(patron, contract[0].ToAddress());
Assert.True(contract[1].ToBoolean());
Assert.Equal(mead, contract[2].ToInteger());
var joinedGuildAddress = nextState.GetJoinedGuild(new AgentAddress(address));
Assert.NotNull(joinedGuildAddress);
Assert.Equal(guildAddress, joinedGuildAddress);
}

[Theory]
Expand Down
52 changes: 52 additions & 0 deletions .Lib9c.Tests/Action/BuyProductTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Lib9c.Tests.Action
using System;
using System.Collections.Generic;
using System.Linq;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
Expand All @@ -13,6 +14,7 @@ namespace Lib9c.Tests.Action
using Nekoyume.Helper;
using Nekoyume.Model;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
using Nekoyume.Model.Market;
using Nekoyume.Model.State;
using Nekoyume.Module;
Expand Down Expand Up @@ -325,6 +327,56 @@ public void Execute_Throw_ArgumentOutOfRangeException()
Assert.Throws<ArgumentOutOfRangeException>(() => action.Execute(new ActionContext()));
}

[Fact]
public void Mail_Serialize_BackwardCompatibility()
{
var favProduct = new FavProduct
{
SellerAgentAddress = SellerAgentAddress,
SellerAvatarAddress = SellerAvatarAddress,
Asset = 1 * RuneHelper.StakeRune,
RegisteredBlockIndex = 1L,
ProductId = ProductId,
Price = 1 * Gold,
Type = ProductType.FungibleAssetValue,
};
var itemProduct = new ItemProduct
{
SellerAgentAddress = SellerAgentAddress,
SellerAvatarAddress = SellerAvatarAddress,
RegisteredBlockIndex = 1L,
ProductId = ProductId,
Price = 1 * Gold,
Type = ProductType.NonFungible,
ItemCount = 1,
TradableItem = TradableItem,
};

var buyerMail = new ProductBuyerMail(1L, ProductId, 1L, ProductId, favProduct);
var buyerSerialized = (Dictionary)buyerMail.Serialize();
var buyerDeserialized = new ProductBuyerMail(buyerSerialized);
Assert.Equal(buyerSerialized, buyerDeserialized.Serialize());
// serialized mail on v200220 buyerMail
buyerSerialized = (Dictionary)buyerSerialized.Remove((Text)ProductBuyerMail.ProductKey);
buyerDeserialized = new ProductBuyerMail(buyerSerialized);
Assert.Equal(buyerDeserialized.ProductId, ProductId);
Assert.Null(buyerDeserialized.Product);
// check serialize not throw exception
buyerDeserialized.Serialize();

var sellerMail = new ProductSellerMail(1L, ProductId, 1L, ProductId, itemProduct);
var sellerSerialized = (Dictionary)sellerMail.Serialize();
var sellerDeserialized = new ProductSellerMail(sellerSerialized);
Assert.Equal(sellerSerialized, sellerDeserialized.Serialize());
// serialized mail on v200220 sellerMail
sellerSerialized = (Dictionary)buyerSerialized.Remove((Text)ProductBuyerMail.ProductKey);
sellerDeserialized = new ProductSellerMail(sellerSerialized);
Assert.Equal(sellerDeserialized.ProductId, ProductId);
Assert.Null(sellerDeserialized.Product);
// check serialize not throw exception
sellerDeserialized.Serialize();
}

public class ExecuteMember
{
public IEnumerable<IProductInfo> ProductInfos { get; set; }
Expand Down
25 changes: 23 additions & 2 deletions .Lib9c.Tests/Action/ClaimRaidRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Lib9c.Tests.Action
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Helper;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Xunit;
Expand Down Expand Up @@ -64,13 +65,24 @@ public void Execute(Type exc, int rank, int latestRank)
HighScore = highScore,
LatestRewardRank = latestRank,
};
IWorld state = _state.SetLegacyState(raiderAddress, raiderState.Serialize());
var avatarState = AvatarState.Create(
avatarAddress,
agentAddress,
0,
_tableSheets.GetAvatarSheets(),
default
);

var state = _state
.SetLegacyState(raiderAddress, raiderState.Serialize())
.SetAvatarState(avatarAddress, avatarState);
var randomSeed = 0;

var rows = _tableSheets.WorldBossRankRewardSheet.Values
.Where(x => x.BossId == bossRow.BossId);
var expectedCrystal = 0;
var expectedRune = 0;
var expectedCircle = 0;
foreach (var row in rows)
{
if (row.Rank <= latestRank ||
Expand All @@ -81,15 +93,17 @@ public void Execute(Type exc, int rank, int latestRank)

expectedCrystal += row.Crystal;
expectedRune += row.Rune;
expectedCircle += row.Circle;
}

const long blockIndex = 5055201L;
var action = new ClaimRaidReward(avatarAddress);
if (exc is null)
{
var nextState = action.Execute(new ActionContext
{
Signer = agentAddress,
BlockIndex = 5055201L,
BlockIndex = blockIndex,
RandomSeed = randomSeed,
PreviousState = state,
});
Expand All @@ -110,6 +124,13 @@ public void Execute(Type exc, int rank, int latestRank)
}

Assert.Equal(expectedRune, rune);

var circleRow = _tableSheets.MaterialItemSheet.Values.First(r => r.ItemSubType == ItemSubType.Circle);
var inventory = nextState.GetAvatarState(avatarAddress).inventory;
var itemCount = inventory.TryGetTradableFungibleItems(circleRow.ItemId, null, blockIndex, out var items)
? items.Sum(item => item.count)
: 0;
Assert.Equal(expectedCircle, itemCount);
}
else
{
Expand Down
Loading

0 comments on commit 84926d5

Please sign in to comment.