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

Websocket: Allow Pokemon Transfer from the GUI #1719

Merged
merged 3 commits into from
Aug 1, 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
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());
}
}
}