-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathChargeActionPoint.cs
78 lines (69 loc) · 3 KB
/
ChargeActionPoint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Bencodex.Types;
using Lib9c.Abstractions;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Model.Item;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Serilog;
using static Lib9c.SerializeKeys;
namespace Nekoyume.Action
{
/// <summary>
/// Hard forked at https://github.com/planetarium/lib9c/pull/430
/// Updated at https://github.com/planetarium/lib9c/pull/474
/// Updated at https://github.com/planetarium/lib9c/pull/602
/// Updated at https://github.com/planetarium/lib9c/pull/861
/// Updated at https://github.com/planetarium/lib9c/pull/957
/// </summary>
[Serializable]
[ActionType("charge_action_point3")]
public class ChargeActionPoint : GameAction, IChargeActionPointV1
{
public Address avatarAddress;
Address IChargeActionPointV1.AvatarAddress => avatarAddress;
public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
var states = context.PreviousState;
var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress);
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}ChargeActionPoint exec started", addressesHex);
if (!states.TryGetAvatarState(context.Signer, avatarAddress, out var avatarState))
{
throw new FailedLoadStateException(
$"{addressesHex}Aborted as the avatar state of the signer was failed to load.");
}
var row = states.GetSheet<MaterialItemSheet>().Values.First(r => r.ItemSubType == ItemSubType.ApStone);
if (!avatarState.inventory.RemoveFungibleItem(row.ItemId, context.BlockIndex))
{
throw new NotEnoughMaterialException(
$"{addressesHex}Aborted as the player has no enough material ({row.Id})");
}
states.TryGetActionPoint(avatarAddress, out var actionPoint);
if (actionPoint == DailyReward.ActionPointMax)
{
throw new ActionPointExceededException();
}
var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}ChargeActionPoint Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states.SetAvatarState(avatarAddress, avatarState, false, true, false, false)
.SetActionPoint(avatarAddress, DailyReward.ActionPointMax);
}
protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>
{
["avatarAddress"] = avatarAddress.Serialize(),
}.ToImmutableDictionary();
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
avatarAddress = plainValue["avatarAddress"].ToAddress();
}
}
}