From 6b0d1762266c6c335e9c4632c7360f3ea07c4c66 Mon Sep 17 00:00:00 2001 From: Nicolas Schmitt Date: Mon, 1 Aug 2016 11:14:41 +0200 Subject: [PATCH 1/3] Websocket: Fix JSON --- PoGo.NecroBot.CLI/WebSocketInterface.cs | 28 +++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/PoGo.NecroBot.CLI/WebSocketInterface.cs b/PoGo.NecroBot.CLI/WebSocketInterface.cs index 4aba85e6a..5ad39b590 100644 --- a/PoGo.NecroBot.CLI/WebSocketInterface.cs +++ b/PoGo.NecroBot.CLI/WebSocketInterface.cs @@ -11,6 +11,7 @@ using SuperSocket.SocketBase; using SuperSocket.SocketBase.Config; using SuperSocket.WebSocket; +using System; #endregion @@ -159,12 +160,31 @@ public void Listen(IEvent evt, Session session) private string Serialize(dynamic evt) { - var jsonSerializerSettings = new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.All - }; + var jsonSerializerSettings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }; + + // Add custom seriaizer to convert uong to string (ulong shoud not appear to json according to json specs) + jsonSerializerSettings.Converters.Add(new IdToStringConverter()); return JsonConvert.SerializeObject(evt, Formatting.None, jsonSerializerSettings); } } + + public class IdToStringConverter : JsonConverter + { + public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) + { + JToken jt = JValue.ReadFrom(reader); + return jt.Value(); + } + + public override bool CanConvert(Type objectType) + { + return typeof(System.Int64).Equals(objectType) || typeof(ulong).Equals(objectType); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.ToString()); + } + } } \ No newline at end of file From fdec79f9d6e556d1b230aa71b11c229465a14541 Mon Sep 17 00:00:00 2001 From: Nicolas Schmitt Date: Mon, 1 Aug 2016 12:08:45 +0200 Subject: [PATCH 2/3] Websocket: Allow Pokemon transfer from the GUI --- PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj | 4 +- .../Tasks/TransferPokemonTask.cs | 60 +++++++++++++++++++ .../TransferPokemonHandler.cs | 26 ++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs create mode 100644 PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/TransferPokemonHandler.cs diff --git a/PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj b/PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj index 7103ee98c..13d972f56 100644 --- a/PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj +++ b/PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj @@ -113,6 +113,8 @@ + + @@ -278,4 +280,4 @@ foreach (var item in filesToCleanup) --> - + \ No newline at end of file diff --git a/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs new file mode 100644 index 000000000..a71d92176 --- /dev/null +++ b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs @@ -0,0 +1,60 @@ +using PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Events; +using PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Helpers; +using PoGo.NecroBot.Logic.State; +using POGOProtos.Inventory.Item; +using SuperSocket.WebSocket; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Tasks +{ + class TransferPokemonTask + { + public static async Task Execute(ISession session, WebSocketSession webSocketSession, ulong pokemonId, string requestID) + { + try + { + var all = await session.Inventory.GetPokemons(); + var pokemons = all.OrderByDescending(x => x.Cp).ThenBy(n => n.StaminaMax); + var pokemon = pokemons.FirstOrDefault(p => p.Id == pokemonId); + + if (pokemon == null) return; + + var pokemonSettings = await session.Inventory.GetPokemonSettings(); + var pokemonFamilies = await session.Inventory.GetPokemonFamilies(); + + await session.Client.Inventory.TransferPokemon(pokemonId); + await session.Inventory.DeletePokemonFromInvById(pokemonId); + + var bestPokemonOfType = (session.LogicSettings.PrioritizeIvOverCp + ? await session.Inventory.GetHighestPokemonOfTypeByIv(pokemon) + : await session.Inventory.GetHighestPokemonOfTypeByCp(pokemon)) ?? pokemon; + + var setting = pokemonSettings.Single(q => q.PokemonId == pokemon.PokemonId); + var family = pokemonFamilies.First(q => q.FamilyId == setting.FamilyId); + + family.Candy_++; + + // Broadcast event as everyone would benefit + session.EventDispatcher.Send(new Logic.Event.TransferPokemonEvent + { + Id = pokemon.PokemonId, + Perfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(pokemon), + Cp = pokemon.Cp, + BestCp = bestPokemonOfType.Cp, + BestPerfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(bestPokemonOfType), + FamilyCandies = family.Candy_ + }); + } + catch (Exception ex) + { + ex.ToString(); + } + + await Task.Delay(500); + } + } +} diff --git a/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/TransferPokemonHandler.cs b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/TransferPokemonHandler.cs new file mode 100644 index 000000000..c205abe92 --- /dev/null +++ b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/TransferPokemonHandler.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SuperSocket.WebSocket; +using PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Tasks; +using PoGo.NecroBot.Logic.State; + +namespace PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands +{ + public class TransferPokemonHandler : IWebSocketRequestHandler + { + public string Command { get; private set;} + + public TransferPokemonHandler() + { + Command = "TransferPokemon"; + } + + public async Task Handle(ISession session, WebSocketSession webSocketSession, dynamic message) + { + await TransferPokemonTask.Execute(session, webSocketSession, (ulong)message.PokemonId, (string)message.RequestID); + } + } +} From e85fd493122a2e54760557aee1336ab9d73beabd Mon Sep 17 00:00:00 2001 From: Nicolas Schmitt Date: Mon, 1 Aug 2016 12:10:51 +0200 Subject: [PATCH 3/3] clean --- .../Tasks/TransferPokemonTask.cs | 67 +++++++++---------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs index a71d92176..f4a9920df 100644 --- a/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs +++ b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs @@ -15,44 +15,37 @@ class TransferPokemonTask { public static async Task Execute(ISession session, WebSocketSession webSocketSession, ulong pokemonId, string requestID) { - try - { - var all = await session.Inventory.GetPokemons(); - var pokemons = all.OrderByDescending(x => x.Cp).ThenBy(n => n.StaminaMax); - var pokemon = pokemons.FirstOrDefault(p => p.Id == pokemonId); - - if (pokemon == null) return; - - var pokemonSettings = await session.Inventory.GetPokemonSettings(); - var pokemonFamilies = await session.Inventory.GetPokemonFamilies(); - - await session.Client.Inventory.TransferPokemon(pokemonId); - await session.Inventory.DeletePokemonFromInvById(pokemonId); - - var bestPokemonOfType = (session.LogicSettings.PrioritizeIvOverCp - ? await session.Inventory.GetHighestPokemonOfTypeByIv(pokemon) - : await session.Inventory.GetHighestPokemonOfTypeByCp(pokemon)) ?? pokemon; - - var setting = pokemonSettings.Single(q => q.PokemonId == pokemon.PokemonId); - var family = pokemonFamilies.First(q => q.FamilyId == setting.FamilyId); - - family.Candy_++; - - // Broadcast event as everyone would benefit - session.EventDispatcher.Send(new Logic.Event.TransferPokemonEvent - { - Id = pokemon.PokemonId, - Perfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(pokemon), - Cp = pokemon.Cp, - BestCp = bestPokemonOfType.Cp, - BestPerfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(bestPokemonOfType), - FamilyCandies = family.Candy_ - }); - } - catch (Exception ex) + var all = await session.Inventory.GetPokemons(); + var pokemons = all.OrderByDescending(x => x.Cp).ThenBy(n => n.StaminaMax); + var pokemon = pokemons.FirstOrDefault(p => p.Id == pokemonId); + + if (pokemon == null) return; + + var pokemonSettings = await session.Inventory.GetPokemonSettings(); + var pokemonFamilies = await session.Inventory.GetPokemonFamilies(); + + await session.Client.Inventory.TransferPokemon(pokemonId); + await session.Inventory.DeletePokemonFromInvById(pokemonId); + + var bestPokemonOfType = (session.LogicSettings.PrioritizeIvOverCp + ? await session.Inventory.GetHighestPokemonOfTypeByIv(pokemon) + : await session.Inventory.GetHighestPokemonOfTypeByCp(pokemon)) ?? pokemon; + + var setting = pokemonSettings.Single(q => q.PokemonId == pokemon.PokemonId); + var family = pokemonFamilies.First(q => q.FamilyId == setting.FamilyId); + + family.Candy_++; + + // Broadcast event as everyone would benefit + session.EventDispatcher.Send(new Logic.Event.TransferPokemonEvent { - ex.ToString(); - } + Id = pokemon.PokemonId, + Perfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(pokemon), + Cp = pokemon.Cp, + BestCp = bestPokemonOfType.Cp, + BestPerfection = Logic.PoGoUtils.PokemonInfo.CalculatePokemonPerfection(bestPokemonOfType), + FamilyCandies = family.Candy_ + }); await Task.Delay(500); }