Skip to content

Commit

Permalink
refactor AgentNCGModifier temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed Oct 18, 2023
1 parent 0644b01 commit 41c0d52
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 263 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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));
}
}
}
26 changes: 6 additions & 20 deletions nekoyume/Assets/Tests/EditMode/StateModifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ public class StateModifierTest
{
private TableSheets _tableSheets;
private AgentState _agentState;
private GoldBalanceState _goldBalanceState;
private FungibleAssetValue _ncgBalance;
private AvatarState _avatarState;

[SetUp]
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,
Expand All @@ -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]
Expand Down
35 changes: 10 additions & 25 deletions nekoyume/Assets/_Scripts/State/LocalLayer.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -37,7 +38,7 @@ public ModifierInfo(Address address)

private ModifierInfo<AgentStateModifier> _agentModifierInfo;

private ModifierInfo<AgentGoldModifier> _agentGoldModifierInfo;
private ModifierInfo<AgentNCGModifier> _agentNCGModifierInfo;

private ModifierInfo<AgentCrystalModifier> _agentCrystalModifierInfo;

Expand Down Expand Up @@ -75,7 +76,7 @@ public void InitializeAgentAndAvatars(AgentState agentState)
// _agentModifierInfo 초기화하기.
_agentModifierInfo =
new ModifierInfo<AgentStateModifier>(address);
_agentGoldModifierInfo = new ModifierInfo<AgentGoldModifier>(address);
_agentNCGModifierInfo = new ModifierInfo<AgentNCGModifier>(address);
_agentCrystalModifierInfo = new ModifierInfo<AgentCrystalModifier>(address);
}

Expand Down Expand Up @@ -209,17 +210,17 @@ 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)
{
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);
Expand Down Expand Up @@ -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);
}

/// <summary>
/// 인자로 받은 잔고 상태에 로컬 세팅을 반영한다.
/// </summary>
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);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion nekoyume/Assets/_Scripts/State/LocalLayerModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 6 additions & 16 deletions nekoyume/Assets/_Scripts/State/Modifiers/AgentCrystalModifier.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Lib9c;
using Libplanet.Types.Assets;
using Nekoyume.Helper;
using UnityEngine;

namespace Nekoyume.State.Modifiers
Expand All @@ -15,9 +15,9 @@ public class AgentCrystalModifier : IAccumulatableValueModifier<FungibleAssetVal

public AgentCrystalModifier(FungibleAssetValue crystal)
{
if (!crystal.Currency.Equals(CrystalCalculator.CRYSTAL))
if (!crystal.Currency.Equals(Currencies.Crystal))
{
this.crystal = CrystalCalculator.CRYSTAL * 0;
this.crystal = Currencies.Crystal * 0;
return;
}

Expand All @@ -26,8 +26,7 @@ public AgentCrystalModifier(FungibleAssetValue crystal)

public void Add(IAccumulatableValueModifier<FungibleAssetValue> modifier)
{
if (!(modifier is AgentCrystalModifier m) ||
!crystal.Currency.Equals(CrystalCalculator.CRYSTAL))
if (modifier is not AgentCrystalModifier m)
{
return;
}
Expand All @@ -37,8 +36,7 @@ public void Add(IAccumulatableValueModifier<FungibleAssetValue> modifier)

public void Remove(IAccumulatableValueModifier<FungibleAssetValue> modifier)
{
if (!(modifier is AgentCrystalModifier m) ||
!crystal.Currency.Equals(CrystalCalculator.CRYSTAL))
if (modifier is not AgentCrystalModifier m)
{
return;
}
Expand All @@ -48,18 +46,10 @@ public void Remove(IAccumulatableValueModifier<FungibleAssetValue> 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();
}
}
Loading

0 comments on commit 41c0d52

Please sign in to comment.