Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for translations, customizing messages to your desire #723

Merged
merged 2 commits into from Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions PoGo.NecroBot.CLI/ConsoleEventListener.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#region using directives

using System;
using PoGo.NecroBot.Logic.Common;
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.State;
Expand All @@ -15,7 +16,7 @@ public class ConsoleEventListener
{
public void HandleEvent(ProfileEvent evt, Context ctx)
{
Logger.Write($"Playing as {evt.Profile.PlayerData.Username ?? ""}");
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventFortUsed, evt.Profile.PlayerData.Username ?? ""));
}

public void HandleEvent(ErrorEvent evt, Context ctx)
Expand All @@ -35,37 +36,35 @@ public void HandleEvent(WarnEvent evt, Context ctx)

public void HandleEvent(UseLuckyEggEvent evt, Context ctx)
{
Logger.Write($"Used Lucky Egg, remaining: {evt.Count}", LogLevel.Egg);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventUsedLuckyEgg, evt.Count), LogLevel.Egg);
}

public void HandleEvent(PokemonEvolveEvent evt, Context ctx)
{
Logger.Write(evt.Result == EvolvePokemonResponse.Types.Result.Success
? $"{evt.Id} successfully for {evt.Exp}xp"
: $"Failed {evt.Id}. EvolvePokemonOutProto.Result was {evt.Result}, stopping evolving {evt.Id}",
? ctx.Translations.GetTranslation(TranslationString.EventPokemonEvolvedSuccess, evt.Id, evt.Exp)
: ctx.Translations.GetTranslation(TranslationString.EventPokemonEvolvedFailed, evt.Id, evt.Result, evt.Id),
LogLevel.Evolve);
}

public void HandleEvent(TransferPokemonEvent evt, Context ctx)
{
Logger.Write(
$"{evt.Id}\t- CP: {evt.Cp} IV: {evt.Perfection.ToString("0.00")}% [Best CP: {evt.BestCp} IV: {evt.BestPerfection.ToString("0.00")}%] (Candies: {evt.FamilyCandies}) ",
LogLevel.Transfer);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventPokemonTransferred, evt.Id, evt.Cp, evt.Perfection.ToString("0.00"), evt.BestCp, evt.BestPerfection.ToString("0.00"), evt.FamilyCandies), LogLevel.Transfer);
}

public void HandleEvent(ItemRecycledEvent evt, Context ctx)
{
Logger.Write($"{evt.Count}x {evt.Id}", LogLevel.Recycling);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventItemRecycled, evt.Count, evt.Id), LogLevel.Recycling);
}

public void HandleEvent(FortUsedEvent evt, Context ctx)
{
Logger.Write($"XP: {evt.Exp}, Gems: {evt.Gems}, Items: {evt.Items}", LogLevel.Pokestop);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventFortUsed, evt.Exp, evt.Gems, evt.Items), LogLevel.Pokestop);
}

public void HandleEvent(FortTargetEvent evt, Context ctx)
{
Logger.Write($"{evt.Name} in ({Math.Round(evt.Distance)}m)", LogLevel.Info, ConsoleColor.DarkRed);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventFortTargeted, evt.Name, Math.Round(evt.Distance)), LogLevel.Info, ConsoleColor.DarkRed);
}

public void HandleEvent(PokemonCaptureEvent evt, Context ctx)
Expand All @@ -75,13 +74,13 @@ public void HandleEvent(PokemonCaptureEvent evt, Context ctx)
switch (a)
{
case ItemId.ItemPokeBall:
return "Poke";
return ctx.Translations.GetTranslation(TranslationString.Pokeball);
case ItemId.ItemGreatBall:
return "Great";
return ctx.Translations.GetTranslation(TranslationString.GreatPokeball);
case ItemId.ItemUltraBall:
return "Ultra";
return ctx.Translations.GetTranslation(TranslationString.UltraPokeball);
case ItemId.ItemMasterBall:
return "Master";
return ctx.Translations.GetTranslation(TranslationString.MasterPokeball);
default:
return "Unknown";
}
Expand All @@ -90,35 +89,35 @@ public void HandleEvent(PokemonCaptureEvent evt, Context ctx)
var catchType = evt.CatchType;

var catchStatus = evt.Attempt > 1
? $"{evt.Status} Attempt #{evt.Attempt}"
: $"{evt.Status}";
? ctx.Translations.GetTranslation(TranslationString.CatchStatusAttempt, evt.Status, evt.Attempt)
: ctx.Translations.GetTranslation(TranslationString.CatchStatus, evt.Status);

var familyCandies = evt.FamilyCandies > 0
? $"Candies: {evt.FamilyCandies}"
? ctx.Translations.GetTranslation(TranslationString.Candies)
: "";

Logger.Write(
$"({catchStatus}) | ({catchType}) {evt.Id} Lvl: {evt.Level} CP: ({evt.Cp}/{evt.MaxCp}) IV: {evt.Perfection.ToString("0.00")}% | Chance: {evt.Probability}% | {Math.Round(evt.Distance)}m dist | with a {returnRealBallName(evt.Pokeball)}Ball ({evt.BallAmount} left). | {familyCandies}",
LogLevel.Caught);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventPokemonCapture, catchStatus, catchType, evt.Id,
evt.Level, evt.Cp, evt.MaxCp, evt.Perfection.ToString("0.00"), evt.Probability, evt.Distance,
returnRealBallName(evt.Pokeball), evt.BallAmount, familyCandies), LogLevel.Caught);
}

