Skip to content

Commit

Permalink
Merge pull request #16 from hmlendea/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
hmlendea authored Sep 27, 2020
2 parents 1a12c8d + 9dafe03 commit cc2c143
Show file tree
Hide file tree
Showing 10 changed files with 336 additions and 385 deletions.
4 changes: 3 additions & 1 deletion Service/INameNormaliser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ namespace MoreCulturalNamesModBuilder.Service
{
public interface INameNormaliser
{
string ToHOI4(string name);
string ToHOI4Charset(string name);

string ToCK3Charset(string name);

string ToWindows1252(string name);
}
Expand Down
76 changes: 51 additions & 25 deletions Service/LocalisationFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public sealed class LocalisationFetcher : ILocalisationFetcher
readonly IRepository<LanguageEntity> languageRepository;
readonly IRepository<LocationEntity> locationRepository;

readonly IDictionary<string, Location> locations;
readonly IDictionary<string, Language> languages;
readonly ConcurrentDictionary<string, IDictionary<string, string>> languageGameIdsCache;

IDictionary<string, Location> locations;
IDictionary<string, Language> languages;

public LocalisationFetcher(
IRepository<LanguageEntity> languageRepository,
Expand All @@ -26,6 +28,13 @@ public LocalisationFetcher(
this.languageRepository = languageRepository;
this.locationRepository = locationRepository;

languageGameIdsCache = new ConcurrentDictionary<string, IDictionary<string, string>>();

LoadData();
}

void LoadData()
{
locations = locationRepository
.GetAll()
.ToServiceModels()
Expand All @@ -37,45 +46,38 @@ public LocalisationFetcher(
.ToDictionary(key => key.Id, val => val);
}

public IEnumerable<Localisation> GetGameLocationLocalisations(string locationGameId, string gameId)
public IEnumerable<Localisation> GetGameLocationLocalisations(string locationGameId, string game)
{
ConcurrentBag<Localisation> localisations = new ConcurrentBag<Localisation>();

IDictionary<string, string> languageGameIds = languages.Values
.SelectMany(x => x.GameIds)
.Where(x => x.Game == gameId)
.ToDictionary(
key => key.Id,
val => languages.Values.First(language => language.GameIds.Any(x => x.Game == gameId && x.Id == val.Id)).Id);

Location location = locations.Values.FirstOrDefault(x => x.GameIds.Any(x => x.Game == gameId && x.Id == locationGameId));
Location location = locations.Values.FirstOrDefault(x => x.GameIds.Any(x => x.Game == game && x.Id == locationGameId));

if (location is null)
{
return localisations;
}

IDictionary<string, string> languageGameIds = GetLanguageGameIds(game);

Parallel.ForEach(languageGameIds, gameLanguage =>
Parallel.ForEach(languageGameIds, languageGameId =>
{
string name = GetLocationName(location, gameLanguage.Value);
if (!string.IsNullOrWhiteSpace(name))
Localisation localisation = GetLocationLocalisation(location, languageGameId.Value);
if (localisation is null)
{
Localisation localisation = new Localisation();
localisation.LocationId = location.Id;
localisation.LocationGameId = locationGameId;
localisation.LanguageId = gameLanguage.Value;
localisation.LanguageGameId = gameLanguage.Key;
localisation.Name = name;
localisations.Add(localisation);
return;
}
localisation.LocationGameId = locationGameId;
localisation.LanguageGameId = languageGameId.Key;
localisations.Add(localisation);
});

return localisations;
}

string GetLocationName(Location location, string languageId)
Localisation GetLocationLocalisation(Location location, string languageId)
{
if (location.IsEmpty())
{
Expand All @@ -98,13 +100,37 @@ string GetLocationName(Location location, string languageId)
{
if (name.LanguageId == languageIdToCheck)
{
return name.Value;
Localisation localisation = new Localisation();
localisation.LocationId = locationIdToCheck;
localisation.LanguageId = languageIdToCheck;
localisation.Name = name.Value;

return localisation;
}
}
}
}

return null;
}

IDictionary<string, string> GetLanguageGameIds(string game)
{
if (languageGameIdsCache.ContainsKey(game))
{
return languageGameIdsCache[game];
}

IDictionary<string, string> languageGameIds = languages.Values
.SelectMany(x => x.GameIds)
.Where(x => x.Game == game)
.ToDictionary(
key => key.Id,
val => languages.Values.First(language => language.GameIds.Any(x => x.Game == game && x.Id == val.Id)).Id);

languageGameIdsCache.TryAdd(game, languageGameIds);

return languageGameIds;
}
}
}
1 change: 0 additions & 1 deletion Service/ModBuilders/CrusaderKings2/CK2HIPModBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public sealed class CK2HIPModBuilder : CK2ModBuilder, ICK2HIPModBuilder
public override string Game => "CK2HIP";

protected override string InputLandedTitlesFileName => "ck2hip_landed_titles.txt";

protected override string OutputLandedTitlesFileName => "swmh_landed_titles.txt";

public CK2HIPModBuilder(
Expand Down
Loading

0 comments on commit cc2c143

Please sign in to comment.