Skip to content

Commit 5b44634

Browse files
committed
Merge branch 'mjmfighter-MapAPI_Updates'
2 parents 2a8c38c + 0c286e7 commit 5b44634

4 files changed

Lines changed: 203 additions & 27 deletions

File tree

src/main/java/com/pokegoapi/api/map/Map.java

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,22 @@
1212
* You should have received a copy of the GNU General Public License
1313
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1414
*/
15-
1615
package com.pokegoapi.api.map;
1716

1817
import POGOProtos.Map.Fort.FortDataOuterClass;
1918
import POGOProtos.Map.Fort.FortTypeOuterClass;
2019
import POGOProtos.Map.MapCellOuterClass;
2120
import POGOProtos.Map.Pokemon.MapPokemonOuterClass;
21+
import POGOProtos.Map.Pokemon.NearbyPokemonOuterClass;
2222
import POGOProtos.Map.Pokemon.WildPokemonOuterClass;
23+
import POGOProtos.Map.SpawnPointOuterClass;
2324
import POGOProtos.Networking.Requests.Messages.*;
2425
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
2526
import POGOProtos.Networking.Responses.*;
2627
import com.google.protobuf.InvalidProtocolBufferException;
2728
import com.pokegoapi.api.PokemonGo;
2829
import com.pokegoapi.api.map.Pokemon.CatchablePokemon;
30+
import com.pokegoapi.api.map.Pokemon.NearbyPokemon;
2931
import com.pokegoapi.api.map.fort.FortDetails;
3032
import com.pokegoapi.exceptions.LoginFailedException;
3133
import com.pokegoapi.exceptions.RemoteServerException;
@@ -36,37 +38,38 @@
3638
import java8.util.function.Function;
3739
import java8.util.stream.Collectors;
3840
import java8.util.stream.StreamSupport;
41+
import lombok.Getter;
42+
import lombok.Setter;
3943

4044
import java.util.ArrayList;
4145
import java.util.List;
4246

4347
import static com.pokegoapi.google.common.geometry.S2CellId.MAX_LEVEL;
4448