public void HandleEvent(NoPokeballEvent evt, Context ctx)
{
Logger.Write($"No Pokeballs - We missed a {evt.Id} with CP {evt.Cp}", LogLevel.Caught);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventNoPokeballs, evt.Id, evt.Cp), LogLevel.Caught);
}

public void HandleEvent(UseBerryEvent evt, Context ctx)
{
Logger.Write($"Used, remaining: {evt.Count}", LogLevel.Berry);
Logger.Write(ctx.Translations.GetTranslation(TranslationString.EventNoPokeballs, evt.Count), LogLevel.Berry);
}

public void HandleEvent(DisplayHighestsPokemonEvent evt, Context ctx)
{
Logger.Write($"====== DisplayHighests{evt.SortedBy} ======", LogLevel.Info, ConsoleColor.Yellow);
foreach (var pokemon in evt.PokemonList)
Logger.Write(
$"# CP {pokemon.Item1.Cp.ToString().PadLeft(4, ' ')}/{pokemon.Item2.ToString().PadLeft(4, ' ')} | ({pokemon.Item3.ToString("0.00")}% perfect)\t| Lvl {pokemon.Item4.ToString("00")}\t NAME: '{pokemon.Item1.PokemonId}'",
LogLevel.Info, ConsoleColor.Yellow);
Logger.Write(
$"# CP {pokemon.Item1.Cp.ToString().PadLeft(4, ' ')}/{pokemon.Item2.ToString().PadLeft(4, ' ')} | ({pokemon.Item3.ToString("0.00")}% perfect)\t| Lvl {pokemon.Item4.ToString("00")}\t NAME: '{pokemon.Item1.PokemonId}'",
LogLevel.Info, ConsoleColor.Yellow);
}

