Skip to content

Adding Sorting Map Objects #396

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

Merged
merged 13 commits into from
Aug 1, 2016
3 changes: 2 additions & 1 deletion src/main/java/com/pokegoapi/api/gym/Gym.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import com.pokegoapi.util.MapPoint;

import java.util.ArrayList;
import java.util.List;

public class Gym {
public class Gym implements MapPoint{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add space between Point and {

private FortData proto;
private GetGymDetailsResponse details;
private PokemonGo api;
Expand Down
85 changes: 61 additions & 24 deletions src/main/java/com/pokegoapi/api/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.pokegoapi.main.ServerRequest;
import com.pokegoapi.util.DummyFuture;
import com.pokegoapi.util.FutureWrapper;
import com.pokegoapi.util.MapUtil;
import com.pokegoapi.util.PokemonFuture;

import java.util.ArrayList;
Expand Down Expand Up @@ -122,6 +123,19 @@ public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException,
return getCatchablePokemonAsync().toBlocking();
}

/**
* Gets catchable pokemon sort by distance.
*
* @return the catchable pokemon sort
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public java.util.Map<Double, CatchablePokemon> getCatchablePokemonSort()
throws LoginFailedException, RemoteServerException {
MapUtil<CatchablePokemon> util = new MapUtil<>();
return util.sortItems(getCatchablePokemon(), api);
}

/**
* Returns a list of nearby pokemon (non-catchable).
*
Expand All @@ -147,8 +161,8 @@ protected List<NearbyPokemon> handle(MapObjects result) throws RemoteServerExcep
* Returns a list of nearby pokemon (non-catchable).
*
* @return a List of NearbyPokemon at your current location
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public List<NearbyPokemon> getNearbyPokemon() throws LoginFailedException, RemoteServerException {
return getNearbyPokemonAsync().toBlocking();
Expand Down Expand Up @@ -178,8 +192,8 @@ protected List<Point> handle(MapObjects result) throws RemoteServerException {
* Returns a list of spawn points.
*
* @return list of spawn points
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public List<Point> getSpawnPoints() throws LoginFailedException, RemoteServerException {
return getSpawnPointsAsync().toBlocking();
Expand Down Expand Up @@ -214,6 +228,18 @@ public List<Gym> getGyms() throws LoginFailedException, RemoteServerException {
return getGymsAsync().toBlocking();
}

/**
* Gets gym sort by distance.
*
* @return the gym sort
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public java.util.Map<Double, Gym> getGymSort() throws LoginFailedException, RemoteServerException {
MapUtil<Gym> util = new MapUtil<>();
return util.sortItems(getGyms(), api);
}

/**
* Returns a list of decimated spawn points at current location.
*
Expand All @@ -237,13 +263,26 @@ protected List<Point> handle(MapObjects result) throws RemoteServerException {
* Returns a list of decimated spawn points at current location.
*
* @return list of spawn points
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public List<Point> getDecimatedSpawnPoints() throws LoginFailedException, RemoteServerException {
return getDecimatedSpawnPointsAsync().toBlocking();
}


/**
* Gets decimated spawn points sort by distance.
*
* @return the decimated spawn points sort
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public java.util.Map<Double, Point> getDecimatedSpawnPointsSort() throws LoginFailedException, RemoteServerException {
MapUtil<Point> util = new MapUtil<>();
return util.sortItems(getDecimatedSpawnPoints(), api);
}

/**
* Returns MapObjects around your current location.
*
Expand Down Expand Up @@ -271,7 +310,7 @@ public PokemonFuture<MapObjects> getMapObjectsAsync(int width) {
*/
public PokemonFuture<MapObjects> getMapObjectsAsync(List<Long> cellIds) {

if ( (api.currentTimeMillis() - lastMapUpdate) < RESEND_REQUEST ) {
if ((api.currentTimeMillis() - lastMapUpdate) < RESEND_REQUEST) {
return new DummyFuture<MapObjects>(cachedMapObjects);
}

Expand Down Expand Up @@ -322,7 +361,6 @@ public FortType apply(FortData fortData) {
}



return result;
}
};
Expand All @@ -332,8 +370,8 @@ public FortType apply(FortData fortData) {
* Returns MapObjects around your current location.
*
* @return MapObjects at your current location
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public MapObjects getMapObjects() throws LoginFailedException, RemoteServerException {
return getMapObjectsAsync().toBlocking();
Expand All @@ -344,9 +382,8 @@ public MapObjects getMapObjects() throws LoginFailedException, RemoteServerExcep
*
* @param width width
* @return MapObjects at your current location
*
* @throws LoginFailedException If login fails.
* @throws RemoteServerException If request errors occurred.
* @throws LoginFailedException If login fails.
* @throws RemoteServerException If request errors occurred.
*/
public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteServerException {
return getMapObjectsAsync(width).toBlocking();
Expand All @@ -358,8 +395,8 @@ public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteSe
* @param latitude latitude
* @param longitude longitude
* @return MapObjects in the given cells
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
@Deprecated
public MapObjects getMapObjects(double latitude, double longitude)
Expand All @@ -374,8 +411,8 @@ public MapObjects getMapObjects(double latitude, double longitude)
* @param latitude latitude
* @param longitude longitude
* @return MapObjects in the given cells
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
@Deprecated
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude)
Expand All @@ -390,8 +427,8 @@ public MapObjects getMapObjects(List<Long> cellIds, double latitude, double long
* @param longitude longitude
* @param width width
* @return MapObjects in the given cells
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
@Deprecated
public MapObjects getMapObjects(double latitude, double longitude, int width)
Expand All @@ -405,10 +442,10 @@ public MapObjects getMapObjects(double latitude, double longitude, int width)
* @param cellIds cellIds
* @param latitude latitude
* @param longitude longitude
* @param altitude altitude
* @param altitude altitude
* @return MapObjects in the given cells
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
@Deprecated
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude, double altitude)
Expand All @@ -424,8 +461,8 @@ public MapObjects getMapObjects(List<Long> cellIds, double latitude, double long
*
* @param cellIds List of cellId
* @return MapObjects in the given cells
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public MapObjects getMapObjects(List<Long> cellIds) throws LoginFailedException, RemoteServerException {
return getMapObjectsAsync(cellIds).toBlocking();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/pokegoapi/api/map/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
package com.pokegoapi.api.map;

import POGOProtos.Map.SpawnPointOuterClass;
import com.pokegoapi.util.MapPoint;
import lombok.Getter;
import lombok.Setter;

public class Point {
public class Point implements MapPoint{
Copy link
Collaborator

@FabianTerhorst FabianTerhorst Aug 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look above

@Getter
@Setter
private double longitude;
Expand Down
74 changes: 34 additions & 40 deletions src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import com.pokegoapi.api.inventory.ItemBag;
import com.pokegoapi.api.inventory.Pokeball;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.NoSuchItemException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.AsyncServerRequest;
import com.pokegoapi.util.FutureWrapper;
import com.pokegoapi.util.MapPoint;
import com.pokegoapi.util.Log;
import com.pokegoapi.util.NestedFutureWrapper;
import com.pokegoapi.util.PokemonFuture;
Expand All @@ -50,7 +52,7 @@
* The type Catchable pokemon.
*/
@ToString
public class CatchablePokemon {
public class CatchablePokemon implements MapPoint{
Copy link
Collaborator

@FabianTerhorst FabianTerhorst Aug 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look above

private static final String TAG = CatchablePokemon.class.getSimpleName();
private final PokemonGo api;

Expand Down Expand Up @@ -174,19 +176,10 @@ public EncounterResult encounterPokemon() throws LoginFailedException,
* @throws RemoteServerException if the server failed to respond
*/
public PokemonFuture<CatchResult> catchPokemonWithRazzBerryAsync()
throws LoginFailedException, RemoteServerException {
final Pokeball pokeball;
throws LoginFailedException, RemoteServerException, NoSuchItemException {
final Pokeball pokeball = getItemBall();


ItemBag bag = api.getInventories().getItemBag();
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
pokeball = Pokeball.POKEBALL;
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
pokeball = Pokeball.GREATBALL;
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
pokeball = Pokeball.ULTRABALL;
} else {
pokeball = Pokeball.MASTERBALL;
}
return new NestedFutureWrapper<CatchItemResult, CatchResult>(useItemAsync(ItemId.ITEM_RAZZ_BERRY)) {
@Override
protected Future<CatchResult> handleFuture(CatchItemResult result) {
Expand All @@ -199,27 +192,40 @@ protected Future<CatchResult> handleFuture(CatchItemResult result) {
}

/**
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
* none will use greatball etc) and uwill use a single razz berry if available.
* Gets item ball to catch a pokemon
*
* @return CatchResult
* @throws LoginFailedException if failed to login
* @throws RemoteServerException if the server failed to respond
* @return the item ball
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
* @throws NoSuchItemException the no such item exception
*/
public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
RemoteServerException {
Pokeball pokeball;

public Pokeball getItemBall() throws LoginFailedException,
RemoteServerException, NoSuchItemException {
ItemBag bag = api.getInventories().getItemBag();
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
pokeball = Pokeball.POKEBALL;
return Pokeball.POKEBALL;
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
pokeball = Pokeball.GREATBALL;
return Pokeball.GREATBALL;
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
pokeball = Pokeball.ULTRABALL;
return Pokeball.ULTRABALL;
} else if (bag.getItem(ItemId.ITEM_MASTER_BALL).getCount() > 0) {
return Pokeball.MASTERBALL;
} else {
pokeball = Pokeball.MASTERBALL;
throw new NoSuchItemException();
}
}

/**
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
* none will use greatball etc) and uwill use a single razz berry if available.
*
* @return CatchResult
* @throws LoginFailedException if failed to login
* @throws RemoteServerException if the server failed to respond
*/
public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
RemoteServerException, NoSuchItemException {
Pokeball pokeball = getItemBall();

useItem(ItemId.ITEM_RAZZ_BERRY);
return catchPokemon(pokeball, -1, -1);
Expand All @@ -234,21 +240,9 @@ public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
* @throws RemoteServerException if the server failed to respond
*/
public CatchResult catchPokemon() throws LoginFailedException,
RemoteServerException {
Pokeball pokeball;

ItemBag bag = api.getInventories().getItemBag();
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
pokeball = Pokeball.POKEBALL;
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
pokeball = Pokeball.GREATBALL;
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
pokeball = Pokeball.ULTRABALL;
} else {
pokeball = Pokeball.MASTERBALL;
}
RemoteServerException, NoSuchItemException {

return catchPokemon(pokeball);
return catchPokemon(getItemBall());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.pokegoapi.api.map.pokemon.EncounterResult;
import com.pokegoapi.auth.PtcCredentialProvider;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.NoSuchItemException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.util.Log;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -78,7 +79,7 @@ public static void main(String[] args) {

}

} catch (LoginFailedException | RemoteServerException e) {
} catch (LoginFailedException | NoSuchItemException | RemoteServerException e) {
// failed to login, invalid credentials, auth issue or server issue.
Log.e("Main", "Failed to login or server issue: ", e);

Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/pokegoapi/util/MapPoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.pokegoapi.util;

/**
* @author Olaf Braun - Software Development
* @version 1.0
*/
public interface MapPoint {
/**
* Gets latitude.
*
* @return the latitude
*/
double getLatitude();

/**
* Gets longitude.
*
* @return the longitude
*/
double getLongitude();
}
Loading