Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #441 from Necrobot-Private/feature/transfer_bulk
Browse files Browse the repository at this point in the history
Add inventory auto fresh
  • Loading branch information
samuraitruong authored Dec 29, 2016
2 parents edabd4d + 82ae39b commit 7e25d6c
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 65 deletions.
2 changes: 1 addition & 1 deletion FeroxRev
4 changes: 3 additions & 1 deletion PoGo.NecroBot.Logic/Common/Translations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ public enum TranslationString
FailedSendNotification,
TelegramBotStarted,
TelegramNeedChatId,
BulkTransferFailed
BulkTransferFailed,
AutoSnipeDisabled
}

public class Translation : ITranslation
Expand Down Expand Up @@ -596,6 +597,7 @@ public class Translation : ITranslation
new KeyValuePair<TranslationString, string>(TranslationString.AccountBanned, "Probably Permanent Ban!"),
new KeyValuePair<TranslationString, string>(TranslationString.GoogleAPIWarning, "Without a Google Api, you will have 2500 free quota limit, if you reach the maximum quota, try to change your IP. To configure \"GoogleAPIKey\", get API Key in link: https://developers.google.com/maps/documentation/directions/get-api-key"),
new KeyValuePair<TranslationString, string>(TranslationString.Only10kmEggs, "Player below level 20, saving this 10 km Egg for later"),
new KeyValuePair<TranslationString, string>(TranslationString.AutoSnipeDisabled,"Your are out of ball because snipe so fast, you can reduce snipe speed by update MinIVForAutoSnipe or SnipePokemonFilters, Auto snipe will be disable in 5 mins"),
new KeyValuePair<TranslationString, string>(TranslationString.SniperCount, "Sniper count {0}"),
new KeyValuePair<TranslationString, string>(TranslationString.SnipeExceeds, "Sniper need to take a rest before your account is banned"),
new KeyValuePair<TranslationString, string>(TranslationString.CatchExceeds, "You are catching too fast. Your cannot catch another one until {0} seconds later"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ public enum SwitchRules
PokestopSoftban,
CatchFlee,
CatchLimitReached,
SpinPokestopReached
SpinPokestopReached,
EmptyMap
}
public class ActiveSwitchByRuleException : Exception
{
public ActiveSwitchByRuleException() { }
public ActiveSwitchByRuleException(SwitchRules rule, double value)
{
this.MatchedRule = rule;
this.ReachedValue = value;
}
public SwitchRules MatchedRule { get; set; }
public double ReachedValue { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,6 @@ public interface ILogicSettings
List<AuthConfig> Bots { get; }
bool AllowMultipleBot { get; }
CaptchaConfig CaptchaConfig { get; }
int BulkTransferStogareBuffer { get;}
int BulkTransferStogareBuffer { get; }
}
}
9 changes: 8 additions & 1 deletion PoGo.NecroBot.Logic/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public Inventory(Client client, ILogicSettings logicSettings)
{
_client = client;
_logicSettings = logicSettings;
//Inventory update will be call everytime getMabObject call
client.OnInventoryUpdated += (refreshedInventoryData) =>
{
_cachedInventory = refreshedInventoryData;
};
}

private Caching.LRUCache<ItemId, int> pokeballCache = new Caching.LRUCache<ItemId, int>(capacity: 10);
Expand Down Expand Up @@ -246,9 +251,10 @@ public async Task<IEnumerable<PokemonData>> GetDuplicatePokemonToTransfer(

return results;
}

