Skip to content

Commit

Permalink
Merge pull request #2904 from planetarium/feature/summon-100
Browse files Browse the repository at this point in the history
Introduce CostumeSummon and etc.
  • Loading branch information
sonohoshi authored Oct 18, 2024
2 parents 6fa652b + 7a0d4ab commit d098f34
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 86 deletions.
10 changes: 4 additions & 6 deletions .Lib9c.Tests/Action/Summon/AuraSummonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,8 @@ public void CumulativeRatio(string version, int groupId)
[Theory]
// success first group
[InlineData("V1", 10001, 1, 800201, 1, 1, new[] { 10610000 }, null)]
[InlineData("V1", 10001, 2, 800201, 2, 54, new[] { 10620000, 10630000 }, null)]
// success second group
[InlineData("V1", 10002, 1, 600201, 1, 1, new[] { 10620001 }, null)]
[InlineData("V1", 10002, 2, 600201, 2, 4, new[] { 10620001, 10630001 }, null)]
// Nine plus zero
[InlineData(
"V1",
Expand All @@ -136,7 +134,7 @@ public void CumulativeRatio(string version, int groupId)
9,
0,
new[] { 10610000, 10610000, 10610000, 10610000, 10610000, 10610000, 10620000, 10620000, 10620000 },
null
typeof(InvalidSummonCountException)
)]
[InlineData(
"V1",
Expand All @@ -146,7 +144,7 @@ public void CumulativeRatio(string version, int groupId)
9,
0,
new[] { 10620001, 10620001, 10620001, 10620001, 10620001, 10630001, 10630001, 10630001, 10630001 },
null
typeof(InvalidSummonCountException)
)]
// Ten plus one
[InlineData(
Expand All @@ -173,9 +171,9 @@ public void CumulativeRatio(string version, int groupId)
[InlineData("V1", 100003, 1, null, 0, 0, new int[] { }, typeof(RowNotInTableException))]
// fail by not enough material
[InlineData("V1", 10001, 1, 800201, 0, 0, new int[] { }, typeof(NotEnoughMaterialException))]
[InlineData("V1", 10001, 2, 800201, 0, 0, new int[] { }, typeof(NotEnoughMaterialException))]
[InlineData("V1", 10001, 10, 800201, 0, 0, new int[] { }, typeof(NotEnoughMaterialException))]
// Fail by exceeding summon limit
[InlineData("V1", 10001, 11, 800201, 22, 1, new int[] { }, typeof(InvalidSummonCountException))]
[InlineData("V1", 10001, 101, 800201, 22, 1, new int[] { }, typeof(InvalidSummonCountException))]
// 15 recipes
[InlineData("V2", 10002, 1, 600201, 1, 5341, new[] { 10650006 }, null)]
// 15 recipes
Expand Down
220 changes: 220 additions & 0 deletions .Lib9c.Tests/Action/Summon/CostumeSummonTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
namespace Lib9c.Tests.Action.Summon
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Mocks;
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.Exceptions;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Nekoyume.TableData.Summon;
using Xunit;
using static SerializeKeys;

public class CostumeSummonTest
{
private readonly Address _agentAddress;
private readonly Address _avatarAddress;
private readonly AvatarState _avatarState;
private readonly Currency _currency;
private TableSheets _tableSheets;
private IWorld _initialState;

public CostumeSummonTest()
{
var sheets = TableSheetsImporter.ImportSheets();
_tableSheets = new TableSheets(sheets);
var privateKey = new PrivateKey();
_agentAddress = privateKey.PublicKey.Address;
var agentState = new AgentState(_agentAddress);

_avatarAddress = _agentAddress.Derive("avatar");
_avatarState = AvatarState.Create(
_avatarAddress,
_agentAddress,
0,
_tableSheets.GetAvatarSheets(),
default
);

agentState.avatarAddresses.Add(0, _avatarAddress);

#pragma warning disable CS0618
// Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319
_currency = Currency.Legacy("NCG", 2, null);
#pragma warning restore CS0618
var gold = new GoldCurrencyState(_currency);

var context = new ActionContext();
_initialState = new World(MockUtil.MockModernWorldState)
.SetAgentState(_agentAddress, agentState)
.SetAvatarState(_avatarAddress, _avatarState)
.SetLegacyState(GoldCurrencyState.Address, gold.Serialize())
.MintAsset(context, GoldCurrencyState.Address, gold.Currency * 100000000000)
.MintAsset(context, _avatarAddress, 100 * Currencies.GetRune("RUNESTONE_FENRIR1"))
.TransferAsset(
context,
Addresses.GoldCurrency,
_agentAddress,
gold.Currency * 1000
);

Assert.Equal(
gold.Currency * 99999999000,
_initialState.GetBalance(Addresses.GoldCurrency, gold.Currency)
);
Assert.Equal(
gold.Currency * 1000,
_initialState.GetBalance(_agentAddress, gold.Currency)
);

foreach (var (key, value) in sheets)
{
_initialState =
_initialState.SetLegacyState(Addresses.TableSheet.Derive(key), value.Serialize());
}
}

[Theory]
[ClassData(typeof(ExecuteMemeber))]
public void Execute(
int groupId,
int summonCount,
int? materialId,
int materialCount,
int seed,
Type expectedExc
)
{
var random = new TestRandom(seed);
var state = _initialState;
state = state.SetLegacyState(
Addresses.TableSheet.Derive(nameof(SummonSheet)),
_tableSheets.SummonSheet.Serialize()
);

if (!(materialId is null))
{
var materialSheet = _tableSheets.MaterialItemSheet;
var material = materialSheet.OrderedList.FirstOrDefault(m => m.Id == materialId);
_avatarState.inventory.AddItem(
ItemFactory.CreateItem(material, random),
materialCount * _tableSheets.SummonSheet[groupId].CostMaterialCount);
state = state.SetAvatarState(_avatarAddress, _avatarState);
}

var action = new CostumeSummon
{
AvatarAddress = _avatarAddress,
GroupId = groupId,
SummonCount = summonCount,
};

if (expectedExc == null)
{
// Success
var ctx = new ActionContext
{
PreviousState = state,
Signer = _agentAddress,
BlockIndex = 1,
};
ctx.SetRandom(random);
var nextState = action.Execute(ctx);
var result = CostumeSummon.SimulateSummon(
string.Empty,
_tableSheets.CostumeItemSheet,
_tableSheets.SummonSheet[groupId],
summonCount,
new TestRandom(seed)
);
var inventory = nextState.GetAvatarState(_avatarAddress).inventory;
foreach (var costume in result)
{
inventory.TryGetNonFungibleItem(costume.ItemId, out Costume outItem);
Assert.Equal(costume, outItem);
}

inventory.TryGetItem((int)materialId!, out var resultMaterial);
Assert.Equal(0, resultMaterial?.count ?? 0);
}
else
{
// Failure
Assert.Throws(expectedExc, () =>
{
action.Execute(new ActionContext
{
PreviousState = state,
Signer = _agentAddress,
BlockIndex = 1,
RandomSeed = random.Seed,
});
});
}
}

private class ExecuteMemeber : IEnumerable<object[]>
{
private readonly List<object[]> _data = new ()
{
new object[]
{
50001, 1, 600202, 1, 1, null,
},
new object[]
{
50001, 2, 600202, 2, 54, typeof(InvalidSummonCountException),
},
// Ten plus one
new object[]
{
50002,
10,
600202,
10,
0,
null,
},
// 100 + 10
new object[]
{
50002,
100,
600202,
100,
0,
null,
},
// fail by invalid group
new object[]
{
100003, 1, null, 0, 0, typeof(RowNotInTableException),
},
// fail by not enough material
new object[]
{
50002, 1, 600202, 0, 0, typeof(NotEnoughMaterialException),
},
// Fail by exceeding summon limit
new object[]
{
50002, 101, 600202, 22, 1, typeof(InvalidSummonCountException),
},
};

public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => _data.GetEnumerator();
}
}
}
30 changes: 2 additions & 28 deletions .Lib9c.Tests/Action/Summon/RuneSummonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,6 @@ public RuneSummonTest()
}
}

