示例#1
0
 internal void OnBlizzardAccountClaimed(ulong hi, ulong lo, string battleTag)
 => BlizzardAccountClaimed?.Invoke(new BlizzardAccountClaimEventArgs(hi, lo, battleTag));
        public static async Task SyncCollection()
        {
            if (!Config.Instance.SyncCollection || !HSReplayNetOAuth.IsFullyAuthenticated)
            {
                return;
            }
            var collection = await CollectionHelper.GetCollection();

            if (collection == null)
            {
                return;
            }
            var hash    = collection.GetHashCode();
            var hi      = collection.AccountHi;
            var lo      = collection.AccountLo;
            var account = hi + "-" + lo;

            if (Account.Instance.CollectionState.TryGetValue(account, out var state) && state.Hash == hash)
            {
                Log.Debug("Collection ready up-to-date");
                state.Date = DateTime.Now;
                Account.Save();
                CollectionAlreadyUpToDate?.Invoke();
                return;
            }
            await CollectionSyncLimiter.Run(async() =>
            {
                if (!HSReplayNetOAuth.AccountData?.BlizzardAccounts?.Any(x => x.AccountHi == hi && x.AccountLo == lo) ?? true)
                {
                    var response = await HSReplayNetOAuth.ClaimBlizzardAccount(hi, lo, collection.BattleTag);
                    var success  = response == HSReplayNetOAuth.ClaimBlizzardAccountResponse.Success;
                    BlizzardAccountClaimed?.Invoke(success);
                    if (success)
                    {
                        HSReplayNetOAuth.UpdateAccountData().Forget();
                    }
                    else if (response == HSReplayNetOAuth.ClaimBlizzardAccountResponse.TokenAlreadyClaimed)
                    {
                        ErrorManager.AddError("HSReplay.net error",
                                              $"Your blizzard account ({collection.BattleTag}, {account}) is already attached to another"
                                              + " HSReplay.net Account. Please contact us at [email protected]"
                                              + " if this is not correct.");
                        return;
                    }
                    else
                    {
                        ErrorManager.AddError("HSReplay.net error",
                                              $"Could not attach your Blizzard account ({collection.BattleTag}, {account}) to"
                                              + $" HSReplay.net Account ({HSReplayNetOAuth.AccountData?.Username})."
                                              + " Please try again later or contact us at [email protected] if this persists.");
                        return;
                    }
                }
                if (await HSReplayNetOAuth.UpdateCollection(collection))
                {
                    Account.Instance.CollectionState[account] = new Account.SyncState(hash);
                    Account.Save();
                    Log.Debug("Collection synced");
                    CollectionUploaded?.Invoke();
                }
                else
                {
                    ErrorManager.AddError("HSReplay.net error",
                                          "Could not update your collection. Please try again later.\n"
                                          + "If this problem persists please try logging out and back in"
                                          + " under 'options > hsreplay.net > my account'");
                    CollectionUploadError?.Invoke();
                }
            }, () =>
            {
                Log.Debug("Waiting for rate limit...");
                CollectionUploadThrottled?.Invoke();
            });
        }