From 8dda29c82ee66c838db1f246677d79e2d1bfd308 Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Wed, 3 Apr 2024 20:35:27 +0900 Subject: [PATCH 1/4] Refactor collection state modifiers --- Lib9c/Action/BattleArena.cs | 6 +----- Lib9c/Action/EventDungeonBattle.cs | 5 +---- Lib9c/Action/HackAndSlash.cs | 5 +---- Lib9c/Action/HackAndSlashSweep.cs | 5 +---- Lib9c/Action/Raid.cs | 5 +---- Lib9c/Model/State/CollectionState.cs | 13 +++++++++++++ 6 files changed, 18 insertions(+), 21 deletions(-) diff --git a/Lib9c/Action/BattleArena.cs b/Lib9c/Action/BattleArena.cs index b7f958979f..79f00cf2cc 100644 --- a/Lib9c/Action/BattleArena.cs +++ b/Lib9c/Action/BattleArena.cs @@ -397,11 +397,7 @@ public override IWorld Execute(IActionContext context) foreach (var (address, state) in collectionStates) #pragma warning restore LAA1002 { - var modifier = modifiers[address]; - foreach (var collectionId in state.Ids) - { - modifier.AddRange(collectionSheet[collectionId].StatModifiers); - } + modifiers[address] = state.GetModifiers(collectionSheet); } } for (var i = 0; i < ticket; i++) diff --git a/Lib9c/Action/EventDungeonBattle.cs b/Lib9c/Action/EventDungeonBattle.cs index 8e75818e83..f2be6660e6 100644 --- a/Lib9c/Action/EventDungeonBattle.cs +++ b/Lib9c/Action/EventDungeonBattle.cs @@ -341,10 +341,7 @@ is Bencodex.Types.List serializedEventDungeonInfoList if (collectionExist) { var collectionSheet = sheets.GetSheet(); - foreach (var collectionId in collectionState.Ids) - { - collectionModifiers.AddRange(collectionSheet[collectionId].StatModifiers); - } + collectionModifiers = collectionState.GetModifiers(collectionSheet); } var deBuffLimitSheet = sheets.GetSheet(); diff --git a/Lib9c/Action/HackAndSlash.cs b/Lib9c/Action/HackAndSlash.cs index b2545d3a7c..97541a28c6 100644 --- a/Lib9c/Action/HackAndSlash.cs +++ b/Lib9c/Action/HackAndSlash.cs @@ -467,10 +467,7 @@ public IWorld Execute( if (collectionExist) { var collectionSheet = sheets.GetSheet(); - foreach (var collectionId in collectionState.Ids) - { - collectionModifiers.AddRange(collectionSheet[collectionId].StatModifiers); - } + collectionModifiers = collectionState.GetModifiers(collectionSheet); } var deBuffLimitSheet = sheets.GetSheet(); diff --git a/Lib9c/Action/HackAndSlashSweep.cs b/Lib9c/Action/HackAndSlashSweep.cs index 2284f26be8..794861e382 100644 --- a/Lib9c/Action/HackAndSlashSweep.cs +++ b/Lib9c/Action/HackAndSlashSweep.cs @@ -256,10 +256,7 @@ public override IWorld Execute(IActionContext context) if (collectionExist) { var collectionSheet = sheets.GetSheet(); - foreach (var collectionId in collectionState.Ids) - { - collectionModifiers.AddRange(collectionSheet[collectionId].StatModifiers); - } + collectionModifiers = collectionState.GetModifiers(collectionSheet); } var costumeStatSheet = sheets.GetSheet(); diff --git a/Lib9c/Action/Raid.cs b/Lib9c/Action/Raid.cs index 8c742a96bd..a86a6201ad 100644 --- a/Lib9c/Action/Raid.cs +++ b/Lib9c/Action/Raid.cs @@ -217,10 +217,7 @@ public override IWorld Execute(IActionContext context) if (collectionExist) { var collectionSheet = sheets.GetSheet(); - foreach (var collectionId in collectionState.Ids) - { - collectionModifiers.AddRange(collectionSheet[collectionId].StatModifiers); - } + collectionModifiers = collectionState.GetModifiers(collectionSheet); } // Simulate. var random = context.GetRandom(); diff --git a/Lib9c/Model/State/CollectionState.cs b/Lib9c/Model/State/CollectionState.cs index b62af1ab83..d77581e5da 100644 --- a/Lib9c/Model/State/CollectionState.cs +++ b/Lib9c/Model/State/CollectionState.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using Bencodex; using Bencodex.Types; +using Nekoyume.Model.Stat; +using Nekoyume.TableData; namespace Nekoyume.Model.State { @@ -29,5 +31,16 @@ public CollectionState(IValue bencoded) : this((List)bencoded) } public IValue Bencoded => List.Empty.Add(new List(Ids)); + + public List GetModifiers(CollectionSheet collectionSheet) + { + var collectionModifiers = new List(); + foreach (var collectionId in Ids) + { + collectionModifiers.AddRange(collectionSheet[collectionId].StatModifiers); + } + + return collectionModifiers; + } } } From 307023c85aa858b408b3a605d251dce28f686dce Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Wed, 3 Apr 2024 21:11:30 +0900 Subject: [PATCH 2/4] Fix duplicate activated collection ids --- .Lib9c.Tests/Model/State/CollectionStateTest.cs | 4 +++- Lib9c/Model/State/CollectionState.cs | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.Lib9c.Tests/Model/State/CollectionStateTest.cs b/.Lib9c.Tests/Model/State/CollectionStateTest.cs index b02dc0cf4b..bdd8d6d862 100644 --- a/.Lib9c.Tests/Model/State/CollectionStateTest.cs +++ b/.Lib9c.Tests/Model/State/CollectionStateTest.cs @@ -18,6 +18,8 @@ public void Bencoded() { 1, 2, + 1, + 2, }, }; @@ -27,7 +29,7 @@ public void Bencoded() Assert.Equal(expected, serialized); var deserialized = new CollectionState(serialized); - Assert.Equal(state.Ids, deserialized.Ids); + Assert.Equal(new List { 1, 2, }, deserialized.Ids); } } } diff --git a/Lib9c/Model/State/CollectionState.cs b/Lib9c/Model/State/CollectionState.cs index d77581e5da..6966218390 100644 --- a/Lib9c/Model/State/CollectionState.cs +++ b/Lib9c/Model/State/CollectionState.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Bencodex; using Bencodex.Types; using Nekoyume.Model.Stat; @@ -24,13 +25,15 @@ public CollectionState(List serialized) { Ids.Add((Integer)value); } + + Ids = Ids.Distinct().ToList(); } public CollectionState(IValue bencoded) : this((List)bencoded) { } - public IValue Bencoded => List.Empty.Add(new List(Ids)); + public IValue Bencoded => List.Empty.Add(new List(Ids.Distinct())); public List GetModifiers(CollectionSheet collectionSheet) { From 3fc752b0a6bfe9ea3245f032dd8fb58df9a1e3d7 Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Thu, 4 Apr 2024 15:26:43 +0900 Subject: [PATCH 3/4] Use SortedSet instead of list --- .Lib9c.Tests/Model/State/CollectionStateTest.cs | 4 +--- .Lib9c.Tests/Module/CollectionModuleTest.cs | 4 ++-- Lib9c/Model/State/CollectionState.cs | 7 ++----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/.Lib9c.Tests/Model/State/CollectionStateTest.cs b/.Lib9c.Tests/Model/State/CollectionStateTest.cs index bdd8d6d862..d4519be9c6 100644 --- a/.Lib9c.Tests/Model/State/CollectionStateTest.cs +++ b/.Lib9c.Tests/Model/State/CollectionStateTest.cs @@ -2,8 +2,6 @@ namespace Lib9c.Tests.Model.State { using System.Collections.Generic; using Bencodex.Types; - using Libplanet.Crypto; - using Nekoyume.Action; using Nekoyume.Model.State; using Xunit; @@ -14,7 +12,7 @@ public void Bencoded() { var state = new CollectionState { - Ids = new List + Ids = new SortedSet { 1, 2, diff --git a/.Lib9c.Tests/Module/CollectionModuleTest.cs b/.Lib9c.Tests/Module/CollectionModuleTest.cs index a996886239..fe3e7c5cc5 100644 --- a/.Lib9c.Tests/Module/CollectionModuleTest.cs +++ b/.Lib9c.Tests/Module/CollectionModuleTest.cs @@ -22,7 +22,7 @@ public void CollectionState() var state = new CollectionState { - Ids = new List + Ids = new SortedSet { 1, }, @@ -47,7 +47,7 @@ public void CollectionStates() var state = new CollectionState { - Ids = new List + Ids = new SortedSet { 1, }, diff --git a/Lib9c/Model/State/CollectionState.cs b/Lib9c/Model/State/CollectionState.cs index 6966218390..1458e83bc4 100644 --- a/Lib9c/Model/State/CollectionState.cs +++ b/Lib9c/Model/State/CollectionState.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using System.Linq; using Bencodex; using Bencodex.Types; using Nekoyume.Model.Stat; @@ -12,7 +11,7 @@ namespace Nekoyume.Model.State /// public class CollectionState : IBencodable { - public List Ids = new(); + public SortedSet Ids = new(); public CollectionState() { @@ -25,15 +24,13 @@ public CollectionState(List serialized) { Ids.Add((Integer)value); } - - Ids = Ids.Distinct().ToList(); } public CollectionState(IValue bencoded) : this((List)bencoded) { } - public IValue Bencoded => List.Empty.Add(new List(Ids.Distinct())); + public IValue Bencoded => List.Empty.Add(new List(Ids)); public List GetModifiers(CollectionSheet collectionSheet) { From 34ef59f8d11e1d1d490bdd1bd7248c6e4b7e6079 Mon Sep 17 00:00:00 2001 From: Yang Chun Ung Date: Thu, 4 Apr 2024 15:33:56 +0900 Subject: [PATCH 4/4] Fix test --- .Lib9c.Tests/Model/State/CollectionStateTest.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.Lib9c.Tests/Model/State/CollectionStateTest.cs b/.Lib9c.Tests/Model/State/CollectionStateTest.cs index d4519be9c6..53c62cbd9a 100644 --- a/.Lib9c.Tests/Model/State/CollectionStateTest.cs +++ b/.Lib9c.Tests/Model/State/CollectionStateTest.cs @@ -15,19 +15,20 @@ public void Bencoded() Ids = new SortedSet { 1, - 2, + 3, 1, 2, + 2, }, }; var serialized = state.Bencoded; var expected = List.Empty - .Add(List.Empty.Add(1).Add(2)); + .Add(List.Empty.Add(1).Add(2).Add(3)); Assert.Equal(expected, serialized); var deserialized = new CollectionState(serialized); - Assert.Equal(new List { 1, 2, }, deserialized.Ids); + Assert.Equal(new SortedSet { 1, 2, 3, }, deserialized.Ids); } } }