private static async void Load()
        {
            _loading = true;
            var data = await LoadFromDisk();

            Log.Info($"Loaded from disk: {data}");
            if (data?.IsStale ?? true)
            {
                Log.Info("Cached data was not found or stale. Fetching latest...");
                data = await ApiWrapper.GetAvailableDecks();

                if (data == null)
                {
                    Log.Warn("No data. Can retry in 30 minutes.");
                    data = new DecksData
                    {
                        ClientTimeStamp = DateTime.Now.Subtract(TimeSpan.FromHours(23.5)),
                    };
                }
                await WriteToDisk(data);
            }
            _data = data;
            Log.Info($"Complete: {data}");
            OnLoaded?.Invoke();
            _loading = false;
        }
示例#2
0
        public static async Task <bool> Authenticate(string successUrl = null, string errorUrl = null)
        {
            Log.Info("Authenticating with HSReplay.net...");
            string url;

            try
            {
                url = Client.Value.GetAuthenticationUrl(_requiredScopes, Ports);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(false);
            }
            if (string.IsNullOrEmpty(url))
            {
                Log.Error("Authentication failed, could not create callback listener");
                return(false);
            }
            var callbackTask = Client.Value.ReceiveAuthenticationCallback(successUrl ?? SuccessUrl,
                                                                          errorUrl ?? ErrorUrl);

            if (!Helper.TryOpenUrl(url))
            {
                ErrorManager.AddError("Could not open your browser.",
                                      "Please open the following url in your browser to continue:\n\n" + url, true);
            }
            Log.Info("Waiting for callback...");
            var data = await callbackTask;

            if (data == null)
            {
                Log.Error("Authentication failed, received no data");
                return(false);
            }
            Data.Value.Code        = data.Code;
            Data.Value.RedirectUrl = data.RedirectUrl;
            Data.Value.TokenData   = null;
            Log.Info("Authentication complete");
            await UpdateToken();

            Save();
            Log.Info("Claiming upload token if necessary");
            if (!Account.Instance.TokenClaimed.HasValue)
            {
                await ApiWrapper.UpdateUploadTokenStatus();
            }
            if (Account.Instance.TokenClaimed == false)
            {
                await ClaimUploadToken(Account.Instance.UploadToken);
            }
            Authenticated?.Invoke();
            return(true);
        }
示例#3
0
        public static async Task Logout()
        {
            Serializer.DeleteCacheFile();
            Data.Value.Account            = null;
            Data.Value.Code               = null;
            Data.Value.RedirectUrl        = null;
            Data.Value.TokenData          = null;
            Data.Value.TokenDataCreatedAt = DateTime.MinValue;
            Data.Value.TwitchUsers        = null;
            Save();
            Account.Instance.Reset();
            await ApiWrapper.UpdateUploadTokenStatus();

            LoggedOut?.Invoke();
        }
示例#4
0
        internal static async void UploadPack(int packId, List <Card> cards)
        {
            Log.Info($"New Pack! Id={packId}, Cards=[{string.Join(", ", cards.Select(x => x.Id + (x.PremiumType > 0 ? " (g)" : "")))}]");
            if (Config.Instance.HsReplayUploadPacks == true)
            {
                try
                {
                    var packData = PackDataGenerator.Generate(packId, cards.Select(x => new CardData {
                        CardId = x.Id, Premium = x.PremiumType > 0
                    }));
                    await ApiWrapper.UploadPack(packData);

                    Log.Info("Successfully uploaded pack");
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
        private static async Task <bool> TryUpload(string[] logLines, GameMetaData gameMetaData, GameStats game, bool submitFailure)
        {
            try
            {
                game?.HsReplay.UploadTry();
                Influx.OnGameUpload(game?.HsReplay.UploadTries ?? 1);
                var lines    = logLines.SkipWhile(x => !x.Contains("CREATE_GAME")).ToArray();
                var metaData = UploadMetaDataGenerator.Generate(lines, gameMetaData, game);
                Log.Info("Creating upload request...");
                var uploadRequest = await ApiWrapper.CreateUploadRequest(metaData);

                Log.Info("Upload Id: " + uploadRequest.ShortId);
                await ApiWrapper.UploadLog(uploadRequest, lines);

                Log.Info("Upload complete");
                if (game != null)
                {
                    game.HsReplay.UploadId  = uploadRequest.ShortId;
                    game.HsReplay.ReplayUrl = uploadRequest.ReplayUrl;
                    if (DefaultDeckStats.Instance.DeckStats.Any(x => x.DeckId == game.DeckId))
                    {
                        DefaultDeckStats.Save();
                    }
                    else
                    {
                        DeckStatsList.Save();
                    }
                }
                return(true);
            }
            catch (WebException ex)
            {
                Log.Error(ex);
                if (submitFailure)
                {
                    Influx.OnGameUploadFailed(ex.Status);
                }
                return(false);
            }
        }
        public static async Task UpdateAccount()
        {
            if (HSReplayNetOAuth.IsAuthenticatedForAnything())
            {
                await HSReplayNetOAuth.UpdateAccountData();

                if (string.IsNullOrEmpty(Account.Instance.UploadToken) ||
                    !Account.Instance.TokenClaimed.HasValue ||
                    (!HSReplayNetOAuth.AccountData?.UploadTokens.Contains(Account.Instance.UploadToken) ?? false))
                {
                    await ApiWrapper.UpdateUploadTokenStatus();
                }
                if (Account.Instance.TokenClaimed == false && !string.IsNullOrEmpty(Account.Instance.UploadToken))
                {
                    await HSReplayNetOAuth.ClaimUploadToken(Account.Instance.UploadToken);
                }
            }
            else
            {
                ApiWrapper.UpdateUploadTokenStatus().Forget();
            }
        }