Example #1
0
        public void Rehearsal()
        {
            var action = new CombinationEquipment8
            {
                avatarAddress = _avatarAddress,
                slotIndex     = 0,
                recipeId      = 1,
                subRecipeId   = 255,
            };
            var slotAddress = _avatarAddress.Derive(
                string.Format(
                    CultureInfo.InvariantCulture,
                    CombinationSlotState.DeriveFormat,
                    0
                    )
                );

            var updatedAddresses = new List <Address>
            {
                _agentAddress,
                _avatarAddress,
                slotAddress,
                _avatarAddress.Derive(LegacyInventoryKey),
                _avatarAddress.Derive(LegacyWorldInformationKey),
                _avatarAddress.Derive(LegacyQuestListKey),
                Addresses.Blacksmith,
            };

            var state = new State();

            var nextState = action.Execute(new ActionContext
            {
                PreviousStates = state,
                Signer         = _agentAddress,
                BlockIndex     = 0,
                Rehearsal      = true,
            });

            Assert.Equal(updatedAddresses.ToImmutableHashSet(), nextState.UpdatedAddresses);
        }
Example #2
0
        private void Execute(bool backward, int recipeId, int?subRecipeId, int mintNCG)
        {
            var currency        = new Currency("NCG", 2, minter: null);
            var row             = _tableSheets.EquipmentItemRecipeSheet[recipeId];
            var requiredStage   = row.UnlockStage;
            var costActionPoint = row.RequiredActionPoint;
            var costNCG         = row.RequiredGold * currency;
            var materialRow     = _tableSheets.MaterialItemSheet[row.MaterialId];
            var material        = ItemFactory.CreateItem(materialRow, _random);

            var avatarState                  = _initialState.GetAvatarState(_avatarAddress);
            var previousActionPoint          = avatarState.actionPoint;
            var previousResultEquipmentCount =
                avatarState.inventory.Equipments.Count(e => e.Id == row.ResultEquipmentId);
            var previousMailCount = avatarState.mailBox.Count;

            avatarState.worldInformation = new WorldInformation(
                0,
                _tableSheets.WorldSheet,
                requiredStage);

            avatarState.inventory.AddItem(material, row.MaterialCount);

            if (subRecipeId.HasValue)
            {
                var subRow = _tableSheets.EquipmentItemSubRecipeSheetV2[subRecipeId.Value];
                costActionPoint += subRow.RequiredActionPoint;
                costNCG         += subRow.RequiredGold * currency;

                foreach (var materialInfo in subRow.Materials)
                {
                    material = ItemFactory.CreateItem(_tableSheets.MaterialItemSheet[materialInfo.Id], _random);
                    avatarState.inventory.AddItem(material, materialInfo.Count);
                }
            }

            IAccountStateDelta previousState;

            if (backward)
            {
                previousState = _initialState.SetState(_avatarAddress, avatarState.Serialize());
            }
            else
            {
                previousState = _initialState
                                .SetState(_avatarAddress.Derive(LegacyInventoryKey), avatarState.inventory.Serialize())
                                .SetState(
                    _avatarAddress.Derive(LegacyWorldInformationKey),
                    avatarState.worldInformation.Serialize())
                                .SetState(_avatarAddress.Derive(LegacyQuestListKey), avatarState.questList.Serialize())
                                .SetState(_avatarAddress, avatarState.SerializeV2());
            }

            previousState = previousState.MintAsset(_agentAddress, mintNCG * currency);
            var goldCurrencyState = previousState.GetGoldCurrency();
            var previousNCG       = previousState.GetBalance(_agentAddress, goldCurrencyState);

            Assert.Equal(mintNCG * currency, previousNCG);

            var action = new CombinationEquipment8
            {
                avatarAddress = _avatarAddress,
                slotIndex     = 0,
                recipeId      = recipeId,
                subRecipeId   = subRecipeId,
            };

            var nextState = action.Execute(new ActionContext
            {
                PreviousStates = previousState,
                Signer         = _agentAddress,
                BlockIndex     = 1,
                Random         = _random,
            });

            var slotState = nextState.GetCombinationSlotState(_avatarAddress, 0);

            Assert.NotNull(slotState.Result);
            Assert.NotNull(slotState.Result.itemUsable);

            if (subRecipeId.HasValue)
            {
                Assert.True(((Equipment)slotState.Result.itemUsable).optionCountFromCombination > 0);
            }
            else
            {
                Assert.Equal(0, ((Equipment)slotState.Result.itemUsable).optionCountFromCombination);
            }

            var nextAvatarState = nextState.GetAvatarStateV2(_avatarAddress);

            Assert.Equal(previousActionPoint - costActionPoint, nextAvatarState.actionPoint);
            Assert.Equal(previousMailCount + 1, nextAvatarState.mailBox.Count);
            Assert.IsType <CombinationMail>(nextAvatarState.mailBox.First());
            Assert.Equal(
                previousResultEquipmentCount + 1,
                nextAvatarState.inventory.Equipments.Count(e => e.Id == row.ResultEquipmentId));

            var agentGold = nextState.GetBalance(_agentAddress, goldCurrencyState);

            Assert.Equal(previousNCG - costNCG, agentGold);

            var blackSmithGold = nextState.GetBalance(Addresses.Blacksmith, goldCurrencyState);

            Assert.Equal(costNCG, blackSmithGold);
        }