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 user to manually evolve through the GUI #1902

Merged
merged 2 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
5 changes: 5 additions & 0 deletions PoGo.NecroBot.CLI/PoGo.NecroBot.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,20 @@
<Compile Include="SimpleSession.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Events\WebResponce.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Events\EggListResponce.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Events\ItemListResponce.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Events\TrainerProfileResponce.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetEggListHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetItemsListHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetPokemonSettingsHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\GetTrainerProfileHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Helpers\TrainerProfileWeb.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\GetPokemonSettingsTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\GetTrainerProfileTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\EvolvePokemonTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\TransferPokemonTask.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\EvolvePokemonHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\TransferPokemonHandler.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Helpers\EggListWeb.cs" />
<Compile Include="WebSocketHandler\BasicGetCommands\Tasks\GetEggListTask.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Events
{
public class WebResponce : IWebSocketResponce
{
public string RequestID { get; set; }
public string Command { get; set; }
public dynamic Data { get; set; }

}
}
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 EvolvePokemonHandler : IWebSocketRequestHandler
{
public string Command { get; private set;}

public EvolvePokemonHandler()
{
Command = "EvolvePokemon";
}

public async Task Handle(ISession session, WebSocketSession webSocketSession, dynamic message)
{
await EvolvePokemonTask.Execute(session, webSocketSession, (ulong)message.PokemonId, (string)message.RequestID);
}
}
}
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 GetPokemonSettingsHandler : IWebSocketRequestHandler
{
public string Command { get; private set;}

public GetPokemonSettingsHandler()
{
Command = "GetPokemonSettings";
}

public async Task Handle(ISession session, WebSocketSession webSocketSession, dynamic message)
{
await GetPokemonSettingsTask.Execute(session, webSocketSession, (string)message.RequestID);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 EvolvePokemonTask
{
public static async Task Execute(ISession session, WebSocketSession webSocketSession, ulong pokemonId, string requestID)
{
await Logic.Tasks.EvolveSpecificPokemonTask.Execute(session, pokemonId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using PoGo.NecroBot.CLI.WebSocketHandler.BasicGetCommands.Events;
using PoGo.NecroBot.Logic.State;
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 GetPokemonSettingsTask
{

public static async Task Execute(ISession session, WebSocketSession webSocketSession, string requestID)
{
var settings = await session.Inventory.GetPokemonSettings();
webSocketSession.Send(EncodingHelper.Serialize(new WebResponce()
{
Command = "PokemonSettings",
Data = settings,
RequestID = requestID
}));

await Task.Delay(500);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,7 @@ 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);
await Logic.Tasks.TransferPokemonTask.Execute(session, pokemonId);
}
}
}
26 changes: 22 additions & 4 deletions PoGo.NecroBot.CLI/WebSocketHandler/EncodingHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,14 +13,31 @@ class EncodingHelper

public static string Serialize(dynamic evt)
{
var jsonSerializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.None
};
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());
}
}
}
}
2 changes: 1 addition & 1 deletion PoGo.NecroBot.Logic/Event/PokemonListEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace PoGo.NecroBot.Logic.Event
{
public class PokemonListEvent : IEvent
{
public List<Tuple<PokemonData, double>> PokemonList;
public List<Tuple<PokemonData, double, int>> PokemonList;
}
}
2 changes: 2 additions & 0 deletions PoGo.NecroBot.Logic/PoGo.NecroBot.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
<Compile Include="Tasks\RecycleItemsTask.cs" />
<Compile Include="Tasks\RenamePokemonTask.cs" />
<Compile Include="Tasks\SnipePokemonTask.cs" />
<Compile Include="Tasks\EvolveSpecificPokemonTask.cs" />
<Compile Include="Tasks\TransferPokemonTask.cs" />
<Compile Include="Tasks\TransferDuplicatePokemonTask.cs" />
<Compile Include="Event\UseLuckyEggEvent.cs" />
<Compile Include="State\VersionCheckState.cs" />
Expand Down
37 changes: 37 additions & 0 deletions PoGo.NecroBot.Logic/Tasks/EvolveSpecificPokemonTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region using directives

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.PoGoUtils;
using PoGo.NecroBot.Logic.State;
using PoGo.NecroBot.Logic.Utils;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.Common;

#endregion

namespace PoGo.NecroBot.Logic.Tasks
{
public class EvolveSpecificPokemonTask
{
public static async Task Execute(ISession session, ulong pokemonId)
{
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 evolveResponse = await session.Client.Inventory.EvolvePokemon(pokemon.Id);

session.EventDispatcher.Send(new PokemonEvolveEvent
{
Id = pokemon.PokemonId,
Exp = evolveResponse.ExperienceAwarded,
Result = evolveResponse.Result
});
}
}
}
17 changes: 16 additions & 1 deletion PoGo.NecroBot.Logic/Tasks/PokemonListTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,23 @@ public class PokemonListTask
{
public static async Task Execute(ISession session)
{
var myPokemonSettings = await session.Inventory.GetPokemonSettings();
var pokemonSettings = myPokemonSettings.ToList();

var myPokemonFamilies = await session.Inventory.GetPokemonFamilies();
var pokemonFamilies = myPokemonFamilies.ToArray();

var allPokemonInBag = await session.Inventory.GetHighestsCp(1000);
var pkmWithIv = allPokemonInBag.Select(p => Tuple.Create(p, PokemonInfo.CalculatePokemonPerfection(p)));

var pkmWithIv = allPokemonInBag.Select(p => {
var settings = pokemonSettings.Single(x => x.PokemonId == p.PokemonId);
return Tuple.Create(
p,
PokemonInfo.CalculatePokemonPerfection(p),
pokemonFamilies.Single(x => settings.FamilyId == x.FamilyId).Candy_
);
});

session.EventDispatcher.Send(
new PokemonListEvent
{
Expand Down
56 changes: 56 additions & 0 deletions PoGo.NecroBot.Logic/Tasks/TransferPokemonTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#region using directives

using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using PoGo.NecroBot.Logic.Event;
using PoGo.NecroBot.Logic.PoGoUtils;
using PoGo.NecroBot.Logic.State;
using PoGo.NecroBot.Logic.Utils;
using PoGo.NecroBot.Logic.Logging;
using PoGo.NecroBot.Logic.Common;

#endregion

namespace PoGo.NecroBot.Logic.Tasks
{
public class TransferPokemonTask
{
public static async Task Execute(ISession session, ulong pokemonId)
{
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_
});

DelayingUtils.Delay(session.LogicSettings.DelayBetweenPlayerActions, 0);
}
}
}