Skip to content

Commit

Permalink
Merge pull request #2404 from planetarium/feature/collection
Browse files Browse the repository at this point in the history
Merge collection system to development
  • Loading branch information
ipdae authored Feb 20, 2024
2 parents 8d17d6e + 9de225f commit 7a666ad
Show file tree
Hide file tree
Showing 50 changed files with 3,289 additions and 228 deletions.
28 changes: 28 additions & 0 deletions .Lib9c.Tests/Action/ActionEvaluationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Lib9c.Tests.Action
using MessagePack.Resolvers;
using Nekoyume.Action;
using Nekoyume.Helper;
using Nekoyume.Model.Collection;
using Nekoyume.Model.Item;
using Nekoyume.Model.Market;
using Nekoyume.Model.State;
Expand Down Expand Up @@ -90,6 +91,7 @@ public ActionEvaluationTest()
[InlineData(typeof(CreatePledge))]
[InlineData(typeof(TransferAssets))]
[InlineData(typeof(RuneSummon))]
[InlineData(typeof(ActivateCollection))]
public void Serialize_With_MessagePack(Type actionType)
{
var action = GetAction(actionType);
Expand Down Expand Up @@ -453,6 +455,32 @@ private ActionBase GetAction(Type type)
GroupId = 20001,
SummonCount = 10,
},
ActivateCollection _ => new ActivateCollection
{
AvatarAddress = _sender,
CollectionData =
{
(
1,
new List<ICollectionMaterial>
{
new FungibleCollectionMaterial
{
ItemId = 1,
ItemCount = 2,
},
new NonFungibleCollectionMaterial
{
ItemId = 2,
ItemCount = 3,
NonFungibleId = Guid.NewGuid(),
Level = 1,
SkillContains = true,
},
}
),
},
},
_ => throw new InvalidCastException(),
};
}
Expand Down
141 changes: 141 additions & 0 deletions .Lib9c.Tests/Action/ActivateCollectionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
namespace Lib9c.Tests.Action
{
using System.Collections.Generic;
using System.Linq;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Model.Collection;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Xunit;

public class ActivateCollectionTest
{
private readonly IWorld _initialState;
private readonly Address _agentAddress;
private readonly Address _avatarAddress;
private readonly TableSheets _tableSheets;

public ActivateCollectionTest()
{
var sheets = TableSheetsImporter.ImportSheets();
// Fix csv data for test
sheets[nameof(CollectionSheet)] = @"id,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,item_id,count,level,skill,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value,stat_type,modify_type,modify_value
1,10110000,1,0,,302000,2,,,200000,2,,,40100000,1,,,,,,,,,,,ATK,Add,1,,,,,,
2,10110000,1,0,,,,,,,,,,,,,,,,,,,,,,ATK,Percentage,1,,,,,,";

_tableSheets = new TableSheets(sheets);

var privateKey = new PrivateKey();
_agentAddress = privateKey.PublicKey.Address;
var agentState = new AgentState(_agentAddress);

_avatarAddress = _agentAddress.Derive("avatar");
var gameConfigState = new GameConfigState(sheets[nameof(GameConfigSheet)]);
var avatarState = new AvatarState(
_avatarAddress,
_agentAddress,
0,
_tableSheets.GetAvatarSheets(),
gameConfigState,
default
)
{
level = 100,
};
agentState.avatarAddresses.Add(0, _avatarAddress);

_initialState = new World(new MockWorldState())
.SetAgentState(_agentAddress, agentState)
.SetAvatarState(_avatarAddress, avatarState, true, true, true, true)
.SetLegacyState(gameConfigState.address, gameConfigState.Serialize());

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

[Fact]
public void Execute()
{
var row = _tableSheets.CollectionSheet.Values.First();
var avatarState = _initialState.GetAvatarState(_avatarAddress);
var materials = new List<ICollectionMaterial>();
var random = new TestRandom();
foreach (var material in row.Materials)
{
var itemRow = _tableSheets.ItemSheet[material.ItemId];
var itemType = itemRow.ItemType;
if (itemType == ItemType.Material)
{
var item = ItemFactory.CreateItem(itemRow, random);
avatarState.inventory.AddItem(item, material.Count);
materials.Add(new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
}
else
{
for (int i = 0; i < material.Count; i++)
{
var item = ItemFactory.CreateItem(itemRow, random);
var nonFungibleId = ((INonFungibleItem)item).NonFungibleId;
avatarState.inventory.AddItem(item);
if (item.ItemType != ItemType.Consumable)
{
materials.Add(new NonFungibleCollectionMaterial
{
ItemId = item.Id,
NonFungibleId = nonFungibleId,
SkillContains = material.SkillContains,
});
}
else
{
// Add consumable material only one.
if (i == 0)
{
materials.Add(new FungibleCollectionMaterial
{
ItemId = item.Id,
ItemCount = material.Count,
});
}
}
}
}
}

var state = _initialState.SetAvatarState(_avatarAddress, avatarState, false, true, false, false);
IActionContext context = new ActionContext()
{
PreviousState = state,
Signer = _agentAddress,
};
ActivateCollection activateCollection = new ActivateCollection()
{
AvatarAddress = _avatarAddress,
CollectionData =
{
(row.Id, materials),
},
};

var nextState = activateCollection.Execute(context);
var collectionState = nextState.GetCollectionState(_avatarAddress);
Assert.Equal(row.Id, collectionState.Ids.Single());

var nextAvatarState = nextState.GetAvatarState(_avatarAddress);
Assert.Empty(nextAvatarState.inventory.Items);
}
}
}
4 changes: 3 additions & 1 deletion .Lib9c.Tests/Action/Scenario/AuraScenarioTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ public void Arena()
var log = simulator.Simulate(
myArenaPlayerDigest,
enemyArenaPlayerDigest,
_tableSheets.GetArenaSimulatorSheets());
_tableSheets.GetArenaSimulatorSheets(),
new List<StatModifier>(),
new List<StatModifier>());
// Check player, enemy equip aura
foreach (var spawn in log.OfType<ArenaSpawnCharacter>())
{
Expand Down
Loading

0 comments on commit 7a666ad

Please sign in to comment.