Files

78 lines
3.0 KiB
C#
Raw Permalink Normal View History

2020-07-19 13:04:31 +02:00
using System;
using System.Collections.Generic;
2020-12-27 12:39:25 +01:00
using System.Linq;
2020-07-19 13:04:31 +02:00
using System.Threading.Tasks;
2020-09-18 16:07:25 +02:00
using AutoMapper;
2020-12-27 12:39:25 +01:00
using Microsoft.Extensions.Logging;
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
2020-11-17 12:36:02 +01:00
using WotBlitzStatisticsPro.Logic.AccountInformationPipeline.OperationContext;
using WotBlitzStatisticsPro.Logic.Model;
2020-07-19 13:04:31 +02:00
using WotBlitzStatisticsPro.Logic.Pipeline;
2020-09-18 16:07:25 +02:00
using WotBlitzStatisticsPro.WgApiClient;
2020-07-19 13:04:31 +02:00
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
{
2020-11-17 12:36:02 +01:00
public class GetTanksInfoOperation: IOperation<IOperationContext>
2020-07-19 13:04:31 +02:00
{
2020-09-18 16:07:25 +02:00
private readonly IWargamingTanksApiClient _wargamingApi;
private readonly IMapper _mapper;
2020-12-27 12:39:25 +01:00
private readonly ILogger<GetTanksInfoOperation> _logger;
private readonly StatisticsCache _cache;
2020-07-19 13:04:31 +02:00
2020-09-18 16:07:25 +02:00
public GetTanksInfoOperation(
IWargamingTanksApiClient wargamingApi,
2020-12-27 12:39:25 +01:00
IMapper mapper,
ILogger<GetTanksInfoOperation> logger,
StatisticsCache cache)
2020-09-18 16:07:25 +02:00
{
_wargamingApi = wargamingApi;
_mapper = mapper;
2020-12-27 12:39:25 +01:00
_logger = logger;
_cache = cache;
2020-09-18 16:07:25 +02:00
}
2020-12-27 12:39:25 +01:00
public async Task Invoke(IOperationContext context, Func<IOperationContext, Task>? next)
2020-09-18 16:07:25 +02:00
{
2020-11-17 12:36:02 +01:00
var contextData = context.Get<AccountInformationPipelineContextData>();
2020-09-18 16:07:25 +02:00
var cache = _cache.GetTanksData(context.Request.AccountId);
if (cache != null)
{
contextData.Tanks = cache.TankInfo;
contextData.TanksHistory = cache.TankInfoHistory;
_logger.LogInformation($"Account tanks data for account {context.Request.AccountId} got from cache");
if (next != null) await next.Invoke(context);
return;
}
2020-11-17 12:36:02 +01:00
var tanksInfo = await
_wargamingApi.GetPlayerAccountTanksInfo(context.Request.AccountId, context.Request.RealmType, context.Request.RequestLanguage);
2020-12-27 12:39:25 +01:00
if (tanksInfo == null)
{
_logger.LogInformation(
$"Tanks not found for AccountId {context.Request.AccountId} and {context.Request.RealmType} realm");
return;
}
2020-11-17 12:36:02 +01:00
contextData.Tanks = new List<TankInfo>();
contextData.TanksHistory = new Dictionary<long, TankInfoHistory>();
2020-12-27 12:39:25 +01:00
tanksInfo.OrderByDescending(t => t.LastBattleTime).ToList().ForEach(tank =>
{
2020-11-06 19:23:20 +01:00
var tankInfo = new TankInfo(tank.AccountId, tank.TankId);
_mapper.Map(tank, tankInfo);
2020-11-17 12:36:02 +01:00
contextData.Tanks.Add(tankInfo);
2020-11-06 19:23:20 +01:00
var stat = new TankInfoHistory(tank.AccountId, tank.TankId, tank.LastBattleTime);
_mapper.Map(tank.All, stat);
2020-11-17 12:36:02 +01:00
contextData.TanksHistory[stat.TankId] = stat;
});
_cache.SetTanksData(context.Request.AccountId, new TanksDataCache(contextData.Tanks, contextData.TanksHistory));
2020-12-27 12:39:25 +01:00
if (next != null) await next.Invoke(context);
2020-07-19 13:04:31 +02:00
}
}
}