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..f4a9920df --- /dev/null +++ b/PoGo.NecroBot.CLI/WebSocketHandler/BasicGetCommands/Tasks/TransferPokemonTask.cs @@ -0,0 +1,53 @@ +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) + { + 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_ + }); + + 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); + } + } +} 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