Esempio 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]);
            }
        }
Esempio n. 2
0
        public void Execute_Throw_InsufficientBalanceException()
        {
            MonsterCollect0 action = new MonsterCollect0
            {
                level = 1,
            };

            Assert.Throws <InsufficientBalanceException>(() => action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = 1,
            }));
        }
Esempio n. 3
0
        public void Execute_Throw_FailedLoadStateException()
        {
            MonsterCollect0 action = new MonsterCollect0
            {
                level           = 1,
                collectionRound = 1,
            };

            Assert.Throws <FailedLoadStateException>(() => action.Execute(new ActionContext
            {
                PreviousStates = new State(),
                Signer         = _signer,
                BlockIndex     = 1,
            }));
        }
Esempio n. 4
0
        public void Execute_Throw_SheetRowNotFoundException()
        {
            int level = 100;

            Assert.False(_tableSheets.MonsterCollectionSheet.Keys.Contains(level));

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

            Assert.Throws <SheetRowNotFoundException>(() => action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = 1,
            }));
        }
Esempio n. 5
0
        public void Execute_Throw_InvalidLevelException(int prevLevel, int level)
        {
            Address collectionAddress = MonsterCollectionState0.DeriveAddress(_signer, 0);
            MonsterCollectionState0 prevMonsterCollectionState = new MonsterCollectionState0(collectionAddress, prevLevel, 0, _tableSheets.MonsterCollectionRewardSheet);

            _initialState = _initialState.SetState(collectionAddress, prevMonsterCollectionState.Serialize());

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

            Assert.Throws <InvalidLevelException>(() => action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = 1,
            }));
        }
Esempio n. 6
0
        public void Execute_Throw_MonsterCollectionExpiredException()
        {
            Address collectionAddress = MonsterCollectionState0.DeriveAddress(_signer, 0);
            MonsterCollectionState0 prevMonsterCollectionState = new MonsterCollectionState0(collectionAddress, 1, 0, _tableSheets.MonsterCollectionRewardSheet);

            Assert.Equal(MonsterCollectionState0.ExpirationIndex, prevMonsterCollectionState.ExpiredBlockIndex);

            _initialState = _initialState.SetState(collectionAddress, prevMonsterCollectionState.Serialize());

            MonsterCollect0 action = new MonsterCollect0
            {
                level           = 2,
                collectionRound = 0,
            };

            Assert.Throws <MonsterCollectionExpiredException>(() => action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = prevMonsterCollectionState.ExpiredBlockIndex + 1,
            }));
        }
Esempio n. 7
0
        public void Rehearsal()
        {
            Address         collectionAddress = MonsterCollectionState0.DeriveAddress(_signer, 1);
            MonsterCollect0 action            = new MonsterCollect0
            {
                level           = 1,
                collectionRound = 1,
            };
            IAccountStateDelta nextState = action.Execute(new ActionContext
            {
                PreviousStates = new State(),
                Signer         = _signer,
                Rehearsal      = true,
            });

            List <Address> updatedAddresses = new List <Address>()
            {
                _signer,
                collectionAddress,
            };

            Assert.Equal(updatedAddresses.ToImmutableHashSet(), nextState.UpdatedAddresses);
        }
Esempio n. 8
0
        public void Execute_Throw_InvalidMonsterCollectionRoundException(int agentCollectionRound, int collectionRound)
        {
            AgentState prevAgentState = _initialState.GetAgentState(_signer);

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

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

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

            Assert.Throws <InvalidMonsterCollectionRoundException>(() => action.Execute(new ActionContext
            {
                PreviousStates = _initialState,
                Signer         = _signer,
                BlockIndex     = 1,
            }));
        }