Skip to content

Commit

Permalink
Transfer action cost if guild joined only
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Oct 18, 2024
1 parent 71d5978 commit 4a0c013
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
64 changes: 64 additions & 0 deletions .Lib9c.Tests/Action/ItemEnhancementTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ namespace Lib9c.Tests.Action
using Libplanet.Types.Assets;
using Nekoyume;
using Nekoyume.Action;
using Nekoyume.Action.Guild;
using Nekoyume.Arena;
using Nekoyume.Extensions;
using Nekoyume.Model.Guild;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Nekoyume.TableData;
using Nekoyume.TypedAddress;
using Xunit;

public class ItemEnhancementTest
Expand Down Expand Up @@ -553,5 +557,65 @@ public void Execute_With_Hammer(bool oldStart)
Assert.Equal(preItemUsable.ItemId, resultEquipment.ItemId);
Assert.Equal(expectedCost, slotResult.gold);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void Execute_With_Guild(bool guild)
{
var state = _initialState;
var guildMasterAddress = new AgentAddress(_agentAddress);
var guildAddress = new GuildAddress(new PrivateKey().Address);

var enhancementCostSheet = new EnhancementCostSheetV3();
enhancementCostSheet.Set(EnhancementCostSheetFixtures.V4);
var costRow = enhancementCostSheet.Values.First(r => r.Cost > 0);
var row = _tableSheets.EquipmentItemSheet.Values.First(r => r.ItemSubType == costRow.ItemSubType && r.Grade == costRow.Grade);
var equipment = (Equipment)ItemFactory.CreateItemUsable(row, default, 0, costRow.Level);
var materialIds = new List<Guid>();
for (int i = 0; i < 5; i++)
{
var material = (Equipment)ItemFactory.CreateItemUsable(row, Guid.NewGuid(), 0, costRow.Level);
material.Exp = costRow.Exp;
materialIds.Add(material.ItemId);
_avatarState.inventory.AddItem(material);
}

_avatarState.inventory.AddItem(equipment);
state = state.SetInventory(_avatarAddress, _avatarState.inventory);
var prevBalance = _initialState.GetBalance(_agentAddress, _currency);
var repository = new GuildRepository(state, new ActionContext());

if (guild)
{
repository.MakeGuild(guildAddress, guildMasterAddress);
repository.JoinGuild(guildAddress, guildMasterAddress);
var joinedGuildAddress = repository.GetJoinedGuild(guildMasterAddress);
Assert.NotNull(joinedGuildAddress);
}

var action = new ItemEnhancement
{
itemId = default,
materialIds = materialIds,
avatarAddress = _avatarAddress,
slotIndex = 0,
};

var nextState = action.Execute(new ActionContext
{
PreviousState = repository.World,
Signer = _agentAddress,
});

if (guild)
{
Assert.True(prevBalance > nextState.GetBalance(_agentAddress, _currency));
}
else
{
Assert.Equal(prevBalance, nextState.GetBalance(_agentAddress, _currency));
}
}
}
}
13 changes: 7 additions & 6 deletions Lib9c/Action/ItemEnhancement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
using Nekoyume.Arena;
using Nekoyume.Extensions;
using Nekoyume.Helper;
using Nekoyume.Model.Guild;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.Module.Guild;
using Nekoyume.TableData;
using Nekoyume.TableData.Crystal;
using Nekoyume.TypedAddress;
using Serilog;
using static Lib9c.SerializeKeys;

Expand Down Expand Up @@ -377,13 +380,11 @@ public override IWorld Execute(IActionContext context)
// TransferAsset (NCG)
// Total cost = Total cost to reach target level - total cost to reach start level (already used)
var requiredNcg = targetCostRow.Cost - startCostRow.Cost;
if (requiredNcg > 0)
var repository = new GuildRepository(states, context);
var guildAddress = repository.GetJoinedGuild(ctx.GetAgentAddress());
if (requiredNcg > 0 && guildAddress is not null)
{
var arenaSheet = states.GetSheet<ArenaSheet>();
var arenaData = arenaSheet.GetRoundByBlockIndex(context.BlockIndex);
var feeStoreAddress =
ArenaHelper.DeriveArenaAddress(arenaData.ChampionshipId, arenaData.Round);
states = states.TransferAsset(ctx, ctx.Signer, feeStoreAddress,
states = states.TransferAsset(ctx, ctx.Signer, guildAddress!.Value,
states.GetGoldCurrency() * requiredNcg);
}

Expand Down

0 comments on commit 4a0c013

Please sign in to comment.