AchievementDictionaries saving done

This commit is contained in:
Mikhail Shabanov committed 2020-06-06 15:54:23 +02:00
1 parent cacc60fc7f
commit 6a9b69b254
11 files changed
+613 -3

No files matched your search

@@ -0,0 +1,76 @@
using System.Collections.Generic;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Achievement dictionary item
/// </summary>
public class AchievementDictionary
{
/// <summary>
/// id
/// </summary>
public string AchievementId { get; set; }
///<summary>
/// Achievement condition
///</summary>
public List<LocalizableString> Condition { get; set; }
///<summary>
/// Achievement description
///</summary>
public List<LocalizableString> Description { get; set; }
///<summary>
/// Historical info
///</summary>
public List<LocalizableString> HeroInfo { get; set; }
///<summary>
/// Image link
///</summary>
public string Image { get; set; }
///<summary>
/// Image 180x180px link
///</summary>
public string ImageBig { get; set; }
///<summary>
/// Achievement name
///</summary>
public List<LocalizableString> Name { get; set; }
///<summary>
/// Sort order
///</summary>
public long? Order { get; set; }
///<summary>
/// Is outdated achievement
///</summary>
public bool Outdated { get; set; }
///<summary>
/// Achievement section
///</summary>
public string SectionId { get; set; }
///<summary>
/// Achievement section order
///</summary>
public long? SectionOrder { get; set; }
///<summary>
/// Achievement Type
///</summary>
public string Type { get; set; }
///<summary>
/// Achievement options
///</summary>
public List<AchievementOption> Options { get; set; }
}
}
@@ -0,0 +1,31 @@
using System.Collections.Generic;
namespace WotBlitzStatisticsPro.Common.Dictionaries
{
/// <summary>
/// Achievement option
/// </summary>
public class AchievementOption
{
///<summary>
/// Achievement option description
///</summary>
public List<LocalizableString> Description { get; set; }
///<summary>
/// Image link
///</summary>
public string Image { get; set; }
///<summary>
/// Image 180x180px link
///</summary>
public string ImageBig { get; set; }
/// <summary>
/// Achievement option name
/// </summary>
public List<LocalizableString> Name { get; set; }
}
}
@@ -13,6 +13,7 @@ namespace WotBlitzStatisticsPro.DataAccess
private const string VehicleTypesCollectionName = "dictionary-vehicle-types";
private const string ClanRolesCollectionName = "dictionary-clan-roles";
private const string AchievementsSectionsCollectionName = "dictionary-achievements-sections";
private const string AchievementsCollectionName = "dictionary-achievements";
private readonly IMongoDatabase _database;
@@ -62,5 +63,14 @@ namespace WotBlitzStatisticsPro.DataAccess
await dbAchievementSections.DeleteManyAsync(Builders<AchievementSectionDictionary>.Filter.Empty);
await dbAchievementSections.InsertManyAsync(achievementSections);
}
public async Task UpdateAchievements(List<AchievementDictionary> achievementDictionary)
{
var dbAchievementSections =
_database.GetCollection<AchievementDictionary>(AchievementsCollectionName);
await dbAchievementSections.DeleteManyAsync(Builders<AchievementDictionary>.Filter.Empty);
await dbAchievementSections.InsertManyAsync(achievementDictionary);
}
}
}
@@ -15,5 +15,7 @@ namespace WotBlitzStatisticsPro.DataAccess
Task UpdateClanRoles(List<ClanRoleDictionary> clanRoles);
Task UpdateAchievementsSections(List<AchievementSectionDictionary> achievementSections);
Task UpdateAchievements(List<AchievementDictionary> achievementDictionary);
}
}
@@ -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
{
@@ -26,9 +30,71 @@ namespace WotBlitzStatisticsPro.Logic.Dictionaries
_mapper = mapper;
}
public virtual Task<UpdateDictionariesResponseItem> Update()
public virtual async Task<UpdateDictionariesResponseItem> Update()
{
throw new System.NotImplementedException();
var achievementDictionary = await GetAndMapAchievementsDictionary();
await _dataAccessor.UpdateAchievements(achievementDictionary);
return new UpdateDictionariesResponseItem
{
DictionaryType = DictionaryType.Achievements,
Description = $"Got and saved {achievementDictionary.Count} achievement dictionary items"
};
}
private async Task<List<AchievementDictionary>> GetAndMapAchievementsDictionary()
{
var defaultRealmType = RealmType.Eu;
var achievements = new List<AchievementDictionary>();
foreach (var requestLanguage in (RequestLanguage[])Enum.GetValues(typeof(RequestLanguage)))
{
var wgAchievements =
await _wargamingDictionariesApiClient.GetAchievementsDictionary(defaultRealmType, requestLanguage);
foreach (var wgAchievement in wgAchievements)
{
var mappedAchievement =
achievements.FirstOrDefault(a => a.AchievementId == wgAchievement.AchievementId);
if (mappedAchievement == null)
{
mappedAchievement =
_mapper.Map<WotEncyclopediaAchievementsResponse, AchievementDictionary>(wgAchievement);
achievements.Add(mappedAchievement);
}
mappedAchievement.Name.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Name });
mappedAchievement.Condition.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Condition });
mappedAchievement.Description.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Description });
mappedAchievement.HeroInfo.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.HeroInfo });
if (wgAchievement.Options != null)
{
if (mappedAchievement.Options == null)
{
mappedAchievement.Options = new List<AchievementOption>();
}
foreach (var wgAchievementOption in wgAchievement.Options)
{
// image is the only unique identifier
var mappedOption =
mappedAchievement.Options.FirstOrDefault(o => o.Image == wgAchievementOption.Image);
if (mappedOption == null)
{
mappedOption =
_mapper.Map<WotEncyclopediaAchievementsOptions, AchievementOption>(
wgAchievementOption);
mappedAchievement.Options.Add(mappedOption);
}
mappedOption.Name.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Name });
mappedOption.Description.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Description });
}
}
}
}
return achievements;
}
}
}
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class AchievementDictionaryProfile : Profile
{
public AchievementDictionaryProfile()
{
CreateMap<WotEncyclopediaAchievementsResponse, AchievementDictionary>()
.ForMember(d => d.Name, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.Condition, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.Description, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.HeroInfo, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.SectionId, o => o.MapFrom(s => s.Section))
.ForMember(d => d.Options, o => o.Ignore());
}
}
}
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using AutoMapper;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Logic.Mappers
{
public class AchievementOptionProfile : Profile
{
public AchievementOptionProfile()
{
CreateMap<WotEncyclopediaAchievementsOptions, AchievementOption>()
.ForMember(d => d.Name, o => o.MapFrom(_ => new List<LocalizableString>()))
.ForMember(d => d.Description, o => o.MapFrom(_ => new List<LocalizableString>()));
}
}
}
@@ -0,0 +1,263 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using AutoMapper.Configuration;
using Bogus;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using WotBlitzStatisticsPro.Common.Dictionaries;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.DataAccess;
using WotBlitzStatisticsPro.Logic.Dictionaries;
using WotBlitzStatisticsPro.Logic.Mappers;
using WotBlitzStatisticsPro.WgApiClient;
using WotBlitzStatisticsPro.WgApiClient.Model;
namespace WotBlitzStatisticsPro.Tests.DictionariesTests
{
[TestFixture]
public class AchievementsDictionaryUpdaterTests
{
private AchievementsDictionaryUpdater _achievementDictionariesUpdater;
private Mock<IWargamingDictionariesApiClient> _wargamingDictionariesApiClientMock;
private Mock<IDictionariesDataAccessor> _dataAccessorMock;
private List<WotEncyclopediaAchievementsResponse> _achievementsInfoResponseEn;
private List<WotEncyclopediaAchievementsResponse> _achievementsInfoResponseRu;
private List<WotEncyclopediaAchievementsResponse> _achievementsInfoResponseDe;
[SetUp]
public void Init()
{
// Generating fake data
_achievementsInfoResponseEn = GetAchievementsInfoResponse("en");
_achievementsInfoResponseRu = GetAchievementsInfoResponse("ru", _achievementsInfoResponseEn);
_achievementsInfoResponseDe = GetAchievementsInfoResponse("de", _achievementsInfoResponseEn);
// Real mapper instance
var configurationExp = new MapperConfigurationExpression();
configurationExp.AddProfile<AchievementDictionaryProfile>();
configurationExp.AddProfile<AchievementOptionProfile>();
var mapperConfiguration = new MapperConfiguration(configurationExp);
IMapper mapper = new Mapper(mapperConfiguration);
// Wargaming client mock
_wargamingDictionariesApiClientMock = new Mock<IWargamingDictionariesApiClient>();
_wargamingDictionariesApiClientMock.Setup(w =>
w.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.En))
.ReturnsAsync(_achievementsInfoResponseEn);
_wargamingDictionariesApiClientMock.Setup(w =>
w.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.Ru))
.ReturnsAsync(_achievementsInfoResponseRu);
_wargamingDictionariesApiClientMock.Setup(w =>
w.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.De))
.ReturnsAsync(_achievementsInfoResponseDe);
// MongoDb data accessor mock
_dataAccessorMock = new Mock<IDictionariesDataAccessor>();
_achievementDictionariesUpdater = new AchievementsDictionaryUpdater(
_wargamingDictionariesApiClientMock.Object,
_dataAccessorMock.Object,
mapper);
}
[Test]
public async Task ShouldCallWargamingApi3Times()
{
await _achievementDictionariesUpdater.Update();
// Should call wargaming API once for each language
_wargamingDictionariesApiClientMock.Verify(d => d.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.En), Times.Once);
_wargamingDictionariesApiClientMock.Verify(d => d.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.Ru), Times.Once);
_wargamingDictionariesApiClientMock.Verify(d => d.GetAchievementsDictionary(It.IsAny<RealmType>(), RequestLanguage.De), Times.Once);
}
[Test]
public async Task ShouldSaveAppropriateAchievementsDictionary()
{
List<AchievementDictionary> targetAchievementDictionaries = null;
_dataAccessorMock
.Setup(d => d.UpdateAchievements(It.IsAny<List<AchievementDictionary>>()))
.Callback((List<AchievementDictionary> dictionary) => targetAchievementDictionaries = dictionary);
await _achievementDictionariesUpdater.Update();
targetAchievementDictionaries.Should().NotBeNull();
targetAchievementDictionaries.Should().HaveCount(targetAchievementDictionaries.Count);
for (var i = 0; i < targetAchievementDictionaries.Count; i++)
{
targetAchievementDictionaries[i].AchievementId.Should()
.Be(_achievementsInfoResponseEn[i].AchievementId);
targetAchievementDictionaries[i].Name.First(n => n.Language == RequestLanguage.En).Value.Should()
.Be(_achievementsInfoResponseEn[i].Name);
targetAchievementDictionaries[i].Name.First(n => n.Language == RequestLanguage.Ru).Value.Should()
.Be(_achievementsInfoResponseRu[i].Name);
targetAchievementDictionaries[i].Name.First(n => n.Language == RequestLanguage.De).Value.Should()
.Be(_achievementsInfoResponseDe[i].Name);
targetAchievementDictionaries[i].Description.First(n => n.Language == RequestLanguage.En).Value.Should()
.Be(_achievementsInfoResponseEn[i].Description);
targetAchievementDictionaries[i].Description.First(n => n.Language == RequestLanguage.Ru).Value.Should()
.Be(_achievementsInfoResponseRu[i].Description);
targetAchievementDictionaries[i].Description.First(n => n.Language == RequestLanguage.De).Value.Should()
.Be(_achievementsInfoResponseDe[i].Description);
targetAchievementDictionaries[i].Condition.First(n => n.Language == RequestLanguage.En).Value.Should()
.Be(_achievementsInfoResponseEn[i].Condition);
targetAchievementDictionaries[i].Condition.First(n => n.Language == RequestLanguage.Ru).Value.Should()
.Be(_achievementsInfoResponseRu[i].Condition);
targetAchievementDictionaries[i].Condition.First(n => n.Language == RequestLanguage.De).Value.Should()
.Be(_achievementsInfoResponseDe[i].Condition);
if (_achievementsInfoResponseEn[i].Options == null)
{
targetAchievementDictionaries[i].Options.Should().BeNull();
}
else
{
targetAchievementDictionaries[i].Options.Should().NotBeNull();
targetAchievementDictionaries[i].Options.Count.Should()
.Be(_achievementsInfoResponseEn[i].Options.Length);
}
}
}
#region Fixtures
private static List<WotEncyclopediaAchievementsResponse> GetAchievementsInfoResponse(string language, List<WotEncyclopediaAchievementsResponse> baseEncyclopedia = null)
{
var result = new List<WotEncyclopediaAchievementsResponse>();
var faker = new Faker(language);
if (baseEncyclopedia == null)
{
var random = faker.Random;
for (int i = 0; i < random.Number(200); i++)
{
result.Add(FakeNewDictionaryItem(faker));
}
}
else
{
foreach (var achievementsResponse in baseEncyclopedia)
{
result.Add(FakeDictionaryItem(faker, achievementsResponse));
}
}
return result;
}
private static WotEncyclopediaAchievementsResponse FakeNewDictionaryItem(Faker faker)
{
return new WotEncyclopediaAchievementsResponse
{
Name = faker.Commerce.ProductName(),
AchievementId = faker.Lorem.Word(),
Description = faker.Lorem.Sentence(),
Condition = faker.Lorem.Sentence(),
Options = faker.Random.Bool() ? FakeOptions(faker) : null
};
}
private static WotEncyclopediaAchievementsResponse FakeDictionaryItem(Faker faker, WotEncyclopediaAchievementsResponse baseResponse)
{
return new WotEncyclopediaAchievementsResponse
{
Name = faker.Commerce.ProductName(),
AchievementId = baseResponse.AchievementId,
Description = faker.Lorem.Sentence(),
Condition = faker.Lorem.Sentence(),
Options = baseResponse.Options
};
}
private static WotEncyclopediaAchievementsOptions[] FakeOptions(Faker faker)
{
var result = new WotEncyclopediaAchievementsOptions[4];
for (int i = 0; i < 4; i++)
{
result[i] = new WotEncyclopediaAchievementsOptions
{
Name = faker.Lorem.Word(),
Description = faker.Lorem.Sentence(),
Image = faker.Internet.Url()
};
}
return result;
}
private static WotClanMembersDictionaryResponse GetClanMembersDictionaryResponse(string language, WotClanMembersDictionaryResponse baseClanRoles = null)
{
return new WotClanMembersDictionaryResponse
{
ClanRoles = baseClanRoles == null ? FakeDictionary(3, language) : FakeDictionary(language, baseClanRoles.ClanRoles)
};
}
private static Dictionary<string, string> FakeDictionary(int elementsCount, string language = "en")
{
var result = new Dictionary<string, string>();
var faker = new Faker(language);
for (int i = 0; i < elementsCount; i++)
{
result[faker.Lorem.Word()] = faker.Lorem.Word();
}
return result;
}
private static Dictionary<string, string> FakeDictionary(string language,
Dictionary<string, string> baseDictionary)
{
var result = new Dictionary<string, string>();
var faker = new Faker(language);
foreach (var baseValue in baseDictionary)
{
result[baseValue.Key] = faker.Lorem.Word();
}
return result;
}
private static Dictionary<string, WotEncyclopediaInfoAchievementSection> FakeAchievementSections(int elementsCount, string language = "en")
{
var result = new Dictionary<string, WotEncyclopediaInfoAchievementSection>();
var faker = new Faker(language);
for (int i = 0; i < elementsCount; i++)
{
result[faker.Lorem.Word()] = new WotEncyclopediaInfoAchievementSection { Name = faker.Lorem.Word() };
}
return result;
}
private static Dictionary<string, WotEncyclopediaInfoAchievementSection> FakeAchievementSections(string language,
Dictionary<string, WotEncyclopediaInfoAchievementSection> baseDictionary)
{
var result = new Dictionary<string, WotEncyclopediaInfoAchievementSection>();
var faker = new Faker(language);
foreach (var baseValue in baseDictionary)
{
result[baseValue.Key] = new WotEncyclopediaInfoAchievementSection { Name = faker.Lorem.Word() };
}
return result;
}
#endregion
}
}
@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.Common.Model;
using WotBlitzStatisticsPro.WgApiClient.Model;
@@ -11,5 +12,9 @@ namespace WotBlitzStatisticsPro.WgApiClient
WotClanMembersDictionaryResponse)> GetStaticDictionariesAsync(
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En);
Task<List<WotEncyclopediaAchievementsResponse>> GetAchievementsDictionary(
RealmType realmType = RealmType.Ru,
RequestLanguage language = RequestLanguage.En);
}
}
@@ -0,0 +1,108 @@
using System;
using Newtonsoft.Json;
namespace WotBlitzStatisticsPro.WgApiClient.Model
{
public class WotEncyclopediaAchievementsResponse
{
[JsonProperty("achievement_id")]
public string AchievementId { get; set; }
///<summary>
/// Achievement condition
///</summary>
[JsonProperty("condition")]
public string Condition { get; set; }
///<summary>
/// Achievement description
///</summary>
[JsonProperty("description")]
public string Description { get; set; }
///<summary>
/// Historical info
///</summary>
[JsonProperty("hero_info")]
public string HeroInfo { get; set; }
///<summary>
/// Image link
///</summary>
[JsonProperty("image")]
public string Image { get; set; }
///<summary>
/// Image 180x180px link
///</summary>
[JsonProperty("image_big")]
public string ImageBig { get; set; }
///<summary>
/// Achievement name
///</summary>
[JsonProperty("name")]
public string Name { get; set; }
///<summary>
/// Sort order
///</summary>
[JsonProperty("order")]
public long? Order { get; set; }
///<summary>
/// Is outdated achievement
///</summary>
[JsonProperty("outdated")]
public bool Outdated { get; set; }
///<summary>
/// Achievement section
///</summary>
[JsonProperty("section")]
public string Section { get; set; }
///<summary>
/// Achievement section order
///</summary>
[JsonProperty("section_order")]
public long? SectionOrder { get; set; }
///<summary>
/// Achievement Type
///</summary>
[JsonProperty("type")]
public string Type { get; set; }
///<summary>
/// Achievement options
///</summary>
[JsonProperty("options")]
public WotEncyclopediaAchievementsOptions[] Options { get; set; }
}
public class WotEncyclopediaAchievementsOptions
{
///<summary>
/// Achievement description
///</summary>
[JsonProperty("description")]
public string Description { get; set; }
///<summary>
/// Image link
///</summary>
[JsonProperty("image")]
public string Image { get; set; }
///<summary>
/// Image 180x180px link
///</summary>
[JsonProperty("image_big")]
public string ImageBig { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
}
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using WotBlitzStatisticsPro.Common;
@@ -65,6 +66,15 @@ namespace WotBlitzStatisticsPro.WgApiClient
return (encyclopedia, clanGlossaryResponse);
}
public async Task<List<WotEncyclopediaAchievementsResponse>> GetAchievementsDictionary(RealmType realmType = RealmType.Ru, RequestLanguage language = RequestLanguage.En)
{
var response = await GetFromBlitzApi<Dictionary<string, WotEncyclopediaAchievementsResponse>>(
realmType,
language,
"encyclopedia/achievements/").ConfigureAwait(false);
return response.Values.ToList();
}
#endregion