From 41c0d5262bf4857546222706be538ea36b4036a3 Mon Sep 17 00:00:00 2001 From: Hyun Seungmin Date: Wed, 18 Oct 2023 15:27:27 +0900 Subject: [PATCH] refactor AgentNCGModifier temporary --- .../State/Modifiers/AgentGoldModifierTest.cs | 102 ------------------ .../State/Modifiers/AgentNCGModifierTest.cs | 88 +++++++++++++++ ...t.cs.meta => AgentNCGModifierTest.cs.meta} | 0 .../Tests/EditMode/StateModifierTest.cs | 26 ++--- nekoyume/Assets/_Scripts/State/LocalLayer.cs | 35 ++---- .../_Scripts/State/LocalLayerModifier.cs | 2 +- .../State/Modifiers/AgentCrystalModifier.cs | 22 ++-- .../State/Modifiers/AgentGoldModifier.cs | 97 ----------------- .../State/Modifiers/AgentNCGModifier.cs | 48 +++++++++ ...ifier.cs.meta => AgentNCGModifier.cs.meta} | 0 nekoyume/Assets/_Scripts/State/States.cs | 4 +- 11 files changed, 161 insertions(+), 263 deletions(-) delete mode 100644 nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs create mode 100644 nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs rename nekoyume/Assets/Tests/EditMode/State/Modifiers/{AgentGoldModifierTest.cs.meta => AgentNCGModifierTest.cs.meta} (100%) delete mode 100644 nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs create mode 100644 nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs rename nekoyume/Assets/_Scripts/State/Modifiers/{AgentGoldModifier.cs.meta => AgentNCGModifier.cs.meta} (100%) diff --git a/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs b/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs deleted file mode 100644 index 5c87059b788..00000000000 --- a/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs +++ /dev/null @@ -1,102 +0,0 @@ -using Libplanet.Crypto; -using Libplanet.Types.Assets; -using Nekoyume.Model.State; -using Nekoyume.State.Modifiers; -using NUnit.Framework; - -namespace Tests.EditMode.State.Modifiers -{ - public class AgentGoldModifierTest - { - private Currency _currency; - private GoldBalanceState _goldBalanceState; - - [SetUp] - public void SetUp() - { -#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 - _goldBalanceState = new GoldBalanceState( - new PrivateKey().ToAddress(), - new FungibleAssetValue(_currency)); - } - - [TearDown] - public void TearDown() - { - _goldBalanceState = null; - _currency = default; - } - - [Test] - public void AddTest() - { - Assert.True(_goldBalanceState.Gold.Equals(new FungibleAssetValue(_currency))); - var fav = new FungibleAssetValue(_currency, 100, 0); - var modifier = new AgentGoldModifier(fav); - var beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav)); - - var fav2 = new FungibleAssetValue(_currency, -100, 0); - modifier.Add(new AgentGoldModifier(fav2)); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav + fav2)); - - var fav3 = new FungibleAssetValue(_currency, -100, 0); - modifier.Add(new AgentGoldModifier(fav3)); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav + fav2 + fav3)); - } - - [Test] - public void RemoveTest() - { - Assert.True(_goldBalanceState.Gold.Equals(new FungibleAssetValue(_currency))); - var fav = new FungibleAssetValue(_currency, 100, 0); - var modifier = new AgentGoldModifier(fav); - var beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav)); - - var fav2 = new FungibleAssetValue(_currency, -100, 0); - modifier.Remove(new AgentGoldModifier(fav2)); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav - fav2)); - - var fav3 = new FungibleAssetValue(_currency, -100, 0); - modifier.Remove(new AgentGoldModifier(fav3)); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav - fav2 - fav3)); - } - - [Test] - public void ModifyTest() - { - Assert.True(_goldBalanceState.Gold.Equals(new FungibleAssetValue(_currency))); - var fav = new FungibleAssetValue(_currency, 100, 0); - var modifier = new AgentGoldModifier(fav); - var beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav)); - - fav = new FungibleAssetValue(_currency, -100, 0); - modifier = new AgentGoldModifier(fav); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav)); - - fav = new FungibleAssetValue(_currency, -100, 0); - modifier = new AgentGoldModifier(fav); - beforeFav = _goldBalanceState.Gold; - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.True(_goldBalanceState.Gold.Equals(beforeFav + fav)); - } - } -} diff --git a/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs b/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs new file mode 100644 index 00000000000..4547fe16e8f --- /dev/null +++ b/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs @@ -0,0 +1,88 @@ +using Libplanet.Types.Assets; +using Nekoyume.State.Modifiers; +using NUnit.Framework; + +namespace Tests.EditMode.State.Modifiers +{ + public class AgentNCGModifierTest + { + private Currency _currency; + private FungibleAssetValue _ncgFav; + + [SetUp] + public void SetUp() + { + _currency = Currency.Legacy("NCG", 2, null); + _ncgFav = new FungibleAssetValue(_currency); + } + + [Test] + public void AddTest() + { + Assert.True(_ncgFav.Equals(new FungibleAssetValue(_currency))); + var fav = new FungibleAssetValue(_currency, 100, 0); + var modifier = new AgentNCGModifier(fav); + var beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav)); + + var fav2 = new FungibleAssetValue(_currency, -100, 0); + modifier.Add(new AgentNCGModifier(fav2)); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav + fav2)); + + var fav3 = new FungibleAssetValue(_currency, -100, 0); + modifier.Add(new AgentNCGModifier(fav3)); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav + fav2 + fav3)); + } + + [Test] + public void RemoveTest() + { + Assert.True(_ncgFav.Equals(new FungibleAssetValue(_currency))); + var fav = new FungibleAssetValue(_currency, 100, 0); + var modifier = new AgentNCGModifier(fav); + var beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav)); + + var fav2 = new FungibleAssetValue(_currency, -100, 0); + modifier.Remove(new AgentNCGModifier(fav2)); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav - fav2)); + + var fav3 = new FungibleAssetValue(_currency, -100, 0); + modifier.Remove(new AgentNCGModifier(fav3)); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav - fav2 - fav3)); + } + + [Test] + public void ModifyTest() + { + Assert.True(_ncgFav.Equals(new FungibleAssetValue(_currency))); + var fav = new FungibleAssetValue(_currency, 100, 0); + var modifier = new AgentNCGModifier(fav); + var beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav)); + + fav = new FungibleAssetValue(_currency, -100, 0); + modifier = new AgentNCGModifier(fav); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav)); + + fav = new FungibleAssetValue(_currency, -100, 0); + modifier = new AgentNCGModifier(fav); + beforeFav = _ncgFav; + _ncgFav = modifier.Modify(_ncgFav); + Assert.True(_ncgFav.Equals(beforeFav + fav)); + } + } +} diff --git a/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs.meta b/nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs.meta similarity index 100% rename from nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentGoldModifierTest.cs.meta rename to nekoyume/Assets/Tests/EditMode/State/Modifiers/AgentNCGModifierTest.cs.meta diff --git a/nekoyume/Assets/Tests/EditMode/StateModifierTest.cs b/nekoyume/Assets/Tests/EditMode/StateModifierTest.cs index f7068fe5007..00fc70d577f 100644 --- a/nekoyume/Assets/Tests/EditMode/StateModifierTest.cs +++ b/nekoyume/Assets/Tests/EditMode/StateModifierTest.cs @@ -18,7 +18,7 @@ public class StateModifierTest { private TableSheets _tableSheets; private AgentState _agentState; - private GoldBalanceState _goldBalanceState; + private FungibleAssetValue _ncgBalance; private AvatarState _avatarState; [SetUp] @@ -26,12 +26,8 @@ public void SetUp() { _tableSheets = TableSheetsHelper.MakeTableSheets(); _agentState = new AgentState(new Address()); -#pragma warning disable CS0618 - // Use of obsolete method Currency.Legacy(): https://github.com/planetarium/lib9c/discussions/1319 var currency = Currency.Legacy("NCG", 2, null); -#pragma warning restore CS0618 - var gold = new FungibleAssetValue(currency, 0, 0); - _goldBalanceState = new GoldBalanceState(_agentState.address, gold); + _ncgBalance = new FungibleAssetValue(currency, 0, 0); _avatarState = new AvatarState( new Address(), _agentState.address, @@ -41,23 +37,13 @@ public void SetUp() new Address()); } - [TearDown] - public void TearDown() - { - _avatarState = null; - _goldBalanceState = null; - _agentState = null; - _tableSheets = null; - } - [Test] public void AgentGoldModifier() { - var gold = _goldBalanceState.Gold; - var modifier = JsonTest(new AgentGoldModifier(gold.Currency, 100)); - _goldBalanceState = modifier.Modify(_goldBalanceState); - Assert.AreEqual(gold + new FungibleAssetValue(gold.Currency, 100, 0), - _goldBalanceState.Gold); + var gold = _ncgBalance; + var modifier = JsonTest(new AgentNCGModifier(gold.Currency * 100)); + _ncgBalance = modifier.Modify(_ncgBalance); + Assert.AreEqual(gold + new FungibleAssetValue(gold.Currency, 100, 0), _ncgBalance); } [Test] diff --git a/nekoyume/Assets/_Scripts/State/LocalLayer.cs b/nekoyume/Assets/_Scripts/State/LocalLayer.cs index 22823ec62df..88ec320307f 100644 --- a/nekoyume/Assets/_Scripts/State/LocalLayer.cs +++ b/nekoyume/Assets/_Scripts/State/LocalLayer.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Lib9c; using Libplanet.Crypto; using Libplanet.Types.Assets; using Nekoyume.Helper; @@ -37,7 +38,7 @@ public ModifierInfo(Address address) private ModifierInfo _agentModifierInfo; - private ModifierInfo _agentGoldModifierInfo; + private ModifierInfo _agentNCGModifierInfo; private ModifierInfo _agentCrystalModifierInfo; @@ -75,7 +76,7 @@ public void InitializeAgentAndAvatars(AgentState agentState) // _agentModifierInfo 초기화하기. _agentModifierInfo = new ModifierInfo(address); - _agentGoldModifierInfo = new ModifierInfo(address); + _agentNCGModifierInfo = new ModifierInfo(address); _agentCrystalModifierInfo = new ModifierInfo(address); } @@ -209,7 +210,7 @@ public void Add(Address agentAddress, AgentStateModifier modifier) } } - public void Add(Address agentAddress, AgentGoldModifier modifier) + public void Add(Address agentAddress, AgentNCGModifier modifier) { // FIXME: 다른 Add() 오버로드와 겹치는 로직이 아주 많음. if (modifier is null || modifier.IsEmpty) @@ -217,9 +218,9 @@ public void Add(Address agentAddress, AgentGoldModifier modifier) return; } - if (agentAddress.Equals(_agentGoldModifierInfo.Address)) + if (agentAddress.Equals(_agentNCGModifierInfo.Address)) { - var modifiers = _agentGoldModifierInfo.Modifiers; + var modifiers = _agentNCGModifierInfo.Modifiers; if (TryGetSameTypeModifier(modifier, modifiers, out var outModifier)) { outModifier.Add(modifier); @@ -476,30 +477,14 @@ public AgentState Modify(AgentState state) return PostModify(state, _agentModifierInfo); } - public FungibleAssetValue ModifyCrystal(FungibleAssetValue value) + public FungibleAssetValue ModifyNCG(FungibleAssetValue ncg) { - if (value.Equals(default) || - !value.Currency.Equals(CrystalCalculator.CRYSTAL) || - value.Sign == 0) - { - return value; - } - - return PostModifyValue(value, _agentCrystalModifierInfo); + return PostModifyValue(ncg, _agentNCGModifierInfo); } - /// - /// 인자로 받은 잔고 상태에 로컬 세팅을 반영한다. - /// - public GoldBalanceState Modify(GoldBalanceState state) + public FungibleAssetValue ModifyCrystal(FungibleAssetValue crystal) { - if (state is null || - !state.address.Equals(_agentGoldModifierInfo.Address)) - { - return state; - } - - return PostModify(state, _agentGoldModifierInfo); + return PostModifyValue(crystal, _agentCrystalModifierInfo); } /// diff --git a/nekoyume/Assets/_Scripts/State/LocalLayerModifier.cs b/nekoyume/Assets/_Scripts/State/LocalLayerModifier.cs index 4ffcac8bd6b..e7d24c06d25 100644 --- a/nekoyume/Assets/_Scripts/State/LocalLayerModifier.cs +++ b/nekoyume/Assets/_Scripts/State/LocalLayerModifier.cs @@ -31,7 +31,7 @@ public static async UniTask ModifyAgentGoldAsync(Address agentAddress, FungibleA return; } - var modifier = new AgentGoldModifier(gold); + var modifier = new AgentNCGModifier(gold); LocalLayer.Instance.Add(agentAddress, modifier); //FIXME Avoid LocalLayer duplicate modify gold. diff --git a/nekoyume/Assets/_Scripts/State/Modifiers/AgentCrystalModifier.cs b/nekoyume/Assets/_Scripts/State/Modifiers/AgentCrystalModifier.cs index a6c0a0215e6..1a16b117cfb 100644 --- a/nekoyume/Assets/_Scripts/State/Modifiers/AgentCrystalModifier.cs +++ b/nekoyume/Assets/_Scripts/State/Modifiers/AgentCrystalModifier.cs @@ -1,6 +1,6 @@ using System; +using Lib9c; using Libplanet.Types.Assets; -using Nekoyume.Helper; using UnityEngine; namespace Nekoyume.State.Modifiers @@ -15,9 +15,9 @@ public class AgentCrystalModifier : IAccumulatableValueModifier modifier) { - if (!(modifier is AgentCrystalModifier m) || - !crystal.Currency.Equals(CrystalCalculator.CRYSTAL)) + if (modifier is not AgentCrystalModifier m) { return; } @@ -37,8 +36,7 @@ public void Add(IAccumulatableValueModifier modifier) public void Remove(IAccumulatableValueModifier modifier) { - if (!(modifier is AgentCrystalModifier m) || - !crystal.Currency.Equals(CrystalCalculator.CRYSTAL)) + if (modifier is not AgentCrystalModifier m) { return; } @@ -48,18 +46,10 @@ public void Remove(IAccumulatableValueModifier modifier) public FungibleAssetValue Modify(FungibleAssetValue value) { - //if (!crystal.Currency.Equals(CrystalCalculator.CRYSTAL)) - //{ - // return value; - //} - //return value + crystal; return value; } - public override string ToString() - { - return $"{nameof(crystal)}: {crystal.MajorUnit}"; - } + public override string ToString() => crystal.ToString(); } } diff --git a/nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs b/nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs deleted file mode 100644 index e12858c2b3f..00000000000 --- a/nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Linq; -using Bencodex; -using Bencodex.Types; -using Libplanet.Common; -using Libplanet.Types.Assets; -using Nekoyume.Model.State; -using UnityEngine; - -namespace Nekoyume.State.Modifiers -{ - [Serializable] - public class AgentGoldModifier : IAccumulatableStateModifier - { - [SerializeField] - private string hex; - - [NonSerialized] - private FungibleAssetValue? _goldCache; - - public bool dirty { get; set; } - - public bool IsEmpty => Gold.Sign == 0; - - private FungibleAssetValue Gold - { - get - { - if (_goldCache.HasValue) - { - return _goldCache.Value; - } - - var serialized = (Bencodex.Types.List) new Codec().Decode(ByteUtil.ParseHex(hex)); - _goldCache = FungibleAssetValue.FromRawValue( - CurrencyExtensions.Deserialize( - (Bencodex.Types.Dictionary) serialized.ElementAt(0)), - serialized.ElementAt(1).ToBigInteger()); - - return _goldCache.Value; - } - set - { - var serialized = new Bencodex.Types.List(new IValue[] - { - CurrencyExtensions.Serialize(value.Currency), - (Integer) value.RawValue, - }); - - hex = ByteUtil.Hex(new Codec().Encode(serialized)); - _goldCache = null; - } - } - - public AgentGoldModifier(FungibleAssetValue gold) - { - Gold = gold; - } - - public AgentGoldModifier(Currency currency, int gold) : this( - new FungibleAssetValue(currency, gold, 0)) - { - } - - public void Add(IAccumulatableStateModifier modifier) - { - if (!(modifier is AgentGoldModifier m)) - { - return; - } - - Gold += m.Gold; - } - - public void Remove(IAccumulatableStateModifier modifier) - { - if (!(modifier is AgentGoldModifier m)) - { - return; - } - - Gold -= m.Gold; - } - - public GoldBalanceState Modify(GoldBalanceState state) - { - return state; - - // return state?.Add(Gold); - } - - public override string ToString() - { - return $"{nameof(Gold)}: {Gold}"; - } - } -} diff --git a/nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs b/nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs new file mode 100644 index 00000000000..ecab94c3a7f --- /dev/null +++ b/nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs @@ -0,0 +1,48 @@ +using System; +using Libplanet.Types.Assets; +using UnityEngine; + +namespace Nekoyume.State.Modifiers +{ + [Serializable] + public class AgentNCGModifier : IAccumulatableValueModifier + { + [SerializeField] + private FungibleAssetValue ncg; + + public bool IsEmpty => ncg.Sign == 0; + + public AgentNCGModifier(FungibleAssetValue ncg) + { + this.ncg = ncg; + } + + public void Add(IAccumulatableValueModifier modifier) + { + if (modifier is not AgentNCGModifier m) + { + return; + } + + ncg += m.ncg; + } + + public void Remove(IAccumulatableValueModifier modifier) + { + if (modifier is not AgentNCGModifier m) + { + return; + } + + ncg -= m.ncg; + } + + public FungibleAssetValue Modify(FungibleAssetValue value) + { + //return value + ncg; + return value; + } + + public override string ToString() => ncg.ToString(); + } +} diff --git a/nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs.meta b/nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs.meta similarity index 100% rename from nekoyume/Assets/_Scripts/State/Modifiers/AgentGoldModifier.cs.meta rename to nekoyume/Assets/_Scripts/State/Modifiers/AgentNCGModifier.cs.meta diff --git a/nekoyume/Assets/_Scripts/State/States.cs b/nekoyume/Assets/_Scripts/State/States.cs index f1010ac85f7..490fc7d765b 100644 --- a/nekoyume/Assets/_Scripts/State/States.cs +++ b/nekoyume/Assets/_Scripts/State/States.cs @@ -154,8 +154,8 @@ public void SetGoldBalanceState(GoldBalanceState goldBalanceState) return; } - GoldBalanceState = LocalLayer.Instance.Modify(goldBalanceState); - AgentStateSubject.OnNextGold(GoldBalanceState.Gold); + var ncg = LocalLayer.Instance.ModifyNCG(goldBalanceState.Gold); + AgentStateSubject.OnNextGold(ncg); } public void SetCrystalBalance(FungibleAssetValue fav)