Skip to content

Make constructor lightweight #388

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 5 commits into from
Jul 31, 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
37 changes: 31 additions & 6 deletions src/main/java/com/pokegoapi/api/PokemonGo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.pokegoapi.api;

import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass;
import POGOProtos.Networking.Envelopes.RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo;
import com.pokegoapi.api.inventory.Inventories;
import com.pokegoapi.api.map.Map;
import com.pokegoapi.api.player.PlayerProfile;
Expand All @@ -41,7 +42,6 @@ public class PokemonGo {
Map map;
@Getter
private PlayerProfile playerProfile;
@Getter
private Inventories inventories;
@Getter
@Setter
Expand All @@ -53,7 +53,6 @@ public class PokemonGo {
@Setter
private double altitude;
private CredentialProvider credentialProvider;
@Getter
private Settings settings;

/**
Expand All @@ -77,10 +76,7 @@ public PokemonGo(CredentialProvider credentialProvider, OkHttpClient client, Tim

// send profile request to get the ball rolling
requestHandler = new RequestHandler(this, client);

playerProfile = new PlayerProfile(this);
inventories = new Inventories(this);
settings = new Settings(this);

// should have proper end point now.
map = new Map(this);
Expand All @@ -106,7 +102,7 @@ public PokemonGo(CredentialProvider credentialProvider, OkHttpClient client)
* @return AuthInfo object
* @throws LoginFailedException when login fails
*/
public RequestEnvelopeOuterClass.RequestEnvelope.AuthInfo getAuthInfo()
public AuthInfo getAuthInfo()
throws LoginFailedException, RemoteServerException {
return credentialProvider.getAuthInfo();
}
Expand All @@ -127,4 +123,33 @@ public void setLocation(double latitude, double longitude, double altitude) {
public long currentTimeMillis() {
return time.currentTimeMillis();
}

/**
* Get the inventories API
*
* @return Inventories
* @throws LoginFailedException when login fails
* @throws RemoteServerException when server down/issue
*/
public Inventories getInventories() throws LoginFailedException, RemoteServerException {
if (inventories == null) {
inventories = new Inventories(this);
}
return inventories;
}


/**
* Get the settings API
*
* @return Settings
* @throws LoginFailedException when login fails
* @throws RemoteServerException when server down/issue
*/
public Settings getSettings() throws LoginFailedException, RemoteServerException {
if (settings == null) {
settings = new Settings(this);
}
return settings;
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/pokegoapi/api/inventory/PokeBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


public class PokeBank {
@Getter
List<Pokemon> pokemons = new ArrayList<Pokemon>();
List<Pokemon> pokemons = Collections.synchronizedList(new ArrayList<Pokemon>());
@Getter
PokemonGo instance;

Expand Down
70 changes: 30 additions & 40 deletions src/main/java/com/pokegoapi/api/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import POGOProtos.Map.Fort.FortDataOuterClass.FortData;
import POGOProtos.Map.Fort.FortTypeOuterClass.FortType;
import POGOProtos.Map.MapCellOuterClass;
import POGOProtos.Map.MapCellOuterClass.MapCell;
import POGOProtos.Map.Pokemon.MapPokemonOuterClass.MapPokemon;
import POGOProtos.Map.Pokemon.NearbyPokemonOuterClass;
import POGOProtos.Map.Pokemon.WildPokemonOuterClass;
Expand All @@ -30,12 +31,14 @@
import POGOProtos.Networking.Requests.Messages.GetMapObjectsMessageOuterClass;
import POGOProtos.Networking.Requests.Messages.GetMapObjectsMessageOuterClass.GetMapObjectsMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
import POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse;
import POGOProtos.Networking.Responses.EncounterResponseOuterClass.EncounterResponse;
import POGOProtos.Networking.Responses.FortDetailsResponseOuterClass;
import POGOProtos.Networking.Responses.FortSearchResponseOuterClass.FortSearchResponse;
import POGOProtos.Networking.Responses.GetMapObjectsResponseOuterClass;

import POGOProtos.Networking.Responses.GetMapObjectsResponseOuterClass.GetMapObjectsResponse;
import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.annimon.stream.function.Function;
Expand All @@ -54,6 +57,7 @@
import com.pokegoapi.main.AsyncServerRequest;
import com.pokegoapi.main.ServerRequest;

import com.pokegoapi.util.DummyFuture;
import com.pokegoapi.util.FutureWrapper;
import com.pokegoapi.util.PokemonFuture;
import lombok.Getter;
Expand All @@ -67,42 +71,31 @@


public class Map {

private static int CELL_WIDTH = 3;
// time between getting a new MapObjects
private static int RESEND_REQUEST = 5000;
private final PokemonGo api;
private MapObjects cachedMapObjects;
@Getter
@Setter
private boolean useCache;

@Setter
@Getter
private long mapObjectsExpiry;

private long lastMapUpdate;

/**
* Instantiates a new Map.
*
* @param api the api
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public Map(PokemonGo api) throws LoginFailedException, RemoteServerException {
this.api = api;
cachedMapObjects = new MapObjects(api);
lastMapUpdate = 0;
useCache = true;
}

public void clearCache() {
this.lastMapUpdate = 0;
this.cachedMapObjects = new MapObjects(api);
}

/**
* Returns a list of catchable pokemon around the current location.
*
* @return a List of CatchablePokemon at your current location
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public PokemonFuture<List<CatchablePokemon>> getCatchablePokemonAsync() {
List<Long> cellIds = getDefaultCells();
Expand Down Expand Up @@ -141,6 +134,8 @@ public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException,
* 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
*/
public PokemonFuture<List<NearbyPokemon>> getNearbyPokemonAsync() {
return new FutureWrapper<MapObjects, List<NearbyPokemon>>(getMapObjectsAsync(getDefaultCells())) {
Expand Down Expand Up @@ -202,8 +197,6 @@ public List<Point> getSpawnPoints() throws LoginFailedException, RemoteServerExc
* Get a list of gyms near the current location.
*
* @return List of gyms
* @throws LoginFailedException if the login failed
* @throws RemoteServerException When a buffer exception is thrown
*/
public PokemonFuture<List<Gym>> getGymsAsync() {
return new FutureWrapper<MapObjects, List<Gym>>(getMapObjectsAsync(getDefaultCells())) {
Expand Down Expand Up @@ -285,11 +278,12 @@ public PokemonFuture<MapObjects> getMapObjectsAsync(int width) {
* @return MapObjects in the given cells
*/
public PokemonFuture<MapObjects> getMapObjectsAsync(List<Long> cellIds) {
if (useCache && (api.currentTimeMillis() - lastMapUpdate > mapObjectsExpiry)) {
lastMapUpdate = 0;
cachedMapObjects = new MapObjects(api);

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

lastMapUpdate = api.currentTimeMillis();
GetMapObjectsMessage.Builder builder = GetMapObjectsMessageOuterClass.GetMapObjectsMessage.newBuilder()
.setLatitude(api.getLatitude())
.setLongitude(api.getLongitude());
Expand All @@ -298,33 +292,32 @@ public PokemonFuture<MapObjects> getMapObjectsAsync(List<Long> cellIds) {
for (Long cellId : cellIds) {
builder.addCellId(cellId);
long time = 0;

builder.addSinceTimestampMs(lastMapUpdate);
builder.addSinceTimestampMs(0);
index++;

}
final AsyncServerRequest asyncServerRequest = new AsyncServerRequest(
RequestTypeOuterClass.RequestType.GET_MAP_OBJECTS, builder.build());
RequestType.GET_MAP_OBJECTS, builder.build());
return new FutureWrapper<ByteString, MapObjects>(api.getRequestHandler()
.sendAsyncServerRequests(asyncServerRequest)) {
@Override
protected MapObjects handle(ByteString byteString) throws RemoteServerException {
GetMapObjectsResponseOuterClass.GetMapObjectsResponse response;
GetMapObjectsResponse response;
try {
response = GetMapObjectsResponseOuterClass.GetMapObjectsResponse.parseFrom(byteString);
response = GetMapObjectsResponse.parseFrom(byteString);
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}

MapObjects result = new MapObjects(api);
for (MapCellOuterClass.MapCell mapCell : response.getMapCellsList()) {
cachedMapObjects = result;
for (MapCell mapCell : response.getMapCellsList()) {
result.addNearbyPokemons(mapCell.getNearbyPokemonsList());
result.addCatchablePokemons(mapCell.getCatchablePokemonsList());
result.addWildPokemons(mapCell.getWildPokemonsList());
result.addDecimatedSpawnPoints(mapCell.getDecimatedSpawnPointsList());
result.addSpawnPoints(mapCell.getSpawnPointsList());


java.util.Map<FortType, List<FortData>> groupedForts = Stream.of(mapCell.getFortsList())
.collect(Collectors.groupingBy(new Function<FortData, FortType>() {
@Override
Expand All @@ -335,11 +328,8 @@ public FortType apply(FortData fortData) {
result.addGyms(groupedForts.get(FortType.GYM));
result.addPokestops(groupedForts.get(FortType.CHECKPOINT));
}
if (useCache) {
cachedMapObjects.update(result);
result = cachedMapObjects;
lastMapUpdate = api.currentTimeMillis();
}



return result;
}
Expand Down Expand Up @@ -371,7 +361,7 @@ public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteSe
}

/**
* Returns 9x9 cells with the requested lattitude/longitude in the center cell.
* Returns 5x5 cells with the requested lattitude/longitude in the center cell.
*
* @param latitude latitude
* @param longitude longitude
Expand All @@ -382,7 +372,7 @@ public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteSe
@Deprecated
public MapObjects getMapObjects(double latitude, double longitude)
throws LoginFailedException, RemoteServerException {
return getMapObjects(latitude, longitude, 9);
return getMapObjects(latitude, longitude, CELL_WIDTH);
}

/**
Expand Down Expand Up @@ -496,7 +486,7 @@ public PokemonFuture<FortDetails> getFortDetailsAsync(String id, long lon, long
.setLongitude(lon)
.build();

AsyncServerRequest serverRequest = new AsyncServerRequest(RequestTypeOuterClass.RequestType.FORT_DETAILS,
AsyncServerRequest serverRequest = new AsyncServerRequest(RequestType.FORT_DETAILS,
reqMsg);
return new FutureWrapper<ByteString, FortDetails>(api.getRequestHandler()
.sendAsyncServerRequests(serverRequest)) {
Expand Down Expand Up @@ -545,7 +535,7 @@ public FortSearchResponse searchFort(FortData fortData) throws LoginFailedExcept
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.FORT_SEARCH, reqMsg);
ServerRequest serverRequest = new ServerRequest(RequestType.FORT_SEARCH, reqMsg);

api.getRequestHandler().sendServerRequests(serverRequest);

Expand Down Expand Up @@ -576,7 +566,7 @@ public EncounterResponse encounterPokemon(MapPokemon catchablePokemon)
.setPlayerLongitude(api.getLongitude())
.setSpawnPointId(catchablePokemon.getSpawnPointId())
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.ENCOUNTER, reqMsg);
ServerRequest serverRequest = new ServerRequest(RequestType.ENCOUNTER, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest);

EncounterResponse response;
Expand Down Expand Up @@ -618,7 +608,7 @@ public CatchPokemonResponse catchPokemon(
.setSpinModifier(spinModifier)
.setPokeball(pokeball)
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.CATCH_POKEMON, reqMsg);
ServerRequest serverRequest = new ServerRequest(RequestType.CATCH_POKEMON, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest);

CatchPokemonResponse response;
Expand All @@ -632,7 +622,7 @@ public CatchPokemonResponse catchPokemon(


private List<Long> getDefaultCells() {
return getCellIds(api.getLatitude(), api.getLongitude(), 9);
return getCellIds(api.getLatitude(), api.getLongitude(), CELL_WIDTH);
}

}
22 changes: 12 additions & 10 deletions src/main/java/com/pokegoapi/api/map/MapObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,26 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

@ToString
public class MapObjects {

@Getter
Collection<NearbyPokemon> nearbyPokemons = new ArrayList<NearbyPokemon>();
Collection<NearbyPokemon> nearbyPokemons = Collections.synchronizedCollection(new ArrayList<NearbyPokemon>());
@Getter
Collection<MapPokemon> catchablePokemons = new ArrayList<MapPokemon>();
Collection<MapPokemon> catchablePokemons = Collections.synchronizedCollection(new ArrayList<MapPokemon>());
@Getter
Collection<WildPokemon> wildPokemons = new ArrayList<WildPokemon>();
Collection<WildPokemon> wildPokemons = Collections.synchronizedCollection(new ArrayList<WildPokemon>());
@Getter
Collection<SpawnPoint> decimatedSpawnPoints = new ArrayList<SpawnPoint>();
Collection<SpawnPoint> decimatedSpawnPoints = Collections.synchronizedCollection(new ArrayList<SpawnPoint>());
@Getter
Collection<SpawnPoint> spawnPoints = new ArrayList<SpawnPoint>();
Collection<SpawnPoint> spawnPoints = Collections.synchronizedCollection(new ArrayList<SpawnPoint>());
@Getter
Collection<FortData> gyms = new ArrayList<FortData>();
Collection<FortData> gyms = Collections.synchronizedCollection(new ArrayList<FortData>());
@Getter
Collection<Pokestop> pokestops = new ArrayList<>();
Collection<Pokestop> pokestops = Collections.synchronizedCollection(new ArrayList<Pokestop>());
boolean complete = false;
private PokemonGo api;

Expand Down Expand Up @@ -167,6 +168,7 @@ public boolean isComplete() {
* updates the object.
* @param other Update this {@link MapObjects} data with the provided data.
*/
@Deprecated
public void update(MapObjects other) {

nearbyPokemons.clear();
Expand All @@ -185,7 +187,7 @@ public void update(MapObjects other) {
addSpawnPoints(other.getSpawnPoints());


for (FortData otherGym: other.getGyms()) {
/* for (FortData otherGym: other.getGyms()) {
Iterator<FortData> iterator = gyms.iterator();
while (iterator.hasNext()) {
FortData gym = iterator.next();
Expand All @@ -197,7 +199,7 @@ public void update(MapObjects other) {
gyms.add(otherGym);
}

for (Pokestop otherPokestop: other.getPokestops()) {
/*for (Pokestop otherPokestop: other.getPokestops()) {
Iterator<Pokestop> iterator = pokestops.iterator();
while (iterator.hasNext()) {
Pokestop pokestop = iterator.next();
Expand All @@ -207,6 +209,6 @@ public void update(MapObjects other) {
}
}
pokestops.add(otherPokestop);
}
}*/
}
}
Loading