Files

107 lines
4.8 KiB
C#
Raw Permalink Normal View History

2020-06-02 14:02:57 +02:00
using System;
2020-06-06 15:54:23 +02:00
using System.Collections.Generic;
using System.Linq;
2020-06-02 14:02:57 +02:00
using System.Threading.Tasks;
using AutoMapper;
2020-06-06 15:54:23 +02:00
using WotBlitzStatisticsPro.Common.Dictionaries;
2020-05-29 23:03:32 +02:00
using WotBlitzStatisticsPro.Common.Model;
2020-06-02 14:02:57 +02:00
using WotBlitzStatisticsPro.DataAccess;
2020-06-24 12:19:03 +02:00
using WotBlitzStatisticsPro.DataAccess.Model;
2020-06-02 14:02:57 +02:00
using WotBlitzStatisticsPro.WgApiClient;
2020-06-06 15:54:23 +02:00
using WotBlitzStatisticsPro.WgApiClient.Model;
2020-05-29 23:03:32 +02:00
namespace WotBlitzStatisticsPro.Logic.Dictionaries
{
public class AchievementsDictionaryUpdater : IDictionaryUpdater
{
2020-06-02 14:02:57 +02:00
private readonly IWargamingDictionariesApiClient _wargamingDictionariesApiClient;
private readonly IDictionariesDataAccessor _dataAccessor;
private readonly IMapper _mapper;
[Obsolete("Parameter-less constructor only for unit tests")]
public AchievementsDictionaryUpdater()
{
2020-12-27 12:39:25 +01:00
_wargamingDictionariesApiClient = null!;
_dataAccessor = null!;
_mapper = null!;
2020-06-02 14:02:57 +02:00
}
public AchievementsDictionaryUpdater(IWargamingDictionariesApiClient wargamingDictionariesApiClient, IDictionariesDataAccessor dataAccessor, IMapper mapper)
{
_wargamingDictionariesApiClient = wargamingDictionariesApiClient;
_dataAccessor = dataAccessor;
_mapper = mapper;
}
2020-06-06 15:54:23 +02:00
public virtual async Task<UpdateDictionariesResponseItem> Update()
2020-05-29 23:03:32 +02:00
{
2020-06-06 15:54:23 +02:00
var achievementDictionary = await GetAndMapAchievementsDictionary();
await _dataAccessor.UpdateAchievements(achievementDictionary);
return new UpdateDictionariesResponseItem
{
DictionaryType = DictionaryType.Achievements,
Description = $"Got and saved {achievementDictionary.Count} achievement dictionary items"
};
}
2020-06-24 12:19:03 +02:00
private async Task<List<IAchievementDictionary>> GetAndMapAchievementsDictionary()
2020-06-06 15:54:23 +02:00
{
var defaultRealmType = RealmType.Eu;
2020-06-24 12:19:03 +02:00
var achievements = new List<IAchievementDictionary>();
2020-06-06 15:54:23 +02:00
foreach (var requestLanguage in (RequestLanguage[])Enum.GetValues(typeof(RequestLanguage)))
{
var wgAchievements =
await _wargamingDictionariesApiClient.GetAchievementsDictionary(defaultRealmType, requestLanguage);
2020-12-27 12:39:25 +01:00
if (wgAchievements == null)
{
continue;
}
2020-06-06 15:54:23 +02:00
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);
}
2020-12-27 12:39:25 +01:00
mappedOption.Name?.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Name });
mappedOption.Description?.Add(new LocalizableString { Language = requestLanguage, Value = wgAchievement.Description });
2020-06-06 15:54:23 +02:00
}
}
}
}
return achievements;
2020-05-29 23:03:32 +02:00
}
}
}