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-
1615package com .pokegoapi .api .map ;
1716
1817import POGOProtos .Map .Fort .FortDataOuterClass ;
1918import POGOProtos .Map .Fort .FortTypeOuterClass ;
2019import POGOProtos .Map .MapCellOuterClass ;
2120import POGOProtos .Map .Pokemon .MapPokemonOuterClass ;
21+ import POGOProtos .Map .Pokemon .NearbyPokemonOuterClass ;
2222import POGOProtos .Map .Pokemon .WildPokemonOuterClass ;
23+ import POGOProtos .Map .SpawnPointOuterClass ;
2324import POGOProtos .Networking .Requests .Messages .*;
2425import POGOProtos .Networking .Requests .RequestTypeOuterClass ;
2526import POGOProtos .Networking .Responses .*;
2627import com .google .protobuf .InvalidProtocolBufferException ;
2728import com .pokegoapi .api .PokemonGo ;
2829import com .pokegoapi .api .map .Pokemon .CatchablePokemon ;
30+ import com .pokegoapi .api .map .Pokemon .NearbyPokemon ;
2931import com .pokegoapi .api .map .fort .FortDetails ;
3032import com .pokegoapi .exceptions .LoginFailedException ;
3133import com .pokegoapi .exceptions .RemoteServerException ;
3638import java8 .util .function .Function ;
3739import java8 .util .stream .Collectors ;
3840import java8 .util .stream .StreamSupport ;
41+ import lombok .Getter ;
42+ import lombok .Setter ;
3943
4044import java .util .ArrayList ;
4145import java .util .List ;
4246
4347import static com .pokegoapi .google .common .geometry .S2CellId .MAX_LEVEL ;
4448
4549public 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+ }
0 commit comments