Skip to content

Commit

Permalink
Operator can put bounty for multiple season in a row
Browse files Browse the repository at this point in the history
  • Loading branch information
U-lis committed Jun 20, 2024
1 parent 6eed407 commit ae7e350
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 42 deletions.
101 changes: 71 additions & 30 deletions .Lib9c.Tests/Action/AdventureBoss/WantedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ namespace Lib9c.Tests.Action.AdventureBoss
using Nekoyume.Action;
using Nekoyume.Action.AdventureBoss;
using Nekoyume.Action.Exceptions.AdventureBoss;
using Nekoyume.Exceptions;
using Nekoyume.Helper;
using Nekoyume.Model.AdventureBoss;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Nekoyume.TableData.Stake;
using Xunit;

public class WantedTest
Expand Down Expand Up @@ -442,42 +444,70 @@ public void InvalidBounty()
}

[Fact]
public void CannotPutBounty()
public void WantedForMultipleSeasons()
{
var state = Stake(_initialState);
var gameConfig = state.GetGameConfigState();
var prevSeason = new SeasonInfo(
1,
0L,
gameConfig.AdventureBossActiveInterval,
gameConfig.AdventureBossInactiveInterval
);
var prevBountyBoard = new BountyBoard(1);
prevBountyBoard.AddOrUpdate(
AvatarAddress,
AvatarState.name,
gameConfig.AdventureBossMinBounty * NCG
);
state = state.SetSeasonInfo(prevSeason).SetBountyBoard(1, prevBountyBoard);
state = state.SetLatestAdventureBossSeason(prevSeason);

var action = new Wanted
state = new Wanted
{
Season = 1,
AvatarAddress = AvatarAddress,
Bounty = gameConfig.AdventureBossMinBounty * NCG,
}.Execute(new ActionContext
{
PreviousState = state,
Signer = AgentAddress,
BlockIndex = 0L,
});

var prevSeason = state.GetSeasonInfo(1);
state = new Wanted
{
Season = 2,
AvatarAddress = AvatarAddress,
Bounty = gameConfig.AdventureBossMinBounty * NCG,
};
Assert.Throws<PreviousBountyException>(() => action.Execute(
new ActionContext
{
PreviousState = state,
Signer = AgentAddress,
BlockIndex = prevSeason.NextStartBlockIndex,
}
));
}.Execute(new ActionContext
{
PreviousState = state,
Signer = AgentAddress,
BlockIndex = prevSeason.NextStartBlockIndex,
});

var requiredStakingLevel =
state.GetGameConfigState().AdventureBossWantedRequiredStakingLevel;
var currentStakeRegularRewardSheetAddr = Addresses.GetSheetAddress(
state.GetSheet<StakePolicySheet>().StakeRegularRewardSheetValue);
if (!state.TryGetSheet<StakeRegularRewardSheet>(
currentStakeRegularRewardSheetAddr,
out var stakeRegularRewardSheet))
{
throw new StateNullException(
ReservedAddresses.LegacyAccount,
currentStakeRegularRewardSheetAddr
);
}

var stakeAmount = stakeRegularRewardSheet[requiredStakingLevel].RequiredGold;

for (var szn = 1; szn <= 2; szn++)
{
var bountyBoard = state.GetBountyBoard(szn);

Assert.Equal(gameConfig.AdventureBossMinBounty * NCG, bountyBoard.totalBounty());
Assert.Contains(
AvatarAddress,
bountyBoard.Investors.Select(inv => inv.AvatarAddress)
);
}

Assert.Equal(
(InitialBalance - stakeAmount - 2 * gameConfig.AdventureBossMinBounty) * NCG,
state.GetBalance(AgentAddress, NCG)
);
}

private IWorld Stake(IWorld world, int amount = 0)
private IWorld Stake(IWorld world, long amount = 0)
{
foreach (var (key, value) in Sheets)
{
Expand All @@ -486,10 +516,21 @@ private IWorld Stake(IWorld world, int amount = 0)

if (amount == 0)
{
var stakeSheet = world.GetSheet<MonsterCollectionSheet>();
amount = stakeSheet.OrderedList.First(row =>
row.Level == world.GetGameConfigState().AdventureBossWantedRequiredStakingLevel
).RequiredGold;
var requiredStakingLevel =
world.GetGameConfigState().AdventureBossWantedRequiredStakingLevel;
var currentStakeRegularRewardSheetAddr = Addresses.GetSheetAddress(
world.GetSheet<StakePolicySheet>().StakeRegularRewardSheetValue);
if (!world.TryGetSheet<StakeRegularRewardSheet>(
currentStakeRegularRewardSheetAddr,
out var stakeRegularRewardSheet))
{
throw new StateNullException(
ReservedAddresses.LegacyAccount,
currentStakeRegularRewardSheetAddr
);
}

amount = stakeRegularRewardSheet[requiredStakingLevel].RequiredGold;
}

var action = new Stake(new BigInteger(amount));
Expand Down
12 changes: 0 additions & 12 deletions Lib9c/Action/AdventureBoss/Wanted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,6 @@ public override IWorld Execute(IActionContext context)
);
}

// Cannot put bounty in two seasons in a row
if (Season > 1)
{
var prevBountyBoard = states.GetBountyBoard(Season - 1);
if (prevBountyBoard.Investors.Select(i => i.AvatarAddress).Contains(AvatarAddress))
{
throw new PreviousBountyException(
"You've put bounty in previous season. Cannot put bounty two seasons in a row"
);
}
}

if (!Addresses.CheckAvatarAddrIsContainedInAgent(context.Signer, AvatarAddress))
{
throw new InvalidAddressException();
Expand Down

0 comments on commit ae7e350

Please sign in to comment.