Vehicles dictionary updater

This commit is contained in:
Mikhail Shabanov committed 2020-06-07 14:39:03 +02:00
1 parent 6a9b69b254
commit 4a11ed61df
11 files changed
+525 -4

No files matched your search

@@ -1,9 +1,13 @@
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.WgApiClient;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic.Dictionaries
{
@@ -16,19 +20,58 @@ namespace WotBlitzStatisticsPro.Logic.Dictionaries
[Obsolete("Parameter-less constructor only for unit tests")]
public VehiclesDictionaryUpdater()
{
}
public VehiclesDictionaryUpdater(IWargamingDictionariesApiClient wargamingDictionariesApiClient, IDictionariesDataAccessor dataAccessor, IMapper mapper)
public VehiclesDictionaryUpdater(IWargamingDictionariesApiClient wargamingDictionariesApiClient,
IDictionariesDataAccessor dataAccessor, IMapper mapper)
{
_wargamingDictionariesApiClient = wargamingDictionariesApiClient;
_dataAccessor = dataAccessor;
_mapper = mapper;
}
public virtual Task<UpdateDictionariesResponseItem> Update()
public virtual async Task<UpdateDictionariesResponseItem> Update()
{
throw new System.NotImplementedException();
var vehiclesDictionary = await GetAndMapVehiclesDictionary();
await _dataAccessor.UpdateVehicles(vehiclesDictionary);
return new UpdateDictionariesResponseItem
{
DictionaryType = DictionaryType.Vehicles,
Description = $"Got and saved {vehiclesDictionary.Count} vehicles dictionary items"
};
}
private async Task<List<VehiclesDictionary>> GetAndMapVehiclesDictionary()
{
var defaultRealmType = RealmType.Eu;
var vehicles = new List<VehiclesDictionary>();
foreach (var requestLanguage in (RequestLanguage[]) Enum.GetValues(typeof(RequestLanguage)))
{
var wgVehicles =
await _wargamingDictionariesApiClient.GetVehicles(defaultRealmType, requestLanguage);
foreach (var wgVehicle in wgVehicles)
{
var mappedVehicle = vehicles.FirstOrDefault(v => v.TankId == wgVehicle.TankId);
if (mappedVehicle == null)
{
mappedVehicle = _mapper.Map<WotEncyclopediaVehiclesResponse, VehiclesDictionary>(wgVehicle);
vehicles.Add(mappedVehicle);
}
mappedVehicle.Name.Add(new LocalizableString {Language = requestLanguage, Value = wgVehicle.Name});
mappedVehicle.Description.Add(new LocalizableString {Language = requestLanguage, Value = wgVehicle.Description});
}
}
return vehicles;
}
}
}
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class VehicleDictionaryProfile : Profile
{
public VehicleDictionaryProfile()
{
CreateMap<WotEncyclopediaVehiclesResponse, VehiclesDictionary>()
.ForMember(d => d.TankId,
o => o.MapFrom(s => s.TankId))
.ForMember(d => d.Name, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.Description, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.TypeId,
o => o.MapFrom(s => s.Type))
.ForMember(d => d.NationId,
o => o.MapFrom(s => s.Nation))
.ForMember(d => d.Tier,
o => o.MapFrom(s => s.Tier))
.ForMember(d => d.PreviewImage,
o => o.MapFrom(s => s.Images["preview"]))
.ForMember(d => d.NormalImage,
o => o.MapFrom(s => s.Images["normal"]))
.ForMember(d => d.PriceCredit,
o => o.MapFrom(s => s.Cost["price_credit"]))
.ForMember(d => d.PriceGold,
o => o.MapFrom(s => s.Cost["price_gold"]))
.ForMember(d => d.NexTanksInTree,
o =>
o.MapFrom(s => s.NextTanks))
.ForMember(d => d.PriceXp,
o =>
o.MapFrom(s => s.PricesXp));
}
}
}
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class VehiclePriceProfile : Profile
{
public VehiclePriceProfile()
{
CreateMap<KeyValuePair<string, string>, VehiclePriceInXp>()
.ForMember(d => d.TankId,
o => o.MapFrom(s => Convert.ToInt64(s.Key)))
.ForMember(d => d.PricesXp,
o => o.MapFrom(s => Convert.ToInt64(s.Value)));
}
}
}