Account info DTO

This commit is contained in:
Mikhail Shabanov committed 2020-09-30 19:30:36 +02:00
1 parent b343e36e0d
commit 5e0369511f
16 files changed
+557 -35

No files matched your search

@@ -29,5 +29,7 @@ namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline
public Dictionary<long, TankInfoHistory> TanksHistory { get; set; }
public AccountInfoResponse Response { get; set; }
}
}
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess;
using WotBlitzStatisticsPro.DataAccess.Model;
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
using WotBlitzStatisticsPro.Logic.Pipeline;
namespace WotBlitzStatisticsPro.Logic.AccountInformationPipeline.Operations
{
public class BuildAccountInfoResponseOperation : IOperation<AccountInformationPipelineContext>
{
private readonly IMapper _mapper;
private readonly IDictionariesDataAccessor _dictionariesDataAccessor;
public BuildAccountInfoResponseOperation(IMapper mapper, IDictionariesDataAccessor dictionariesDataAccessor)
{
_mapper = mapper;
_dictionariesDataAccessor = dictionariesDataAccessor;
}
public async Task Invoke(AccountInformationPipelineContext context, Func<AccountInformationPipelineContext, Task> next)
{
context.Response = _mapper.Map<AccountInfo, AccountInfoResponse>(context.AccountInfo);
context.Response = _mapper.Map(context.AccountInfoHistory, context.Response);
var tankIds = context.Tanks.Select(t => t.TankId).ToArray();
var vehicles = await _dictionariesDataAccessor.GetVehicles(tankIds);
var nations = await _dictionariesDataAccessor.GetNations(context.RequestLanguage);
var tankTypes = await _dictionariesDataAccessor.GetTankTypes(context.RequestLanguage);
context.Response.Tanks = _mapper.Map<List<TankInfo>, List<TankInfoResponse>>(context.Tanks);
context.Response.Tanks.ForEach(t =>
{
t = _mapper.Map(context.TanksHistory[t.TankId], t);
var vehicle = vehicles.ContainsKey(t.TankId) ? vehicles[t.TankId] : CreateStubVehicle();
t = _mapper.Map(vehicle, t,
opt =>
{
opt.Items["language"] = context.RequestLanguage;
opt.AfterMap((src, dest) =>
{
dest.TankType = tankTypes.ContainsKey(dest.TankTypeId) ? tankTypes[dest.TankTypeId] : "-";
dest.TankNation = nations.ContainsKey(dest.TankNationId) ? nations[dest.TankNationId] : "-";
});
});
});
await next.Invoke(context);
}
private static IVehiclesDictionary CreateStubVehicle()
{
return new VehiclesDictionary
{
Name = new List<LocalizableString>()
{
new LocalizableString
{Language = RequestLanguage.En, Value = "[Vehicle is not provided in the dictionary]"},
new LocalizableString {Language = RequestLanguage.Ru, Value = "[Танк отсутствует в танкопедии]"},
new LocalizableString
{Language = RequestLanguage.De, Value = "[Tank ist nicht im Wörterbuch enthalten]"},
}
};
}
}
}
@@ -0,0 +1,17 @@
using System;
namespace WotBlitzStatisticsPro.Logic.Calculations
{
public static class DateTimeHelper
{
public static DateTime ToDateTime(this int unixTimeStamp)
{
// Unix timestamp is seconds past epoch
var dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
}
}
@@ -14,9 +14,9 @@ namespace WotBlitzStatisticsPro.Logic.Calculations
}
foreach (var tank in allTanks)
{
if (tankTires.ContainsKey(tank.TankId) && tank.Battles.HasValue && tank.Battles > 0)
if (tankTires.ContainsKey(tank.TankId) && tank.Battles > 0)
{
x += tankTires[tank.TankId] * tank.Battles.Value;
x += tankTires[tank.TankId] * tank.Battles;
}
}
if (account.Battles > 0)
@@ -26,17 +26,13 @@ namespace WotBlitzStatisticsPro.Logic.Calculations
public static void CalculateWn7(this TankInfoHistory tank, double tier)
{
if (!tank.Battles.HasValue)
{
return;
}
double avdFrags = tank.Frags.DoubleValue() / tank.Battles.DoubleValue();
double avgDamage = tank.DamageDealt.DoubleValue() / tank.Battles.DoubleValue();
double avgSpot = tank.Spotted.DoubleValue() / tank.Battles.DoubleValue();
double avgDef = tank.DroppedCapturePoints.DoubleValue() / tank.Battles.DoubleValue();
double winRate = (100d * tank.Wins.DoubleValue() / tank.Battles.DoubleValue()); // - 48;
double avdFrags = (double) tank.Frags / tank.Battles;
double avgDamage = (double)tank.DamageDealt / tank.Battles;
double avgSpot = (double)tank.Spotted / tank.Battles;
double avgDef = (double)tank.DroppedCapturePoints / tank.Battles;
double winRate = (100d * (double) tank.Wins / tank.Battles); // - 48;
tank.Wn7 = CalculateWn7(tank.Battles.Value, tier, avdFrags, avgDamage, avgSpot, avgDef, winRate);
tank.Wn7 = CalculateWn7(tank.Battles, tier, avdFrags, avgDamage, avgSpot, avgDef, winRate);
}
public static void CalculateWn7(this AccountInfoHistory account)
@@ -5,6 +5,6 @@ namespace WotBlitzStatisticsPro.Logic
{
public interface IWargamingAccounts
{
Task<bool> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage);
Task<AccountInfoResponse> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage);
}
}
@@ -0,0 +1,46 @@
using System.Linq;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess.Model;
using WotBlitzStatisticsPro.DataAccess.Model.Accounts;
using WotBlitzStatisticsPro.Logic.Calculations;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class AccountDtoProfile: Profile
{
public AccountDtoProfile()
{
CreateMap<AccountInfo, AccountInfoResponse>()
.ForMember(dest => dest.CreatedAt,
o => o.MapFrom(s => s.CreatedAt.ToDateTime()))
.ForMember(dest => dest.LastBattleTime,
o => o.MapFrom(s => s.LastBattleTime.ToDateTime()));
CreateMap<AccountInfoHistory, AccountInfoResponse>()
.ForMember(d => d.LastBattleTime, o => o.Ignore())
.ForMember(d => d.Battles,
o => o.MapFrom(s => s.Battles ?? 0));
CreateMap<TankInfo, TankInfoResponse>()
.ForMember(dest => dest.LastBattleTime,
o => o.MapFrom(s => s.LastBattleTime.ToDateTime()));
CreateMap<TankInfoHistory, TankInfoResponse>()
.ForMember(d => d.LastBattleTime,
o => o.Ignore());
CreateMap<IVehiclesDictionary, TankInfoResponse>()
.ForMember(d => d.TankId, o => o.Ignore())
.ForMember(d => d.TankNationId,
o => o.MapFrom(s => string.IsNullOrEmpty(s.NationId) ? "-" : s.NationId))
.ForMember(d => d.TankTypeId,
o => o.MapFrom(s => string.IsNullOrEmpty(s.TypeId) ? "-" : s.TypeId))
.ForMember(d => d.Name,
o => o.MapFrom((src, dest, destMember, context) =>
src.Name.FirstOrDefault(l => l.Language == (RequestLanguage) context.Items["language"])?.Value));
}
}
}
@@ -15,7 +15,7 @@ namespace WotBlitzStatisticsPro.Logic
_operationFactory = operationFactory;
}
public async Task<bool> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage)
public async Task<AccountInfoResponse> GatherAccountInformation(RealmType realm, long accountId, RequestLanguage requestLanguage)
{
var context = new AccountInformationPipelineContext(accountId, realm, requestLanguage);
var pipeline = new Pipeline<AccountInformationPipelineContext>(_operationFactory);
@@ -23,6 +23,7 @@ namespace WotBlitzStatisticsPro.Logic
pipeline.AddOperation<GetAccountInfoOperation>()
.AddOperation<GetTanksInfoOperation>()
.AddOperation<CalculateStatisticsOperation>()
.AddOperation<BuildAccountInfoResponseOperation>()
;
// ToDo: add other operations
@@ -32,7 +33,7 @@ namespace WotBlitzStatisticsPro.Logic
// ToDo: return context response
return true;
return context.Response;
}
}
}
@@ -62,6 +62,7 @@ namespace WotBlitzStatisticsPro.Logic
services.AddTransient<GetAccountInfoOperation>();
services.AddTransient<GetTanksInfoOperation>();
services.AddTransient<CalculateStatisticsOperation>();
services.AddTransient<BuildAccountInfoResponseOperation>();
services.AddTransient<IOperationFactory>(serviceProvider =>
new ServiceProviderOperationFactory(serviceProvider));