Skip to content

Commit

Permalink
Merge pull request NecronomiconCoding#1719 from nicoschmitt/transfergui
Browse files Browse the repository at this point in the history
Websocket: Allow Pokemon Transfer from the GUI
  • Loading branch information
NecronomiconCoding authored Aug 1, 2016
2 parents 9a4bea4 + e85fd49 commit 4490eb9
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 5 deletions.
4 changes: 3 additions & 1 deletion PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
<Compile Include="WebSocketHandler\BasicGetCommands\Events\ItemListResponce.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetEggListHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetItemsListHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\TransferPokemonTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\TransferPokemonHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Helpers\EggListWeb.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\GetEggListTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\GetItemListTask.cs" />
Expand Down Expand Up @@ -278,4 +280,4 @@ foreach (var item in filesToCleanup)
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
28 changes: 24 additions & 4 deletions PoGo.NecroBot.CLI/WebSocketInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.WebSocket;
using System;

#endregion

Expand Down Expand Up @@ -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<long>();
}

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());
}
}
}

0 comments on commit 4490eb9

Please sign in to comment.