private StatisticsCollectorOperationContext PrepareOperationContextForProlongAccessToken() { var operationContext = new StatisticsCollectorOperationContext(); operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = new AccountInfo { LastBattleTime = _notChangingBattleLifeTime }, WargamingAccountInfo = new AccountInfo() { LastBattleTime = _notChangingBattleLifeTime } }); operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = new AccountInfo { LastBattleTime = _previousBattleLifeTimeFromDb }, WargamingAccountInfo = new AccountInfo() { LastBattleTime = _newBattleLifeTimeFromWg } }); return(operationContext); }
public Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; var accountsToRemove = new List <SatisticsCollectorAccountOperationContext>(); foreach (var accountOperationContext in operationContext.Accounts) { if (accountOperationContext.WargamingAccountInfo.LastBattleTime <= accountOperationContext.CurrentAccountInfo.LastBattleTime) { accountsToRemove.Add(accountOperationContext); } operationContext.OperationStateMessage += $"Filtering account '{accountOperationContext.CurrentAccountInfo.AccountId}' by last battle '{accountOperationContext.CurrentAccountInfo.LastBattleTime}'; "; } foreach (var acc in accountsToRemove) { operationContext.Accounts.Remove(acc); } if (operationContext.Accounts.Count == 0) { operationContext.OperationState = OperationState.NoAccounts; operationContext.OperationStateMessage += "No accounts for process"; } else { operationContext.OperationStateMessage += $"Remained {operationContext.Accounts.Count} accounts for process"; } return(Task.CompletedTask); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { var tankIds = accountInfo.AccountInfoTanks .Select(t => Convert.ToInt32(t.TankId)) .ToList(); var allAchievements = await _wargamingApiClient.GetAccountTankAchievementsAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.AccessToken, tankIds); foreach (var tank in accountInfo.AccountInfoTanks) { tank.Achievements = allAchievements .Where(a => a.TankId == tank.TankId && a.IsMaxSeries == false) .ToList(); tank.AchievementsMaxSeries = allAchievements .Where(a => a.TankId == tank.TankId && a.IsMaxSeries == true) .ToList(); } operationContext.OperationStateMessage += $"Got {allAchievements.Count} from WG for account {accountInfo.CurrentAccountInfo.AccountId}; "; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext .Accounts .Where(a => a.CurrentAccountInfo.AccessTokenExpiration.HasValue && (a.CurrentAccountInfo.AccessTokenExpiration.Value - DateTime.Now).TotalDays <= _daysBeforeExpire)) { if (accountInfo.CurrentAccountInfo.AccessTokenExpiration.Value < DateTime.Now) { accountInfo.CurrentAccountInfo.AccessToken = string.Empty; _logger.LogWarning($"Account '{accountInfo.CurrentAccountInfo.AccountId}' has expired access token"); return; } var prolongation = await _wargamingApiClient.ProlongateAccount(accountInfo.CurrentAccountInfo.AccessToken); accountInfo.CurrentAccountInfo.AccessToken = prolongation.AccessToken; accountInfo.CurrentAccountInfo.AccessTokenExpiration = prolongation.AccessTokenExpiration; await _accountDataAccessor.SaveProlongedAccountAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.AccessToken, accountInfo.CurrentAccountInfo.AccessTokenExpiration.Value); operationContext.OperationStateMessage += $"Saved new token for account {accountInfo.CurrentAccountInfo.AccountId} until 'accountInfo.CurrentAccountInfo.AccessTokenExpiration.Value'; "; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { var accountInfoFromWg = operationContext.Accounts.Single(); _guestAccountInfo.AccountInfo = new AccountInfoDto { AccountId = accountInfoFromWg.WargamingAccountInfo.AccountId, NickName = accountInfoFromWg.WargamingAccountInfo.NickName, AccountCreatedAt = accountInfoFromWg.WargamingAccountInfo.AccountCreatedAt.Value, LastBattleTime = accountInfoFromWg.WargamingAccountInfo.LastBattleTime.Value }; _guestAccountInfo.AccountInfo.PlayerStatistics = _playerStatDtoMapper.Map <AccountInfoStatistics, PlayerStatDto>(accountInfoFromWg.WargamingAccountInfo.AccountInfoStatistics.Single()); await GetMaxXpAndFragsTanksInfo(); GetAccountMasteryInfo(accountInfoFromWg.AccountInfoTanks); _guestAccountInfo.AccountInfo.PlayerClanInfo = _clanInfoMapper.Map <AccountClanInfo, PlayerClanInfoDto>(accountInfoFromWg.WargamingAccountInfo.AccountClanInfo); var(tanks, aggregated) = GetTanks(accountInfoFromWg.AccountInfoTanks); _guestAccountInfo.Tanks = tanks; _guestAccountInfo.AggregatedAccountInfo = aggregated.OrderBy(t => t.Tier).ToList(); _guestAccountInfo.Achievements = GetAccountAchievements(accountInfoFromWg.WargamingAccountInfo.Achievements); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { accountInfo.WargamingAccountInfo.AccountClanInfo = await _wargamingApiClient.GetAccountClanInfoAsync( accountInfo.CurrentAccountInfo.AccountId); operationContext.OperationStateMessage += $"Got clan info {(accountInfo.WargamingAccountInfo.AccountClanInfo == null ? "null" : accountInfo.WargamingAccountInfo.AccountClanInfo.ClanTag)} for account {accountInfo.CurrentAccountInfo.AccountId}; "; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { var tankTires = await _blitzStaticianDictionary.GetVehiclesTires(); foreach (var account in operationContext.Accounts) { account.WargamingAccountInfo.AccountInfoStatistics.Single() .CalculateMiddleTier(account.AccountInfoTanks, tankTires); } operationContext.OperationStateMessage = $"Calculated middle tier for {operationContext.Accounts.Count} accounts"; }
public Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = new AccountInfo { AccountId = _accountId } }); operationContext.OperationStateMessage = $"Account {_accountId} was set as guest account."; return(Task.CompletedTask); }
public FilterByLastBattleTimeOperationTest() { _notChangingBattleLifeTime = DateTime.Now.AddDays(-7); _previousBattleLifeTimeFromDb = DateTime.Now.AddDays(-2); _newBattleLifeTimeFromWg = DateTime.Now.AddHours(-1); _operationContext = PrepareOperationContextForProlongAccessToken(); var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType <FilterByLastBattleTimeOperation>().AsSelf(); _container = containerBuilder.Build(); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { var accountInfoFromWg = await _wargamingApiClient.GetAccountInfoAllStatisticsAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.AccessToken); accountInfo.WargamingAccountInfo = accountInfoFromWg; operationContext.OperationStateMessage = $"Got account info from WG for account {accountInfo.CurrentAccountInfo.AccountId}; "; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { accountInfo.AccountInfoTanks = await _wargamingApiClient.GetTanksStatisticsAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.AccessToken); operationContext.OperationStateMessage += $"Got {accountInfo.AccountInfoTanks.Count} tanks info from WG for account {accountInfo.CurrentAccountInfo.AccountId}; "; } }
public Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var account in operationContext.Accounts) { var tanksNotPlayedLastSession = account.AccountInfoTanks .Where(t => t.LastBattleTime <= account.CurrentAccountInfo.LastBattleTime) .ToList(); tanksNotPlayedLastSession.ForEach(t => account.AccountInfoTanks.Remove(t)); operationContext.OperationStateMessage += $"For account {account.CurrentAccountInfo.AccountId} filtered tanks by last battle time '{account.CurrentAccountInfo.LastBattleTime}', remained {account.AccountInfoTanks.Count} tanks; "; } return(Task.CompletedTask); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { var accountForProcess = await _accountDataAccessor.GetShortAccountInfo(_accountId); if (accountForProcess == null) { operationContext.OperationState = OperationState.NoDataFound; operationContext.OperationStateMessage = "No accounts found"; } operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = accountForProcess }); operationContext.OperationStateMessage = $"Account {accountForProcess.AccountId} has read from DB."; }
public ProlongAccessTokenOperationTest() { _operationContext = PrepareOperationContextForProlongAccessToken(); _prolongatedAccountInfo = PrepareProlongatedAccountInfo(); var containerBuilder = new ContainerBuilder(); containerBuilder.AddWargamingApiClientMock(_prolongatedAccountInfo) .AddInMemoryDataBase() .AddLoggerMock <ProlongAccessTokenIfNeeded>() .RegisterType <ProlongAccessTokenIfNeeded>().AsSelf(); _container = containerBuilder.Build(); _dbContext = _container.Resolve <BlitzStaticianDbContext>(); PopulateDatabase(_dbContext, _operationContext.Accounts.Single().CurrentAccountInfo); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { var accountInfoAchievements = await _wargamingApiClient.GetAccountAchievementsAsync( accountInfo.CurrentAccountInfo.AccountId); accountInfo.WargamingAccountInfo.Achievements = accountInfoAchievements .Where(a => a.IsMaxSeries == false) .ToList(); accountInfo.WargamingAccountInfo.AchievementsMaxSeries = accountInfoAchievements .Where(a => a.IsMaxSeries == true) .ToList(); operationContext.OperationStateMessage += $"Got {accountInfoAchievements.Count} achievements for account {accountInfo.CurrentAccountInfo.AccountId}; "; } }
private static StatisticsCollectorOperationContext PrepareOperationContextForProlongAccessToken() { var rnd = new Random(); var operationContext = new StatisticsCollectorOperationContext(); operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = new AccountInfo { AccountId = rnd.Next(), AccessToken = "OldAccessToken", AccessTokenExpiration = DateTime.Now.AddDays(1) } }); return(operationContext); }
public async Task TestProlongExpiredAccessToken() { var operationContextWithExpiredAccessToken = new StatisticsCollectorOperationContext(); operationContextWithExpiredAccessToken.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = new AccountInfo { AccessToken = "ExpiredToken", AccessTokenExpiration = DateTime.Now.AddDays(-1) } }); var prolongAccessTokenOperation = _container.Resolve <ProlongAccessTokenIfNeeded>(); await prolongAccessTokenOperation.Execute(operationContextWithExpiredAccessToken); Assert.Equal(string.Empty, operationContextWithExpiredAccessToken.Accounts.Single().CurrentAccountInfo.AccessToken); }
public Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var account in operationContext.Accounts) { account.CurrentAccountInfo.AccountCreatedAt = account.WargamingAccountInfo.AccountCreatedAt; account.CurrentAccountInfo.LastBattleTime = account.WargamingAccountInfo.LastBattleTime; account.CurrentAccountInfo.AccountInfoStatistics = account.WargamingAccountInfo.AccountInfoStatistics; account.CurrentAccountInfo.AccountClanInfo = account.WargamingAccountInfo.AccountClanInfo; if (account.CurrentAccountInfo.AccountClanInfo != null) { account.CurrentAccountInfo.AccountClanInfo.AccountId = account.CurrentAccountInfo.AccountId; } account.CurrentAccountInfo.Achievements = account.WargamingAccountInfo.Achievements; account.CurrentAccountInfo.AchievementsMaxSeries = account.WargamingAccountInfo.AchievementsMaxSeries; operationContext.OperationStateMessage += $"Copied WG info to current account info for account {account.CurrentAccountInfo.AccountId}; "; } return(Task.CompletedTask); }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { var tankTires = await _blitzStaticianDictionary.GetVehiclesTires(); foreach (var account in operationContext.Accounts) { account.WargamingAccountInfo.AccountInfoStatistics .Single() .CalculateWn7(); account.AccountInfoTanks.ForEach(t => { if (tankTires.ContainsKey(t.TankId)) { t.CalculateWn7(tankTires[t.TankId]); } }); } operationContext.OperationStateMessage = $"Calculated Wn7 for {operationContext.Accounts.Count} accounts and their tanks"; }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { var accountsForProcess = await _accountDataAccessor.GetAllAccountsAsync(); foreach (var accountForProcess in accountsForProcess) { operationContext.Accounts.Add(new SatisticsCollectorAccountOperationContext { CurrentAccountInfo = accountForProcess }); } if (operationContext.Accounts.Count == 0) { operationContext.OperationState = OperationState.NoDataFound; operationContext.OperationStateMessage = "No accounts found"; } else { operationContext.OperationStateMessage = $"Found in DB {accountsForProcess.Count} acconts for process"; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { operationContext.OperationStateMessage = string.Empty; foreach (var accountInfo in operationContext.Accounts) { if (string.IsNullOrEmpty(accountInfo.CurrentAccountInfo.AccessToken)) { continue; } var allTanksByAccount = await _accountDataAccessor.GetActualTanksAsync( accountInfo.CurrentAccountInfo.AccountId); var tanksBeforLastSession = accountInfo.AccountInfoTanks .Where(t => t.LastBattleTime <= accountInfo.CurrentAccountInfo.LastBattleTime) .ToList(); var changedGarageInfoList = new List <AccountTankStatistics>(); foreach (var tank in tanksBeforLastSession) { var tankWithChangedInGarage = allTanksByAccount .FirstOrDefault(t => t.TankId == tank.TankId && t.AccountId == tank.AccountId && t.InGarage != tank.InGarage); if (tankWithChangedInGarage != null) { tankWithChangedInGarage.InGarage = tank.InGarage; tankWithChangedInGarage.InGarageUpdated = tank.InGarageUpdated; changedGarageInfoList.Add(tankWithChangedInGarage); } } if (changedGarageInfoList.Count > 0) { await _accountDataAccessor.UpdateInnGarageInfoAsync(changedGarageInfoList); operationContext.OperationStateMessage += "Updated {changedGarageInfoList.Count} tanks in garage info for account {accountInfo.CurrentAccountInfo.AccountId}; "; } } }
public async Task Collect(IStatisticsCollector collector) { var operationContext = new StatisticsCollectorOperationContext(); using (collector) { for (int i = 0; i < collector.OperationsCount; i++) { var operation = collector.GetOperation(i); await operation.Execute(operationContext); if (operationContext.OperationState != OperationState.Ok) { _logger.LogWarning( $"Operation '{operation.GetType()}' was terminate because of its state '{operationContext.OperationState}': {operationContext.OperationStateMessage}"); break; } else { _logger.LogInformation($"Operation '{operation.GetType()}'; done with message '{operationContext.OperationStateMessage}'"); } } } }
public async Task CreateClanHistoryOperationTest( long accountId, int databaseClanId, int wgClanId, string databasePlayerRole, string wgPlayerRole, bool historyMustBeCreated) { var operation = _container.Resolve <CreateAccountClanHistoryOperation>(); // FillingDatabase if (databaseClanId > 0) { var clanInfo = new AccountClanInfo { AccountId = accountId, ClanId = databaseClanId, PlayerRole = databasePlayerRole }; await _dbContext.AccountClanInfo.AddAsync(clanInfo); await _dbContext.SaveChangesAsync(); _dbContext.Entry(clanInfo).State = EntityState.Detached; } var operationContext = new StatisticsCollectorOperationContext { Accounts = new List <SatisticsCollectorAccountOperationContext> { new SatisticsCollectorAccountOperationContext { WargamingAccountInfo = new AccountInfo { AccountId = accountId, AccountClanInfo = new AccountClanInfo { ClanId = wgClanId, PlayerRole = wgPlayerRole } }, CurrentAccountInfo = new AccountInfo { AccountId = accountId } } } }; await operation.Execute(operationContext); var history = operationContext.Accounts.Single().AccountClanHistory; if (historyMustBeCreated) { Assert.NotNull(history); Assert.Equal(wgClanId, history.ClanId ?? 0); Assert.Equal(wgPlayerRole, history.PlayerRole); } else { Assert.Null(history); } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { foreach (var accountInfo in operationContext.Accounts) { using (var transaction = await _accountDataAccessor.OpenTransactionAsync()) { // Save account lastBattle await _accountDataAccessor.SaveLastBattleInfoAsync(accountInfo.CurrentAccountInfo); // Save account statistics await _accountDataAccessor.SaveAccountStatisticsAsync(accountInfo .CurrentAccountInfo .AccountInfoStatistics .Single()); // Merge AccountInfoStatistics.FragsList await _accountDataAccessor.MergeFragsAsync(accountInfo .CurrentAccountInfo .AccountInfoStatistics .Single() .FragsList); // Save account clan info await _accountDataAccessor.SaveAccountClanInfoAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.AccountClanInfo); // Save account clan history if (accountInfo.AccountClanHistory != null) { await _accountDataAccessor.SaveAccountClanHistoryAsync(accountInfo.AccountClanHistory); } // Save account acievements await _accountDataAccessor.MergeAccountAchievementsAsync( accountInfo.CurrentAccountInfo.AccountId, accountInfo.CurrentAccountInfo.Achievements, accountInfo.CurrentAccountInfo.AchievementsMaxSeries); // Save tanks statistics await _accountDataAccessor.SaveTankStatisticsAsync(accountInfo.AccountInfoTanks); // Save PresentTanksList var prestTanks = new List <PresentAccountTanks>(); accountInfo.AccountInfoTanks .ForEach(t => prestTanks.Add(new PresentAccountTanks { AccountId = t.AccountId, TankId = t.TankId, AccountTankStatisticId = t.AccountTankStatisticId })); await _accountDataAccessor.MergePresentAccountTanksInfoAsync(prestTanks); // Save tanks achievements await _accountDataAccessor.MergeAccountInfoTankAchievementsAsync (accountInfo.AccountInfoTanks); // Save frags by allTanks using MergeFragsAsync var tankFrags = new List <FragListItem>(); foreach (var tank in accountInfo.AccountInfoTanks.Where(t => t.FragsList != null)) { tankFrags.AddRange(tank.FragsList); } await _accountDataAccessor.MergeFragsAsync(tankFrags); transaction.Commit(); } operationContext.OperationStateMessage = $"Saved all info for {operationContext.Accounts.Count} accounts"; } }
public async Task Execute(StatisticsCollectorOperationContext operationContext) { foreach (var accountInfo in operationContext.Accounts) { accountInfo.AccountClanHistory = null; var existingAccountClanInfo = await _clanInfoDataAccessor .GetAccountClanAsync(accountInfo.CurrentAccountInfo.AccountId); operationContext.OperationStateMessage = $"For account {accountInfo.CurrentAccountInfo.AccountId} existingAccountClanId {(existingAccountClanInfo == null ? "null" : existingAccountClanInfo.ClanId.ToString())}; "; operationContext.OperationStateMessage += $"WargamingAccountInfo.ClanId {(accountInfo.WargamingAccountInfo.AccountClanInfo == null ? "null" : accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId.ToString())}; "; if (existingAccountClanInfo == null && (accountInfo.WargamingAccountInfo.AccountClanInfo == null || accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId == 0)) { operationContext.OperationStateMessage += "Notning changes"; return; } operationContext.OperationStateMessage += "Adding clan history operation"; if (existingAccountClanInfo == null && accountInfo.WargamingAccountInfo.AccountClanInfo != null && accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId != 0) { accountInfo.AccountClanHistory = new Model.AccountClanHistory { AccountId = accountInfo.CurrentAccountInfo.AccountId, ClanId = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId, ClanName = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanName, ClanTag = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanTag, PlayerJoinedAt = accountInfo.WargamingAccountInfo.AccountClanInfo.PlayerJoinedAt, PlayerRole = accountInfo.WargamingAccountInfo.AccountClanInfo.PlayerRole }; return; } if (existingAccountClanInfo != null && (accountInfo.WargamingAccountInfo.AccountClanInfo == null || accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId == 0)) { accountInfo.AccountClanHistory = new Model.AccountClanHistory { AccountId = accountInfo.CurrentAccountInfo.AccountId, ClanId = null, ClanName = null, ClanTag = null, PlayerJoinedAt = DateTime.Now, PlayerRole = null }; return; } if (existingAccountClanInfo != null && accountInfo.WargamingAccountInfo.AccountClanInfo != null && accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId != 0 && (existingAccountClanInfo.ClanId != accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId || existingAccountClanInfo.PlayerRole != accountInfo.WargamingAccountInfo.AccountClanInfo.PlayerRole)) { accountInfo.AccountClanHistory = new Model.AccountClanHistory { AccountId = accountInfo.CurrentAccountInfo.AccountId, ClanId = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanId, ClanName = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanName, ClanTag = accountInfo.WargamingAccountInfo.AccountClanInfo.ClanTag, PlayerJoinedAt = accountInfo.WargamingAccountInfo.AccountClanInfo.PlayerJoinedAt, PlayerRole = accountInfo.WargamingAccountInfo.AccountClanInfo.PlayerRole }; } } }