-
Notifications
You must be signed in to change notification settings - Fork 46
/
CreateAvatar.cs
599 lines (527 loc) · 21.9 KB
/
CreateAvatar.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Bencodex.Types;
using Lib9c;
using Lib9c.Abstractions;
using Libplanet.Action;
using Libplanet.Action.State;
using Libplanet.Crypto;
using Nekoyume.Extensions;
using Nekoyume.Helper;
using Nekoyume.Model.Item;
using Nekoyume.Model.Stat;
using Nekoyume.Model.State;
using Nekoyume.Module;
using Nekoyume.TableData;
using Serilog;
using static Lib9c.SerializeKeys;
#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
using Lib9c.DevExtensions;
using Lib9c.DevExtensions.Model;
using Nekoyume.Model.Skill;
#endif
namespace Nekoyume.Action
{
/// <summary>
/// Hard forked at https://github.com/planetarium/lib9c/pull/2195
/// </summary>
[Serializable]
[ActionType("create_avatar11")]
public class CreateAvatar : GameAction, ICreateAvatarV2
{
public const string DeriveFormat = "avatar-state-{0}";
public int index;
public int hair;
public int lens;
public int ear;
public int tail;
public string name;
int ICreateAvatarV2.Index => index;
int ICreateAvatarV2.Hair => hair;
int ICreateAvatarV2.Lens => lens;
int ICreateAvatarV2.Ear => ear;
int ICreateAvatarV2.Tail => tail;
string ICreateAvatarV2.Name => name;
protected override IImmutableDictionary<string, IValue> PlainValueInternal =>
new Dictionary<string, IValue>()
{
["index"] = (Integer)index,
["hair"] = (Integer)hair,
["lens"] = (Integer)lens,
["ear"] = (Integer)ear,
["tail"] = (Integer)tail,
["name"] = (Text)name,
}.ToImmutableDictionary();
protected override void LoadPlainValueInternal(IImmutableDictionary<string, IValue> plainValue)
{
index = (int)((Integer)plainValue["index"]).Value;
hair = (int)((Integer)plainValue["hair"]).Value;
lens = (int)((Integer)plainValue["lens"]).Value;
ear = (int)((Integer)plainValue["ear"]).Value;
tail = (int)((Integer)plainValue["tail"]).Value;
name = (Text)plainValue["name"];
}
public override IWorld Execute(IActionContext context)
{
context.UseGas(1);
var ctx = context;
var signer = ctx.Signer;
var states = ctx.PreviousState;
var avatarAddress = signer.Derive(
string.Format(
CultureInfo.InvariantCulture,
DeriveFormat,
index
)
);
var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress);
ValidateName(addressesHex);
var sw = new Stopwatch();
sw.Start();
var started = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}CreateAvatar exec started", addressesHex);
var agentState = GetAgentState(states, signer, avatarAddress, addressesHex);
sw.Stop();
Log.Verbose("{AddressesHex}CreateAvatar Get AgentAvatarStates: {Elapsed}", addressesHex, sw.Elapsed);
sw.Restart();
Log.Verbose("{AddressesHex}Execute CreateAvatar; player: {AvatarAddress}", addressesHex, avatarAddress);
agentState.avatarAddresses.Add(index, avatarAddress);
// Avoid NullReferenceException in test
var materialItemSheet = ctx.PreviousState.GetSheet<MaterialItemSheet>();
var avatarState = CreateAvatarState(name, avatarAddress, ctx, materialItemSheet, default);
CustomizeAvatar(avatarState);
var allCombinationSlotState = CreateCombinationSlots(avatarAddress);
states = states.SetCombinationSlotState(avatarAddress, allCombinationSlotState);
avatarState.UpdateQuestRewards(materialItemSheet);
#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
states = ExecuteDevExtensions(ctx, avatarAddress, states, avatarState);
#endif
var sheets = ctx.PreviousState.GetSheets(containItemSheet: true,
sheetTypes: new[]
{
typeof(CreateAvatarItemSheet), typeof(CreateAvatarFavSheet),
});
var itemSheet = sheets.GetItemSheet();
var createAvatarItemSheet = sheets.GetSheet<CreateAvatarItemSheet>();
AddItem(itemSheet, createAvatarItemSheet, avatarState, context.GetRandom());
var createAvatarFavSheet = sheets.GetSheet<CreateAvatarFavSheet>();
states = MintAsset(createAvatarFavSheet, avatarState, states, context);
sw.Stop();
Log.Verbose("{AddressesHex}CreateAvatar CreateAvatarState: {Elapsed}", addressesHex, sw.Elapsed);
var ended = DateTimeOffset.UtcNow;
Log.Debug("{AddressesHex}CreateAvatar Total Executed Time: {Elapsed}", addressesHex, ended - started);
return states
.SetAgentState(signer, agentState)
.SetAvatarState(avatarAddress, avatarState)
.SetActionPoint(avatarAddress, DailyReward.ActionPointMax)
.SetDailyRewardReceivedBlockIndex(avatarAddress, 0L);
}
private void ValidateName(string addressesHex)
{
if (!Regex.IsMatch(name, GameConfig.AvatarNickNamePattern))
{
throw new InvalidNamePatternException(
$"{addressesHex}Aborted as the input name {name} does not follow the allowed name pattern.");
}
}
private AgentState GetAgentState(IWorld states, Address signer, Address avatarAddress, string addressesHex)
{
var existingAgentState = states.GetAgentState(signer);
var agentState = existingAgentState ?? new AgentState(signer);
var avatarState = states.GetAvatarState(avatarAddress);
if (avatarState is not null)
{
throw new InvalidAddressException(
$"{addressesHex}Aborted as there is already an avatar at {avatarAddress}.");
}
if (index is < 0 or >= GameConfig.SlotCount)
{
throw new AvatarIndexOutOfRangeException(
$"{addressesHex}Aborted as the index is out of range #{index}.");
}
if (agentState.avatarAddresses.ContainsKey(index))
{
throw new AvatarIndexAlreadyUsedException(
$"{addressesHex}Aborted as the signer already has an avatar at index #{index}.");
}
return agentState;
}
private void CustomizeAvatar(AvatarState avatarState)
{
if (hair < 0)
{
hair = 0;
}
if (lens < 0)
{
lens = 0;
}
if (ear < 0)
{
ear = 0;
}
if (tail < 0)
{
tail = 0;
}
avatarState.Customize(hair, lens, ear, tail);
}
private AllCombinationSlotState CreateCombinationSlots(Address avatarAddress)
{
var allCombinationSlotState = new AllCombinationSlotState();
for (var i = 0; i < AvatarState.DefaultCombinationSlotCount; i++)
{
var slotAddr = Addresses.GetCombinationSlotAddress(avatarAddress, i);
var slot = new CombinationSlotState(slotAddr, i);
allCombinationSlotState.AddSlot(slot);
}
return allCombinationSlotState;
}
public static void AddItem(ItemSheet itemSheet, CreateAvatarItemSheet createAvatarItemSheet,
AvatarState avatarState, IRandom random)
{
foreach (var row in createAvatarItemSheet.Values)
{
var itemId = row.ItemId;
var count = row.Count;
var itemRow = itemSheet[itemId];
if (itemRow is MaterialItemSheet.Row materialRow)
{
var item = ItemFactory.CreateMaterial(materialRow);
avatarState.inventory.AddItem(item, count);
}
else
{
for (var i = 0; i < count; i++)
{
var item = ItemFactory.CreateItem(itemRow, random);
avatarState.inventory.AddItem(item);
}
}
}
}
public static IWorld MintAsset(CreateAvatarFavSheet favSheet,
AvatarState avatarState, IWorld states, IActionContext context)
{
foreach (var row in favSheet.Values)
{
var currency = row.Currency;
var targetAddress = row.Target switch
{
CreateAvatarFavSheet.Target.Agent => avatarState.agentAddress,
CreateAvatarFavSheet.Target.Avatar => avatarState.address,
_ => throw new ArgumentOutOfRangeException(),
};
states = states.MintAsset(context, targetAddress, currency * row.Quantity);
}
return states;
}
public static AvatarState CreateAvatarState(string name,
Address avatarAddress,
IActionContext ctx,
MaterialItemSheet materialItemSheet,
Address rankingMapAddress)
{
var state = ctx.PreviousState;
var random = ctx.GetRandom();
var avatarState = AvatarState.Create(
avatarAddress,
ctx.Signer,
ctx.BlockIndex,
state.GetAvatarSheets(),
rankingMapAddress,
name
);
#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
AddTestItems(ctx, avatarState, random, materialItemSheet);
#endif
return avatarState;
}
#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
private static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddress, IWorld states, AvatarState avatarState)
{
// prepare for test when executing on editor mode.
var data = TestbedHelper.LoadData<TestbedCreateAvatar>("TestbedCreateAvatar");
states = AddRunesForTest(ctx, avatarAddress, states, data.RuneStoneCount);
states = AddSoulStoneForTest(ctx, avatarAddress, states, data.SoulStoneCount);
if (data.AddPet)
{
states = AddPetsForTest(avatarAddress, states);
}
var equipmentSheet = states.GetSheet<EquipmentItemSheet>();
var recipeSheet = states.GetSheet<EquipmentItemRecipeSheet>();
var subRecipeSheet = states.GetSheet<EquipmentItemSubRecipeSheetV2>();
var optionSheet = states.GetSheet<EquipmentItemOptionSheet>();
var skillSheet = states.GetSheet<SkillSheet>();
var characterLevelSheet = states.GetSheet<CharacterLevelSheet>();
var enhancementCostSheet = states.GetSheet<EnhancementCostSheetV2>();
var random = ctx.GetRandom();
avatarState.level = data.Level;
avatarState.exp = characterLevelSheet[data.Level].Exp;
foreach (var recipeId in data.FullOptionEquipmentRecipeIds)
{
AddFullOptionEquipment(
avatarState,
random,
equipmentSheet,
recipeSheet,
subRecipeSheet,
optionSheet,
skillSheet,
enhancementCostSheet,
recipeId);
}
return states;
}
private static void AddFullOptionEquipment(
AvatarState avatarState,
IRandom random,
EquipmentItemSheet equipmentSheet,
EquipmentItemRecipeSheet recipeSheet,
EquipmentItemSubRecipeSheetV2 subRecipeSheet,
EquipmentItemOptionSheet optionSheet,
SkillSheet skillSheet,
EnhancementCostSheetV2 enhancementCostSheet,
int recipeId)
{
var recipeRow = recipeSheet[recipeId];
var subRecipeId = recipeRow.SubRecipeIds[1];
var subRecipeRow = subRecipeSheet[subRecipeId];
var equipmentRow = equipmentSheet[recipeRow.ResultEquipmentId];
var equipment = (Equipment)ItemFactory.CreateItemUsable(
equipmentRow,
random.GenerateRandomGuid(),
0L,
madeWithMimisbrunnrRecipe: subRecipeRow.IsMimisbrunnrSubRecipe ?? false);
foreach (var option in subRecipeRow.Options)
{
var optionRow = optionSheet[option.Id];
AddOptionToEquipment(equipment, optionRow, skillSheet);
}
EnhanceEquipmentToMaxLevel(equipment, enhancementCostSheet, random);
avatarState.inventory.AddItem(equipment);
}
private static void AddOptionToEquipment(Equipment equipment, EquipmentItemOptionSheet.Row optionRow, SkillSheet skillSheet)
{
// Add stats.
if (optionRow.StatType != StatType.NONE)
{
var statMap = new DecimalStat(optionRow.StatType, optionRow.StatMax);
equipment.StatsMap.AddStatAdditionalValue(statMap.StatType, statMap.TotalValue);
equipment.optionCountFromCombination++;
}
// Add skills.
else
{
var skillRow = skillSheet.OrderedList.First(r => r.Id == optionRow.SkillId);
var skill = SkillFactory.Get(
skillRow,
optionRow.SkillDamageMax,
optionRow.SkillChanceMax,
optionRow.StatDamageRatioMax,
optionRow.ReferencedStatType);
if (skill != null)
{
equipment.Skills.Add(skill);
equipment.optionCountFromCombination++;
}
}
}
private static void EnhanceEquipmentToMaxLevel(Equipment equipment, EnhancementCostSheetV2 enhancementCostSheet, IRandom random)
{
for (int i = 1; i <= 20; ++i)
{
var subType = equipment.ItemSubType;
var grade = equipment.Grade;
var costRow = enhancementCostSheet.Values
.First(x => x.ItemSubType == subType &&
x.Grade == grade &&
x.Level == i);
equipment.LevelUp(random, costRow, true);
}
}
private static void AddTestItems(
IActionContext ctx,
AvatarState avatarState,
IRandom random,
MaterialItemSheet materialItemSheet)
{
var data = TestbedHelper.LoadData<TestbedCreateAvatar>("TestbedCreateAvatar");
var costumeItemSheet = ctx.PreviousState.GetSheet<CostumeItemSheet>();
var equipmentItemSheet = ctx.PreviousState.GetSheet<EquipmentItemSheet>();
var consumableItemSheet = ctx.PreviousState.GetSheet<ConsumableItemSheet>();
AddItemsForTest(
avatarState: avatarState,
random: random,
costumeItemSheet: costumeItemSheet,
materialItemSheet: materialItemSheet,
equipmentItemSheet: equipmentItemSheet,
consumableItemSheet: consumableItemSheet,
data.MaterialCount,
data.TradableMaterialCount,
data.FoodCount);
var skillSheet = ctx.PreviousState.GetSheet<SkillSheet>();
var optionSheet = ctx.PreviousState.GetSheet<EquipmentItemOptionSheet>();
var items = data.CustomEquipmentItems;
foreach (var item in items)
{
AddCustomEquipment(
avatarState: avatarState,
random: random,
skillSheet: skillSheet,
equipmentItemSheet: equipmentItemSheet,
equipmentItemOptionSheet: optionSheet,
// Set level of equipment here.
level: item.Level,
// Set recipeId of target equipment here.
recipeId: item.ID,
// Add optionIds here.
item.OptionIds);
}
}
private static IWorld AddRunesForTest(
IActionContext context,
Address avatarAddress,
IWorld states,
int count = int.MaxValue)
{
var runeSheet = states.GetSheet<RuneSheet>();
foreach (var row in runeSheet.Values)
{
var rune = RuneHelper.ToFungibleAssetValue(row, count);
states = states.MintAsset(context, avatarAddress, rune);
}
return states;
}
private static IWorld AddSoulStoneForTest(
IActionContext context,
Address avatarAddress,
IWorld states,
int count = int.MaxValue)
{
var petSheet = states.GetSheet<PetSheet>();
foreach (var row in petSheet.Values)
{
var soulStone = Currencies.GetSoulStone(row.SoulStoneTicker) * count;
states = states.MintAsset(context, avatarAddress, soulStone);
}
return states;
}
private static IWorld AddPetsForTest(
Address avatarAddress,
IWorld states)
{
var petSheet = states.GetSheet<PetSheet>();
foreach (var id in petSheet.Keys)
{
var petState = new PetState(id);
petState.LevelUp();
var petStateAddress = PetState.DeriveAddress(avatarAddress, id);
states = states.SetLegacyState(petStateAddress, petState.Serialize());
}
return states;
}
private static void AddItemsForTest(
AvatarState avatarState,
IRandom random,
CostumeItemSheet costumeItemSheet,
MaterialItemSheet materialItemSheet,
EquipmentItemSheet equipmentItemSheet,
ConsumableItemSheet consumableItemSheet,
int materialCount,
int tradableMaterialCount,
int foodCount)
{
foreach (var row in costumeItemSheet.OrderedList)
{
avatarState.inventory.AddItem2(ItemFactory.CreateCostume(row, random.GenerateRandomGuid()));
}
foreach (var row in materialItemSheet.OrderedList)
{
avatarState.inventory.AddItem2(ItemFactory.CreateMaterial(row), materialCount);
if (row.ItemSubType == ItemSubType.Hourglass ||
row.ItemSubType == ItemSubType.ApStone)
{
avatarState.inventory.AddItem2(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount);
}
}
foreach (var row in equipmentItemSheet.OrderedList.Where(row =>
row.Id > GameConfig.DefaultAvatarWeaponId))
{
var itemId = random.GenerateRandomGuid();
avatarState.inventory.AddItem2(ItemFactory.CreateItemUsable(row, itemId, default));
}
foreach (var row in consumableItemSheet.OrderedList)
{
for (var i = 0; i < foodCount; i++)
{
var itemId = random.GenerateRandomGuid();
var consumable = (Consumable)ItemFactory.CreateItemUsable(row, itemId,
0, 0);
avatarState.inventory.AddItem2(consumable);
}
}
}
private static void AddCustomEquipment(
AvatarState avatarState,
IRandom random,
SkillSheet skillSheet,
EquipmentItemSheet equipmentItemSheet,
EquipmentItemOptionSheet equipmentItemOptionSheet,
int level,
int recipeId,
params int[] optionIds
)
{
if (!equipmentItemSheet.TryGetValue(recipeId, out var equipmentRow))
{
return;
}
var itemId = random.GenerateRandomGuid();
var equipment = (Equipment)ItemFactory.CreateItemUsable(equipmentRow, itemId, 0, level);
var optionRows = new List<EquipmentItemOptionSheet.Row>();
foreach (var optionId in optionIds)
{
if (!equipmentItemOptionSheet.TryGetValue(optionId, out var optionRow))
{
continue;
}
optionRows.Add(optionRow);
}
AddOption(skillSheet, equipment, optionRows, random);
avatarState.inventory.AddItem2(equipment);
}
private static HashSet<int> AddOption(
SkillSheet skillSheet,
Equipment equipment,
IEnumerable<EquipmentItemOptionSheet.Row> optionRows,
IRandom random)
{
var optionIds = new HashSet<int>();
foreach (var optionRow in optionRows.OrderBy(r => r.Id))
{
if (optionRow.StatType != StatType.NONE)
{
var stat = CombinationEquipment5.GetStat(optionRow, random);
equipment.StatsMap.AddStatAdditionalValue(stat.StatType, stat.BaseValue);
}
else
{
var skill = CombinationEquipment5.GetSkill(optionRow, skillSheet, random);
if (!(skill is null))
{
equipment.Skills.Add(skill);
}
}
optionIds.Add(optionRow.Id);
}
return optionIds;
}
#endif
}
}