-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathItemEnhancement.cs
467 lines (406 loc) · 17.8 KB
/
ItemEnhancement.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Bencodex.Types;
using Lib9c.Abstractions;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Action.Guild.Migration.LegacyModels;
using Nekoyume.Arena;
using Nekoyume.Extensions;
using Nekoyume.Helper;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Nekoyume.TableData.Crystal;
using Serilog;
using static Lib9c.SerializeKeys;
namespace Nekoyume.Action
{
/// <summary>
/// Updated at https://github.com/planetarium/lib9c/pull/2195
/// </summary>
[Serializable]
[ActionType("item_enhancement14")]
public class ItemEnhancement : GameAction, IItemEnhancementV5
{
public const int MaterialCountLimit = 50;
public static readonly IReadOnlyCollection<ItemSubType> HammerBannedTypes =
new List<ItemSubType>
{
ItemSubType.Aura,
ItemSubType.Grimoire,
};
public static readonly IReadOnlyCollection<int> HammerIds = new List<int>
{
600301,
600302,
600303,
600304,
600305,
600306,
};
public Guid itemId;
public List<Guid> materialIds;
public Address avatarAddress;
public int slotIndex;
public Dictionary<int, int> hammers = new();
Guid IItemEnhancementV5.ItemId => itemId;
List<Guid> IItemEnhancementV5.MaterialIds => materialIds;
Address IItemEnhancementV5.AvatarAddress => avatarAddress;
int IItemEnhancementV5.SlotIndex => slotIndex;
Dictionary<int, int> IItemEnhancementV5.Hammers => hammers;
protected override IImmutableDictionary<string, IValue> PlainValueInternal
{
get
{
var dict = new Dictionary<string, IValue>
{
["itemId"] = itemId.Serialize(),
["materialIds"] = new List(
materialIds.OrderBy(i => i).Select(i => i.Serialize())
),
["avatarAddress"] = avatarAddress.Serialize(),
["slotIndex"] = slotIndex.Serialize(),
};
#pragma warning disable LAA1002
if (hammers.Any())
#pragma warning restore LAA1002
{
dict["hammers"] = new List(hammers.OrderBy(kv => kv.Key)
.Select(kv => List.Empty.Add(kv.Key).Add(kv.Value)));
}
return dict.ToImmutableDictionary();
}
}
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
itemId = plainValue["itemId"].ToGuid();
materialIds = plainValue["materialIds"].ToList(StateExtensions.ToGuid);
avatarAddress = plainValue["avatarAddress"].ToAddress();
if (plainValue.TryGetValue((Text)"slotIndex", out var value))
{
slotIndex = value.ToInteger();
}
hammers = new Dictionary<int, int>();
if (plainValue.TryGetValue((Text)"hammers", out var serializedHammers))
{
var serializedList = (List) serializedHammers;
foreach (var iValue in serializedList)
{
var innerList = (List)iValue;
hammers.Add((Integer)innerList[0], (Integer)innerList[1]);
}
}
}
public override IWorld Execute(IActionContext context)
{
GasTracer.UseGas(1);
var ctx = context;
var states = ctx.PreviousState;
// Collect addresses
var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress);
var sw = new Stopwatch();
sw.Start();
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex} ItemEnhancement exec started", addressesHex);
// Validate avatar
var agentState = states.GetAgentState(ctx.Signer);
if (agentState is null)
{
throw new FailedLoadStateException(
$"{addressesHex} Aborted as the agent state of the signer was failed to load."
);
}
if (!states.TryGetAvatarState(ctx.Signer, avatarAddress, out var avatarState))
{
throw new FailedLoadStateException(
$"{addressesHex} Aborted as the avatar state of the signer was failed to load."
);
}
// Validate target equipment item
if (!avatarState.inventory.TryGetNonFungibleItem(itemId,
out ItemUsable enhancementItem))
{
throw new ItemDoesNotExistException(
$"{addressesHex} Aborted as the NonFungibleItem ({itemId}) was failed to load from avatar's inventory."
);
}
if (enhancementItem.RequiredBlockIndex > context.BlockIndex)
{
throw new RequiredBlockIndexException(
$"{addressesHex} Aborted as the equipment to enhance ({itemId}) is not available yet;" +
$" it will be available at the block #{enhancementItem.RequiredBlockIndex}."
);
}
if (!(enhancementItem is Equipment enhancementEquipment))
{
throw new InvalidCastException(
$"{addressesHex} Aborted as the item is not a {nameof(Equipment)}, but {enhancementItem.GetType().Name}."
);
}
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
sw.Stop();
Log.Verbose("{AddressesHex} ItemEnhancement Get Equipment: {Elapsed}", addressesHex,
sw.Elapsed);
sw.Restart();
Dictionary<Type, (Address, ISheet)> sheets = states.GetSheets(sheetTypes: new[]
{
typeof(EquipmentItemSheet),
typeof(EnhancementCostSheetV3),
typeof(MaterialItemSheet),
typeof(CrystalEquipmentGrindingSheet),
typeof(CrystalMonsterCollectionMultiplierSheet),
typeof(StakeRegularRewardSheet)
});
// Validate from sheet
var enhancementCostSheet = sheets.GetSheet<EnhancementCostSheetV3>();
EnhancementCostSheetV3.Row startCostRow;
if (enhancementEquipment.level == 0)
{
startCostRow = new EnhancementCostSheetV3.Row();
}
else
{
if (!TryGetRow(enhancementEquipment, enhancementCostSheet, out startCostRow))
{
throw new SheetRowNotFoundException(addressesHex, nameof(WorldSheet),
enhancementEquipment.level);
}
}
var maxLevel = GetEquipmentMaxLevel(enhancementEquipment, enhancementCostSheet);
if (enhancementEquipment.level > maxLevel)
{
throw new EquipmentLevelExceededException(
$"{addressesHex} Aborted due to invalid equipment level: {enhancementEquipment.level} < {maxLevel}");
}
// Validate enhancement materials
var uniqueMaterialIds = materialIds.Distinct().ToList();
// Validate hammer without materials count
if (hammers.Any())
{
if (HammerBannedTypes.Contains(enhancementEquipment.ItemSubType))
{
throw new InvalidItemTypeException($"target equipment({enhancementEquipment.ItemSubType}) does not allow use hammer");
}
int hammerCount = 0;
#pragma warning disable LAA1002
foreach (var kv in hammers)
#pragma warning restore LAA1002
{
if (!HammerIds.Contains(kv.Key))
{
throw new InvalidItemCountException("target material is not hammer");
}
hammerCount += kv.Value;
}
if (hammerCount <= 0)
{
throw new InvalidItemCountException("material or hammer must be greater than 0");
}
}
if (uniqueMaterialIds.Count > MaterialCountLimit)
{
throw new InvalidItemCountException();
}
var materialEquipments = new List<Equipment>();
foreach (var materialId in uniqueMaterialIds)
{
if (!avatarState.inventory.TryGetNonFungibleItem(materialId,
out ItemUsable materialItem))
{
throw new NotEnoughMaterialException(
$"{addressesHex} Aborted as the signer does not have a necessary material ({materialId})."
);
}
if (materialItem.RequiredBlockIndex > context.BlockIndex)
{
throw new RequiredBlockIndexException(
$"{addressesHex} Aborted as the material ({materialId}) is not available yet;" +
$" it will be available at the block #{materialItem.RequiredBlockIndex}."
);
}
if (!(materialItem is Equipment materialEquipment))
{
throw new InvalidCastException(
$"{addressesHex} Aborted as the material item is not an {nameof(Equipment)}, but {materialItem.GetType().Name}."
);
}
if (enhancementEquipment.ItemId == materialId)
{
throw new InvalidMaterialException(
$"{addressesHex} Aborted as an equipment to enhance ({materialId}) was used as a material too."
);
}
if (materialEquipment.ItemSubType != enhancementEquipment.ItemSubType)
{
throw new InvalidMaterialException(
$"{addressesHex} Aborted as the material item is not a {enhancementEquipment.ItemSubType}," +
$" but {materialEquipment.ItemSubType}."
);
}
materialEquipments.Add(materialEquipment);
}
sw.Stop();
Log.Verbose("{AddressesHex} ItemEnhancement Get Material: {Elapsed}",
addressesHex, sw.Elapsed);
sw.Restart();
// Do the action
var equipmentItemSheet = sheets.GetSheet<EquipmentItemSheet>();
// Unequip items
enhancementEquipment.Unequip();
foreach (var materialEquipment in materialEquipments)
{
materialEquipment.Unequip();
}
// clone enhancement item
var preItemUsable = new Equipment((Dictionary)enhancementEquipment.Serialize());
// Equipment level up & Update
enhancementEquipment.Exp = enhancementEquipment.GetRealExp(equipmentItemSheet,
enhancementCostSheet);
// Calculate material exp
enhancementEquipment.Exp +=
materialEquipments.Aggregate(0L,
(total, m) => total + m.GetRealExp(equipmentItemSheet, enhancementCostSheet));
// Calculate hammer exp
var hammerExp = 0L;
foreach (var (hammerId, hammerCount) in hammers.OrderBy(kv => kv.Key))
{
if (avatarState.inventory.RemoveMaterial(hammerId, context.BlockIndex, hammerCount))
{
var exp = enhancementCostSheet.GetHammerExp(hammerId);
hammerExp += exp * hammerCount;
}
else
{
throw new NotEnoughMaterialException($"not enough hammer material({hammerId})");
}
}
enhancementEquipment.Exp += hammerExp;
var row = enhancementCostSheet
.OrderByDescending(r => r.Value.Exp)
.FirstOrDefault(row =>
row.Value.ItemSubType == enhancementEquipment.ItemSubType &&
row.Value.Grade == enhancementEquipment.Grade &&
row.Value.Exp <= enhancementEquipment.Exp
).Value;
var random = ctx.GetRandom();
if (!(row is null) && row.Level > enhancementEquipment.level)
{
enhancementEquipment.SetLevel(random, row.Level, enhancementCostSheet);
}
EnhancementCostSheetV3.Row targetCostRow;
if (enhancementEquipment.level == 0)
{
targetCostRow = new EnhancementCostSheetV3.Row();
}
else
{
if (!TryGetRow(enhancementEquipment, enhancementCostSheet, out targetCostRow))
{
throw new SheetRowNotFoundException(addressesHex, nameof(WorldSheet),
enhancementEquipment.level);
}
}
// 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 feeAddress = states.GetFeeAddress(context.BlockIndex);
states = states.TransferAsset(ctx, ctx.Signer, feeAddress,
states.GetGoldCurrency() * requiredNcg);
}
// Required block index = Total required block to reach target level - total required block to reach start level (already elapsed)
var requiredBlockIndex =
ctx.BlockIndex +
(targetCostRow.RequiredBlockIndex - startCostRow.RequiredBlockIndex);
enhancementEquipment.Update(requiredBlockIndex);
// Remove materials
foreach (var materialId in uniqueMaterialIds)
{
avatarState.inventory.RemoveNonFungibleItem(materialId);
}
sw.Stop();
Log.Verbose("{AddressesHex} ItemEnhancement Upgrade Equipment: {Elapsed}", addressesHex,
sw.Elapsed);
// Send scheduled mail
var result = new ItemEnhancement13.ResultModel
{
preItemUsable = preItemUsable,
itemUsable = enhancementEquipment,
materialItemIdList = uniqueMaterialIds.ToArray(),
enhancementResult = ItemEnhancement13.EnhancementResult.Success, // Result is fixed to Success
gold = requiredNcg,
CRYSTAL = 0 * CrystalCalculator.CRYSTAL,
};
var mail = new ItemEnhanceMail(
result, ctx.BlockIndex, random.GenerateRandomGuid(), requiredBlockIndex
);
result.id = mail.id;
avatarState.inventory.RemoveNonFungibleItem(enhancementEquipment);
avatarState.Update(mail);
avatarState.UpdateFromItemEnhancement(enhancementEquipment);
// Update quest reward
var materialSheet = sheets.GetSheet<MaterialItemSheet>();
avatarState.UpdateQuestRewards(materialSheet);
// Update slot state
slotState.Update(result, ctx.BlockIndex, requiredBlockIndex);
allSlotState.SetSlot(slotState);
// Set state
sw.Restart();
states = states.SetAvatarState(avatarAddress, avatarState);
sw.Stop();
Log.Verbose("{AddressesHex} ItemEnhancement Set AvatarState: {Elapsed}", addressesHex,
sw.Elapsed);
var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex} ItemEnhancement Total Executed Time: {Elapsed}", addressesHex,
ended - started);
return states
.SetCombinationSlotState(avatarAddress, allSlotState);
}
public static int GetRequiredBlockCount(Equipment preEquipment, Equipment targetEquipment,
EnhancementCostSheetV3 sheet)
{
return sheet.OrderedList
.Where(e =>
e.ItemSubType == targetEquipment.ItemSubType &&
e.Grade == targetEquipment.Grade &&
e.Level > preEquipment.level &&
e.Level <= targetEquipment.level)
.Aggregate(0, (blocks, row) => blocks + row.RequiredBlockIndex);
}
public static bool TryGetRow(Equipment equipment, EnhancementCostSheetV3 sheet,
out EnhancementCostSheetV3.Row row)
{
row = sheet.OrderedList.FirstOrDefault(x =>
x.Grade == equipment.Grade &&
x.Level == equipment.level &&
x.ItemSubType == equipment.ItemSubType
);
return row != null;
}
public static int GetEquipmentMaxLevel(Equipment equipment, EnhancementCostSheetV3 sheet)
{
return sheet.OrderedList.Where(x => x.Grade == equipment.Grade).Max(x => x.Level);
}
}
}