Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Mar 28, 2024
1 parent e0b0ced commit 2d0b5d7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 13 deletions.
10 changes: 5 additions & 5 deletions .Lib9c.Tests/Action/DailyRewardTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public void Execute(bool legacy)
var nextState = ExecuteInternal(previousStates, 2448);
var nextGameConfigState = nextState.GetGameConfigState();
nextState.TryGetAvatarState(_agentAddress, _avatarAddress, out var nextAvatarState);
Assert.NotNull(nextAvatarState);
Assert.NotNull(nextAvatarState.inventory);
Assert.NotNull(nextAvatarState.questList);
Assert.NotNull(nextAvatarState.worldInformation);
Assert.Equal(nextGameConfigState.ActionPointMax, nextAvatarState.actionPoint);
var dailyRewardInfo = nextState.GetDailyRewardInfo(_avatarAddress);
Assert.NotNull(dailyRewardInfo);
Assert.Equal(2448L, dailyRewardInfo.ReceivedBlockIndex);
var actionPoint = nextState.GetActionPoint(_avatarAddress);
Assert.Equal(nextGameConfigState.ActionPointMax, actionPoint);

var avatarRuneAmount = nextState.GetBalance(_avatarAddress, RuneHelper.DailyRewardRune);
var expectedRune = RuneHelper.DailyRewardRune * nextGameConfigState.DailyRuneRewardAmount;
Expand Down
25 changes: 17 additions & 8 deletions Lib9c/Action/DailyReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,18 @@ public override IWorld Execute(IActionContext context)
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}DailyReward exec started", addressesHex);

if (!states.TryGetAvatarState(context.Signer, avatarAddress, out AvatarState avatarState))
if (!states.TryGetDailyRewardInfo(context.Signer, avatarAddress, out var dailyRewardInfo))
{
throw new FailedLoadStateException(
$"{addressesHex}Aborted as the avatar state of the signer was failed to load.");
var agentState = states.GetAgentState(context.Signer);
if (agentState is not null && agentState.avatarAddresses.ContainsValue(avatarAddress))
{
dailyRewardInfo =
new DailyRewardModule.DailyRewardInfo(context.Signer, 0L);
}
else
{
throw new FailedLoadStateException("failed to load agent state.");
}
}

var gameConfigState = states.GetGameConfigState();
Expand All @@ -48,18 +56,17 @@ public override IWorld Execute(IActionContext context)
throw new FailedLoadStateException($"{addressesHex}Aborted as the game config was failed to load.");
}

if (context.BlockIndex < avatarState.dailyRewardReceivedIndex + gameConfigState.DailyRewardInterval)
if (context.BlockIndex < dailyRewardInfo.ReceivedBlockIndex + gameConfigState.DailyRewardInterval)
{
var sb = new StringBuilder()
.Append($"{addressesHex}Not enough block index to receive daily rewards.")
.Append(
$" Expected: Equals or greater than ({avatarState.dailyRewardReceivedIndex + gameConfigState.DailyRewardInterval}).")
$" Expected: Equals or greater than ({dailyRewardInfo.ReceivedBlockIndex + gameConfigState.DailyRewardInterval}).")
.Append($" Actual: ({context.BlockIndex})");
throw new RequiredBlockIndexException(sb.ToString());
}

avatarState.dailyRewardReceivedIndex = context.BlockIndex;
avatarState.actionPoint = gameConfigState.ActionPointMax;
dailyRewardInfo.ReceivedBlockIndex = context.BlockIndex;

if (gameConfigState.DailyRuneRewardAmount > 0)
{
Expand All @@ -71,7 +78,9 @@ public override IWorld Execute(IActionContext context)

var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}DailyReward Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states.SetAvatarState(avatarAddress, avatarState);
return states
.SetDailyRewardInfo(avatarAddress, dailyRewardInfo)
.SetActionPoint(avatarAddress, gameConfigState.ActionPointMax);
}

protected override IImmutableDictionary<string, IValue> PlainValueInternal => new Dictionary<string, IValue>
Expand Down
2 changes: 2 additions & 0 deletions Lib9c/Addresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static class Addresses
public static readonly Address WorldInformation = new Address("000000000000000000000000000000000000001d");
public static readonly Address QuestList = new Address("000000000000000000000000000000000000001e");
public static readonly Address Collection = new Address("000000000000000000000000000000000000001f");
public static readonly Address DailyReward = new Address("0000000000000000000000000000000000000020");
public static readonly Address ActionPoint = new Address("0000000000000000000000000000000000000021");

public static Address GetSheetAddress<T>() where T : ISheet => GetSheetAddress(typeof(T).Name);

Expand Down
27 changes: 27 additions & 0 deletions Lib9c/Module/ActionPointModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;

namespace Nekoyume.Module
{
public static class ActionPointModule
{
public static long GetActionPoint(this IWorldState worldState, Address address)
{
var value = worldState.GetAccountState(Addresses.ActionPoint).GetState(address);
if (value is Integer integer)
{
return integer;
}

return 0;
}

public static IWorld SetActionPoint(this IWorld world, Address address, long actionPoint)
{
var account = world.GetAccount(Addresses.ActionPoint);
account = account.SetState(address, (Integer)actionPoint);
return world.SetAccount(Addresses.ActionPoint, account);
}
}
}
69 changes: 69 additions & 0 deletions Lib9c/Module/DailyRewardModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System;
using Bencodex;
using Bencodex.Types;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Action;
using Nekoyume.Model.State;

namespace Nekoyume.Module
{
public static class DailyRewardModule
{
public class DailyRewardInfo : IBencodable
{
public Address AgentAddress;
public long ReceivedBlockIndex;

public DailyRewardInfo(Address address, long blockIndex)
{
AgentAddress = address;
ReceivedBlockIndex = blockIndex;
}

public IValue Bencoded => List.Empty.Add(AgentAddress.Serialize()).Add(ReceivedBlockIndex);
}

public static DailyRewardInfo GetDailyRewardInfo(this IWorldState worldState,
Address avatarAddress)
{
IAccountState account = worldState.GetAccountState(Addresses.DailyReward);
var value = account.GetState(avatarAddress);
if (value is List l)
{
return new DailyRewardInfo(l[0].ToAddress(), (Integer)l[1]);
}

throw new FailedLoadStateException("");
}

public static bool TryGetDailyRewardInfo(this IWorldState worldState,
Address agentAddress, Address avatarAddress, out DailyRewardInfo dailyRewardInfo)
{
dailyRewardInfo = null;
try
{
var temp= GetDailyRewardInfo(worldState, avatarAddress);
if (!temp.AgentAddress.Equals(agentAddress))
{
return false;
}

dailyRewardInfo = temp;
return true;
}
catch (FailedLoadStateException)
{
return false;
}
}

public static IWorld SetDailyRewardInfo(this IWorld world, Address avatarAddress,
DailyRewardInfo dailyRewardInfo)
{
IAccount account = world.GetAccount(Addresses.DailyReward);
account = account.SetState(avatarAddress, dailyRewardInfo.Bencoded);
return world.SetAccount(Addresses.DailyReward, account);
}
}
}

0 comments on commit 2d0b5d7

Please sign in to comment.