private async Task WaitInQueue() { int id = -1; int current = -1; using (IEnumerator <Ticker> enumerator = AuthResult.Tickers.Where(ticker => ticker.Node == AuthResult.Node).GetEnumerator()) { if (enumerator.MoveNext()) { Ticker current1 = enumerator.Current; id = current1.Id; current = current1.Current; } } int cycleNum = 1; double averageMillisPerCycle = 0.0; while (id - current > 0) { DateTime cycleStartTime = DateTime.Now; HttpResponseMessage httpResponseMessage = await _httpClient.GetAsync(_tickerUrl + AuthResult.Champ).ConfigureAwait(false); if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { try { current = int.Parse((string)JsonConvert.DeserializeObject <Dictionary <string, object> >(await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false))[AuthResult.Node.ToString()], NumberStyles.HexNumber); int num = Math.Max(0, id - current); if (num == 0) { break; } OnLoginQueueUpdate?.Invoke(num); OnUpdateStatusMessage?.Invoke(this, string.Format("In login queue at position {0}", num)); } catch (Exception ex) { Tools.Log(ex.StackTrace); } await Task.Delay(this.AuthResult.Delay).ConfigureAwait(false); } double totalMilliseconds = (DateTime.Now - cycleStartTime).TotalMilliseconds; averageMillisPerCycle = averageMillisPerCycle > 0.0 ? (averageMillisPerCycle + totalMilliseconds) / 2.0 : totalMilliseconds; if (cycleNum == 2) { double num = averageMillisPerCycle / (1 / AuthResult.Rate * 60); if ((int)(averageMillisPerCycle * (num / (1.0 - num)) * ((1.0 + Math.Pow(1.333, 2.0)) / 2.0) / 1000.0) > this.TimeoutSeconds) { throw new TimeoutException(); } } ++cycleNum; } }
public async Task <bool> GetAuthToken() { Stopwatch sw = new Stopwatch(); sw.Start(); int retriesRemaining = AuthRetries; try { while (sw.ElapsedMilliseconds / 1000L <= TimeoutSeconds) { int num = 0; try { AuthResult = await GetAuthResult().ConfigureAwait(false); User = AuthResult.User; Token = AuthResult.Lqt.PartnerToken; //await GetToken(); //this.UserId = this.AuthResult.Lqt.AccountId; } catch (IpBannedException ex) { Tools.Log(ex.StackTrace); OnAuthFailed?.Invoke("Your IP has been banned due to too many requests."); return(false); } catch (JsonReaderException ex) { Tools.Log(ex.StackTrace); num = 1; } catch (Exception ex) { Tools.Log(ex.StackTrace); Console.WriteLine(ex.StackTrace); } if (num == 1) { await Task.Delay(1000).ConfigureAwait(false); } else if (this.AuthResult.Reason == null && !this._useGarena) { if (retriesRemaining > 0) { --retriesRemaining; } else { OnAuthFailed?.Invoke("Unable to get Auth Token."); return(false); } } else if (this._useGarena) { if (this.AuthResult.Status == "LOGIN") { this.AuthToken = this.AuthResult.Lqt.ToString(); return(true); } await Task.Delay(1000).ConfigureAwait(false); } else { string reason = this.AuthResult.Reason; if (!(reason == "attempt_rate_too_fast")) { if (!(reason == "invalid_credentials")) { if (!(reason == "account_banned")) { if (!(reason == "account_transferred")) { if (!(reason == "account_inactive")) { if (reason == "server_full") { OnAuthFailed?.Invoke("Server is full. Try again later."); return(false); } if (AuthResult.Status == "QUEUE" && this.AuthResult.Tickers != null) { await WaitInQueue().ConfigureAwait(false); AuthResult.Lqt.Resources = null; AuthResult.Lqt.Other = null; HttpResponseMessage httpResponseMessage = await _httpClient.PostAsync(_tokenUrl, AuthResult.Lqt.ToString(), true).ConfigureAwait(false); if (httpResponseMessage.StatusCode == HttpStatusCode.OK) { AuthToken = JsonConvert.DeserializeObject <AuthResult>(await httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false)).Lqt.ToString(); return(true); } } if (AuthResult.Status == "LOGIN") { AuthToken = AuthResult.Lqt.ToString(); return(true); } await Task.Delay(1000).ConfigureAwait(false); } else { OnAuthFailed?.Invoke("Account currently inactive."); return(false); } } else { OnAuthFailed?.Invoke(string.Format("Account transferred {0}", AuthResult.Destination).Trim()); return(false); } } else { DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddMilliseconds(AuthResult.Banned); OnAuthFailed?.Invoke(string.Format("Account banned {0}", dateTime.ToString("d", (IFormatProvider)CultureInfo.CurrentCulture))); return(false); } } else { OnAuthFailed?.Invoke("Incorrect username or password."); return(false); } } else { OnUpdateStatusMessage?.Invoke(this, string.Format("Login rate too fast. Waiting {0} s.", AuthResult.RetryWait)); await Task.Delay(TimeSpan.FromSeconds(AuthResult.RetryWait)).ConfigureAwait(false); sw.Restart(); } } } sw.Stop(); throw new TimeoutException(); } catch (TimeoutException ex) { Tools.Log(ex.StackTrace); OnAuthFailed?.Invoke("Timeout: queue time too long."); } finally { sw.Stop(); } return(false); }