2020-05-29 23:03:32 +02:00
using System ;
2020-05-30 17:15:43 +02:00
using System.Collections.Generic ;
using System.Linq ;
2020-05-29 23:03:32 +02:00
using System.Threading.Tasks ;
2020-05-30 17:15:43 +02:00
using AutoMapper ;
using WotBlitzStatisticsPro.Common.Dictionaries ;
2020-05-29 23:03:32 +02:00
using WotBlitzStatisticsPro.Common.Model ;
2020-05-31 15:43:39 +02:00
using WotBlitzStatisticsPro.DataAccess ;
2020-06-24 12:19:03 +02:00
using WotBlitzStatisticsPro.DataAccess.Model ;
2020-05-29 23:03:32 +02:00
using WotBlitzStatisticsPro.WgApiClient ;
2020-05-30 17:15:43 +02:00
using WotBlitzStatisticsPro.WgApiClient.Model ;
2020-05-29 23:03:32 +02:00
namespace WotBlitzStatisticsPro.Logic.Dictionaries
{
public class StaticDictionariesUpdater : IDictionaryUpdater
{
private readonly IWargamingDictionariesApiClient _wargamingDictionariesApiClient ;
2020-05-31 15:43:39 +02:00
private readonly IDictionariesDataAccessor _dataAccessor ;
2020-05-30 17:15:43 +02:00
private readonly IMapper _mapper ;
2020-05-29 23:03:32 +02:00
2020-06-02 14:02:57 +02:00
[Obsolete("Parameter-less constructor only for unit tests")]
public StaticDictionariesUpdater ()
{
2020-12-27 12:39:25 +01:00
_wargamingDictionariesApiClient = null !;
_dataAccessor = null !;
_mapper = null !;
2020-06-02 14:02:57 +02:00
}
2020-05-30 17:15:43 +02:00
public StaticDictionariesUpdater (
IWargamingDictionariesApiClient wargamingDictionariesApiClient ,
2020-05-31 15:43:39 +02:00
IDictionariesDataAccessor dataAccessor ,
2020-05-30 17:15:43 +02:00
IMapper mapper )
2020-05-29 23:03:32 +02:00
{
_wargamingDictionariesApiClient = wargamingDictionariesApiClient ;
2020-05-31 15:43:39 +02:00
_dataAccessor = dataAccessor ;
2020-05-30 17:15:43 +02:00
_mapper = mapper ;
2020-05-29 23:03:32 +02:00
}
2020-06-02 14:02:57 +02:00
public virtual async Task < UpdateDictionariesResponseItem > Update ()
2020-05-29 23:03:32 +02:00
{
2020-05-30 17:15:43 +02:00
var (
languages ,
nations ,
vehicleTypes ,
clanRoles ,
achievementsSections ) = await GetAndMapDictionaries ();
2020-05-29 23:03:32 +02:00
2020-12-27 12:39:25 +01:00
if ( languages != null )
{
await _dataAccessor . UpdateLanguages ( languages );
}
if ( nations != null )
{
await _dataAccessor . UpdateNations ( nations );
}
if ( vehicleTypes != null )
{
await _dataAccessor . UpdateVehicleTypes ( vehicleTypes );
}
if ( clanRoles != null )
{
await _dataAccessor . UpdateClanRoles ( clanRoles );
}
if ( achievementsSections != null )
{
await _dataAccessor . UpdateAchievementsSections ( achievementsSections );
}
2020-05-29 23:03:32 +02:00
2020-05-31 15:43:39 +02:00
return new UpdateDictionariesResponseItem
{
DictionaryType = DictionaryType . StaticDictionaries ,
Description =
2020-12-27 12:39:25 +01:00
$"Updated {languages?.Count} items in Languages dictionary; {nations?.Count} items in Nations dictionary; {vehicleTypes?.Count} items in VehicleTypes dictionary; {clanRoles?.Count} items in ClanRoles dictionary; {achievementsSections?.Count} items in AchievementsSections dictionary"
2020-05-31 15:43:39 +02:00
};
2020-05-29 23:03:32 +02:00
}
2020-05-30 17:15:43 +02:00
private async Task <(
2020-12-27 12:39:25 +01:00
List < ILanguageDictionary >?,
List < INationDictionary >?,
List < IVehicleTypeDictionary >?,
List < IClanRoleDictionary >?,
List < IAchievementSectionDictionary >?)> GetAndMapDictionaries ()
2020-05-30 17:15:43 +02:00
{
var defaultRealmType = RealmType . Eu ;
2020-12-27 12:39:25 +01:00
List < ILanguageDictionary >? languages = null ;
2020-06-24 12:19:03 +02:00
var nations = new List < INationDictionary >();
var vehicleTypes = new List < IVehicleTypeDictionary >();
var clanRoles = new List < IClanRoleDictionary >();
var achievementsSections = new List < IAchievementSectionDictionary >();
2020-05-30 17:15:43 +02:00
foreach ( var requestLanguage in ( RequestLanguage []) Enum . GetValues ( typeof ( RequestLanguage )))
{
var ( wotEncyclopediaInfoResponse , wotClanMembersDictionaryResponse ) =
await _wargamingDictionariesApiClient . GetStaticDictionariesAsync ( defaultRealmType , requestLanguage );
2020-12-27 12:39:25 +01:00
if ( wotEncyclopediaInfoResponse ?. Languages != null )
languages ??= _mapper
. Map < Dictionary < string , string >, List < LanguageDictionary >>( wotEncyclopediaInfoResponse . Languages ). Cast < ILanguageDictionary >(). ToList ();
2020-05-30 17:15:43 +02:00
2020-12-27 12:39:25 +01:00
MapVehicleNations ( requestLanguage , wotEncyclopediaInfoResponse ?. VehicleNations , nations );
MapVehicleTypes ( requestLanguage , wotEncyclopediaInfoResponse ?. VehicleTypes , vehicleTypes );
MapClanRoles ( requestLanguage , wotClanMembersDictionaryResponse ?. ClanRoles , clanRoles );
MapAchievementsSections ( requestLanguage , wotEncyclopediaInfoResponse ?. AchievementSections , achievementsSections );
2020-05-30 17:15:43 +02:00
}
return ( languages , nations , vehicleTypes , clanRoles , achievementsSections );
}
private void MapVehicleNations (
RequestLanguage requestLanguage ,
2020-12-27 12:39:25 +01:00
Dictionary < string , string >? sourceDictionary ,
2020-06-24 12:19:03 +02:00
List < INationDictionary > destinationDictionary )
2020-05-30 17:15:43 +02:00
{
2020-12-27 12:39:25 +01:00
if ( sourceDictionary == null )
{
return ;
}
2020-05-30 17:15:43 +02:00
foreach ( var source in sourceDictionary )
{
var destinationItem = destinationDictionary . FirstOrDefault ( d => d . NationId == source . Key );
if ( destinationItem == null )
{
destinationItem = new NationDictionary
{
2020-05-31 15:43:39 +02:00
NationId = source . Key , NationNames = new List < LocalizableString >()
2020-05-30 17:15:43 +02:00
};
destinationDictionary . Add ( destinationItem );
}
2020-05-31 15:43:39 +02:00
destinationItem . NationNames . Add (
new LocalizableString { Language = requestLanguage , Value = source . Value });
2020-05-30 17:15:43 +02:00
}
}
private void MapVehicleTypes (
RequestLanguage requestLanguage ,
2020-12-27 12:39:25 +01:00
Dictionary < string , string >? sourceDictionary ,
2020-06-24 12:19:03 +02:00
List < IVehicleTypeDictionary > destinationDictionary )
2020-05-30 17:15:43 +02:00
{
2020-12-27 12:39:25 +01:00
if ( sourceDictionary == null )
{
return ;
}
2020-05-30 17:15:43 +02:00
foreach ( var source in sourceDictionary )
{
var destinationItem = destinationDictionary . FirstOrDefault ( d => d . VehicleTypeId == source . Key );
if ( destinationItem == null )
{
destinationItem = new VehicleTypeDictionary
{
2020-05-31 15:43:39 +02:00
VehicleTypeId = source . Key , VehicleTypeNames = new List < LocalizableString >()
2020-05-30 17:15:43 +02:00
};
destinationDictionary . Add ( destinationItem );
}
2020-05-31 15:43:39 +02:00
destinationItem . VehicleTypeNames . Add (
new LocalizableString { Language = requestLanguage , Value = source . Value });
2020-05-30 17:15:43 +02:00
}
}
private void MapClanRoles (
RequestLanguage requestLanguage ,
2020-12-27 12:39:25 +01:00
Dictionary < string , string >? sourceDictionary ,
2020-06-24 12:19:03 +02:00
List < IClanRoleDictionary > destinationDictionary )
2020-05-30 17:15:43 +02:00
{
2020-12-27 12:39:25 +01:00
if ( sourceDictionary == null )
{
return ;
}
2020-05-30 17:15:43 +02:00
foreach ( var source in sourceDictionary )
{
var destinationItem = destinationDictionary . FirstOrDefault ( d => d . ClanRoleId == source . Key );
if ( destinationItem == null )
{
destinationItem = new ClanRoleDictionary
{
2020-05-31 15:43:39 +02:00
ClanRoleId = source . Key , ClanRoleNames = new List < LocalizableString >()
2020-05-30 17:15:43 +02:00
};
destinationDictionary . Add ( destinationItem );
}
2020-05-31 15:43:39 +02:00
destinationItem . ClanRoleNames . Add (
new LocalizableString { Language = requestLanguage , Value = source . Value });
2020-05-30 17:15:43 +02:00
}
}
private void MapAchievementsSections (
RequestLanguage requestLanguage ,
2020-12-27 12:39:25 +01:00
Dictionary < string , WotEncyclopediaInfoAchievementSection >? sourceSections ,
2020-06-24 12:19:03 +02:00
List < IAchievementSectionDictionary > destinationDictionary )
2020-05-30 17:15:43 +02:00
{
2020-12-27 12:39:25 +01:00
if ( sourceSections == null )
{
return ;
}
2020-05-30 17:15:43 +02:00
foreach ( var section in sourceSections )
{
var destinationItem =
destinationDictionary . FirstOrDefault ( d => d . AchievementSectionId == section . Key );
if ( destinationItem == null )
{
destinationItem = new AchievementSectionDictionary
{
AchievementSectionId = section . Key ,
Order = Convert . ToInt32 ( section . Value . Order ),
2020-05-31 15:43:39 +02:00
AchievementSectionNames = new List < LocalizableString >()
2020-05-30 17:15:43 +02:00
};
destinationDictionary . Add ( destinationItem );
}
2020-05-31 15:43:39 +02:00
destinationItem . AchievementSectionNames . Add (
new LocalizableString { Language = requestLanguage , Value = section . Value . Name });
2020-05-30 17:15:43 +02:00
}
}
2020-05-29 23:03:32 +02:00
}
}