Skip to content

Commit

Permalink
Fixed memory leak in navigate, added log support, small text changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SippieCup committed Aug 14, 2016
1 parent 2a83b97 commit 28d117e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dekk/pw/pokemate/PokeMate.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private PokeMate() throws LoginFailedException, RemoteServerException {
context.setLng(lng);
go.setLocation(context.getLat().get(), context.getLng().get(), 0);
Time.sleep(5000);
new Update(context).runOnce();
new Update(context).run();

if (Config.isShowUI()) {
PokeMateUI.setPoke(this);
Expand Down
22 changes: 14 additions & 8 deletions src/main/java/dekk/pw/pokemate/Walking.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
import com.google.maps.model.LatLng;
import dekk.pw.pokemate.tasks.Navigate;
import dekk.pw.pokemate.tasks.TagPokestop;
import dekk.pw.pokemate.util.Time;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by TimD on 7/21/2016.
*/
public class Walking {

private static final Logger logger = LogManager.getLogger(Walking.class);
private static final double VARIANCE = Config.getRange();
private static double getSmallRandom() {
return Math.random() * 0.0001 - 0.00005;
Expand All @@ -25,9 +30,9 @@ public static void setLocation(Context context) {

private static void setLocation(boolean random, Context context) {
if (random) {
context.getApi().setLocation(context.getLat().get() + getSmallRandom(), context.getLng().get() + getSmallRandom(), 0);
context.getApi().setLocation(context.getLat().get() + getSmallRandom(), context.getLng().get() + getSmallRandom(), new Random().nextInt(10));
} else {
context.getApi().setLocation(context.getLat().get(), context.getLng().get(), 0);
context.getApi().setLocation(context.getLat().get(), context.getLng().get(), new Random().nextInt(10));
}
}

Expand All @@ -41,16 +46,17 @@ public static void walk( final Context context, S2LatLng end) { // PokeStop Walk
final AtomicDouble stepsRequired = new AtomicDouble(timeRequired / (timeout / 1000D));
double deltaLat = diff.latDegrees() / stepsRequired.get();
double deltaLng = diff.lngDegrees() / stepsRequired.get();

logger.debug("starting to walk");
//Schedule a timer to walk every 200 ms


new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
context.getApi().setLocation(context.getLat().addAndGet(deltaLat), context.getLng().addAndGet(deltaLng), 0);
context.getApi().setLocation(context.getLat().addAndGet(deltaLat), context.getLng().addAndGet(deltaLng), new Random().nextInt(10));
stepsRequired.getAndAdd(-1);
if (stepsRequired.get() <= 0) {
logger.debug("Setting a new destination");
context.getWalking().set(false);
context.addTask(new TagPokestop(context));
context.addTask(new Navigate(context, new LatLng(context.getLat().get() - VARIANCE, context.getLng().get() - VARIANCE),
Expand All @@ -65,7 +71,7 @@ public static void walk(Context context, DirectionsStep[] steps) { // Streets W
context.getWalking().set(true);
if (steps != null) {
for (DirectionsStep step : steps) {
//Log.i("WALKER", "Heading to: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]");
logger.debug("WALKER Heading to: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]");
context.getApi().setLocation(step.startLocation.lat, step.startLocation.lng, 0);
context.getLat().set(step.startLocation.lat);
context.getLng().set(step.startLocation.lng);
Expand All @@ -89,13 +95,13 @@ public static void walk(Context context, DirectionsStep[] steps) { // Streets W
} catch (InterruptedException e) {
e.printStackTrace();
}
// Log.i("WALKER", "Set location: [" + context.getLat().get() + ", " + context.getLng().get() + "]");
logger.debug( "Set location: [" + context.getLat().get() + ", " + context.getLng().get() + "]");
remainingSteps--;
}
//Log.i("WALKER", "Arrived at: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]");
logger.debug( "Arrived at: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]");
}
}else{
System.out.println("WALKING ERROR");
logger.error("WALKING ERROR");
}
context.getWalking().set(false);

Expand Down
28 changes: 18 additions & 10 deletions src/main/java/dekk/pw/pokemate/tasks/CatchPokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.NoSuchItemException;
import com.pokegoapi.exceptions.RemoteServerException;
import dekk.pw.pokemate.Config;
import dekk.pw.pokemate.Context;
import dekk.pw.pokemate.PokeMateUI;
import dekk.pw.pokemate.Walking;
import dekk.pw.pokemate.*;
import dekk.pw.pokemate.util.StringConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -30,6 +29,8 @@
*/
class CatchPokemon extends Task implements Runnable {

private static final Logger logger = LogManager.getLogger(CatchPokemon.class);

CatchPokemon(final Context context) {
super(context);
}
Expand Down Expand Up @@ -96,27 +97,34 @@ public void run() {
log(output + " [IV: " + getIvRatio(p) + "%]");
}
context.setConsoleString("CatchPokemon", String.format("%s [IV: %d%%]", output, getIvRatio(p)));
} catch (LoginFailedException | RemoteServerException e) {
} catch (LoginFailedException e) {
logger.error("Login Failed", e);

} catch (RemoteServerException e) {
context.setConsoleString("CatchPokemon", "Server Error.");
logger.error("Remote Server Exception", e);
}


});
} catch (NullPointerException ex) {
ex.printStackTrace();
} catch (NullPointerException e) {
logger.error("NullPointerException.", e);
}
}
} catch (LoginFailedException | RemoteServerException e) {
//e.printStackTrace();
} catch (LoginFailedException e) {
logger.error("Login Failed", e);

} catch (RemoteServerException e) {
context.setConsoleString("CatchPokemon", "Server Error.");
logger.error("Remote Server Exception", e);
} catch (NoSuchItemException e) {
logger.error("Out of Pokeballs", e);
context.setConsoleString("CatchPokemon","Out of Pokeballs.");
} finally {
context.addTask(new CatchPokemon(context));
}
}


private boolean shouldIgnore(final CatchablePokemon p) {
return !Config.getIgnoreCatchingPokemon().contains(p.getPokemonId());
}
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/dekk/pw/pokemate/tasks/DropItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import com.pokegoapi.exceptions.RemoteServerException;
import dekk.pw.pokemate.Config;
import dekk.pw.pokemate.Context;
import dekk.pw.pokemate.PokeMate;
import dekk.pw.pokemate.PokeMateUI;
import dekk.pw.pokemate.util.StringConverter;
import dekk.pw.pokemate.util.Time;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -18,6 +21,8 @@
*/
class DropItems extends Task implements Runnable {

private static final Logger logger = LogManager.getLogger(DropItems.class);

DropItems(final Context context) {
super(context);
}
Expand All @@ -35,11 +40,13 @@ public void run() {
String removedItem = "Removed " + StringConverter.titleCase(id.name()) + "(x" + countToDrop + ")";
removedItemsString.append(removedItem + " ");
PokeMateUI.toast(removedItem, "Items removed!", "icons/items/" + id.getNumber() + ".png");

}
} catch (RemoteServerException | LoginFailedException e) {
context.setConsoleString("DropItems", "Server Error");
PokeMateUI.toast("Server Error", "DropItems", "icons/items/" + id.getNumber() + ".png");
} catch (LoginFailedException e) {
logger.error("Login Failed", e);
context.setConsoleString("DropItems", "Login Failed.");
} catch (RemoteServerException e) {
context.setConsoleString("DropItems", "Server Error.");
logger.error("Remote Server Exception", e);
}
});
} finally {
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/dekk/pw/pokemate/tasks/EvolvePokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import dekk.pw.pokemate.PokeMateUI;
import dekk.pw.pokemate.util.StringConverter;
import dekk.pw.pokemate.util.Time;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.DataInputStream;
import java.text.SimpleDateFormat;
Expand All @@ -20,6 +22,8 @@
* Created by TimD on 7/22/2016.
*/
class EvolvePokemon extends Task implements Runnable {

private static final Logger logger = LogManager.getLogger(EvolvePokemon.class);
private static final ConcurrentHashMap<Integer, Integer> CANDY_AMOUNTS = new ConcurrentHashMap<>();

static {
Expand All @@ -32,7 +36,7 @@ class EvolvePokemon extends Task implements Runnable {
CANDY_AMOUNTS.put(dis.readInt(), dis.readInt());
}
} catch (Exception e) {
e.printStackTrace();
logger.debug("Error", e);
}
}

Expand All @@ -42,7 +46,6 @@ class EvolvePokemon extends Task implements Runnable {

@Override
public void run() {
// System.out.println("[Evolve] Activating..");
try {
CopyOnWriteArrayList<Pokemon> pokeList = new CopyOnWriteArrayList<>(context.getInventories().getPokebank().getPokemons());
for (Pokemon pokemon : pokeList)
Expand All @@ -61,9 +64,12 @@ public void run() {
}
}
}
} catch (RemoteServerException | LoginFailedException e1) {
context.setConsoleString("EvolvePokemon", "Server Error");
e1.printStackTrace();
} catch (LoginFailedException e) {
logger.error("Login Failed", e);

} catch (RemoteServerException e) {
context.setConsoleString("CatchPokemon", "Server Error.");
logger.debug("Remote Server Exception", e);
} finally {
context.addTask(new EvolvePokemon(context));
}
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/dekk/pw/pokemate/tasks/HatchEgg.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
import com.pokegoapi.exceptions.RemoteServerException;
import dekk.pw.pokemate.Context;
import dekk.pw.pokemate.PokeMateUI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;

class HatchEgg extends Task implements Runnable{

private static final Logger logger = LogManager.getLogger(HatchEgg.class);

HatchEgg(final Context context) {
super(context);
}
Expand All @@ -32,8 +37,12 @@ public void run() {
context.setConsoleString("HatchEgg", "Hatched " + hatchedPokemon.getPokemonId() + " with " + hatchedPokemon.getCp() + " CP " + " - " + details);
}
});
} catch (LoginFailedException | RemoteServerException e) {
} catch (LoginFailedException e) {
logger.error("Login Failed", e);

} catch (RemoteServerException e) {
context.setConsoleString("HatchEgg", "Server Error");
logger.error("Remote Server Exception", e);
} finally {
context.addTask(new HatchEgg(context));
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/dekk/pw/pokemate/tasks/IncubateEgg.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass;
import com.pokegoapi.api.inventory.EggIncubator;
import com.pokegoapi.api.pokemon.EggPokemon;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import dekk.pw.pokemate.Context;
import dekk.pw.pokemate.PokeMateUI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


import java.util.List;
Expand All @@ -15,6 +19,8 @@
*/
class IncubateEgg extends Task implements Runnable{

private static final Logger logger = LogManager.getLogger(IncubateEgg.class);

IncubateEgg(final Context context) {
super(context);
}
Expand All @@ -26,7 +32,7 @@ public void run() {
try {
return !i.isInUse();
} catch (Exception e) {
e.printStackTrace();
logger.error("Incubator Error", e);
return false;
}
}).collect(Collectors.toList());
Expand All @@ -41,8 +47,12 @@ public void run() {
context.setConsoleString("IncubateEgg", eggresult);
}
}
} catch (Exception e) {
e.printStackTrace();
} catch (LoginFailedException e) {
logger.error("Login Failed", e);
context.setConsoleString("IncubateEgg", "Login Error.");
} catch (RemoteServerException e) {
context.setConsoleString("IncubateEgg", "Server Error.");
logger.error("Remote Server Exception", e);
} finally {
context.addTask(new IncubateEgg(context));
}
Expand Down
Loading

0 comments on commit 28d117e

Please sign in to comment.