public void UpdatePlayerMatchResult_ValidData_NormalResponse(int accountId, int place, int matchId)
        {
            //Arrange
            var warship = new Warship {
                Id = 1, AccountId = accountId, CurrentSkinTypeId = SkinTypeEnum.Hare, WarshipTypeId = WarshipTypeEnum.Hare
            };
            var account = new Account {
                Id = accountId, ServiceId = "", Username = "", Warships = new List <Warship> {
                    warship
                }
            };
            var matchResult = new MatchResult {
                Id = 1, MatchId = matchId, Warship = warship
            };
            var match = new DataLayer.Tables.Match {
                Id = matchId, GameServerIp = "", MatchResults = new List <MatchResult> {
                    matchResult
                }
            };

            DbContext.Accounts.Add(account);
            DbContext.Matches.Add(match);
            DbContext.SaveChanges();

            var service = GetSetUpService();

            //Act
            var result = service.UpdatePlayerMatchResultInDbAsync(accountId, place, matchId).Result;

            //Assert
            Assert.IsTrue(result, "Method returned false.");
            Assert.IsTrue(matchResult.IsFinished, "Match result was not marked as finished.");
            Assert.AreEqual(place, matchResult.PlaceInMatch, "Incorrect place in match.");
            Assert.AreEqual(accountId, matchResult.Transaction.AccountId, "Incorrect account id.");
        }
        public void FinishMatchAndWriteToDb_ValidData_NormalResponse()
        {
            //Arrange
            var match = new DataLayer.Tables.Match {
                Id = 1, GameServerIp = "", MatchResults = new List <MatchResult>(10)
            };

            for (var i = 1; i <= 10; i++)
            {
                var account = new Account {
                    Id = i, ServiceId = i.ToString(), Username = i.ToString()
                };
                var warship = new Warship {
                    Id = i, Account = account, CurrentSkinTypeId = SkinTypeEnum.Hare, WarshipTypeId = WarshipTypeEnum.Hare
                };
                match.MatchResults.Add(new MatchResult {
                    Id = i, Match = match, Warship = warship
                });
            }
            DbContext.Matches.Add(match);
            DbContext.SaveChanges();

            var service = GetSetUpService();

            //Act
            service.FinishMatchAndWriteToDbAsync(match.Id).Wait();

            //Assert
            Assert.IsTrue(match.MatchResults.All(r => r.IsFinished), "Match was not finished.");
        }
        public void UpdatePlayerMatchResult_WrongData_Exception(int accountId, int place, int matchId)
        {
            //Arrange
            var warship = new Warship {
                Id = 1, AccountId = 1, CurrentSkinTypeId = SkinTypeEnum.Hare, WarshipTypeId = WarshipTypeEnum.Hare
            };
            var account = new Account {
                Id = 1, ServiceId = "", Username = "", Warships = new List <Warship> {
                    warship
                }
            };
            var matchResult = new MatchResult {
                Id = 1, MatchId = 1, Warship = warship
            };
            var match = new DataLayer.Tables.Match {
                Id = 1, GameServerIp = "", MatchResults = new List <MatchResult> {
                    matchResult
                }
            };

            DbContext.Accounts.Add(account);
            DbContext.Matches.Add(match);
            DbContext.SaveChanges();

            var service = GetSetUpService();

            //Act
            service.UpdatePlayerMatchResultInDbAsync(accountId, place, matchId).Wait();

            //Assert
            Assert.Fail("Exception was not raised.");
        }
        public void UpdatePlayerMatchResult_RewardCalculations_NormalResponse(int lootboxReward, int softCurrencyReward, int ratingReward)
        {
            //Arrange
            var warship = new Warship {
                Id = 1, AccountId = 1, CurrentSkinTypeId = SkinTypeEnum.Hare, WarshipTypeId = WarshipTypeEnum.Hare
            };
            var account = new Account {
                Id = 1, ServiceId = "", Username = "", Warships = new List <Warship> {
                    warship
                }
            };
            var matchResult = new MatchResult {
                Id = 1, MatchId = 1, Warship = warship
            };
            var match = new DataLayer.Tables.Match {
                Id = 1, GameServerIp = "", MatchResults = new List <MatchResult> {
                    matchResult
                }
            };

            DbContext.Accounts.Add(account);
            DbContext.Matches.Add(match);
            DbContext.SaveChanges();

            var service = GetSetUpService(lootboxReward, softCurrencyReward, ratingReward);

            //Act
            var result = service.UpdatePlayerMatchResultInDbAsync(1, 1, 1).Result;

            //Assert
            var transaction = matchResult.Transaction;

            Assert.IsTrue(result, "Method returned false.");
            Assert.IsFalse(transaction.WasShown, "Transaction was shown.");

            var increments = transaction.Increments;
            var decrements = transaction.Decrements;

            Assert.IsTrue(increments.All(i => i.MatchRewardTypeId == MatchRewardTypeEnum.RankingReward), "Incorrect reward type.");

            CheckReward(lootboxReward, IncrementTypeEnum.LootboxPoints, DecrementTypeEnum.LootboxPoints, "lootbox");
            CheckReward(softCurrencyReward, IncrementTypeEnum.SoftCurrency, DecrementTypeEnum.SoftCurrency, "soft currency");
            CheckReward(ratingReward, IncrementTypeEnum.WarshipRating, DecrementTypeEnum.WarshipRating, "rating", true);

            void CheckReward(int expectedReward, IncrementTypeEnum incrementType, DecrementTypeEnum decrementType, string rewardName, bool canHaveDecrement = false)
            {
                var increment = increments.SingleOrDefault(i => i.IncrementTypeId == incrementType);
                var decrement = decrements.SingleOrDefault(d => d.DecrementTypeId == decrementType);

                // ReSharper disable PossibleNullReferenceException
                if (expectedReward > 0)
                {
                    Assert.AreEqual(expectedReward, increment.Amount, $"Incorrect {rewardName} increment.");
                    Assert.IsNull(decrement, $"The {rewardName} decrement was not null.");
                }
                else if (expectedReward < 0 && canHaveDecrement)
                {
                    Assert.IsNull(increment, $"The {rewardName} increment was not null.");
                    Assert.AreEqual(-expectedReward, decrement.Amount, $"Incorrect {rewardName} decrement.");
                }
                else
                {
                    Assert.IsNull(increment, $"The {rewardName} increment was not null.");
                    Assert.IsNull(decrement, $"The {rewardName} decrement was not null.");
                }
                // ReSharper restore PossibleNullReferenceException
            }
        }