public bool Check(string answer)
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();


            int flagID = context.Games
                         .Where(m => m.PlayerID == Player.Id && m.IsAnswered == false)
                         .Select(m => m.FlagID)
                         .FirstOrDefault();

            if (flagID > 0)
            {
                GivenFlag = context.Flags
                            .Where(m => m.Id == flagID)
                            .FirstOrDefault();

                bool result = false;
                if (answer == GivenFlag.Country)
                {
                    result = true;
                }

                UpdateDB(result);

                return(result);
            }
            else
            {
                return(false);
            }
        }
        public string GetImageURL()
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            int[] unplayed = GetUnplayedFlagsIds();


            if (unplayed.Length > 0)
            {
                Random rnd          = new Random();
                int    r            = rnd.Next(unplayed.Length);
                int    image_number = unplayed[r];

                GivenFlag = context.Flags
                            .Where(m => m.Id == image_number)
                            .Select(m => new Flag
                {
                    Id        = m.Id,
                    Number    = m.Number,
                    ImageName = m.ImageName,
                    Country   = m.Country,
                })
                            .FirstOrDefault();

                return(GivenFlag.ImageName);
            }
            else
            {
                return(null);
            }
        }
        public Answer(int tgId)
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            Player = context.Players
                     .Where(m => m.TGId == tgId)
                     .FirstOrDefault();
        }
        private void SaveToDB()
        {
            CurrentGame = new Game
            {
                PlayerID   = Player.Id,
                FlagID     = GivenFlag.Id,
                IsAnswered = false
            };
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            context.Games.Add(CurrentGame);
            context.SaveChanges();
        }
        public static bool Check(int tgId)
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            var user = context.Players
                       .Where(m => m.TGId == tgId)
                       .FirstOrDefault();

            if (user != null)
            {
                return(true);
            }

            return(false);
        }
        public bool ProcessLoggining()
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            var player = new Player
            {
                TGId      = TGId,
                FirstName = FirstName,
                LastName  = LastName,
                UserName  = UserName
            };

            context.Players.Add(player);
            context.SaveChanges();
            return(true);
        }
        private void SetAllGamesPlayed()
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            //set all games played
            var unplayedGames = context.Games.Where(m => m.PlayerID == Player.Id && m.IsAnswered == false);

            if (unplayedGames.Any())
            {
                foreach (Game game in unplayedGames)
                {
                    game.IsAnswered = true;
                }
                context.SaveChanges();
            }
            ;
        }
        public bool IsGameExist()
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();


            int flagID = context.Games
                         .Where(m => m.PlayerID == Player.Id && m.IsAnswered == false)
                         .Select(m => m.FlagID)
                         .FirstOrDefault();

            if (flagID > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public static string GetTop5()
        {
            string message;
            var    context = new LibraryDesignTimeDbContextFactory()
                             .CreateDbContext();

            var result = context.Games
                         .GroupBy(m => m.PlayerID)
                         .Select(m => new
            {
                Key     = m.Key,
                Total   = m.Count(),
                Win     = m.Sum(c => c.IsWin),
                Persent = m.Sum(c => c.IsWin) * 100 / m.Count()
            })
                         .OrderByDescending(m => m.Persent)
                         .Take(5)
            ;
            var list = result.Join(context.Players,
                                   p => p.Key,
                                   c => c.Id,
                                   (p, c) => new StatisticsDTO
            {
                PlayerName = c.FirstName + " " + c.LastName,
                Total      = p.Total,
                Win        = p.Win,
                Persent    = p.Persent
            })
                       .ToList();
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0,-3}{1,-25}{2,-8}{3,-8}{4,-8}\n", "#", "Игрок", "Всего", "Побед", "Проц.");
            foreach (StatisticsDTO p in list)
            {
                sb.AppendFormat("{0,-3}{1,-25}{2,-8}{3,-8}{4,-8:F0}\n", "\U0001F6A9", p.PlayerName, p.Total, p.Win, p.Persent);
            }

            message = sb.ToString();
            return(message);
        }
        protected void UpdateDB(bool result)
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            Game game = context.Games
                        .Where(m => m.PlayerID == Player.Id && m.IsAnswered == false)
                        .FirstOrDefault();

            switch (result)
            {
            case true:
                game.IsWin = 1;
                break;

            case false:
                game.IsWin = 0;
                break;
            }
            game.IsAnswered = true;
            context.SaveChanges();
        }
        private string[] GetVariants()
        {
            string[] variants = new string[4];
            string[] result   = new string[4];

            int[] nums = new int[4];

            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            nums[0] = GivenFlag.Number;

            Random rnd = new Random();


            for (int i = 1; i < 4; i++)
            {
                int value = rnd.Next(1, 195);
                while (Array.Exists(nums, element => element == value))
                {
                    value = rnd.Next(1, 195);
                }
                nums[i] = value;
            }

            for (int i = 0; i < 4; i++)
            {
                variants[i] = context.Flags
                              .Where(m => m.Number == nums[i])
                              .Select(m => m.Country)
                              .FirstOrDefault();
            }

            result = variants.OrderBy(x => rnd.Next()).ToArray();

            return(result);
        }
        private int[] GetUnplayedFlagsIds()
        {
            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();

            List <int> userfalgs = context.Games
                                   .Where(m => m.PlayerID == Player.Id)
                                   .Select(m =>
                                           m.FlagID
                                           )
                                   .ToList()
            ;

            List <int> allflags = context.Flags
                                  .Select(m =>
                                          m.Id
                                          )
                                  .ToList()
            ;

            int[] uplayed = allflags.Except(userfalgs).ToArray();

            return(uplayed);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("program start: populate database");
            Console.ReadKey();

            var context = new LibraryDesignTimeDbContextFactory()
                          .CreateDbContext();
            var firstDestination = new Destination
            {
                Name     = "Florence",
                Country  = "Italy",
                Packages = new List <Package>()
                {
                    new Package
                    {
                        Name = "Summer in Florence",
                        StartValidityDate = new DateTime(2019, 6, 1),
                        EndValidityDate   = new DateTime(2019, 10, 1),
                        DuratioInDays     = 7,
                        Price             = 1000
                    },
                    new Package
                    {
                        Name = "Winter in Florence",
                        StartValidityDate = new DateTime(2019, 12, 1),
                        EndValidityDate   = new DateTime(2020, 2, 1),
                        DuratioInDays     = 7,
                        Price             = 500
                    }
                }
            };

            context.Destinations.Add(firstDestination);
            context.SaveChanges();
            Console.WriteLine(
                "DB populated: first destination id is " +
                firstDestination.Id);
            Console.ReadKey();

            var toModify = context.Destinations
                           .Where(m => m.Name == "Florence")
                           .Include(m => m.Packages)
                           .FirstOrDefault();

            toModify.Description =
                "Florence is a famous historical Italian town";
            foreach (var package in toModify.Packages)
            {
                package.Price = package.Price * 1.1m;
            }
            context.SaveChanges();

            var verifyChanges = context.Destinations
                                .Where(m => m.Name == "Florence")
                                .FirstOrDefault();

            Console.WriteLine(
                "New Florence description: " +
                verifyChanges.Description);
            Console.ReadKey();
            var period = new DateTime(2019, 8, 10);
            var list   = context.Packages
                         .Where(m => period >= m.StartValidityDate &&
                                period <= m.EndValidityDate)
                         .Select(m => new PackagesListDTO
            {
                StartValidityDate = m.StartValidityDate,
                EndValidityDate   = m.EndValidityDate,
                Name            = m.Name,
                DuratioInDays   = m.DuratioInDays,
                Id              = m.Id,
                Price           = m.Price,
                DestinationName = m.MyDestination.Name,
                DestinationId   = m.DestinationId
            })
                         .ToList();

            foreach (var result in list)
            {
                Console.WriteLine(result.ToString());
            }
            Console.ReadKey();
        }