Example #1
0
        public bool Login(Game game)
        {
            if (game == null || !game.Id.HasValue)
            {
                return(false);
            }
            //Get Mutex
            if (!SafeLoginDict.ContainsKey(game.Id.Value))
            {
                SafeLoginDict.Add(game.Id.Value, new Mutex());
            }
            var mutex = SafeLoginDict[game.Id.Value];

            if (mutex.WaitOne(MutexWaitTime))
            {
                //action
                try
                {
                    //Check DB
                    if (game != null && !String.IsNullOrEmpty(game.Cookies))
                    {
                        if (!game.HadErrorsWhileLogin)
                        {
                            ConnectionCookie = new CookieCollection();
                            game.Cookies.Split(";")
                            .ToList()
                            .Select(x => new Cookie(x.Split("=")[0].Trim(), x.Split("=")[1].Trim()))
                            .ToList()
                            .ForEach(x => ConnectionCookie.Add(x));
                            return(true);
                        }
                        else if (!game.TryToLogIn)
                        {
                            return(false);
                        }
                    }

                    //Check Settings
                    if (LoginUrl == null || LoginPostValues == null)
                    {
                        return(false);
                    }
                    if (_login == null || _password == null)
                    {
                        return(false);
                    }

                    //get Cookies
                    ConnectionCookie = WebConnectHelper.MakePost4Cookies(LoginUrl, LoginPostValues);

                    //Save Changes
                    if (ConnectionCookie != null && ConnectionCookie.Count > 0)
                    {
                        if (game != null)
                        {
                            List <string> cookiesStringList = new List <string>();
                            foreach (Cookie?cookie in ConnectionCookie)
                            {
                                if (cookie != null)
                                {
                                    cookiesStringList.Add($"{cookie.Name}={cookie.Value}");
                                }
                            }
                            string cookiesString = String.Join("; ", cookiesStringList);
                            game.Cookies = cookiesString;

                            game.HadErrorsWhileLogin = false;
                            game.TryToLogIn          = true;
                            GameManager.Update(game);
                        }
                        return(true);
                    }
                    else
                    {
                        if (game != null)
                        {
                            game.Cookies             = null;
                            game.TryToLogIn          = !game.HadErrorsWhileLogin;
                            game.HadErrorsWhileLogin = true;
                            game.Login    = null;
                            game.Password = null;
                            GameManager.Update(game);
                        }
                        return(false);
                    }
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
            else
            {
                return(false);
            }
        }