Refactor getMaster response translation application

This commit is contained in:
Les De Ridder 2017-09-12 04:16:23 +02:00
parent fe9248ddec
commit d20cc3ee1c
2 changed files with 67 additions and 126 deletions

View File

@ -123,144 +123,50 @@ Json handleResponse(string endpoint, Json json)
import models.master;
auto getMasterResponse = json.deserializeJson!GetMasterResponse;
foreach(character; getMasterResponse.masterCharacter)
{
auto characterId = character[0].to!uint;
import dataMixins : applyMasterTranslation;
import translations.character;
if(characterId in characterNames)
{
character[5] = characterNames[characterId];
}
}
mixin(applyMasterTranslation!("Character", 0,
5, "characterNames"));
foreach(characterSkill; getMasterResponse.masterCharacterSkill)
{
auto skillId = characterSkill[0].to!uint;
mixin(applyMasterTranslation!("CharacterSkill", 0,
1, "skillNames",
6, "skillDescriptions"));
import translations.characterSkill;
if(skillId in skillNames)
{
characterSkill[1] = skillNames[skillId];
characterSkill[6] = skillDescriptions[skillId];
}
}
mixin(applyMasterTranslation!("Stage", 0,
1, "stageNames"));
foreach(stage; getMasterResponse.masterStage)
{
auto stageId = stage[0].to!uint;
//TODO: Figure out a way to find the wrong ones
mixin(applyMasterTranslation!("Mission", 0,
1, "missionNames",
2, "missionDescriptions",
3, "missionRewards"));
import translations.stage;
if(stageId in stageNames)
{
stage[1] = stageNames[stageId];
}
}
mixin(applyMasterTranslation!("Dungeon", 10,
12, "dungeonNames"));
foreach(mission; getMasterResponse.masterMission)
{
//TODO: Figure out a way to find the wrong ones
auto missionId = mission[0].to!uint;
mixin(applyMasterTranslation!("Guest", 0,
1, "guestPartyNames"));
import translations.mission;
if(missionId in missionNames)
{
mission[1] = missionNames[missionId];
mission[2] = missionDescriptions[missionId];
mission[3] = missionRewards[missionId];
}
}
mixin(applyMasterTranslation!("Item", 0,
1, "itemNames"));
foreach(dungeon; getMasterResponse.masterDungeon)
{
auto dungeonId = dungeon[10].to!uint;
mixin(applyMasterTranslation!("WorldMap", 0,
3, "worldMapLocationNames",
4, "worldMapLocationCaptions",
5, "worldMapLocationDescriptions"));
import translations.dungeon;
if(dungeonId in dungeonNames)
{
dungeon[12] = dungeonNames[dungeonId];
}
}
mixin(applyMasterTranslation!("CharacterTextResource", 0,
3, "characterTextResource"));
foreach(guest; getMasterResponse.masterGuest)
{
auto guestId = guest[0].to!uint;
mixin(applyMasterTranslation!("CharacterLeaderSkill", 0,
1, "characterLeaderSkillNames",
14, "characterLeaderSkillDescriptions"));
import translations.guest;
if(guestId in guestPartyNames)
{
guest[1] = guestPartyNames[guestId];
}
}
mixin(applyMasterTranslation!("CharacterBook", 0,
5, "characterBookFlowerLanguage"));
foreach(item; getMasterResponse.masterItem)
{
auto itemId = item[0].to!uint;
import translations.item;
if(itemId in itemNames)
{
item[1] = itemNames[itemId];
}
}
foreach(location; getMasterResponse.masterWorldMap)
{
auto locationId = location[0].to!uint;
import translations.worldMap;
if(locationId in worldMapLocationNames)
{
location[3] = worldMapLocationNames[locationId];
location[4] = worldMapLocationCaptions[locationId];
location[5] = worldMapLocationDescriptions[locationId];
}
}
foreach(resource; getMasterResponse.masterCharacterTextResource)
{
auto resourceId = resource[0].to!uint;
import translations.characterTextResource;
if(resourceId in characterTextResource)
{
resource[3] = characterTextResource[resourceId];
}
}
foreach(leaderSkill; getMasterResponse.masterCharacterLeaderSkill)
{
auto leaderSkillId = leaderSkill[0].to!uint;
import translations.characterLeaderSkill;
if(leaderSkillId in characterLeaderSkillNames)
{
leaderSkill[1] = characterLeaderSkillNames[leaderSkillId];
leaderSkill[14] = characterLeaderSkillDescriptions[leaderSkillId];
}
}
foreach(characterBookEntry; getMasterResponse.masterCharacterBook)
{
auto characterId = characterBookEntry[0].to!uint;
import translations.characterBook;
if(characterId in characterBookFlowerLanguage)
{
characterBookEntry[5] = characterBookFlowerLanguage[characterId];
}
}
foreach(characterCategory; getMasterResponse.masterCharacterCategory)
{
auto categoryId = characterCategory[0].to!uint;
import translations.characterCategory;
if(categoryId in characterCategories)
{
characterCategory[1] = characterCategories[categoryId];
}
}
mixin(applyMasterTranslation!("CharacterCategory", 0,
1, "characterCategories"));
return getMasterResponse.serializeToJson;
}

35
source/dataMixins.d Normal file
View File

@ -0,0 +1,35 @@
module dataMixins;
template applyMasterTranslation(string Table, uint IdFieldIndex, Translations...)
{
static assert(Translations.length >= 2 && Translations.length % 2 == 0);
import std.conv : to;
import std.uni : toLower;
private template getMasterColumnTranslations(Translations...)
{
static if(Translations.length > 0)
{
enum getMasterColumnTranslations = "row[" ~ Translations[0].to!string ~ "] = " ~ Translations[1] ~ "[id]; "
~ getMasterColumnTranslations!(Translations[2 .. $]);
}
else
{
enum getMasterColumnTranslations = "";
}
}
enum applyMasterTranslation = "
foreach(row; getMasterResponse.master" ~ Table ~ ")
{
import std.conv : to;
auto id = row[" ~ IdFieldIndex.to!string ~ "].to!uint;
import translations." ~ Table[0].toLower.to!string ~ Table[1 .. $] ~ ";
if(id in " ~ Translations[1] ~ ")
{
" ~ getMasterColumnTranslations!(Translations) ~ "
}
}";
}