private static async Task<bool> UpdateAccount(Account account) { if (!AccountOK(account)) return false; string html = string.Empty, url, response = string.Empty; NameValueCollection post; Pages page; LoginResult result; //* flag we are updating account.IsUpdating = true; //* lets try to log in url = GetRegionURL(account.Region, true); if (url == null) { account.IsUpdating = false; CreateWebError(LoginResult.InvalidRegion, account); return false; } try { html = await account.Client.GetHTML(url); } catch (Exception ex) { CreateException(ex, account); return false; } //* we wanted to go to the login page, if it redirected us, we are probably already logged in page = GetPage(html); if (page == Pages.AccountManagement || page == Pages.Root) { //* navigate to account management and read available games try { await GetAccountGames(account, html); } catch (Exception ex) { CreateException(ex, account); return false; } account.LastUpdated = DateTime.Now; account.IsUpdating = false; return true; } //* we got a page we didn't expect; for now just return false if (page != Pages.Login) return false; post = CreateLoginCollection(account, GetLoginData(html, false)); //* now log in try { response = await account.Client.PostData(url, post); } catch (Exception ex) { CreateException(ex, account); return false; } post.Clear(); post = null; result = GetLoginResult(response); if (result == LoginResult.Success) { //* navigate to account management and read available games try { await GetAccountGames(account, response); } catch (Exception ex) { CreateException(ex, account); return false; } account.LastUpdated = DateTime.Now; account.IsUpdating = false; return true; } else if (result == LoginResult.AuthenticationRequired) { //* everything went fine BUT we need to authenticate (either authenticator or sms) InputRequiredEventArgs e = new InputRequiredEventArgs(); e.Account = account; e.HTML = response; e.Type = GetAuthenticationType(response); NotifyInputRequired(e); return true; } else { CreateWebError(result, account); return false; } }
private static void NotifyInputRequired(InputRequiredEventArgs e) { if (InputRequired != null) InputRequired(null, e); }