public void Listen(IEvent evt, Context ctx)
Expand Down
12 changes: 7 additions & 5 deletions PoGo.NecroBot.CLI/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AuthSettings()
{
Console.Clear();
return;
}
}
do
{
Console.WriteLine("Username:");
Expand All @@ -52,7 +52,7 @@ public AuthSettings()
public string GoogleRefreshToken;
public string PtcUsername;
public string PtcPassword;


[JsonIgnore]
private string FilePath;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void Save(string path)

public void Save()
{
if(!string.IsNullOrEmpty(FilePath))
if (!string.IsNullOrEmpty(FilePath))
{
Save(FilePath);
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public static GlobalSettings Load(string path)
settings = new GlobalSettings();
}

if(settings.WebSocketPort == 0)
if (settings.WebSocketPort == 0)
{
settings.WebSocketPort = 14251;
}
Expand All @@ -155,6 +155,7 @@ public void Save(string fullPath)
}

public bool AutoUpdate = false;
public string TranslationLanguageCode = "en";
public double DefaultAltitude = 10;
public double DefaultLatitude = 52.379189;
public double DefaultLongitude = 4.899431;
Expand All @@ -177,7 +178,7 @@ public void Save(string fullPath)
public int AmountOfPokemonToDisplayOnStart = 10;
public bool RenameAboveIv = false;
public int WebSocketPort = 14251;

[JsonIgnore]
internal AuthSettings Auth = new AuthSettings();

Expand Down Expand Up @@ -298,6 +299,7 @@ public LogicSettings(GlobalSettings settings)
public float EvolveAboveIvValue => _settings.EvolveAboveIvValue;
public bool RenameAboveIv => _settings.RenameAboveIv;
public int AmountOfPokemonToDisplayOnStart => _settings.AmountOfPokemonToDisplayOnStart;
public string TranslationLanguageCode => _settings.TranslationLanguageCode;
public ICollection<KeyValuePair<ItemId, int>> ItemRecycleFilter => _settings.ItemRecycleFilter;
public ICollection<PokemonId> PokemonsToEvolve => _settings.PokemonsToEvolve;
public ICollection<PokemonId> PokemonsNotToTransfer => _settings.PokemonsNotToTransfer;
Expand Down
123 changes: 123 additions & 0 deletions PoGo.NecroBot.Logic/Common/Translations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace PoGo.NecroBot.Logic.Common
{
public class Translations
{
public static Translations Default => new Translations();
public static string ProfilePath;
public static string ConfigPath;

public static Translations Load(string translationsLanguageCode)
{
ProfilePath = Directory.GetCurrentDirectory();
ConfigPath = ProfilePath + "\\config\\translations";

var fullPath = ConfigPath + "\\translation." + translationsLanguageCode + ".json";

Translations translations = null;
if (File.Exists(fullPath))
{
var input = File.ReadAllText(fullPath);

JsonSerializerSettings jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;

translations = JsonConvert.DeserializeObject<Translations>(input, jsonSettings);
translations.Save(fullPath);
}
else
{
translations = new Translations();
translations.Save(ConfigPath + "\\translation.en.json");
}
return translations;
}

public void Save(string fullPath)
{
var output = JsonConvert.SerializeObject(this, Formatting.Indented, new StringEnumConverter { CamelCaseText = true });

string folder = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}

File.WriteAllText(fullPath, output);
}

public string GetTranslation(TranslationString translationString, params object[] data)
{
string translation = TranslationStrings.FirstOrDefault(t => t.Key.Equals(translationString)).Value;
if (translation != default(string))
return string.Format(translation, data);
return $"Translation for {translationString} is missing";
}

public string GetTranslation(TranslationString translationString)
{
string translation = TranslationStrings.FirstOrDefault(t => t.Key.Equals(translationString)).Value;
if (translation != default(string))
return translation;
return $"Translation for {translationString} is missing";
}

//Default Translations (ENGLISH)
public List<KeyValuePair<TranslationString, string>> TranslationStrings = new List<KeyValuePair<TranslationString, string>>
{
new KeyValuePair<TranslationString, string>(Common.TranslationString.Pokeball, "PokeBall"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.GreatPokeball, "GreatBall"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.UltraPokeball, "UltraBall"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.MasterPokeball, "MasterBall"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.WrongAuthType, "Unknown AuthType in config.json"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.FarmPokestopsOutsideRadius, "You're outside of your defined radius! Walking to start ({0}m away) in 5 seconds. Is your Coords.ini file correct?"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.FarmPokestopsNoUsableFound, "No usable PokeStops found in your area. Is your maximum distance too small?"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventFortUsed, "XP: {0}, Gems: {1}, Items: {2}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventFortTargeted, "{0} in ({1}m)"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventProfileLogin, "Playing as {0}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventUsedLuckyEgg, "Used Lucky Egg, remaining: {0}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventPokemonEvolvedSuccess, "{0} successfully for {1}xp"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventPokemonEvolvedFailed, "Failed {0}. Result was {1}, stopping evolving {2}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventPokemonTransferred, "{0}\t- CP: {1} IV: {2}% [Best CP: {3} IV: {4}%] (Candies: {5})"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventItemRecycled, "{0}x {1}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventPokemonCapture, "({0}) | ({1}) {2} Lvl: {3} CP: ({4}/{5}) IV: {6}% | Chance: {7}% | {8}m dist | with a {9} ({10} left). | {11}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.EventNoPokeballs, "No Pokeballs - We missed a {0} with CP {1}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.CatchStatusAttempt, "{0} Attempt #{1}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.CatchStatus, "{0}"),
new KeyValuePair<TranslationString, string>(Common.TranslationString.Candies, "Candies: {0}")
};
}

public enum TranslationString
{
Pokeball,
GreatPokeball,
UltraPokeball,
MasterPokeball,
LogLevelDebug,
LogLevelPokestop,
WrongAuthType,
FarmPokestopsOutsideRadius,
FarmPokestopsNoUsableFound,
EventFortUsed,
EventFortTargeted,
EventProfileLogin,
EventUsedLuckyEgg,
EventPokemonEvolvedSuccess,
EventPokemonEvolvedFailed,
EventPokemonTransferred,
EventItemRecycled,
EventPokemonCapture,
EventNoPokeballs,
CatchStatusAttempt,
CatchStatus,
Candies,
}
}
1 change: 1 addition & 0 deletions PoGo.NecroBot.Logic/ILogicSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface ILogicSettings
float EvolveAboveIvValue { get; }
bool RenameAboveIv { get; }
int AmountOfPokemonToDisplayOnStart { get; }
string TranslationLanguageCode { get; }

ICollection<KeyValuePair<ItemId, int>> ItemRecycleFilter { get; }

Expand Down
5 changes: 5 additions & 0 deletions PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<HintPath>..\packages\Google.Protobuf.3.0.0-beta4\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="POGOProtos, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\POGOProtos.1.3.0\lib\net45\POGOProtos.dll</HintPath>
<Private>True</Private>
Expand All @@ -51,6 +55,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Common\Translations.cs" />
<Compile Include="Event\DisplayHighestsPokemonEvent.cs" />
<Compile Include="Event\FortTargetEvent.cs" />
<Compile Include="Event\FortUsedEvent.cs" />
Expand Down
5 changes: 4 additions & 1 deletion PoGo.NecroBot.Logic/State/Context.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#region using directives

using PoGo.NecroBot.Logic.Common;
using PokemonGo.RocketAPI;
using POGOProtos.Networking.Responses;

Expand All @@ -13,7 +14,7 @@ public Context(ISettings settings, ILogicSettings logicSettings)
{
Settings = settings;
LogicSettings = logicSettings;

Translations = Translations.Load(logicSettings.TranslationLanguageCode);
Reset(settings, LogicSettings);
}

Expand All @@ -30,6 +31,8 @@ public Context(ISettings settings, ILogicSettings logicSettings)

public LogicClient LogicClient { get; private set; }

public Translations Translations { get; private set; }

public void Reset(ISettings settings, ILogicSettings logicSettings)
{
Client = new Client(Settings);
Expand Down
Loading