Example #1
0
        public void Execute()
        {
            var shopState = _initialState.GetShopState();

            Assert.Empty(shopState.Products);

            var avatarState = _initialState.GetAvatarState(_avatarAddress);

            Assert.Single(avatarState.inventory.Equipments);

            var equipment = avatarState.inventory.Equipments.FirstOrDefault();

            Assert.NotNull(equipment);

            var currencyState = _initialState.GetGoldCurrency();
            var price         = new FungibleAssetValue(currencyState, 100, 0);
            var sellAction    = new Sell4
            {
                itemId = equipment.ItemId,
                price  = price,
                sellerAvatarAddress = _avatarAddress,
            };
            var nextState = sellAction.Execute(new ActionContext
            {
                BlockIndex     = 0,
                PreviousStates = _initialState,
                Rehearsal      = false,
                Signer         = _agentAddress,
                Random         = new TestRandom(),
            });

            var nextAvatarState = nextState.GetAvatarState(_avatarAddress);

            Assert.Empty(nextAvatarState.inventory.Equipments);

            var nextShopState = nextState.GetShopState();

            Assert.Single(nextShopState.Products);

            var(_, shopItem) = nextShopState.Products.FirstOrDefault();
            Assert.NotNull(shopItem);
            Assert.Equal(equipment.ItemId, shopItem.ItemUsable.ItemId);
            Assert.Equal(price, shopItem.Price);
            Assert.Equal(_agentAddress, shopItem.SellerAgentAddress);
            Assert.Equal(_avatarAddress, shopItem.SellerAvatarAddress);
        }
Example #2
0
        public void Execute(ItemType itemType, bool shopItemExist, int blockIndex)
        {
            var avatarState = _initialState.GetAvatarState(_avatarAddress);
            List <Inventory.Item> inventoryItem = avatarState.inventory.Items.Where(i => i.item.ItemType == itemType).ToList();

            Assert.Single(inventoryItem);
            var previousStates = _initialState;
            var currencyState  = previousStates.GetGoldCurrency();
            var price          = new FungibleAssetValue(currencyState, ProductPrice, 0);
            INonFungibleItem nonFungibleItem = (INonFungibleItem)inventoryItem.First().item;

            nonFungibleItem.RequiredBlockIndex = blockIndex;
            Assert.Equal(blockIndex, nonFungibleItem.RequiredBlockIndex);
            ItemSubType itemSubType = ItemSubType.Food;
            Guid        productId   = new Guid("6f460c1a-755d-48e4-ad67-65d5f519dbc8");

            if (nonFungibleItem is ItemUsable itemUsable)
            {
                itemSubType = itemUsable.ItemSubType;
            }
            else if (nonFungibleItem is Costume costume)
            {
                itemSubType = costume.ItemSubType;
            }

            Address shopAddress = ShardedShopState.DeriveAddress(itemSubType, productId);

            if (shopItemExist)
            {
                var si = new ShopItem(
                    _agentAddress,
                    _avatarAddress,
                    productId,
                    new FungibleAssetValue(currencyState, 100, 0),
                    blockIndex,
                    nonFungibleItem);
                ShardedShopState shardedShopState =
                    new ShardedShopState(shopAddress);
                shardedShopState.Register(si);
                Assert.Single(shardedShopState.Products);
                previousStates = previousStates.SetState(shopAddress, shardedShopState.Serialize());
            }
            else
            {
                Assert.Null(previousStates.GetState(shopAddress));
            }

            var sellAction = new Sell4
            {
                itemId = nonFungibleItem.NonFungibleId,
                price  = price,
                sellerAvatarAddress = _avatarAddress,
                itemSubType         = itemSubType,
            };

            var nextState = sellAction.Execute(new ActionContext
            {
                BlockIndex     = 1,
                PreviousStates = previousStates,
                Rehearsal      = false,
                Signer         = _agentAddress,
                Random         = new TestRandom(),
            });

            const long expiredBlockIndex = Sell6.ExpiredBlockIndex + 1;
            var        nextAvatarState   = nextState.GetAvatarState(_avatarAddress);

            Assert.True(nextAvatarState.inventory.TryGetNonFungibleItem(nonFungibleItem.NonFungibleId, out var nextItem));
            INonFungibleItem nextNonFungibleItem = (INonFungibleItem)nextItem.item;

            Assert.Equal(expiredBlockIndex, nextNonFungibleItem.RequiredBlockIndex);

            var nextShopState = new ShardedShopState((Dictionary)nextState.GetState(shopAddress));

            Assert.Single(nextShopState.Products);

            var products = nextShopState.Products.Values;

            var shopItem          = products.First();
            INonFungibleItem item = itemType == ItemType.Costume ? (INonFungibleItem)shopItem.Costume : shopItem.ItemUsable;

            Assert.Equal(price, shopItem.Price);
            Assert.Equal(expiredBlockIndex, shopItem.ExpiredBlockIndex);
            Assert.Equal(expiredBlockIndex, item.RequiredBlockIndex);
            Assert.Equal(_agentAddress, shopItem.SellerAgentAddress);
            Assert.Equal(_avatarAddress, shopItem.SellerAvatarAddress);

            var mailList = nextAvatarState.mailBox.Where(m => m is SellCancelMail).ToList();

            Assert.Single(mailList);

            Assert.Equal(expiredBlockIndex, mailList.First().requiredBlockIndex);
        }