Files

172 lines
6.9 KiB
C#
Raw Permalink Normal View History

2020-11-17 11:01:35 +01:00
using System;
using System.Threading.Tasks;
2020-12-27 17:20:57 +01:00
using Microsoft.Extensions.Logging;
2020-07-19 13:04:31 +02:00
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline;
2020-11-17 12:36:02 +01:00
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
2020-07-19 13:04:31 +02:00
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations;
using WotBlitzStatisticsPro.Logic.Calculations;
2020-07-19 13:04:31 +02:00
using WotBlitzStatisticsPro.Logic.Pipeline;
namespace WotBlitzStatisticsPro.Logic
{
public class WargamingAccounts: IWargamingAccounts
{
private readonly IOperationFactory _operationFactory;
2020-12-27 17:20:57 +01:00
private readonly ILogger<WargamingAccounts> _logger;
2020-07-19 13:04:31 +02:00
2020-12-27 17:20:57 +01:00
public WargamingAccounts(IOperationFactory operationFactory, ILogger<WargamingAccounts> logger)
2020-07-19 13:04:31 +02:00
{
_operationFactory = operationFactory;
2020-12-27 17:20:57 +01:00
_logger = logger;
2020-07-19 13:04:31 +02:00
}
public async Task<AccountInfoResponse> GatherAccountInformation(
RealmType realm,
long accountId,
2020-12-27 17:20:57 +01:00
RequestLanguage requestLanguage)
2020-07-19 13:04:31 +02:00
{
2020-11-17 12:36:02 +01:00
var contextData = new AccountInformationPipelineContextData();
2020-12-27 17:20:57 +01:00
try
2020-12-27 12:39:25 +01:00
{
2020-12-27 17:20:57 +01:00
var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage));
context.AddOrReplace(contextData);
var pipeline = new Pipeline<IOperationContext>(_operationFactory);
pipeline.AddOperation<GetAccountInfoOperation>()
.AddOperation<GetTanksInfoOperation>()
.AddOperation<CalculateStatisticsOperation>()
.AddOperation<BuildAccountInfoResponseOperation>()
;
var firstOperation = pipeline.Build();
if (firstOperation != null)
{
await firstOperation
.Invoke(context, null)
.ConfigureAwait(false);
}
}
catch (Exception e)
{
_logger.LogError(e, "GatherAccountInformation error");
throw;
2020-12-27 12:39:25 +01:00
}
2020-07-19 13:04:31 +02:00
2020-12-27 12:39:25 +01:00
return contextData?.Response ?? new AccountInfoResponse();
2020-10-25 19:05:08 +01:00
}
public async Task<DateTime> GatherAndSaveAccountInformation(
RealmType realm,
long accountId,
RequestLanguage requestLanguage,
string wargamingToken)
2020-10-25 19:05:08 +01:00
{
2020-11-17 12:36:02 +01:00
var contextData = new AccountInformationPipelineContextData();
2020-12-27 17:20:57 +01:00
try
2020-12-27 12:39:25 +01:00
{
2020-07-19 13:04:31 +02:00
var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage, wargamingToken));
2020-12-27 17:20:57 +01:00
context.AddOrReplace(contextData);
var pipeline = new Pipeline<IOperationContext>(_operationFactory);
pipeline.AddOperation<GetAccountInfoOperation>()
.AddOperation<ReadAccountInfoFromDbOperation>()
.AddOperation<CheckLastBattleDateOperation>()
.AddOperation<GetTanksInfoOperation>()
.AddOperation<CalculateStatisticsOperation>()
.AddOperation<SaveAccountAndTanksOperation>()
;
var firstOperation = pipeline.Build();
if (firstOperation != null)
{
await firstOperation
.Invoke(context, null)
.ConfigureAwait(false);
}
}
catch (Exception e)
{
_logger.LogError(e, "GatherAndSaveAccountInformation error");
throw;
}
return contextData?.AccountInfo?.LastBattleTime.ToDateTime() ?? new DateTime(1970,1,1);
2020-07-19 13:04:31 +02:00
}
2020-11-17 11:01:35 +01:00
public async Task<AccountInfoHistoryResponse> GetAccountInfoHistory(RealmType realm, long accountId, DateTime startDate, RequestLanguage requestLanguage)
{
2020-12-23 16:34:12 +01:00
var contextData = new AccountHistoryInformationPipelineContextData(startDate);
2020-12-27 17:20:57 +01:00
try
2020-12-27 12:39:25 +01:00
{
2020-12-27 17:20:57 +01:00
var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage));
context.AddOrReplace(contextData);
var pipeline = new Pipeline<IOperationContext>(_operationFactory);
2020-11-17 11:01:35 +01:00
2020-12-27 17:20:57 +01:00
pipeline.AddOperation<ReadAccountInfoFromDbOperation>()
.AddOperation<ReadAccountInfoHistoryFromDbOperation>()
.AddOperation<CheckIfHistoryIsEmptyOperation>()
.AddOperation<FillOverallStatisticsOperation>()
.AddOperation<FillPeriodStatisticsOperation>()
.AddOperation<FillPeriodDifferenceOperation>()
.AddOperation<FillStatisticsDifferenceOperation>()
.AddOperation<FillAccountInfoHistoryResponse>()
;
var firstOperation = pipeline.Build();
if (firstOperation != null)
{
await firstOperation
.Invoke(context, null)
.ConfigureAwait(false);
}
}
catch (Exception e)
{
_logger.LogError(e, "GetAccountInfoHistory error");
throw;
}
2020-12-27 12:39:25 +01:00
return contextData?.Response ?? new AccountInfoHistoryResponse();
2020-11-17 11:01:35 +01:00
}
2021-01-05 13:04:13 +01:00
public async Task<TankInfoHistoryResponse> GetTankInfoHistory(RealmType realm, long accountId, long tankId, DateTime startDate,
RequestLanguage requestLanguage)
{
var contextData = new TankHistoryInformationContextData(startDate, tankId);
try
{
var context = new OperationContext(new AccountRequest(accountId, realm, requestLanguage));
context.AddOrReplace(contextData);
var pipeline = new Pipeline<IOperationContext>(_operationFactory);
pipeline.AddOperation<ReadTankInfoFromDbOperation>()
.AddOperation<ReadTankHistoryFromDbOperation>()
.AddOperation<CheckIfHistoryIsEmptyOperation>()
.AddOperation<FillOverallStatisticsOperation>()
.AddOperation<FillPeriodStatisticsOperation>()
.AddOperation<FillPeriodDifferenceOperation>()
.AddOperation<FillStatisticsDifferenceOperation>()
.AddOperation<FillTankHistoryResponseOperation>()
;
var firstOperation = pipeline.Build();
if (firstOperation != null)
{
await firstOperation
.Invoke(context, null)
.ConfigureAwait(false);
}
}
catch (Exception e)
{
_logger.LogError(e, "GetAccountInfoHistory error");
throw;
}
return contextData?.Response ?? new TankInfoHistoryResponse();
}
2020-07-19 13:04:31 +02:00
}
}