public async Task UpdateCandy(Candy family, int change)
{
return; //in testing, may not need to use this function any more sinceInventory update
var lookupItem = (from item in _cachedInventory.InventoryDelta.InventoryItems
where item.InventoryItemData?.Candy != null
where item.InventoryItemData?.Candy.FamilyId == family.FamilyId
Expand Down Expand Up @@ -467,6 +473,7 @@ group item by item.InventoryItemData?.Candy.FamilyId into family

public async Task AddPokemonToCache(PokemonData data)
{
return;
_cachedInventory.InventoryDelta.InventoryItems.Add(new InventoryItem()
{
InventoryItemData = new InventoryItemData()
Expand Down
19 changes: 0 additions & 19 deletions PoGo.NecroBot.Logic/State/LoginState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public async Task<IState> Execute(ISession session, CancellationToken cancellati
if (session.Settings.AuthType == AuthType.Google || session.Settings.AuthType == AuthType.Ptc)
{
await session.Client.Login.DoLogin();
await LogLoginHistory(session, cancellationToken);
}
else
{
Expand Down Expand Up @@ -189,24 +188,6 @@ public async Task<IState> Execute(ISession session, CancellationToken cancellati
return new LoadSaveState();
}

private async Task LogLoginHistory(ISession session, CancellationToken cancellationToken)
{
string logFile = $"config\\login{DateTime.Now:dd-MM-yyyy}.log";
//if(!File.Exists(logFile) )
//{
// File.CreateText(logFile);
//}

await Task.Run(() =>
{
try {
string username = session.Settings.AuthType == AuthType.Ptc ? session.Settings.PtcUsername : session.Settings.GoogleUsername;
File.AppendAllText(logFile, $"{DateTime.Now:dd-MM-yyyy hh:mm:ss}\t\t{username}\r\n");
}
catch(Exception) { }
});
}

private static async Task CheckLogin(ISession session, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
2 changes: 1 addition & 1 deletion PoGo.NecroBot.Logic/State/Session.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ public List<AuthConfig> Accounts
private List<BotActions> botActions = new List<BotActions>();
public void Reset(ISettings settings, ILogicSettings logicSettings)
{

Client = new Client(settings);
// ferox wants us to set this manually
Inventory = new Inventory(Client, logicSettings);
Navigation = new Navigation(Client, logicSettings);
Navigation.WalkStrategy.UpdatePositionEvent +=
(lat, lng) => this.EventDispatcher.Send(new UpdatePositionEvent { Latitude = lat, Longitude = lng });

}
//TODO : Need add BotManager to manage all feature related to multibot,
public bool ReInitSessionWithNextBot(AuthConfig bot = null, double lat = 0, double lng = 0, double att = 0)
Expand Down
15 changes: 13 additions & 2 deletions PoGo.NecroBot.Logic/State/StateMachine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ public async Task Start(IState initialState, ISession session, string subPath, b
catch (ActiveSwitchByRuleException se)
{
session.EventDispatcher.Send(new WarnEvent { Message = $"Switch bot account activated by : {se.MatchedRule.ToString()} - {se.ReachedValue} " });

if (se.MatchedRule == SwitchRules.EmptyMap)
{
session.BlockCurrentBot(90);
session.ReInitSessionWithNextBot();
}
else
if (se.MatchedRule == SwitchRules.PokestopSoftban)
{
session.BlockCurrentBot();
Expand All @@ -151,12 +156,18 @@ public async Task Start(IState initialState, ISession session, string subPath, b
if (se.MatchedRule == SwitchRules.CatchLimitReached || se.MatchedRule == SwitchRules.SpinPokestopReached)
{
PushNotificationClient.SendNotification(session, $"{se.MatchedRule} - {session.Settings.GoogleUsername}{session.Settings.PtcUsername}", "This bot has reach limit, it will be blocked for 60 mins for safety.", true);
session.EventDispatcher.Send(new WarnEvent() { Message = "You reach limited. bot will sleep for 60 min" });

session.BlockCurrentBot(60);

if (!session.LogicSettings.AllowMultipleBot)
{
session.EventDispatcher.Send(new WarnEvent() { Message = "You reach limited. bot will sleep for 60 min" });
await Task.Delay(60 * 1000 * 60);
}
else
{
session.ReInitSessionWithNextBot();
}
}
else {
if (session.LogicSettings.MultipleBotConfig.StartFromDefaultLocation)
Expand Down
13 changes: 13 additions & 0 deletions PoGo.NecroBot.Logic/Tasks/CatchNearbyPokemonsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using POGOProtos.Enums;
using System.Collections;
using System;
using PoGo.NecroBot.Logic.Exceptions;

#endregion

Expand All @@ -26,6 +27,17 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
cancellationToken.ThrowIfCancellationRequested();
if (!session.LogicSettings.CatchPokemon) return;

if (session.Stats.CatchThresholdExceeds(session))
{
if (session.LogicSettings.AllowMultipleBot && session.LogicSettings.MultipleBotConfig.SwitchOnCatchLimit)
{
throw new Exceptions.ActiveSwitchByRuleException() { MatchedRule = SwitchRules.CatchLimitReached, ReachedValue = session.LogicSettings.CatchPokemonLimit };
}

return;
}


Logger.Write(session.Translation.GetTranslation(TranslationString.LookingForPokemon), LogLevel.Debug);

var nearbyPokemons = await GetNearbyPokemons(session);
Expand Down Expand Up @@ -136,6 +148,7 @@ await CatchPokemonTask.Execute(session, cancellationToken, encounter, pokemon,

public static async Task<IOrderedEnumerable<MapPokemon>> GetNearbyPokemons(ISession session)
{

var mapObjects = await session.Client.Map.GetMapObjects();
session.AddForts(mapObjects.Item1.MapCells.SelectMany(p => p.Forts).ToList());
var pokemons = mapObjects.Item1.MapCells.SelectMany(i => i.CatchablePokemons)
Expand Down
2 changes: 1 addition & 1 deletion PoGo.NecroBot.Logic/Tasks/CatchPokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static async Task<bool> Execute(ISession session,
// Exit if user defined max limits reached
if (session.Stats.CatchThresholdExceeds(session))
{
if(session.LogicSettings.MultipleBotConfig.SwitchOnCatchLimit)
if(session.LogicSettings.AllowMultipleBot && session.LogicSettings.MultipleBotConfig.SwitchOnCatchLimit)
{
throw new Exceptions.ActiveSwitchByRuleException() { MatchedRule = SwitchRules.CatchLimitReached, ReachedValue = session.LogicSettings.CatchPokemonLimit };
}
Expand Down
2 changes: 1 addition & 1 deletion PoGo.NecroBot.Logic/Tasks/EggsListTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class EggsListTask
public static async Task Execute(ISession session)
{
// Refresh inventory so that the player stats are fresh
await session.Inventory.RefreshCachedInventory();
//await session.Inventory.RefreshCachedInventory();

var playerStats = (await session.Inventory.GetPlayerStats()).FirstOrDefault();
if (playerStats == null)
Expand Down
3 changes: 2 additions & 1 deletion PoGo.NecroBot.Logic/Tasks/EvolvePokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,14 @@ private static async Task Evolve(ISession session, List<PokemonData> pokemonToEv
if (family.Candy_ < setting.CandyToEvolve) continue;
// no cancellationToken.ThrowIfCancellationRequested here, otherwise the lucky egg would be wasted.
var evolveResponse = await session.Client.Inventory.EvolvePokemon(pokemon.Id);
if(evolveResponse.Result == POGOProtos.Networking.Responses.EvolvePokemonResponse.Types.Result.Success)
if (evolveResponse.Result == POGOProtos.Networking.Responses.EvolvePokemonResponse.Types.Result.Success)
{
family.Candy_ += -setting.CandyToEvolve;
await session.Inventory.UpdateCandy(family, -setting.CandyToEvolve);
await session.Inventory.DeletePokemonFromInvById(pokemon.Id);
await session.Inventory.AddPokemonToCache(evolveResponse.EvolvedPokemonData);
}
await Task.Delay(50);//hack it to fix log
session.EventDispatcher.Send(new PokemonEvolveEvent
{
Id = pokemon.PokemonId,
Expand Down
8 changes: 6 additions & 2 deletions PoGo.NecroBot.Logic/Tasks/MSniperServiceTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,10 @@ public static async Task CatchWithSnipe(ISession session, MSniperInfo2 encounter

public static async Task Execute(ISession session, CancellationToken cancellationToken)
{
if (session.Stats.CatchThresholdExceeds(session)) return;

if (inProgress || OutOffBallBlock > DateTime.Now)
return;
return;
inProgress = true;

var pth = Path.Combine(Directory.GetCurrentDirectory(), "SnipeMS.json");
Expand All @@ -412,7 +414,7 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
{
session.EventDispatcher.Send(new WarnEvent()
{
Message = "Your are out of ball because snipe so fast, you can reduce snipe speed by update MinIVForAutoSnipe or SnipePokemonFilters, Auto snipe will be disable in 5 mins"
Message = session.Translation.GetTranslation(Common.TranslationString.AutoSnipeDisabled)
});

OutOffBallBlock = DateTime.Now.AddMinutes(5);
Expand Down Expand Up @@ -441,6 +443,8 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
}
foreach (var location in mSniperLocation2)
{
if (session.Stats.CatchThresholdExceeds(session)) break;

if (location.EncounterId > 0 && session.Cache[location.EncounterId.ToString()] != null) continue;

//If bot already catch the same pokemon, and very close this location.
Expand Down
5 changes: 4 additions & 1 deletion PoGo.NecroBot.Logic/Tasks/RecycleItemsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,13 @@ await session.Inventory.GetItemAmountByType(ItemId.ItemIncenseCool) +

foreach (var item in items)
{
if (item.Count <= 0) continue;

cancellationToken.ThrowIfCancellationRequested();

await session.Client.Inventory.RecycleItem(item.ItemId, item.Count);
await session.Inventory.UpdateInventoryItem(item.ItemId, -item.Count);

if (session.LogicSettings.VerboseRecycling)
session.EventDispatcher.Send(new ItemRecycledEvent { Id = item.ItemId, Count = item.Count });

Expand All @@ -123,7 +126,7 @@ private static async Task RecycleItems(ISession session, CancellationToken cance
itemsToKeep = Math.Min(itemsToKeep, maxItemToKeep);
}
itemsToRecycle = itemCount - itemsToKeep;
if (itemsToRecycle != 0)
if (itemsToRecycle > 0)
{
_diff -= itemsToRecycle;
cancellationToken.ThrowIfCancellationRequested();
Expand Down
2 changes: 1 addition & 1 deletion PoGo.NecroBot.Logic/Tasks/TransferDuplicatePokemonTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static async Task Execute(ISession session, CancellationToken cancellatio
}
}

private static async Task PrintPokemonInfo(ISession session, System.Collections.Generic.IEnumerable<POGOProtos.Settings.Master.PokemonSettings> pokemonSettings, System.Collections.Generic.List<POGOProtos.Inventory.Candy> pokemonFamilies, POGOProtos.Data.PokemonData duplicatePokemon)
public static async Task PrintPokemonInfo(ISession session, System.Collections.Generic.IEnumerable<POGOProtos.Settings.Master.PokemonSettings> pokemonSettings, System.Collections.Generic.List<POGOProtos.Inventory.Candy> pokemonFamilies, POGOProtos.Data.PokemonData duplicatePokemon)
{
var bestPokemonOfType = (session.LogicSettings.PrioritizeIvOverCp
? await session.Inventory.GetHighestPokemonOfTypeByIv(duplicatePokemon)
Expand Down
Loading

0 comments on commit 7e25d6c

Please sign in to comment.