Skip to content

Commit

Permalink
Merge pull request #3110 from planetarium/feature/fix-create-avatar-r…
Browse files Browse the repository at this point in the history
…andom

Feature/fix create avatar random
  • Loading branch information
eugene-doobu authored Dec 31, 2024
2 parents 510d830 + 6041706 commit f3396de
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
27 changes: 19 additions & 8 deletions Lib9c.DevExtensions/Manager/Contents/CreateAvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ namespace Lib9c.DevExtensions.Manager.Contents
/// </summary>
public static class CreateAvatarManager
{
// TODO: Costume과 Grimoire, Aura에 한해 하드코딩같은 형태로 구현되어 있음. 추후 수정 필요.
private const int AddItemCount = 10;

/// <summary>
/// Create an avatar and world state for testing.
/// </summary>
/// <param name="ctx">action context</param>
/// <param name="avatarAddress">avatar address</param>
/// <param name="states">base world state</param>
/// <param name="avatarState">avatar state</param>
/// <param name="random">random object</param>
/// <returns>world state with dev avatar</returns>
public static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddress, IWorld states, AvatarState avatarState)
public static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddress, IWorld states, AvatarState avatarState, IRandom random)
{
// prepare for test when executing on editor mode.
var data = TestbedHelper.LoadData<TestbedCreateAvatar>("TestbedCreateAvatar");
Expand All @@ -49,7 +53,6 @@ public static IWorld ExecuteDevExtensions(IActionContext ctx, Address avatarAddr
var characterLevelSheet = states.GetSheet<CharacterLevelSheet>();
var enhancementCostSheet = states.GetSheet<EnhancementCostSheetV2>();
var materialItemSheet = states.GetSheet<MaterialItemSheet>();
var random = ctx.GetRandom();

AddTestItems(ctx, avatarState, random, materialItemSheet);

Expand Down Expand Up @@ -252,17 +255,19 @@ private static void AddItemsForTest(
int tradableMaterialCount,
int foodCount)
{
foreach (var row in costumeItemSheet.OrderedList)
for (var i = 0; i < AddItemCount; ++i)
{
avatarState.inventory.AddItem(ItemFactory.CreateCostume(row, random.GenerateRandomGuid()));
foreach (var row in costumeItemSheet.OrderedList)
{
avatarState.inventory.AddItem(ItemFactory.CreateCostume(row, random.GenerateRandomGuid()));
}
}

foreach (var row in materialItemSheet.OrderedList)
{
avatarState.inventory.AddItem(ItemFactory.CreateMaterial(row), materialCount);

if (row.ItemSubType == ItemSubType.Hourglass ||
row.ItemSubType == ItemSubType.ApStone)
if (row.ItemSubType is ItemSubType.Hourglass || row.ItemSubType is ItemSubType.ApStone)
{
avatarState.inventory.AddItem(ItemFactory.CreateTradableMaterial(row), tradableMaterialCount);
}
Expand All @@ -271,8 +276,14 @@ private static void AddItemsForTest(
foreach (var row in equipmentItemSheet.OrderedList.Where(row =>
row.Id > GameConfig.DefaultAvatarWeaponId))
{
var itemId = random.GenerateRandomGuid();
avatarState.inventory.AddItem(ItemFactory.CreateItemUsable(row, itemId, default));
if (row.ItemSubType is ItemSubType.Grimoire || row.ItemSubType is ItemSubType.Aura)
{
for (var i = 0; i < AddItemCount; ++i)
{
avatarState.inventory.AddItem(ItemFactory.CreateItemUsable(row, random.GenerateRandomGuid(), default));
}
}
avatarState.inventory.AddItem(ItemFactory.CreateItemUsable(row, random.GenerateRandomGuid(), default));
}

foreach (var row in consumableItemSheet.OrderedList)
Expand Down
6 changes: 3 additions & 3 deletions Lib9c/Action/CreateAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public override IWorld Execute(IActionContext context)
)
);

var random = ctx.GetRandom();
var addressesHex = GetSignerAndOtherAddressesHex(context, avatarAddress);
ValidateName(addressesHex);

Expand Down Expand Up @@ -110,7 +111,7 @@ public override IWorld Execute(IActionContext context)
avatarState.UpdateQuestRewards(materialItemSheet);

#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
states = CreateAvatarManager.ExecuteDevExtensions(ctx, avatarAddress, states, avatarState);
states = CreateAvatarManager.ExecuteDevExtensions(ctx, avatarAddress, states, avatarState, random);
#endif

var sheets = ctx.PreviousState.GetSheets(containItemSheet: true,
Expand All @@ -120,7 +121,7 @@ public override IWorld Execute(IActionContext context)
});
var itemSheet = sheets.GetItemSheet();
var createAvatarItemSheet = sheets.GetSheet<CreateAvatarItemSheet>();
AddItem(itemSheet, createAvatarItemSheet, avatarState, context.GetRandom());
AddItem(itemSheet, createAvatarItemSheet, avatarState, random);
var createAvatarFavSheet = sheets.GetSheet<CreateAvatarFavSheet>();
states = MintAsset(createAvatarFavSheet, avatarState, states, context);

Expand Down Expand Up @@ -257,7 +258,6 @@ public static AvatarState CreateAvatarState(string name,
Address rankingMapAddress)
{
var state = ctx.PreviousState;
var random = ctx.GetRandom();
var avatarState = AvatarState.Create(
avatarAddress,
ctx.Signer,
Expand Down
30 changes: 30 additions & 0 deletions Lib9c/Model/Item/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,36 @@ public KeyValuePair<int, int> AddItem(ItemBase itemBase, int count = 1, ILock iL
throw new InvalidItemCountException();
}

#if LIB9C_DEV_EXTENSIONS || UNITY_EDITOR
foreach (var item in _items)
{
Guid? inventoryItemGuid;
Guid? createItemGuid;
{
inventoryItemGuid = item.item switch
{
ItemUsable itemUsable => itemUsable.ItemId,
Costume costume => costume.ItemId,
_ => null,
};
}
{
createItemGuid = itemBase switch
{
ItemUsable itemUsable => itemUsable.ItemId,
Costume costume => costume.ItemId,
_ => null,
};
}
if (inventoryItemGuid.HasValue && createItemGuid.HasValue && inventoryItemGuid.Value == createItemGuid.Value)
{
throw new ArgumentException(
$"Aborted because {nameof(itemBase)} is already in the inventory."
);
}
}
#endif

switch (itemBase.ItemType)
{
case ItemType.Consumable:
Expand Down

0 comments on commit f3396de

Please sign in to comment.