public void SerializeWithDotnetAPI()
        {
            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            action.Execute(new ActionContext()
            {
                PreviousStates = _initialState,
                Signer         = _agent1Address,
                Random         = new TestRandom(),
                Rehearsal      = false,
            });

            var formatter = new BinaryFormatter();

            using var ms = new MemoryStream();
            formatter.Serialize(ms, action);
            ms.Seek(0, SeekOrigin.Begin);

            var deserialized = (RankingBattle7)formatter.Deserialize(ms);

            Assert.Equal(action.PlainValue, deserialized.PlainValue);
        }
        public void ExecuteThrowWeeklyArenaStateNotContainsAvatarAddressException()
        {
            var targetAddress = _avatar2Address;

            var previousWeeklyArenaState = _initialState.GetWeeklyArenaState(_weeklyArenaAddress);

            previousWeeklyArenaState.Remove(targetAddress);

            var previousState = _initialState.SetState(
                _weeklyArenaAddress,
                previousWeeklyArenaState.Serialize());

            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Throws <WeeklyArenaStateNotContainsAvatarAddressException>(() =>
            {
                action.Execute(new ActionContext()
                {
                    PreviousStates = previousState,
                    Signer         = _agent1Address,
                    Random         = new TestRandom(),
                    Rehearsal      = false,
                });
            });
        }
        public void Rehearsal()
        {
            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            var updatedAddresses = new List <Address>()
            {
                _avatar1Address,
                _weeklyArenaAddress,
                _avatar1Address.Derive(LegacyInventoryKey),
                _avatar1Address.Derive(LegacyWorldInformationKey),
                _avatar1Address.Derive(LegacyQuestListKey),
            };

            var state = new State();

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

            Assert.Equal(updatedAddresses.ToImmutableHashSet(), nextState.UpdatedAddresses);
        }
        public void ExecuteThrowNotEnoughClearedStageLevelException()
        {
            var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address);

            previousAvatar1State.worldInformation = new WorldInformation(
                0,
                _tableSheets.WorldSheet,
                false
                );
            var previousState = _initialState.SetState(
                _avatar1Address,
                previousAvatar1State.Serialize());

            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Throws <NotEnoughClearedStageLevelException>(() =>
            {
                action.Execute(new ActionContext()
                {
                    PreviousStates = previousState,
                    Signer         = _agent1Address,
                    Random         = new TestRandom(),
                    Rehearsal      = false,
                });
            });
        }
        public void MultipleEquipmentTest(ItemSubType type, int maxCount)
        {
            var previousAvatarState = _initialState.GetAvatarState(_avatar1Address);
            var maxLevel            = _tableSheets.CharacterLevelSheet.Max(row => row.Value.Level);
            var expRow      = _tableSheets.CharacterLevelSheet[maxLevel];
            var maxLevelExp = expRow.Exp;

            previousAvatarState.level = maxLevel;
            previousAvatarState.exp   = maxLevelExp;

            var weaponRows = _tableSheets
                             .EquipmentItemSheet
                             .Values
                             .Where(r => r.ItemSubType == type)
                             .Take(maxCount + 1);

            var equipments = new List <Guid>();

            foreach (var row in weaponRows)
            {
                var equipment = ItemFactory.CreateItem(
                    _tableSheets.EquipmentItemSheet[row.Id],
                    new TestRandom())
                                as Equipment;

                equipments.Add(equipment.ItemId);
                previousAvatarState.inventory.AddItem(equipment);
            }

            var state = _initialState.SetState(_avatar1Address, previousAvatarState.Serialize());

            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = equipments,
                consumableIds      = new List <Guid>(),
            };

            Assert.Null(action.Result);

            Assert.Throws <DuplicateEquipmentException>(() => action.Execute(new ActionContext
            {
                PreviousStates = state,
                Signer         = _agent1Address,
                Random         = new TestRandom(),
                Rehearsal      = false,
            }));
        }
        public void ExecuteThrowFailedLoadStateException(int caseIndex)
        {
            Address signer        = default;
            Address avatarAddress = default;
            Address enemyAddress  = default;

            switch (caseIndex)
            {
            case 0:
                signer        = new PrivateKey().ToAddress();
                avatarAddress = _avatar1Address;
                enemyAddress  = _avatar2Address;
                break;

            case 1:
                signer        = _agent1Address;
                avatarAddress = _avatar1Address;
                enemyAddress  = new PrivateKey().ToAddress();
                break;
            }

            var action = new RankingBattle7
            {
                avatarAddress      = avatarAddress,
                enemyAddress       = enemyAddress,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Throws <FailedLoadStateException>(() =>
            {
                action.Execute(new ActionContext()
                {
                    PreviousStates = _initialState,
                    Signer         = signer,
                    Random         = new TestRandom(),
                    Rehearsal      = false,
                });
            });
        }
        public void ExecuteThrowNotEnoughWeeklyArenaChallengeCountException()
        {
            var previousAvatarState      = _initialState.GetAvatarState(_avatar1Address);
            var previousWeeklyArenaState = _initialState.GetWeeklyArenaState(_weeklyArenaAddress);

            while (true)
            {
                var arenaInfo = previousWeeklyArenaState.GetArenaInfo(_avatar1Address);
                arenaInfo.UpdateV3(previousAvatarState, arenaInfo, BattleLog.Result.Lose);
                if (arenaInfo.DailyChallengeCount == 0)
                {
                    break;
                }
            }

            var previousState = _initialState.SetState(
                _weeklyArenaAddress,
                previousWeeklyArenaState.Serialize());

            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Throws <NotEnoughWeeklyArenaChallengeCountException>(() =>
            {
                action.Execute(new ActionContext()
                {
                    PreviousStates = previousState,
                    Signer         = _agent1Address,
                    Random         = new TestRandom(),
                    Rehearsal      = false,
                });
            });
        }
        public void ExecuteThrowInvalidAddressException()
        {
            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar1Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid>(),
                equipmentIds       = new List <Guid>(),
                consumableIds      = new List <Guid>(),
            };

            Assert.Throws <InvalidAddressException>(() =>
            {
                action.Execute(new ActionContext()
                {
                    PreviousStates = _initialState,
                    Signer         = _agent1Address,
                    Random         = new TestRandom(),
                    Rehearsal      = false,
                });
            });
        }
        public void Execute(bool isNew, bool avatarBackward, bool enemyBackward)
        {
            var previousWeeklyState  = _initialState.GetWeeklyArenaState(0);
            var previousAvatar1State = _initialState.GetAvatarState(_avatar1Address);

            previousAvatar1State.level = 10;
            var prevScore = previousWeeklyState[_avatar1Address].Score;

            if (isNew)
            {
                previousWeeklyState.Remove(_avatar1Address);
            }

            var previousState = _initialState.SetState(
                _avatar1Address,
                previousAvatar1State.Serialize());

            var itemIds = _tableSheets.WeeklyArenaRewardSheet.Values
                          .Select(r => r.Reward.ItemId)
                          .ToList();

            Assert.All(itemIds, id => Assert.False(previousAvatar1State.inventory.HasItem(id)));

            var row     = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.ATK);
            var costume = (Costume)ItemFactory.CreateItem(
                _tableSheets.ItemSheet[row.CostumeId], new TestRandom());

            costume.equipped = true;
            previousAvatar1State.inventory.AddItem(costume);

            var row2         = _tableSheets.CostumeStatSheet.Values.First(r => r.StatType == StatType.DEF);
            var enemyCostume = (Costume)ItemFactory.CreateItem(
                _tableSheets.ItemSheet[row2.CostumeId], new TestRandom());

            enemyCostume.equipped = true;
            var enemyAvatarState = _initialState.GetAvatarState(_avatar2Address);

            enemyAvatarState.inventory.AddItem(enemyCostume);

            if (avatarBackward)
            {
                previousState = previousState.SetState(_avatar1Address, previousAvatar1State.Serialize());
            }
            else
            {
                previousState = previousState
                                .SetState(_avatar1Address.Derive(LegacyInventoryKey), previousAvatar1State.inventory.Serialize())
                                .SetState(_avatar1Address.Derive(LegacyWorldInformationKey), previousAvatar1State.worldInformation.Serialize())
                                .SetState(_avatar1Address.Derive(LegacyQuestListKey), previousAvatar1State.questList.Serialize())
                                .SetState(_avatar1Address, previousAvatar1State.SerializeV2());
            }

            if (enemyBackward)
            {
                previousState = previousState.SetState(_avatar2Address, enemyAvatarState.Serialize());
            }
            else
            {
                previousState = previousState
                                .SetState(_avatar2Address.Derive(LegacyInventoryKey), enemyAvatarState.inventory.Serialize())
                                .SetState(_avatar2Address.Derive(LegacyWorldInformationKey), enemyAvatarState.worldInformation.Serialize())
                                .SetState(_avatar2Address.Derive(LegacyQuestListKey), enemyAvatarState.questList.Serialize())
                                .SetState(_avatar2Address, enemyAvatarState.SerializeV2());
            }

            var action = new RankingBattle7
            {
                avatarAddress      = _avatar1Address,
                enemyAddress       = _avatar2Address,
                weeklyArenaAddress = _weeklyArenaAddress,
                costumeIds         = new List <Guid> {
                    costume.ItemId
                },
                equipmentIds  = new List <Guid>(),
                consumableIds = new List <Guid>(),
            };

            Assert.Null(action.Result);

            var nextState = action.Execute(new ActionContext()
            {
                PreviousStates = previousState,
                Signer         = _agent1Address,
                Random         = new TestRandom(),
                Rehearsal      = false,
            });

            var nextAvatar1State = nextState.GetAvatarStateV2(_avatar1Address);
            var nextWeeklyState  = nextState.GetWeeklyArenaState(0);

            Assert.Contains(nextAvatar1State.inventory.Materials, i => itemIds.Contains(i.Id));
            Assert.NotNull(action.Result);
            Assert.Contains(typeof(GetReward), action.Result.Select(e => e.GetType()));
            Assert.Equal(BattleLog.Result.Win, action.Result.result);
            Assert.True(nextWeeklyState[_avatar1Address].Score > prevScore);
        }