[Theory]
[InlineData(20001)]
public void CumulativeRatio(int groupId)
{
var sheet = _tableSheets.SummonSheet;
var targetRow = sheet.OrderedList.First(r => r.GroupId == groupId);

for (var i = 1; i <= SummonSheet.Row.MaxRecipeCount; i++)
{
var sum = 0;
for (var j = 0; j < i; j++)
{
if (j < targetRow.Recipes.Count)
{
sum += targetRow.Recipes[j].Item2;
}
}

Assert.Equal(sum, targetRow.CumulativeRatio(i));
}
}

[Theory]
[ClassData(typeof(ExecuteMemeber))]
public void Execute(
Expand Down Expand Up @@ -194,10 +172,6 @@ private class ExecuteMemeber : IEnumerable<object[]>
{
20001, 1, 600201, 1, 1, null,
},
new object[]
{
20001, 2, 600201, 2, 54, null,
},
// Nine plus zero
new object[]
{
Expand All @@ -206,7 +180,7 @@ private class ExecuteMemeber : IEnumerable<object[]>
600201,
9,
0,
null,
typeof(InvalidSummonCountException),
},
// Ten plus one
new object[]
Expand All @@ -231,7 +205,7 @@ private class ExecuteMemeber : IEnumerable<object[]>
// Fail by exceeding summon limit
new object[]
{
20001, 11, 600201, 22, 1, typeof(InvalidSummonCountException),
20001, 101, 600201, 22, 1, typeof(InvalidSummonCountException),
},
new object[]
{
Expand Down
29 changes: 29 additions & 0 deletions .Lib9c.Tests/TableData/Summon/SummonSheetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,34 @@ public void Total_Cumulative_Ratio()
// If index exceeds recipe count, just return total cumulative ratio.
Assert.Equal(100, row.CumulativeRatio(3));
}

[Theory]
[InlineData(10001)]
[InlineData(10002)]
[InlineData(20001)]
[InlineData(30001)]
[InlineData(30002)]
[InlineData(40001)]
[InlineData(50001)]
[InlineData(50002)]
public void CumulativeRatio(int groupId)
{
var sheet = _summonSheet;
var targetRow = sheet.OrderedList.First(r => r.GroupId == groupId);

for (var i = 1; i <= SummonSheet.Row.MaxRecipeCount; i++)
{
var sum = 0;
for (var j = 0; j < i; j++)
{
if (j < targetRow.Recipes.Count)
{
sum += targetRow.Recipes[j].Item2;
}
}

Assert.Equal(sum, targetRow.CumulativeRatio(i));
}
}
}
}
Loading

0 comments on commit d098f34

Please sign in to comment.