Ejemplo n.º 1
0
        public void Execute(bool exist, int level, int monsterCollectionRound, int prevLevel, long blockIndex)
        {
            Address monsterCollectionAddress = MonsterCollectionState0.DeriveAddress(_signer, monsterCollectionRound);

            if (exist)
            {
                List <MonsterCollectionRewardSheet.RewardInfo> rewards = _tableSheets.MonsterCollectionRewardSheet[prevLevel].Rewards;
                MonsterCollectionState0 prevMonsterCollectionState     = new MonsterCollectionState0(monsterCollectionAddress, prevLevel, 0, _tableSheets.MonsterCollectionRewardSheet);
                _initialState = _initialState.SetState(monsterCollectionAddress, prevMonsterCollectionState.Serialize());
                Assert.All(prevMonsterCollectionState.RewardLevelMap, kv => Assert.Equal(rewards, kv.Value));
            }

            AgentState prevAgentState = _initialState.GetAgentState(_signer);

            while (prevAgentState.MonsterCollectionRound < monsterCollectionRound)
            {
                prevAgentState.IncreaseCollectionRound();
            }

            _initialState = _initialState.SetState(_signer, prevAgentState.Serialize());

            Currency currency = _initialState.GetGoldCurrency();

            for (int i = 1; i < level + 1; i++)
            {
                if (i > prevLevel)
                {
                    MonsterCollectionSheet.Row row = _tableSheets.MonsterCollectionSheet[i];
                    _initialState = _initialState.MintAsset(_signer, row.RequiredGold * currency);
                }
            }

            MonsterCollect0 action = new MonsterCollect0
            {
                level           = level,
                collectionRound = monsterCollectionRound,
            };

            IAccountStateDelta nextState = action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = blockIndex,
            });

            MonsterCollectionState0 nextMonsterCollectionState = new MonsterCollectionState0((Dictionary)nextState.GetState(monsterCollectionAddress));
            AgentState nextAgentState = nextState.GetAgentState(_signer);

            Assert.Equal(level, nextMonsterCollectionState.Level);
            Assert.Equal(0 * currency, nextState.GetBalance(_signer, currency));
            Assert.Equal(monsterCollectionRound, nextAgentState.MonsterCollectionRound);
            long rewardLevel = nextMonsterCollectionState.GetRewardLevel(blockIndex);

            for (long i = rewardLevel; i < 4; i++)
            {
                List <MonsterCollectionRewardSheet.RewardInfo> expected = _tableSheets.MonsterCollectionRewardSheet[level].Rewards;
                Assert.Equal(expected, nextMonsterCollectionState.RewardLevelMap[i + 1]);
            }
        }
Ejemplo n.º 2
0
        public void Set()
        {
            var sheet = new MonsterCollectionSheet();

            sheet.Set("level,required_gold,reward_id\n1,500,1");
            MonsterCollectionSheet.Row row = sheet[1];
            Assert.Equal(1, row.Level);
            Assert.Equal(500, row.RequiredGold);
            Assert.Equal(1, row.RewardId);
        }
Ejemplo n.º 3
0
        public void Execute(int balance, int?prevLevel, int level, long blockIndex, Type exc, int?expectedStakings)
        {
            Address            monsterCollectionAddress = MonsterCollectionState.DeriveAddress(_signer, 0);
            Currency           currency   = _initialState.GetGoldCurrency();
            FungibleAssetValue balanceFav = currency * balance;
            FungibleAssetValue staked     = currency * 0;

            if (prevLevel is { } prevLevelNotNull)
            {
                List <MonsterCollectionRewardSheet.RewardInfo> rewards = _tableSheets.MonsterCollectionRewardSheet[prevLevelNotNull].Rewards;
                var prevMonsterCollectionState = new MonsterCollectionState(
                    address: monsterCollectionAddress,
                    level: prevLevelNotNull,
                    blockIndex: 0,
                    monsterCollectionRewardSheet: _tableSheets.MonsterCollectionRewardSheet
                    );
                _initialState = _initialState.SetState(monsterCollectionAddress, prevMonsterCollectionState.Serialize());
                for (int i = 0; i < prevLevel; i++)
                {
                    MonsterCollectionSheet.Row row = _tableSheets.MonsterCollectionSheet[i + 1];
                    staked       += row.RequiredGold * currency;
                    _initialState = _initialState.MintAsset(monsterCollectionAddress, row.RequiredGold * currency);
                }
            }

            balanceFav -= staked;

            _initialState = _initialState.MintAsset(_signer, balanceFav);
            var action = new MonsterCollect
            {
                level = level,
            };

            if (exc is { } excType)
            {
                Assert.Throws(excType, () => action.Execute(new ActionContext
                {
                    PreviousStates = _initialState,
                    Signer         = _signer,
                    BlockIndex     = blockIndex,
                }));
            }
Ejemplo n.º 4
0
        public async Task Query()
        {
            const string query = @"
            {
                level
                requiredGold
                rewards {
                    itemId
                    quantity
                }
            }";

            MonsterCollectionSheet.Row row = Fixtures.TableSheetsFX.MonsterCollectionSheet.First !;
            List <MonsterCollectionRewardSheet.RewardInfo> rewards = Fixtures.TableSheetsFX.MonsterCollectionRewardSheet[row.Level].Rewards;

            Assert.Single(rewards);
            var queryResult = await ExecuteQueryAsync <MonsterCollectionRowType>(
                query,
                source : (row, Fixtures.TableSheetsFX.MonsterCollectionRewardSheet)
                );

            var expected = new Dictionary <string, object>
            {
                ["level"]        = row.Level,
                ["requiredGold"] = row.RequiredGold,
                ["rewards"]      = new List <Dictionary <string, object> >
                {
                    new Dictionary <string, object>
                    {
                        ["itemId"]   = rewards.First().ItemId,
                        ["quantity"] = rewards.First().Quantity,
                    }
                }
            };

            Assert.Equal(expected, queryResult.Data);
        }