4549
public class Map {
46-
private static long NEW_MAP_OBJECTS_EXPIRY = 60000; // 60 seconds
47-
private PokemonGo api;
50+
51+
private final PokemonGo api;
52+
private MapObjects cachedMapObjects;
53+
@Getter
54+
@Setter
55+
private boolean useCache;
56+
57+
@Setter
58+
@Getter
59+
private long mapObjectsExpiry;
60+
4861
private long lastMapUpdate;
49-
private MapObjects lastMapObjects;
50-
private long lastLong;
51-
private long lastLat;
5262

5363
public Map(PokemonGo api) {
5464
this.api = api;
65+
cachedMapObjects = new MapObjects(api);
5566
lastMapUpdate = 0;
67+
useCache = true;
5668
}
5769

58-
/**
59-
* Gets a new map objects if there has been a lat/long change or the last request was done greater then NEW_MAP_OBJECTS_EXPIRY
60-
*
61-
* @return List<CatchablePokemon> at your current location
62-
*/
63-
private MapObjects getRetainedMapObject() throws LoginFailedException, RemoteServerException {
64-
// get new MapObjects or used existing one
65-
if (api.getLatitude() != lastLat && api.getLongitude() != lastLong || (System.currentTimeMillis() - lastMapUpdate) > NEW_MAP_OBJECTS_EXPIRY) {
66-
getMapObjects(); // should update the lastMapObjects variable
67-
}
68-
69-
return lastMapObjects;
70+
public void clearCache(){
71+
this.lastMapUpdate = 0;
72+
this.cachedMapObjects = new MapObjects(api);
7073
}
7174

7275
/**
@@ -76,18 +79,66 @@ private MapObjects getRetainedMapObject() throws LoginFailedException, RemoteSer
7679
*/
7780
public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException, RemoteServerException {
7881
List<CatchablePokemon> catchablePokemons = new ArrayList<>();
79-
MapObjects objects = getRetainedMapObject();
82+
MapObjects objects = getMapObjects();
8083

81-
for (MapPokemonOuterClass.MapPokemon mapPokemon : objects.getCatchablePokemons()) {
84+
for(MapPokemonOuterClass.MapPokemon mapPokemon : objects.getCatchablePokemons()){
8285
catchablePokemons.add(new CatchablePokemon(api, mapPokemon));
8386
}
84-
for (WildPokemonOuterClass.WildPokemon wildPokemon : objects.getWildPokemons()) {
87+
88+
for(WildPokemonOuterClass.WildPokemon wildPokemon : objects.getWildPokemons()){
8589
catchablePokemons.add(new CatchablePokemon(api, wildPokemon));
8690
}
8791

8892
return catchablePokemons;
8993
}
9094

95+
/**
96+
* Returns a list of nearby pokemon (non-catchable)
97+
*
98+
* @return List<NearbyPokemon> at your current location
99+
*/
100+
public List<NearbyPokemon> getNearbyPokemon() throws LoginFailedException, RemoteServerException {
101+
List<NearbyPokemon> pokemons = new ArrayList<>();
102+
MapObjects objects = getMapObjects();
103+
104+
for (NearbyPokemonOuterClass.NearbyPokemon pokemon : objects.getNearbyPokemons()) {
105+
pokemons.add(new NearbyPokemon(pokemon));
106+
}
107+
108+
return pokemons;
109+
}
110+
111+
/**
112+
* Returns a list of spawn points
113+
*
114+
* @return List<Point> list of spawn points
115+
*/
116+
public List<Point> getSpawnPoints() throws LoginFailedException, RemoteServerException {
117+
List<Point> points = new ArrayList<>();
118+
MapObjects objects = getMapObjects();
119+
120+
for (SpawnPointOuterClass.SpawnPoint point : objects.getSpawnPoints()) {
121+
points.add(new Point(point));
122+
}
123+
124+
return points;
125+
}
126+
127+
/**
128+
* Returns a list of decimated spawn points at current location
129+
*
130+
* @return List<Point> list of spawn points
131+
*/
132+
public List<Point> getDecimatedSpawnPoints() throws LoginFailedException, RemoteServerException {
133+
List<Point> points = new ArrayList<>();
134+
MapObjects objects = getMapObjects();
135+
136+
for (SpawnPointOuterClass.SpawnPoint point : objects.getDecimatedSpawnPoints()) {
137+
points.add(new Point(point));
138+
}
139+
140+
return points;
141+
}
91142

92143
/**
93144
* Returns MapObjects around your current location
@@ -169,15 +220,20 @@ public MapObjects getMapObjects(List<Long> cellIds, double latitude, double long
169220
* @return MapObjects in the given cells
170221
*/
171222
public MapObjects getMapObjects(List<Long> cellIds) throws LoginFailedException, RemoteServerException {
223+
224+
if(useCache && (System.currentTimeMillis() - lastMapUpdate > mapObjectsExpiry)){
225+
lastMapUpdate = 0;
226+
cachedMapObjects = new MapObjects(api);
227+
}
228+
172229
GetMapObjectsMessageOuterClass.GetMapObjectsMessage.Builder builder = GetMapObjectsMessageOuterClass.GetMapObjectsMessage.newBuilder()
173230
.setLatitude(api.getLatitude())
174231
.setLongitude(api.getLongitude());
175232

176-
int i = 0;
177233
for (Long cellId : cellIds) {
178234
builder.addCellId(cellId);
179235
builder.addSinceTimestampMs(lastMapUpdate);
180-
i++;
236+
181237
}
182238

183239
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.GET_MAP_OBJECTS, builder.build());
@@ -209,8 +265,12 @@ public FortTypeOuterClass.FortType apply(FortDataOuterClass.FortData t) {
209265
result.addPokestops(groupedForts.get(FortTypeOuterClass.FortType.CHECKPOINT));
210266
}
211267

212-
lastMapObjects = result;
213-
lastMapUpdate = System.currentTimeMillis();
268+
if(useCache){
269+
cachedMapObjects.update(result);
270+
result = cachedMapObjects;
271+
lastMapUpdate = System.currentTimeMillis();
272+
}
273+
214274
return result;
215275
}
216276

@@ -244,7 +304,7 @@ public List<Long> getCellIds(double latitude, double longitude, int width) {
244304
return cells;
245305
}
246306

247-
public FortDetails getFortDetails(String id, long lon, long lat) throws LoginFailedException, RemoteServerException {
307+
public FortDetails getFortDetails(String id, double lon, double lat) throws LoginFailedException, RemoteServerException {
248308
FortDetailsMessageOuterClass.FortDetailsMessage reqMsg = FortDetailsMessageOuterClass.FortDetailsMessage.newBuilder()
249309
.setFortId(id)
250310
.setLatitude(lat)
@@ -326,4 +386,4 @@ public CatchPokemonResponseOuterClass.CatchPokemonResponse catchPokemon(MapPokem
326386
}
327387
return response;
328388
}
329-
}
389+
}

src/main/java/com/pokegoapi/api/map/MapObjects.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,42 @@ public void addPokestops(Collection<FortDataOuterClass.FortData> pokestops) {
117117
public boolean isComplete() {
118118
return complete;
119119
}
120+
121+
public void update(MapObjects other){
122+
123+
nearbyPokemons.clear();
124+
addNearbyPokemons(other.getNearbyPokemons());
125+
126+
catchablePokemons.clear();
127+
addCatchablePokemons(other.getCatchablePokemons());
128+
129+
wildPokemons.clear();
130+
addWildPokemons(other.getWildPokemons());
131+
132+
decimatedSpawnPoints.clear();
133+
addDecimatedSpawnPoints(other.getDecimatedSpawnPoints());
134+
135+
spawnPoints.clear();
136+
addSpawnPoints(other.getSpawnPoints());
137+
138+
for(FortDataOuterClass.FortData otherGym : other.getGyms()){
139+
for(FortDataOuterClass.FortData gym : getGyms()){
140+
if(otherGym.getId().equals(gym.getId())){
141+
gyms.remove(gym);
142+
break;
143+
}
144+
}
145+
gyms.add(otherGym);
146+
}
147+
148+
for(Pokestop otherPokestop : other.getPokestops()){
149+
for(Pokestop pokestop : pokestops){
150+
if(otherPokestop.getId().equals(pokestop.getId())){
151+
pokestops.remove(pokestop);
152+
break;
153+
}
154+
}
155+
pokestops.add(otherPokestop);
156+
}
157+
}
120158
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package com.pokegoapi.api.map;
17+
18+
import POGOProtos.Map.SpawnPointOuterClass;
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
22+
public class Point {
23+
@Getter
24+
@Setter
25+
private static double longitude;
26+
@Getter
27+
@Setter
28+
private static double latitude;
29+
30+
public Point(double latitude, double longitude) {
31+
this.latitude = latitude;
32+
this.longitude = longitude;
33+
}
34+
35+
public Point(SpawnPointOuterClass.SpawnPoint spawnpoint) {
36+
this.latitude = spawnpoint.getLatitude();
37+
this.longitude = spawnpoint.getLongitude();
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package com.pokegoapi.api.map.Pokemon;
17+
18+
import POGOProtos.Enums.PokemonIdOuterClass;
19+
import POGOProtos.Map.Pokemon.NearbyPokemonOuterClass;
20+
21+
public class NearbyPokemon {
22+
private NearbyPokemonOuterClass.NearbyPokemon proto;
23+
24+
public NearbyPokemon(NearbyPokemonOuterClass.NearbyPokemon proto) {
25+
this.proto = proto;
26+
}
27+
28+
public PokemonIdOuterClass.PokemonId getPokemonId() {
29+
return proto.getPokemonId();
30+
}
31+
32+
public float getDistanceInMeters() {
33+
return proto.getDistanceInMeters();
34+
}
35+
36+
public long getEncounterId() {
37+
return proto.getEncounterId();
38+
}
39+
}

0 commit comments

Comments
 (0)