-
Notifications
You must be signed in to change notification settings - Fork 46
/
CombinationConsumable.cs
223 lines (195 loc) · 8.53 KB
/
CombinationConsumable.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#nullable enable
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.Mail;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Serilog;
namespace Nekoyume.Action
{
/// <summary>
/// Hard forked at https://github.com/planetarium/lib9c/pull/2195
/// </summary>
[Serializable]
[ActionType("combination_consumable9")]
public class CombinationConsumable : GameAction, ICombinationConsumableV1
{
public const string AvatarAddressKey = "a";
public Address avatarAddress;
public const string SlotIndexKey = "s";
public int slotIndex;
public const string RecipeIdKey = "r";
public int recipeId;
Address ICombinationConsumableV1.AvatarAddress => avatarAddress;
int ICombinationConsumableV1.RecipeId => recipeId;
int ICombinationConsumableV1.SlotIndex => slotIndex;
protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>
{
[AvatarAddressKey] = avatarAddress.Serialize(),
[SlotIndexKey] = slotIndex.Serialize(),
[RecipeIdKey] = recipeId.Serialize(),
}.ToImmutableDictionary();
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
avatarAddress = plainValue[AvatarAddressKey].ToAddress();
slotIndex = plainValue[SlotIndexKey].ToInteger();
recipeId = plainValue[RecipeIdKey].ToInteger();
}
public override IWorld Execute(IActionContext context)
{
context.UseGas(1);
var states = context.PreviousState;
var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress);
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}Combination 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 allSlotState = states.GetAllCombinationSlotState(avatarAddress);
if (allSlotState is null)
{
throw new FailedLoadStateException($"Aborted as the allSlotState was failed to load.");
}
// Validate SlotIndex
var slotState = allSlotState.GetSlot(slotIndex);
if (!slotState.ValidateV2(context.BlockIndex))
{
throw new CombinationSlotUnlockException(
$"{addressesHex}Aborted as the slot state is invalid: {slotState} @ {slotIndex}");
}
// ~Validate SlotIndex
// Validate Work
var costActionPoint = 0;
var endBlockIndex = context.BlockIndex;
var requiredFungibleItems = new Dictionary<int, int>();
// Validate RecipeId
var consumableItemRecipeSheet = states.GetSheet<ConsumableItemRecipeSheet>();
if (!consumableItemRecipeSheet.TryGetValue(recipeId, out var recipeRow))
{
throw new SheetRowNotFoundException(
addressesHex,
nameof(ConsumableItemRecipeSheet),
recipeId);
}
// ~Validate RecipeId
// Validate Recipe ResultEquipmentId
var consumableItemSheet = states.GetSheet<ConsumableItemSheet>();
if (!consumableItemSheet.TryGetValue(recipeRow.ResultConsumableItemId, out var consumableRow))
{
throw new SheetRowNotFoundException(
addressesHex,
nameof(consumableItemSheet),
recipeRow.ResultConsumableItemId);
}
// ~Validate Recipe ResultEquipmentId
// Validate Recipe Material
var materialItemSheet = states.GetSheet<MaterialItemSheet>();
for (var i = recipeRow.Materials.Count; i > 0; i--)
{
var materialInfo = recipeRow.Materials[i - 1];
if (!materialItemSheet.TryGetValue(materialInfo.Id, out var materialRow))
{
throw new SheetRowNotFoundException(
addressesHex,
nameof(MaterialItemSheet),
materialInfo.Id);
}
if (requiredFungibleItems.ContainsKey(materialRow.Id))
{
requiredFungibleItems[materialRow.Id] += materialInfo.Count;
}
else
{
requiredFungibleItems[materialRow.Id] = materialInfo.Count;
}
}
// ~Validate Recipe Material
costActionPoint += recipeRow.RequiredActionPoint;
endBlockIndex += recipeRow.RequiredBlockIndex;
// ~Validate Work
// Remove Required Materials
foreach (var pair in requiredFungibleItems.OrderBy(pair => pair.Key))
{
if (!materialItemSheet.TryGetValue(pair.Key, out var materialRow) ||
!avatarState.inventory.RemoveFungibleItem(materialRow.ItemId, context.BlockIndex, pair.Value))
{
throw new NotEnoughMaterialException(
$"{addressesHex}Aborted as the player has no enough material ({pair.Key} * {pair.Value})");
}
}
// ~Remove Required Materials
// Subtract Required ActionPoint
// 2024-03-29 기준: 레시피에 CostActionPoint가 포함된 케이스는 없으나 TableSheets 상태에 의해 동작이 변경될 수 있기에 작성해둔다.
if (costActionPoint > 0)
{
if (!states.TryGetActionPoint(avatarAddress, out var actionPoint))
{
actionPoint = avatarState.actionPoint;
}
if (actionPoint < costActionPoint)
{
throw new NotEnoughActionPointException(
$"{addressesHex}Aborted due to insufficient action point: {actionPoint} < {costActionPoint}"
);
}
actionPoint -= costActionPoint;
states = states.SetActionPoint(avatarAddress, actionPoint);
}
// ~Subtract Required ActionPoint
// Create Consumable
var random = context.GetRandom();
var consumable = (Consumable) ItemFactory.CreateItemUsable(
consumableRow,
random.GenerateRandomGuid(),
endBlockIndex
);
// ~Create Consumable
// Add or Update Consumable
avatarState.blockIndex = context.BlockIndex;
avatarState.updatedAt = context.BlockIndex;
avatarState.UpdateFromCombination(consumable);
avatarState.UpdateQuestRewards(materialItemSheet);
// ~Add or Update Consumable
// Update Slot
var mailId = random.GenerateRandomGuid();
var attachmentResult = new CombinationConsumable5.ResultModel
{
id = mailId,
actionPoint = costActionPoint,
materials = requiredFungibleItems.ToDictionary(
e => ItemFactory.CreateMaterial(materialItemSheet, e.Key),
e => e.Value),
itemUsable = consumable,
recipeId = recipeId,
};
slotState.Update(attachmentResult, context.BlockIndex, endBlockIndex);
allSlotState.SetSlot(slotState);
// ~Update Slot
// Create Mail
var mail = new CombinationMail(
attachmentResult,
context.BlockIndex,
mailId,
endBlockIndex);
avatarState.Update(mail);
// ~Create Mail
var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}Combination Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states
.SetAvatarState(avatarAddress, avatarState)
.SetCombinationSlotState(avatarAddress, allSlotState);
}
}
}