From ade453b7d18dcb92698336692456445a845702fa Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:25:21 +0900 Subject: [PATCH 1/4] code sweep dev extensions --- .../Action/Craft/UnlocakCraftActionTest.cs | 7 +- .../Action/CreateOrReplaceAvatarTest.cs | 12 ++- .../CreateOrReplaceAvatarFactoryTest.cs | 24 +---- .../Action/FaucetCurrencyTest.cs | 12 +-- .../Action/FaucetRuneTest.cs | 18 ++-- .../Action/ManipulateStateTest.cs | 50 +++++----- .../Action/Stage/ClearStageTest.cs | 1 - .../TableSheetsImporter.cs | 6 +- .Lib9c.DevExtensions.Tests/UtilsTest.cs | 6 +- .../Action/Craft/UnlockCraftAction.cs | 6 +- .../Action/Craft/UnlockRecipe.cs | 2 +- .../Action/CreateArenaDummy.cs | 3 +- .../Action/CreateOrReplaceAvatar.cs | 13 +-- Lib9c.DevExtensions/Action/CreateTestbed.cs | 5 +- .../Factory/CreateOrReplaceAvatarFactory.cs | 1 - Lib9c.DevExtensions/Action/FaucetCurrency.cs | 2 +- Lib9c.DevExtensions/Action/FaucetRune.cs | 4 +- .../Action/Loader/NCDevActionLoader.cs | 5 +- .../Action/Stage/ClearStage.cs | 1 + Lib9c.DevExtensions/Model/BaseTestbedModel.cs | 2 +- .../Model/TestbedCreateAvatar.cs | 2 +- Lib9c.DevExtensions/Model/TestbedSell.cs | 19 ++-- Lib9c.DevExtensions/TestbedHelper.cs | 11 +-- Lib9c.DevExtensions/Utils.cs | 91 ++++++++++--------- 24 files changed, 151 insertions(+), 152 deletions(-) diff --git a/.Lib9c.DevExtensions.Tests/Action/Craft/UnlocakCraftActionTest.cs b/.Lib9c.DevExtensions.Tests/Action/Craft/UnlocakCraftActionTest.cs index 9eb82fcb9a..4e624d019f 100644 --- a/.Lib9c.DevExtensions.Tests/Action/Craft/UnlocakCraftActionTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/Craft/UnlocakCraftActionTest.cs @@ -1,4 +1,3 @@ -using Bencodex.Types; using Lib9c.DevExtensions.Action.Craft; using Lib9c.Tests; using Lib9c.Tests.Action; @@ -7,8 +6,6 @@ using Libplanet.Action.State; using Libplanet.Crypto; using Nekoyume; -using Nekoyume.Action; -using Nekoyume.Model; using Nekoyume.Module; using Xunit; @@ -45,14 +42,14 @@ public void StageUnlockTest(string typeIdentifier, int expectedStage) var action = new UnlockCraftAction { AvatarAddress = _avatarAddress, - ActionType = new ActionTypeAttribute(typeIdentifier) + ActionType = new ActionTypeAttribute(typeIdentifier), }; var state = action.Execute(new ActionContext { PreviousState = _initialStateV2, Signer = _agentAddress, - BlockIndex = 0L + BlockIndex = 0L, }); var avatarState = state.GetAvatarState(_avatarAddress); diff --git a/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs b/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs index bc324fe817..dabab140b0 100644 --- a/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/CreateOrReplaceAvatarTest.cs @@ -82,14 +82,14 @@ public CreateOrReplaceAvatarTest() (200000, 1), (201010, 2), }, - new[] { 40100000 }, + new[] { 40100000, }, new[] { (10001, 0), (20001, 1), (30001, 10), }, - (1, new[] { 1, 2, 3 }), + (1, new[] { 1, 2, 3, }), }; } @@ -221,7 +221,7 @@ public static IEnumerable 0, 0, "AB", 0, 0, 0, 0, 1, null, null, - new[] { -1 }, + new[] { -1, }, null, null, }; @@ -273,7 +273,7 @@ public static IEnumerable null, null, null, - (0, new[] { -1 }), + (0, new[] { -1, }), }; } @@ -304,6 +304,7 @@ public void Serialize( equipments.Add((row.ResultEquipmentId, data.level)); } } + var action = new CreateOrReplaceAvatar( avatarIndex, name, @@ -371,6 +372,7 @@ public void Execute_Success( equipments.Add((row.ResultEquipmentId, data.level)); } } + var agentAddr = new PrivateKey().Address; Execute( _initialStates, @@ -521,7 +523,7 @@ private static void Execute( Assert.Contains( equipment.StatsMap.GetAdditionalStats(false), stat => stat.statType == statOption.Key && - stat.additionalValue == statOption.Value); + stat.additionalValue == statOption.Value); } var skillOptions = options diff --git a/.Lib9c.DevExtensions.Tests/Action/Factory/CreateOrReplaceAvatarFactoryTest.cs b/.Lib9c.DevExtensions.Tests/Action/Factory/CreateOrReplaceAvatarFactoryTest.cs index 2ff7e3035a..e9eae99ba5 100644 --- a/.Lib9c.DevExtensions.Tests/Action/Factory/CreateOrReplaceAvatarFactoryTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/Factory/CreateOrReplaceAvatarFactoryTest.cs @@ -12,12 +12,7 @@ namespace Lib9c.DevExtensions.Tests.Action.Factory { public class CreateOrReplaceAvatarFactoryTest { - private readonly TableSheets _tableSheets; - - public CreateOrReplaceAvatarFactoryTest() - { - _tableSheets = new TableSheets(TableSheetsImporter.ImportSheets()); - } + private readonly TableSheets _tableSheets = new TableSheets(TableSheetsImporter.ImportSheets()); [Theory] [MemberData( @@ -50,9 +45,7 @@ public void TryGetByBlockIndex_Success( } var (e, r) = CreateOrReplaceAvatarFactory - .TryGetByBlockIndex( - blockIndex, - avatarIndex, + .TryGetByBlockIndex(avatarIndex, name, hair, lens, @@ -73,14 +66,7 @@ public void TryGetByBlockIndex_Success( Assert.Equal(ear, r.Ear); Assert.Equal(tail, r.Tail); Assert.Equal(level, r.Level); - if (equipments is null) - { - Assert.Empty(r.Equipments); - } - else - { - Assert.True(equipments.SequenceEqual(r.Equipments)); - } + Assert.True(equipments.SequenceEqual(r.Equipments)); if (foods is null) { @@ -142,9 +128,7 @@ public void TryGetByBlockIndex_Failure( (int stageId, int[] crystalRandomBuffIds)? crystalRandomBuff) { var (e, r) = CreateOrReplaceAvatarFactory - .TryGetByBlockIndex( - blockIndex, - avatarIndex, + .TryGetByBlockIndex(avatarIndex, name, hair, lens, diff --git a/.Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs b/.Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs index 7b4fd7f1f4..732d51021c 100644 --- a/.Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/FaucetCurrencyTest.cs @@ -64,16 +64,16 @@ int expectedCrystal FaucetNcg = faucetNcg, FaucetCrystal = faucetCrystal, }; - var state = action.Execute(new ActionContext { PreviousState = _initialState }); - AgentState agentState = state.GetAgentState(_agentAddress); - FungibleAssetValue expectedNcgAsset = + var state = action.Execute(new ActionContext { PreviousState = _initialState, }); + var agentState = state.GetAgentState(_agentAddress); + var expectedNcgAsset = new FungibleAssetValue(_ncg, expectedNcg, 0); - FungibleAssetValue ncg = state.GetBalance(_agentAddress, state.GetGoldCurrency()); + var ncg = state.GetBalance(_agentAddress, state.GetGoldCurrency()); Assert.Equal(expectedNcgAsset, ncg); - FungibleAssetValue expectedCrystalAsset = + var expectedCrystalAsset = new FungibleAssetValue(_crystal, expectedCrystal, 0); - FungibleAssetValue crystal = state.GetBalance(_agentAddress, _crystal); + var crystal = state.GetBalance(_agentAddress, _crystal); Assert.Equal(expectedCrystalAsset, crystal); } } diff --git a/.Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs b/.Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs index a363067850..5501eb79e1 100644 --- a/.Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/FaucetRuneTest.cs @@ -45,7 +45,7 @@ public FaucetRuneTest(ITestOutputHelper outputHelper) var tableSheets = new TableSheets(sheets); _runeSheet = _initialState.GetSheet(); - Address agentAddress = new PrivateKey().Address; + var agentAddress = new PrivateKey().Address; _avatarAddress = new PrivateKey().Address; var agentState = new AgentState(agentAddress); var avatarState = AvatarState.Create( @@ -58,8 +58,8 @@ public FaucetRuneTest(ITestOutputHelper outputHelper) agentState.avatarAddresses.Add(0, _avatarAddress); _initialState = _initialState - .SetAgentState(agentAddress, agentState) - .SetAvatarState(_avatarAddress, avatarState); + .SetAgentState(agentAddress, agentState) + .SetAvatarState(_avatarAddress, avatarState); } [Theory] @@ -71,7 +71,7 @@ public void Execute_FaucetRune(List faucetRuneInfos) AvatarAddress = _avatarAddress, FaucetRuneInfos = faucetRuneInfos, }; - var states = action.Execute(new ActionContext { PreviousState = _initialState }); + var states = action.Execute(new ActionContext { PreviousState = _initialState, }); foreach (var rune in faucetRuneInfos) { var expectedRune = RuneHelper.ToCurrency( @@ -113,9 +113,15 @@ private class FaucetRuneInfoGenerator : IEnumerable }, }; - public IEnumerator GetEnumerator() => _data.GetEnumerator(); + public IEnumerator GetEnumerator() + { + return _data.GetEnumerator(); + } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } } } } diff --git a/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs b/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs index 83741b33d2..3887982e34 100644 --- a/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/ManipulateStateTest.cs @@ -48,7 +48,7 @@ public ManipulateStateTest() { (_tableSheets, _agentAddress, _avatarAddress, _initialStateV2) = InitializeUtil.InitializeStates( - adminAddr: AdminAddr, + AdminAddr, isDevEx: true); _recipeAddress = _avatarAddress.Derive("recipe_ids"); _avatarState = _initialStateV2.GetAvatarState(_avatarAddress); @@ -69,42 +69,42 @@ public ManipulateStateTest() { "newAvatar", null, null, null, null, null, - null, null, null, null + null, null, null, null, }; // Change level and exp yield return new object?[] { null, random.Next(1, 300), (long)random.Next(0, 100), null, null, null, - null, null, null, null + null, null, null, null, }; // Change AP yield return new object?[] { null, null, null, random.Next(0, 120), null, null, - null, null, null, null + null, null, null, null, }; // Change block indexes yield return new object?[] { null, null, null, null, blockIndex + 1700, blockIndex, // Get another daily reward - null, null, null, null + null, null, null, null, }; // Change outfit yield return new object?[] { null, null, null, null, null, null, - random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), random.Next(0, 4) + random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), }; // Change multiple things yield return new object?[] { "newAvatar", random.Next(1, 300), (long)random.Next(0, 100), random.Next(0, 120), blockIndex + 1700, blockIndex, - random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), random.Next(0, 4) + random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), random.Next(0, 4), }; } @@ -131,7 +131,7 @@ public static IEnumerable FetchInventory() // Clear Inventory yield return new object[] { - new Inventory() + new Inventory(), }; // Equipment @@ -139,7 +139,7 @@ public static IEnumerable FetchInventory() equipmentInventory.AddItem(equipment); yield return new object[] { - equipmentInventory + equipmentInventory, }; // Material @@ -147,14 +147,14 @@ public static IEnumerable FetchInventory() materialInventory.AddItem(material); yield return new object[] { - materialInventory + materialInventory, }; // Consumable var consumableInventory = new Inventory(); consumableInventory.AddItem(equipment); yield return new object[] { - consumableInventory + consumableInventory, }; // Mixed var inventory = new Inventory(); @@ -163,7 +163,7 @@ public static IEnumerable FetchInventory() inventory.AddItem(material); yield return new object[] { - inventory + inventory, }; } @@ -175,20 +175,20 @@ public static IEnumerable FetchWorldInfo() yield return new object[] { 0, - new WorldInformation(0L, worldSheet, 0) + new WorldInformation(0L, worldSheet, 0), }; var targetStage = random.Next(1, 300); yield return new object[] { targetStage, - new WorldInformation(0L, worldSheet, targetStage) + new WorldInformation(0L, worldSheet, targetStage), }; yield return new object[] { tableSheets.WorldSheet.OrderedList.Last(world => world.Id < 100).StageEnd, - new WorldInformation(0L, worldSheet, true) + new WorldInformation(0L, worldSheet, true), }; } @@ -213,7 +213,7 @@ public static IEnumerable FetchQuest() tableSheets.QuestItemRewardSheet, tableSheets.EquipmentItemRecipeSheet, tableSheets.EquipmentItemSubRecipeSheet - ) + ), }; // Clear combination quest @@ -231,7 +231,7 @@ public static IEnumerable FetchQuest() yield return new object[] { combinationQuestList.completedQuestIds, - combinationQuestList + combinationQuestList, }; // Clear trade quest @@ -247,7 +247,7 @@ public static IEnumerable FetchQuest() yield return new object[] { tradeQuestList.completedQuestIds, - tradeQuestList + tradeQuestList, }; // Clear stage quest @@ -269,7 +269,7 @@ public static IEnumerable FetchQuest() yield return new object[] { stageQuestList.completedQuestIds, - stageQuestList + stageQuestList, }; // Clear multiple @@ -291,7 +291,7 @@ public static IEnumerable FetchQuest() yield return new object[] { questList.completedQuestIds, - questList + questList, }; } @@ -355,7 +355,7 @@ private IWorld Manipulate( { PreviousState = state, Signer = _agentAddress, - BlockIndex = int.MaxValue / 2 + BlockIndex = int.MaxValue / 2, }); } @@ -455,7 +455,7 @@ public void SetAvatarState( _initialStateV2, new List<(Address, Address, IValue)> { - (Addresses.Avatar, _avatarAddress, newAvatarState.SerializeList()) + (Addresses.Avatar, _avatarAddress, newAvatarState.SerializeList()), }, new List<(Address, FungibleAssetValue)>() ); @@ -547,7 +547,7 @@ public void SetWorldInformation(int lastClearedStage, WorldInformation targetInf _initialStateV2, new List<(Address, Address, IValue)> { - (Addresses.WorldInformation, _avatarAddress, targetInfo.Serialize()) + (Addresses.WorldInformation, _avatarAddress, targetInfo.Serialize()), }, new List<(Address, FungibleAssetValue)>() ); @@ -562,7 +562,7 @@ public void SetQuestState(List targetQuestIdList, QuestList questList) var state = Manipulate(_initialStateV2, new List<(Address, Address, IValue)> { - (Addresses.QuestList, _avatarAddress, questList.Serialize()) + (Addresses.QuestList, _avatarAddress, questList.Serialize()), }, new List<(Address, FungibleAssetValue)>() ); @@ -579,7 +579,7 @@ public void SetBalance(Address addr, FungibleAssetValue fav) new List<(Address, Address, IValue)>(), new List<(Address, FungibleAssetValue)> { - (addr, fav) + (addr, fav), } ); diff --git a/.Lib9c.DevExtensions.Tests/Action/Stage/ClearStageTest.cs b/.Lib9c.DevExtensions.Tests/Action/Stage/ClearStageTest.cs index 6a02914b13..f64eef2cd6 100644 --- a/.Lib9c.DevExtensions.Tests/Action/Stage/ClearStageTest.cs +++ b/.Lib9c.DevExtensions.Tests/Action/Stage/ClearStageTest.cs @@ -5,7 +5,6 @@ using Lib9c.Tests.Util; using Libplanet.Crypto; using Libplanet.Action.State; -using Nekoyume.Action; using Nekoyume.Module; using Xunit; diff --git a/.Lib9c.DevExtensions.Tests/TableSheetsImporter.cs b/.Lib9c.DevExtensions.Tests/TableSheetsImporter.cs index 0786212966..42a03b8d6b 100644 --- a/.Lib9c.DevExtensions.Tests/TableSheetsImporter.cs +++ b/.Lib9c.DevExtensions.Tests/TableSheetsImporter.cs @@ -5,11 +5,13 @@ namespace Lib9c.DevExtensions.Tests { public static class TableSheetsImporter { - public static Dictionary ImportSheets() => - Lib9c.Tests.TableSheetsImporter.ImportSheets(Path + public static Dictionary ImportSheets() + { + return Lib9c.Tests.TableSheetsImporter.ImportSheets(Path .GetFullPath("../../") .Replace( Path.Combine(".Lib9c.DevExtensions.Tests", "bin"), Path.Combine("Lib9c", "TableCSV"))); + } } } diff --git a/.Lib9c.DevExtensions.Tests/UtilsTest.cs b/.Lib9c.DevExtensions.Tests/UtilsTest.cs index d6c9043fff..408eab75f9 100644 --- a/.Lib9c.DevExtensions.Tests/UtilsTest.cs +++ b/.Lib9c.DevExtensions.Tests/UtilsTest.cs @@ -33,8 +33,8 @@ public void ImportSheetTest() { IDictionary sheets = Utils.ImportSheets(); - string enhancement = Assert.Contains("EnhancementCostSheet", sheets); - string gameConfig = Assert.Contains("GameConfigSheet", sheets); + var enhancement = Assert.Contains("EnhancementCostSheet", sheets); + var gameConfig = Assert.Contains("GameConfigSheet", sheets); Assert.Contains("id,item_sub_type,grade,level,cost", enhancement); Assert.Contains("key,value", gameConfig); @@ -99,7 +99,7 @@ public void GetActivatedAccounts() var configPath = Path.GetTempFileName(); File.WriteAllText(configPath, json); - ImmutableHashSet
activatedAccounts = Utils.GetActivatedAccounts(configPath); + var activatedAccounts = Utils.GetActivatedAccounts(configPath); Assert.Equal(5, activatedAccounts.Count); Assert.Contains(new Address("0000000000000000000000000000000000000001"), activatedAccounts); Assert.Contains(new Address("0000000000000000000000000000000000000002"), activatedAccounts); diff --git a/Lib9c.DevExtensions/Action/Craft/UnlockCraftAction.cs b/Lib9c.DevExtensions/Action/Craft/UnlockCraftAction.cs index cdb8abace7..10b1a9e6d0 100644 --- a/Lib9c.DevExtensions/Action/Craft/UnlockCraftAction.cs +++ b/Lib9c.DevExtensions/Action/Craft/UnlockCraftAction.cs @@ -60,7 +60,7 @@ public override IWorld Execute(IActionContext context) var addressesHex = GetSignerAndOtherAddressesHex(context, AvatarAddress); throw new FailedLoadStateException($"{addressesHex}Aborted as the avatar state of the signer was failed to load."); } - + avatarState.worldInformation = new WorldInformation( context.BlockIndex, states.GetSheet(), @@ -73,12 +73,12 @@ public override IWorld Execute(IActionContext context) new Dictionary { ["avatarAddress"] = AvatarAddress.Serialize(), - ["typeIdentifier"] = ActionType.TypeIdentifier + ["typeIdentifier"] = ActionType.TypeIdentifier, }.ToImmutableDictionary(); protected override void LoadPlainValueInternal( IImmutableDictionary plainValue - ) + ) { AvatarAddress = plainValue["avatarAddress"].ToAddress(); ActionType = new ActionTypeAttribute(plainValue["typeIdentifier"].ToDotnetString()); diff --git a/Lib9c.DevExtensions/Action/Craft/UnlockRecipe.cs b/Lib9c.DevExtensions/Action/Craft/UnlockRecipe.cs index 4f0d118a70..77d64d13de 100644 --- a/Lib9c.DevExtensions/Action/Craft/UnlockRecipe.cs +++ b/Lib9c.DevExtensions/Action/Craft/UnlockRecipe.cs @@ -38,7 +38,7 @@ public override IWorld Execute(IActionContext context) new Dictionary { ["avatarAddress"] = AvatarAddress.Serialize(), - ["targetStage"] = TargetStage.Serialize() + ["targetStage"] = TargetStage.Serialize(), }.ToImmutableDictionary(); protected override void LoadPlainValueInternal(IImmutableDictionary plainValue) diff --git a/Lib9c.DevExtensions/Action/CreateArenaDummy.cs b/Lib9c.DevExtensions/Action/CreateArenaDummy.cs index 8e85d7892f..7e37aba7dc 100644 --- a/Lib9c.DevExtensions/Action/CreateArenaDummy.cs +++ b/Lib9c.DevExtensions/Action/CreateArenaDummy.cs @@ -90,7 +90,7 @@ public override IWorld Execute(IActionContext context) // create ArenaScore var sheets = states.GetSheets( - sheetTypes: new[] + new[] { typeof(ItemRequirementSheet), typeof(EquipmentItemRecipeSheet), @@ -190,6 +190,5 @@ public override IWorld Execute(IActionContext context) return states; } - } } diff --git a/Lib9c.DevExtensions/Action/CreateOrReplaceAvatar.cs b/Lib9c.DevExtensions/Action/CreateOrReplaceAvatar.cs index 8f4b5bf291..8c4694af20 100644 --- a/Lib9c.DevExtensions/Action/CreateOrReplaceAvatar.cs +++ b/Lib9c.DevExtensions/Action/CreateOrReplaceAvatar.cs @@ -41,11 +41,7 @@ public class CreateOrReplaceAvatar : GameAction, ICreateOrReplaceAvatar public IOrderedEnumerable CostumeIds { get; private set; } public IOrderedEnumerable<(int runeId, int level)> Runes { get; private set; } - public (int stageId, IOrderedEnumerable crystalRandomBuffIds)? CrystalRandomBuff - { - get; - private set; - } + public (int stageId, IOrderedEnumerable crystalRandomBuffIds)? CrystalRandomBuff { get; private set; } // public protected override IImmutableDictionary PlainValueInternal @@ -395,7 +391,7 @@ public IWorld Execute( // ~Set AgentState. var sheets = states.GetSheets( - containAvatarSheets: true, + true, containQuestSheet: true, sheetTypes: new[] { @@ -564,6 +560,7 @@ public IWorld Execute( var slot = new CombinationSlotState(slotAddr, i); allCombinationSlotState.AddSlot(slot); } + states = states.SetCombinationSlotState(avatarAddr, allCombinationSlotState); // ~Set CombinationSlot. @@ -592,8 +589,8 @@ public IWorld Execute( crb.stageId); var crystalStageBuffGachaSheet = sheets.GetSheet(); if (crystalStageBuffGachaSheet.TryGetValue( - crb.stageId, - out var crystalStageBuffGachaRow)) + crb.stageId, + out var crystalStageBuffGachaRow)) { crystalRandomSkillState.Update( crystalStageBuffGachaRow.MaxStar, diff --git a/Lib9c.DevExtensions/Action/CreateTestbed.cs b/Lib9c.DevExtensions/Action/CreateTestbed.cs index cfd770563e..712f643f38 100644 --- a/Lib9c.DevExtensions/Action/CreateTestbed.cs +++ b/Lib9c.DevExtensions/Action/CreateTestbed.cs @@ -59,7 +59,7 @@ public ItemInfos(Guid orderId, Guid tradableId, ItemSubType itemSubType, BigInte protected override IImmutableDictionary PlainValueInternal => new Dictionary() { - {"w", weeklyArenaAddress.Serialize()}, + { "w", weeklyArenaAddress.Serialize() }, }.ToImmutableDictionary(); protected override void LoadPlainValueInternal( @@ -121,7 +121,7 @@ public override IWorld Execute(IActionContext context) rankingMapAddress); // Add item - var costumeItemSheet = context.PreviousState.GetSheet(); + var costumeItemSheet = context.PreviousState.GetSheet(); var equipmentItemSheet = context.PreviousState.GetSheet(); var optionSheet = context.PreviousState.GetSheet(); var skillSheet = context.PreviousState.GetSheet(); @@ -148,6 +148,7 @@ public override IWorld Execute(IActionContext context) var slot = new CombinationSlotState(slotAddr, i); allCombinationSlotState.AddSlot(slot); } + states = states.SetCombinationSlotState(avatarAddress, allCombinationSlotState); avatarState.UpdateQuestRewards(materialItemSheet); diff --git a/Lib9c.DevExtensions/Action/Factory/CreateOrReplaceAvatarFactory.cs b/Lib9c.DevExtensions/Action/Factory/CreateOrReplaceAvatarFactory.cs index 9ca1608469..02f33566f6 100644 --- a/Lib9c.DevExtensions/Action/Factory/CreateOrReplaceAvatarFactory.cs +++ b/Lib9c.DevExtensions/Action/Factory/CreateOrReplaceAvatarFactory.cs @@ -9,7 +9,6 @@ public static class CreateOrReplaceAvatarFactory { public static (Exception? exception, ICreateOrReplaceAvatar? result) TryGetByBlockIndex( - long blockIndex, int avatarIndex = 0, string name = "Avatar", int hair = 0, diff --git a/Lib9c.DevExtensions/Action/FaucetCurrency.cs b/Lib9c.DevExtensions/Action/FaucetCurrency.cs index b49ac362a6..734805f368 100644 --- a/Lib9c.DevExtensions/Action/FaucetCurrency.cs +++ b/Lib9c.DevExtensions/Action/FaucetCurrency.cs @@ -47,7 +47,7 @@ public override IWorld Execute(IActionContext context) { ["agentAddress"] = AgentAddress.Serialize(), ["faucetNcg"] = FaucetNcg.Serialize(), - ["faucetCrystal"] = FaucetCrystal.Serialize() + ["faucetCrystal"] = FaucetCrystal.Serialize(), }.ToImmutableDictionary(); protected override void LoadPlainValueInternal( diff --git a/Lib9c.DevExtensions/Action/FaucetRune.cs b/Lib9c.DevExtensions/Action/FaucetRune.cs index 8f936ff013..ae0993fd73 100644 --- a/Lib9c.DevExtensions/Action/FaucetRune.cs +++ b/Lib9c.DevExtensions/Action/FaucetRune.cs @@ -29,7 +29,7 @@ public override IWorld Execute(IActionContext context) var states = context.PreviousState; if (!(FaucetRuneInfos is null)) { - RuneSheet runeSheet = states.GetSheet(); + var runeSheet = states.GetSheet(); if (runeSheet.OrderedList != null) { foreach (var rune in FaucetRuneInfos) @@ -53,7 +53,7 @@ public override IWorld Execute(IActionContext context) .OrderBy(x => x.RuneId) .ThenBy(x => x.Amount) .Select(x => x.Serialize()) - .Serialize() + .Serialize(), }.ToImmutableDictionary(); protected override void LoadPlainValueInternal( diff --git a/Lib9c.DevExtensions/Action/Loader/NCDevActionLoader.cs b/Lib9c.DevExtensions/Action/Loader/NCDevActionLoader.cs index fd612d0f66..995e482a18 100644 --- a/Lib9c.DevExtensions/Action/Loader/NCDevActionLoader.cs +++ b/Lib9c.DevExtensions/Action/Loader/NCDevActionLoader.cs @@ -26,6 +26,9 @@ public NCDevActionLoader() } /// - public IAction LoadAction(long index, IValue value) => _actionLoader.LoadAction(index, value); + public IAction LoadAction(long index, IValue value) + { + return _actionLoader.LoadAction(index, value); + } } } diff --git a/Lib9c.DevExtensions/Action/Stage/ClearStage.cs b/Lib9c.DevExtensions/Action/Stage/ClearStage.cs index 68e2bbdc64..fec03fe307 100644 --- a/Lib9c.DevExtensions/Action/Stage/ClearStage.cs +++ b/Lib9c.DevExtensions/Action/Stage/ClearStage.cs @@ -30,6 +30,7 @@ public override IWorld Execute(IActionContext context) var addressesHex = GetSignerAndOtherAddressesHex(context, AvatarAddress); throw new FailedLoadStateException($"{addressesHex}Aborted as the avatar state of the signer was failed to load."); } + avatarState.worldInformation = new WorldInformation( context.BlockIndex, states.GetSheet(), diff --git a/Lib9c.DevExtensions/Model/BaseTestbedModel.cs b/Lib9c.DevExtensions/Model/BaseTestbedModel.cs index 00d277b3c0..9350a13727 100644 --- a/Lib9c.DevExtensions/Model/BaseTestbedModel.cs +++ b/Lib9c.DevExtensions/Model/BaseTestbedModel.cs @@ -1,6 +1,6 @@ namespace Lib9c.DevExtensions.Model { - public class BaseTestbedModel + public abstract class BaseTestbedModel { } } diff --git a/Lib9c.DevExtensions/Model/TestbedCreateAvatar.cs b/Lib9c.DevExtensions/Model/TestbedCreateAvatar.cs index 82080965ad..4d657889a0 100644 --- a/Lib9c.DevExtensions/Model/TestbedCreateAvatar.cs +++ b/Lib9c.DevExtensions/Model/TestbedCreateAvatar.cs @@ -19,7 +19,7 @@ public class TestbedCreateAvatar : BaseTestbedModel [Serializable] public class CustomEquipmentItem { - public int ID; + public int Id; public int Level; public int[] OptionIds; } diff --git a/Lib9c.DevExtensions/Model/TestbedSell.cs b/Lib9c.DevExtensions/Model/TestbedSell.cs index bfa679ea1f..be28956d5b 100644 --- a/Lib9c.DevExtensions/Model/TestbedSell.cs +++ b/Lib9c.DevExtensions/Model/TestbedSell.cs @@ -22,15 +22,22 @@ protected bool Equals(Avatar other) public override bool Equals(object obj) { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Avatar)obj); + if (ReferenceEquals(null, obj)) + { + return false; + } + + if (ReferenceEquals(this, obj)) + { + return true; + } + + return obj.GetType() == GetType() && Equals((Avatar)obj); } public override int GetHashCode() { - return (Name != null ? Name.GetHashCode() : 0); + return Name != null ? Name.GetHashCode() : 0; } } @@ -38,7 +45,7 @@ public override int GetHashCode() public class Item { public ItemSubType ItemSubType; - public int ID; + public int Id; public int Level; public int Count; public int Price; diff --git a/Lib9c.DevExtensions/TestbedHelper.cs b/Lib9c.DevExtensions/TestbedHelper.cs index 1970d5508a..c09b1095b7 100644 --- a/Lib9c.DevExtensions/TestbedHelper.cs +++ b/Lib9c.DevExtensions/TestbedHelper.cs @@ -74,7 +74,7 @@ public static void AddItem(CostumeItemSheet costumeItemSheet, case ItemSubType.EyeCostume: case ItemSubType.TailCostume: case ItemSubType.Title: - if (costumeItemSheet.TryGetValue(item.ID, out var costumeRow)) + if (costumeItemSheet.TryGetValue(item.Id, out var costumeRow)) { var costume = ItemFactory.CreateCostume(costumeRow, addedItemInfo.TradableId); @@ -88,7 +88,7 @@ public static void AddItem(CostumeItemSheet costumeItemSheet, case ItemSubType.Belt: case ItemSubType.Necklace: case ItemSubType.Ring: - if (equipmentItemSheet.TryGetValue(item.ID, out var equipmentRow)) + if (equipmentItemSheet.TryGetValue(item.Id, out var equipmentRow)) { var equipment = (Equipment)ItemFactory.CreateItemUsable(equipmentRow, addedItemInfo.TradableId, @@ -118,7 +118,7 @@ public static void AddItem(CostumeItemSheet costumeItemSheet, case ItemSubType.Hourglass: case ItemSubType.ApStone: - if (materialItemSheet.TryGetValue(item.ID, out var materialRow)) + if (materialItemSheet.TryGetValue(item.Id, out var materialRow)) { var material = ItemFactory.CreateTradableMaterial(materialRow); avatarState.inventory.AddItem(material, item.Count); @@ -128,7 +128,7 @@ public static void AddItem(CostumeItemSheet costumeItemSheet, break; case ItemSubType.Food: - if (consumableItemSheet.TryGetValue(item.ID, out var consumableRow)) + if (consumableItemSheet.TryGetValue(item.Id, out var consumableRow)) { var consumable = (Consumable)ItemFactory.CreateItemUsable(consumableRow, addedItemInfo.TradableId, @@ -237,8 +237,5 @@ private static T LoadJsonFile(string path) return result; #endif } - - - } } diff --git a/Lib9c.DevExtensions/Utils.cs b/Lib9c.DevExtensions/Utils.cs index ed794d6706..436194a8b4 100644 --- a/Lib9c.DevExtensions/Utils.cs +++ b/Lib9c.DevExtensions/Utils.cs @@ -34,6 +34,7 @@ public static Logger ConfigureLogger(bool verbose) { logConfig = logConfig.WriteTo.Console(); } + return logConfig.CreateLogger(); } @@ -42,29 +43,30 @@ public static ( IStore Store, IKeyValueStore StateKVStore, IStateStore StateStore - ) GetBlockChain( - ILogger logger, - string storePath, - Guid? chainId = null, - IKeyValueStore stateKeyValueStore = null - ) + ) GetBlockChain( + ILogger logger, + string storePath, + Guid? chainId = null, + IKeyValueStore stateKeyValueStore = null + ) { var policySource = new BlockPolicySource(); - IBlockPolicy policy = policySource.GetPolicy(); + var policy = policySource.GetPolicy(); IStagePolicy stagePolicy = new VolatileStagePolicy(); IStore store = new RocksDBStore(storePath); if (stateKeyValueStore is null) { stateKeyValueStore = new RocksDBKeyValueStore(Path.Combine(storePath, "states")); } + IStateStore stateStore = new TrieStateStore(stateKeyValueStore); - Guid chainIdValue + var chainIdValue = chainId ?? - store.GetCanonicalChainId() ?? - throw new CommandExitedException( - "No canonical chain ID. Available chain IDs:\n " + - string.Join("\n ", store.ListChainIds()), - 1); + store.GetCanonicalChainId() ?? + throw new CommandExitedException( + "No canonical chain ID. Available chain IDs:\n " + + string.Join("\n ", store.ListChainIds()), + 1); BlockHash genesisBlockHash; try @@ -75,16 +77,17 @@ Guid chainIdValue { throw new CommandExitedException( $"The chain {chainIdValue} seems empty; try with another chain ID:\n " + - string.Join("\n ", store.ListChainIds()), + string.Join("\n ", store.ListChainIds()), 1 ); } - Block genesis = store.GetBlock( + + var genesis = store.GetBlock( genesisBlockHash ); var blockChainStates = new BlockChainStates(store, stateStore); var actionLoader = new NCDevActionLoader(); - ActionEvaluator actionEvaluator = new ActionEvaluator( + var actionEvaluator = new ActionEvaluator( policy.PolicyActionsRegistry, stateStore, actionLoader); @@ -93,27 +96,28 @@ Guid chainIdValue if (store.GetCanonicalChainId() is null) { chain = BlockChain.Create( - policy: policy, - stagePolicy: stagePolicy, - store: store, - stateStore: stateStore, - genesisBlock: genesis, - actionEvaluator: actionEvaluator + policy, + stagePolicy, + store, + stateStore, + genesis, + actionEvaluator ); } else { chain = new BlockChain( - policy: policy, - stagePolicy: stagePolicy, - store: store, - stateStore: stateStore, - genesisBlock: genesis, + policy, + stagePolicy, + store, + stateStore, + genesis, renderers: null, blockChainStates: blockChainStates, actionEvaluator: actionEvaluator ); } + return (chain, store, stateKeyValueStore, stateStore); } @@ -154,8 +158,8 @@ public static Block ParseBlockOffset( return chain[defaultIndex]; } - if (long.TryParse(blockStr, out long idx) || - blockStr.StartsWith("#") && long.TryParse(blockStr.Substring(1), out idx)) + if (long.TryParse(blockStr, out var idx) || + (blockStr.StartsWith("#") && long.TryParse(blockStr.Substring(1), out idx))) { try { @@ -167,7 +171,7 @@ public static Block ParseBlockOffset( } } - BlockHash blockHash = ParseBlockHash(blockStr); + var blockHash = ParseBlockHash(blockStr); return chain[blockHash]; } @@ -184,11 +188,12 @@ public static Dictionary ImportSheets(string dir = null) $"Lib9c{Path.DirectorySeparatorChar}" + $"TableCSV{Path.DirectorySeparatorChar}"); } + var files = Directory.GetFiles(dir, "*.csv", SearchOption.AllDirectories); var sheets = new Dictionary(); foreach (var filePath in files) { - string fileName = Path.GetFileName(filePath); + var fileName = Path.GetFileName(filePath); if (fileName.EndsWith(".csv")) { fileName = fileName.Replace(".csv", ""); @@ -211,7 +216,7 @@ public static void CreateActivationKey( { var pendingKey = new PrivateKey(); var nonce = pendingKey.PublicKey.Address.ToByteArray(); - (ActivationKey ak, PendingActivationState s) = + (var ak, var s) = ActivationKey.Create(pendingKey, nonce); ps.Add(s); ks.Add(ak); @@ -229,12 +234,12 @@ public static AuthorizedMinersState GetAuthorizedMinersState(string configPath) PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; - string json = File.ReadAllText(configPath); + var json = File.ReadAllText(configPath); var config = JsonSerializer.Deserialize(json, options); return new AuthorizedMinersState( - miners: config.Miners.Select(addr => new Address(addr)), - interval: config.Interval, - validUntil: config.ValidUntil); + config.Miners.Select(addr => new Address(addr)), + config.Interval, + config.ValidUntil); } public static AdminState GetAdminState(string configPath) @@ -245,11 +250,11 @@ public static AdminState GetAdminState(string configPath) PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; - string json = File.ReadAllText(configPath); + var json = File.ReadAllText(configPath); var config = JsonSerializer.Deserialize(json, options); return new AdminState( - adminAddress: new Address(config.AdminAddress), - validUntil: config.ValidUntil); + new Address(config.AdminAddress), + config.ValidUntil); } public static ImmutableHashSet
GetActivatedAccounts(string listPath) @@ -260,8 +265,8 @@ public static ImmutableHashSet
GetActivatedAccounts(string listPath) PropertyNamingPolicy = JsonNamingPolicy.CamelCase, }; - string json = File.ReadAllText(listPath); - ActivatedAccounts activatedAccounts = JsonSerializer.Deserialize(json, options); + var json = File.ReadAllText(listPath); + var activatedAccounts = JsonSerializer.Deserialize(json, options); return activatedAccounts.Accounts .Select(account => new Address(account)) @@ -270,8 +275,8 @@ public static ImmutableHashSet
GetActivatedAccounts(string listPath) public static void ExportBlock(Block block, string path) { - Bencodex.Types.Dictionary dict = block.MarshalBlock(); - byte[] encoded = new Codec().Encode(dict); + var dict = block.MarshalBlock(); + var encoded = new Codec().Encode(dict); File.WriteAllBytes(path, encoded); } From 854e6fe84759999aa30a5323a61c69a6be6c7fbf Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:32:46 +0900 Subject: [PATCH 2/4] fix id naming --- Lib9c/Action/CreateAvatar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib9c/Action/CreateAvatar.cs b/Lib9c/Action/CreateAvatar.cs index 94cd04ecf4..600eca9423 100644 --- a/Lib9c/Action/CreateAvatar.cs +++ b/Lib9c/Action/CreateAvatar.cs @@ -435,7 +435,7 @@ private static void AddTestItems( // Set level of equipment here. level: item.Level, // Set recipeId of target equipment here. - recipeId: item.ID, + recipeId: item.Id, // Add optionIds here. item.OptionIds); } From 4efceda1a5e1660639d185419e6e9927a7d6a452 Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:39:59 +0900 Subject: [PATCH 3/4] add create avatar manager in dev extensions --- .../Action/Coupons/RedeemCouponTest.cs | 3 - .../Manager/Contents/CreateAvatarManager.cs | 337 ++++++++++++++++++ Lib9c/Action/CreateAvatar.cs | 333 +---------------- 3 files changed, 340 insertions(+), 333 deletions(-) create mode 100644 Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs diff --git a/.Lib9c.Tests/Action/Coupons/RedeemCouponTest.cs b/.Lib9c.Tests/Action/Coupons/RedeemCouponTest.cs index 7a8c36de54..8cea66a623 100644 --- a/.Lib9c.Tests/Action/Coupons/RedeemCouponTest.cs +++ b/.Lib9c.Tests/Action/Coupons/RedeemCouponTest.cs @@ -52,7 +52,6 @@ public void Execute() Signer = CouponsFixture.AgentAddress1, BlockIndex = 0, }, - state.GetSheet(), default); var agent1Avatar1State = CreateAvatar.CreateAvatarState( "agent1avatar1", @@ -63,7 +62,6 @@ public void Execute() Signer = CouponsFixture.AgentAddress1, BlockIndex = 0, }, - state.GetSheet(), default); var agent2Avatar0State = CreateAvatar.CreateAvatarState( "agent2avatar0", @@ -74,7 +72,6 @@ public void Execute() Signer = CouponsFixture.AgentAddress2, BlockIndex = 0, }, - state.GetSheet(), default); state = state diff --git a/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs b/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs new file mode 100644 index 0000000000..a6d0d38111 --- /dev/null +++ b/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs @@ -0,0 +1,337 @@ +using System.Collections.Generic; +using System.Linq; +using Libplanet.Action; +using Libplanet.Action.State; +using Libplanet.Crypto; +using Lib9c.DevExtensions.Model; +using Nekoyume; +using Nekoyume.Helper; +using Nekoyume.Model.Item; +using Nekoyume.Model.Stat; +using Nekoyume.Model.State; +using Nekoyume.Model.Skill; +using Nekoyume.Module; +using Nekoyume.TableData; +using Nekoyume.Action; + +namespace Lib9c.DevExtensions.Manager.Contents +{ + public static class CreateAvatarManager + { + public static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddress, IWorld states, AvatarState avatarState) + { + // prepare for test when executing on editor mode. + var data = TestbedHelper.LoadData("TestbedCreateAvatar"); + + states = AddRunesForTest(ctx, avatarAddress, states, data.RuneStoneCount); + states = AddSoulStoneForTest(ctx, avatarAddress, states, data.SoulStoneCount); + if (data.AddPet) + { + states = AddPetsForTest(avatarAddress, states); + } + + var equipmentSheet = states.GetSheet(); + var recipeSheet = states.GetSheet(); + var subRecipeSheet = states.GetSheet(); + var optionSheet = states.GetSheet(); + var skillSheet = states.GetSheet(); + var characterLevelSheet = states.GetSheet(); + var enhancementCostSheet = states.GetSheet(); + var materialItemSheet = states.GetSheet(); + var random = ctx.GetRandom(); + + AddTestItems(ctx, avatarState, random, materialItemSheet); + + avatarState.level = data.Level; + avatarState.exp = characterLevelSheet[data.Level].Exp; + + foreach (var recipeId in data.FullOptionEquipmentRecipeIds) + { + AddFullOptionEquipment( + avatarState, + random, + equipmentSheet, + recipeSheet, + subRecipeSheet, + optionSheet, + skillSheet, + enhancementCostSheet, + recipeId); + } + + return states; + } + + private static void AddFullOptionEquipment( + AvatarState avatarState, + IRandom random, + EquipmentItemSheet equipmentSheet, + EquipmentItemRecipeSheet recipeSheet, + EquipmentItemSubRecipeSheetV2 subRecipeSheet, + EquipmentItemOptionSheet optionSheet, + SkillSheet skillSheet, + EnhancementCostSheetV2 enhancementCostSheet, + int recipeId) + { + var recipeRow = recipeSheet[recipeId]; + var subRecipeId = recipeRow.SubRecipeIds[1]; + var subRecipeRow = subRecipeSheet[subRecipeId]; + var equipmentRow = equipmentSheet[recipeRow.ResultEquipmentId]; + + var equipment = (Equipment)ItemFactory.CreateItemUsable( + equipmentRow, + random.GenerateRandomGuid(), + 0L, + madeWithMimisbrunnrRecipe: subRecipeRow.IsMimisbrunnrSubRecipe ?? false); + + foreach (var option in subRecipeRow.Options) + { + var optionRow = optionSheet[option.Id]; + AddOptionToEquipment(equipment, optionRow, skillSheet); + } + + EnhanceEquipmentToMaxLevel(equipment, enhancementCostSheet, random); + + avatarState.inventory.AddItem(equipment); + } + + private static void AddOptionToEquipment(Equipment equipment, EquipmentItemOptionSheet.Row optionRow, SkillSheet skillSheet) + { + // Add stats. + if (optionRow.StatType != StatType.NONE) + { + var statMap = new DecimalStat(optionRow.StatType, optionRow.StatMax); + equipment.StatsMap.AddStatAdditionalValue(statMap.StatType, statMap.TotalValue); + equipment.optionCountFromCombination++; + } + // Add skills. + else + { + var skillRow = skillSheet.OrderedList.First(r => r.Id == optionRow.SkillId); + var skill = SkillFactory.Get( + skillRow, + optionRow.SkillDamageMax, + optionRow.SkillChanceMax, + optionRow.StatDamageRatioMax, + optionRow.ReferencedStatType); + if (skill != null) + { + equipment.Skills.Add(skill); + equipment.optionCountFromCombination++; + } + } + } + + private static void EnhanceEquipmentToMaxLevel(Equipment equipment, EnhancementCostSheetV2 enhancementCostSheet, IRandom random) + { + for (int i = 1; i <= 20; ++i) + { + var subType = equipment.ItemSubType; + var grade = equipment.Grade; + var costRow = enhancementCostSheet.Values + .First(x => x.ItemSubType == subType && + x.Grade == grade && + x.Level == i); + equipment.LevelUp(random, costRow, true); + } + } + + public static void AddTestItems( + IActionContext ctx, + AvatarState avatarState, + IRandom random, + MaterialItemSheet materialItemSheet) + { + var data = TestbedHelper.LoadData("TestbedCreateAvatar"); + var costumeItemSheet = ctx.PreviousState.GetSheet(); + var equipmentItemSheet = ctx.PreviousState.GetSheet(); + var consumableItemSheet = ctx.PreviousState.GetSheet(); + + AddItemsForTest( + avatarState: avatarState, + random: random, + costumeItemSheet: costumeItemSheet, + materialItemSheet: materialItemSheet, + equipmentItemSheet: equipmentItemSheet, + consumableItemSheet: consumableItemSheet, + data.MaterialCount, + data.TradableMaterialCount, + data.FoodCount); + + var skillSheet = ctx.PreviousState.GetSheet(); + var optionSheet = ctx.PreviousState.GetSheet(); + + var items = data.CustomEquipmentItems; + foreach (var item in items) + { + AddCustomEquipment( + avatarState: avatarState, + random: random, + skillSheet: skillSheet, + equipmentItemSheet: equipmentItemSheet, + equipmentItemOptionSheet: optionSheet, + // Set level of equipment here. + level: item.Level, + // Set recipeId of target equipment here. + recipeId: item.Id, + // Add optionIds here. + item.OptionIds); + } + } + + private static IWorld AddRunesForTest( + IActionContext context, + Address avatarAddress, + IWorld states, + int count = int.MaxValue) + { + var runeSheet = states.GetSheet(); + foreach (var row in runeSheet.Values) + { + var rune = RuneHelper.ToFungibleAssetValue(row, count); + states = states.MintAsset(context, avatarAddress, rune); + } + return states; + } + + private static IWorld AddSoulStoneForTest( + IActionContext context, + Address avatarAddress, + IWorld states, + int count = int.MaxValue) + { + var petSheet = states.GetSheet(); + foreach (var row in petSheet.Values) + { + var soulStone = Currencies.GetSoulStone(row.SoulStoneTicker) * count; + states = states.MintAsset(context, avatarAddress, soulStone); + } + return states; + } + + private static IWorld AddPetsForTest( + Address avatarAddress, + IWorld states) + { + var petSheet = states.GetSheet(); + foreach (var id in petSheet.Keys) + { + var petState = new PetState(id); + petState.LevelUp(); + var petStateAddress = PetState.DeriveAddress(avatarAddress, id); + states = states.SetLegacyState(petStateAddress, petState.Serialize()); + } + + return states; + } + + private static void AddItemsForTest( + AvatarState avatarState, + IRandom random, + CostumeItemSheet costumeItemSheet, + MaterialItemSheet materialItemSheet, + EquipmentItemSheet equipmentItemSheet, + ConsumableItemSheet consumableItemSheet, + int materialCount, + int tradableMaterialCount, + int foodCount) + { + foreach (var row in costumeItemSheet.OrderedList) + { + avatarState.inventory.AddItem2(ItemFactory.CreateCostume(row, random.GenerateRandomGuid())); + } + + foreach (var row in materialItemSheet.OrderedList) + { + avatarState.inventory.AddItem2(ItemFactory.CreateMaterial(row), materialCount); + + if (row.ItemSubType == ItemSubType.Hourglass || + row.ItemSubType == ItemSubType.ApStone) + { + avatarState.inventory.AddItem2(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount); + } + } + + foreach (var row in equipmentItemSheet.OrderedList.Where(row => + row.Id > GameConfig.DefaultAvatarWeaponId)) + { + var itemId = random.GenerateRandomGuid(); + avatarState.inventory.AddItem2(ItemFactory.CreateItemUsable(row, itemId, default)); + } + + foreach (var row in consumableItemSheet.OrderedList) + { + for (var i = 0; i < foodCount; i++) + { + var itemId = random.GenerateRandomGuid(); + var consumable = (Consumable)ItemFactory.CreateItemUsable(row, itemId, + 0, 0); + avatarState.inventory.AddItem2(consumable); + } + } + } + + private static void AddCustomEquipment( + AvatarState avatarState, + IRandom random, + SkillSheet skillSheet, + EquipmentItemSheet equipmentItemSheet, + EquipmentItemOptionSheet equipmentItemOptionSheet, + int level, + int recipeId, + params int[] optionIds + ) + { + if (!equipmentItemSheet.TryGetValue(recipeId, out var equipmentRow)) + { + return; + } + + var itemId = random.GenerateRandomGuid(); + var equipment = (Equipment)ItemFactory.CreateItemUsable(equipmentRow, itemId, 0, level); + var optionRows = new List(); + foreach (var optionId in optionIds) + { + if (!equipmentItemOptionSheet.TryGetValue(optionId, out var optionRow)) + { + continue; + } + optionRows.Add(optionRow); + } + + AddOption(skillSheet, equipment, optionRows, random); + + avatarState.inventory.AddItem2(equipment); + } + + private static HashSet AddOption( + SkillSheet skillSheet, + Equipment equipment, + IEnumerable optionRows, + IRandom random) + { + var optionIds = new HashSet(); + + foreach (var optionRow in optionRows.OrderBy(r => r.Id)) + { + if (optionRow.StatType != StatType.NONE) + { + var stat = CombinationEquipment5.GetStat(optionRow, random); + equipment.StatsMap.AddStatAdditionalValue(stat.StatType, stat.BaseValue); + } + else + { + var skill = CombinationEquipment5.GetSkill(optionRow, skillSheet, random); + if (!(skill is null)) + { + equipment.Skills.Add(skill); + } + } + + optionIds.Add(optionRow.Id); + } + + return optionIds; + } + } +} diff --git a/Lib9c/Action/CreateAvatar.cs b/Lib9c/Action/CreateAvatar.cs index 600eca9423..dcebf3fc55 100644 --- a/Lib9c/Action/CreateAvatar.cs +++ b/Lib9c/Action/CreateAvatar.cs @@ -3,28 +3,21 @@ using System.Collections.Immutable; using System.Diagnostics; using System.Globalization; -using System.Linq; using System.Text.RegularExpressions; using Bencodex.Types; -using Lib9c; using Lib9c.Abstractions; using Libplanet.Action; using Libplanet.Action.State; using Libplanet.Crypto; using Nekoyume.Extensions; -using Nekoyume.Helper; using Nekoyume.Model.Item; -using Nekoyume.Model.Stat; using Nekoyume.Model.State; using Nekoyume.Module; using Nekoyume.TableData; using Serilog; -using static Lib9c.SerializeKeys; #if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR -using Lib9c.DevExtensions; -using Lib9c.DevExtensions.Model; -using Nekoyume.Model.Skill; +using Lib9c.DevExtensions.Manager.Contents; #endif namespace Nekoyume.Action @@ -107,7 +100,7 @@ public override IWorld Execute(IActionContext context) // Avoid NullReferenceException in test var materialItemSheet = ctx.PreviousState.GetSheet(); - var avatarState = CreateAvatarState(name, avatarAddress, ctx, materialItemSheet, default); + var avatarState = CreateAvatarState(name, avatarAddress, ctx, default); CustomizeAvatar(avatarState); @@ -117,7 +110,7 @@ public override IWorld Execute(IActionContext context) avatarState.UpdateQuestRewards(materialItemSheet); #if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR - states = ExecuteDevExtensions(ctx, avatarAddress, states, avatarState); + states = CreateAvatarManager.ExecuteDevExtensions(ctx, avatarAddress, states, avatarState); #endif var sheets = ctx.PreviousState.GetSheets(containItemSheet: true, @@ -261,7 +254,6 @@ public static IWorld MintAsset(CreateAvatarFavSheet favSheet, public static AvatarState CreateAvatarState(string name, Address avatarAddress, IActionContext ctx, - MaterialItemSheet materialItemSheet, Address rankingMapAddress) { var state = ctx.PreviousState; @@ -275,326 +267,7 @@ public static AvatarState CreateAvatarState(string name, name ); -#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR - AddTestItems(ctx, avatarState, random, materialItemSheet); -#endif - return avatarState; } - -#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR - private static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddress, IWorld states, AvatarState avatarState) - { - // prepare for test when executing on editor mode. - var data = TestbedHelper.LoadData("TestbedCreateAvatar"); - - states = AddRunesForTest(ctx, avatarAddress, states, data.RuneStoneCount); - states = AddSoulStoneForTest(ctx, avatarAddress, states, data.SoulStoneCount); - if (data.AddPet) - { - states = AddPetsForTest(avatarAddress, states); - } - - var equipmentSheet = states.GetSheet(); - var recipeSheet = states.GetSheet(); - var subRecipeSheet = states.GetSheet(); - var optionSheet = states.GetSheet(); - var skillSheet = states.GetSheet(); - var characterLevelSheet = states.GetSheet(); - var enhancementCostSheet = states.GetSheet(); - var random = ctx.GetRandom(); - - avatarState.level = data.Level; - avatarState.exp = characterLevelSheet[data.Level].Exp; - - foreach (var recipeId in data.FullOptionEquipmentRecipeIds) - { - AddFullOptionEquipment( - avatarState, - random, - equipmentSheet, - recipeSheet, - subRecipeSheet, - optionSheet, - skillSheet, - enhancementCostSheet, - recipeId); - } - - return states; - } - - private static void AddFullOptionEquipment( - AvatarState avatarState, - IRandom random, - EquipmentItemSheet equipmentSheet, - EquipmentItemRecipeSheet recipeSheet, - EquipmentItemSubRecipeSheetV2 subRecipeSheet, - EquipmentItemOptionSheet optionSheet, - SkillSheet skillSheet, - EnhancementCostSheetV2 enhancementCostSheet, - int recipeId) - { - var recipeRow = recipeSheet[recipeId]; - var subRecipeId = recipeRow.SubRecipeIds[1]; - var subRecipeRow = subRecipeSheet[subRecipeId]; - var equipmentRow = equipmentSheet[recipeRow.ResultEquipmentId]; - - var equipment = (Equipment)ItemFactory.CreateItemUsable( - equipmentRow, - random.GenerateRandomGuid(), - 0L, - madeWithMimisbrunnrRecipe: subRecipeRow.IsMimisbrunnrSubRecipe ?? false); - - foreach (var option in subRecipeRow.Options) - { - var optionRow = optionSheet[option.Id]; - AddOptionToEquipment(equipment, optionRow, skillSheet); - } - - EnhanceEquipmentToMaxLevel(equipment, enhancementCostSheet, random); - - avatarState.inventory.AddItem(equipment); - } - - private static void AddOptionToEquipment(Equipment equipment, EquipmentItemOptionSheet.Row optionRow, SkillSheet skillSheet) - { - // Add stats. - if (optionRow.StatType != StatType.NONE) - { - var statMap = new DecimalStat(optionRow.StatType, optionRow.StatMax); - equipment.StatsMap.AddStatAdditionalValue(statMap.StatType, statMap.TotalValue); - equipment.optionCountFromCombination++; - } - // Add skills. - else - { - var skillRow = skillSheet.OrderedList.First(r => r.Id == optionRow.SkillId); - var skill = SkillFactory.Get( - skillRow, - optionRow.SkillDamageMax, - optionRow.SkillChanceMax, - optionRow.StatDamageRatioMax, - optionRow.ReferencedStatType); - if (skill != null) - { - equipment.Skills.Add(skill); - equipment.optionCountFromCombination++; - } - } - } - - private static void EnhanceEquipmentToMaxLevel(Equipment equipment, EnhancementCostSheetV2 enhancementCostSheet, IRandom random) - { - for (int i = 1; i <= 20; ++i) - { - var subType = equipment.ItemSubType; - var grade = equipment.Grade; - var costRow = enhancementCostSheet.Values - .First(x => x.ItemSubType == subType && - x.Grade == grade && - x.Level == i); - equipment.LevelUp(random, costRow, true); - } - } - - private static void AddTestItems( - IActionContext ctx, - AvatarState avatarState, - IRandom random, - MaterialItemSheet materialItemSheet) - { - var data = TestbedHelper.LoadData("TestbedCreateAvatar"); - var costumeItemSheet = ctx.PreviousState.GetSheet(); - var equipmentItemSheet = ctx.PreviousState.GetSheet(); - var consumableItemSheet = ctx.PreviousState.GetSheet(); - - AddItemsForTest( - avatarState: avatarState, - random: random, - costumeItemSheet: costumeItemSheet, - materialItemSheet: materialItemSheet, - equipmentItemSheet: equipmentItemSheet, - consumableItemSheet: consumableItemSheet, - data.MaterialCount, - data.TradableMaterialCount, - data.FoodCount); - - var skillSheet = ctx.PreviousState.GetSheet(); - var optionSheet = ctx.PreviousState.GetSheet(); - - var items = data.CustomEquipmentItems; - foreach (var item in items) - { - AddCustomEquipment( - avatarState: avatarState, - random: random, - skillSheet: skillSheet, - equipmentItemSheet: equipmentItemSheet, - equipmentItemOptionSheet: optionSheet, - // Set level of equipment here. - level: item.Level, - // Set recipeId of target equipment here. - recipeId: item.Id, - // Add optionIds here. - item.OptionIds); - } - } - - private static IWorld AddRunesForTest( - IActionContext context, - Address avatarAddress, - IWorld states, - int count = int.MaxValue) - { - var runeSheet = states.GetSheet(); - foreach (var row in runeSheet.Values) - { - var rune = RuneHelper.ToFungibleAssetValue(row, count); - states = states.MintAsset(context, avatarAddress, rune); - } - return states; - } - - private static IWorld AddSoulStoneForTest( - IActionContext context, - Address avatarAddress, - IWorld states, - int count = int.MaxValue) - { - var petSheet = states.GetSheet(); - foreach (var row in petSheet.Values) - { - var soulStone = Currencies.GetSoulStone(row.SoulStoneTicker) * count; - states = states.MintAsset(context, avatarAddress, soulStone); - } - return states; - } - - private static IWorld AddPetsForTest( - Address avatarAddress, - IWorld states) - { - var petSheet = states.GetSheet(); - foreach (var id in petSheet.Keys) - { - var petState = new PetState(id); - petState.LevelUp(); - var petStateAddress = PetState.DeriveAddress(avatarAddress, id); - states = states.SetLegacyState(petStateAddress, petState.Serialize()); - } - - return states; - } - - private static void AddItemsForTest( - AvatarState avatarState, - IRandom random, - CostumeItemSheet costumeItemSheet, - MaterialItemSheet materialItemSheet, - EquipmentItemSheet equipmentItemSheet, - ConsumableItemSheet consumableItemSheet, - int materialCount, - int tradableMaterialCount, - int foodCount) - { - foreach (var row in costumeItemSheet.OrderedList) - { - avatarState.inventory.AddItem2(ItemFactory.CreateCostume(row, random.GenerateRandomGuid())); - } - - foreach (var row in materialItemSheet.OrderedList) - { - avatarState.inventory.AddItem2(ItemFactory.CreateMaterial(row), materialCount); - - if (row.ItemSubType == ItemSubType.Hourglass || - row.ItemSubType == ItemSubType.ApStone) - { - avatarState.inventory.AddItem2(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount); - } - } - - foreach (var row in equipmentItemSheet.OrderedList.Where(row => - row.Id > GameConfig.DefaultAvatarWeaponId)) - { - var itemId = random.GenerateRandomGuid(); - avatarState.inventory.AddItem2(ItemFactory.CreateItemUsable(row, itemId, default)); - } - - foreach (var row in consumableItemSheet.OrderedList) - { - for (var i = 0; i < foodCount; i++) - { - var itemId = random.GenerateRandomGuid(); - var consumable = (Consumable)ItemFactory.CreateItemUsable(row, itemId, - 0, 0); - avatarState.inventory.AddItem2(consumable); - } - } - } - - private static void AddCustomEquipment( - AvatarState avatarState, - IRandom random, - SkillSheet skillSheet, - EquipmentItemSheet equipmentItemSheet, - EquipmentItemOptionSheet equipmentItemOptionSheet, - int level, - int recipeId, - params int[] optionIds - ) - { - if (!equipmentItemSheet.TryGetValue(recipeId, out var equipmentRow)) - { - return; - } - - var itemId = random.GenerateRandomGuid(); - var equipment = (Equipment)ItemFactory.CreateItemUsable(equipmentRow, itemId, 0, level); - var optionRows = new List(); - foreach (var optionId in optionIds) - { - if (!equipmentItemOptionSheet.TryGetValue(optionId, out var optionRow)) - { - continue; - } - optionRows.Add(optionRow); - } - - AddOption(skillSheet, equipment, optionRows, random); - - avatarState.inventory.AddItem2(equipment); - } - - private static HashSet AddOption( - SkillSheet skillSheet, - Equipment equipment, - IEnumerable optionRows, - IRandom random) - { - var optionIds = new HashSet(); - - foreach (var optionRow in optionRows.OrderBy(r => r.Id)) - { - if (optionRow.StatType != StatType.NONE) - { - var stat = CombinationEquipment5.GetStat(optionRow, random); - equipment.StatsMap.AddStatAdditionalValue(stat.StatType, stat.BaseValue); - } - else - { - var skill = CombinationEquipment5.GetSkill(optionRow, skillSheet, random); - if (!(skill is null)) - { - equipment.Skills.Add(skill); - } - } - - optionIds.Add(optionRow.Id); - } - - return optionIds; - } -#endif } } From 2854aecc8ff0e59e7bed24b3975152051a71b071 Mon Sep 17 00:00:00 2001 From: eugene-hong <58686228+eugene-doobu@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:41:36 +0900 Subject: [PATCH 4/4] change obsolete additem --- .../Manager/Contents/CreateAvatarManager.cs | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs b/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs index a6d0d38111..c5980296ca 100644 --- a/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs +++ b/Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs @@ -114,11 +114,13 @@ private static void AddOptionToEquipment(Equipment equipment, EquipmentItemOptio optionRow.SkillChanceMax, optionRow.StatDamageRatioMax, optionRow.ReferencedStatType); - if (skill != null) + if (skill == null) { - equipment.Skills.Add(skill); - equipment.optionCountFromCombination++; + return; } + + equipment.Skills.Add(skill); + equipment.optionCountFromCombination++; } } @@ -238,17 +240,17 @@ private static void AddItemsForTest( { foreach (var row in costumeItemSheet.OrderedList) { - avatarState.inventory.AddItem2(ItemFactory.CreateCostume(row, random.GenerateRandomGuid())); + avatarState.inventory.AddItem(ItemFactory.CreateCostume(row, random.GenerateRandomGuid())); } foreach (var row in materialItemSheet.OrderedList) { - avatarState.inventory.AddItem2(ItemFactory.CreateMaterial(row), materialCount); + avatarState.inventory.AddItem(ItemFactory.CreateMaterial(row), materialCount); if (row.ItemSubType == ItemSubType.Hourglass || row.ItemSubType == ItemSubType.ApStone) { - avatarState.inventory.AddItem2(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount); + avatarState.inventory.AddItem(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount); } } @@ -256,7 +258,7 @@ private static void AddItemsForTest( row.Id > GameConfig.DefaultAvatarWeaponId)) { var itemId = random.GenerateRandomGuid(); - avatarState.inventory.AddItem2(ItemFactory.CreateItemUsable(row, itemId, default)); + avatarState.inventory.AddItem(ItemFactory.CreateItemUsable(row, itemId, default)); } foreach (var row in consumableItemSheet.OrderedList) @@ -266,7 +268,7 @@ private static void AddItemsForTest( var itemId = random.GenerateRandomGuid(); var consumable = (Consumable)ItemFactory.CreateItemUsable(row, itemId, 0, 0); - avatarState.inventory.AddItem2(consumable); + avatarState.inventory.AddItem(consumable); } } } @@ -301,7 +303,7 @@ params int[] optionIds AddOption(skillSheet, equipment, optionRows, random); - avatarState.inventory.AddItem2(equipment); + avatarState.inventory.AddItem(equipment); } private static HashSet AddOption(