Skip to content

Commit

Permalink
Merge pull requests Grover-c13#135, Grover-c13#131 and Grover-c13#130
Browse files Browse the repository at this point in the history
Update protobufs
  • Loading branch information
jabbink committed Jul 22, 2016
2 parents 7d9d2ab + 21892d6 commit 4f5b715
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 107 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sourceSets {
main {
proto {
// Need to use custom dir cause Gradle doesn't like us otherwise :(
srcDir 'src/resources/protobuf/pogo'
srcDir 'src/resources/protobuf/src'
include '**/*.proto'
}
}
Expand Down
74 changes: 34 additions & 40 deletions src/main/java/com/pokegoapi/api/inventory/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Data.Player.PlayerStatsOuterClass;
import POGOProtos.Enums.PokemonFamilyIdOuterClass;
import POGOProtos.Enums.PokemonIdOuterClass;
import POGOProtos.Inventory.InventoryItemDataOuterClass;
import POGOProtos.Inventory.InventoryItemOuterClass;
import POGOProtos.Inventory.ItemIdOuterClass;
import POGOProtos.Inventory.ItemOuterClass;
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import POGOProtos.Networking.Requests.Messages.GetInventoryMessageOuterClass.GetInventoryMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse;
Expand All @@ -44,27 +43,30 @@ public class Inventories {
@Getter
private CandyJar candyjar;
@Getter
private PlayerStatsOuterClass.PlayerStats stats;
private Pokedex pokedex;

private long lastInventoryUpdate = 0;

/**
* Creates Inventories and initializes content.
*
* @param api PokemonGo api
* @throws LoginFailedException the login failed exception
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public Inventories(PokemonGo api) throws LoginFailedException, RemoteServerException {
this.api = api;
itemBag = new ItemBag(api);
pokebank = new PokeBank(api);
candyjar = new CandyJar(api);
pokedex = new Pokedex(api);
updateInventories();
}

/**
* Updates the inventories with latest data.
* @throws LoginFailedException the login failed exception
*
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public void updateInventories() throws LoginFailedException, RemoteServerException {
Expand All @@ -73,8 +75,9 @@ public void updateInventories() throws LoginFailedException, RemoteServerExcepti

/**
* Updates the inventories with the latest data.
*
* @param forceUpdate For a full update if true
* @throws LoginFailedException the login failed exception
* @throws LoginFailedException the login failed exception
* @throws RemoteServerException the remote server exception
*/
public void updateInventories(boolean forceUpdate) throws LoginFailedException, RemoteServerException {
Expand All @@ -83,13 +86,13 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
itemBag = new ItemBag(api);
pokebank = new PokeBank(api);
candyjar = new CandyJar(api);
pokedex = new Pokedex(api);
}
GetInventoryMessage invReqMsg = GetInventoryMessage.newBuilder()
.setLastTimestampMs(lastInventoryUpdate)
.build();
ServerRequest inventoryRequest = new ServerRequest(RequestTypeOuterClass.RequestType.GET_INVENTORY, invReqMsg);
api.getRequestHandler().request(inventoryRequest);
api.getRequestHandler().sendServerRequests();
api.getRequestHandler().sendServerRequests(inventoryRequest);

GetInventoryResponse response = null;
try {
Expand All @@ -101,37 +104,28 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
for (InventoryItemOuterClass.InventoryItem inventoryItem
: response.getInventoryDelta().getInventoryItemsList()) {
InventoryItemDataOuterClass.InventoryItemData itemData = inventoryItem.getInventoryItemData();
if (inventoryItem.getDeletedItemKey() > 0) {
if (itemData.getPokemonData().getPokemonId() != PokemonIdOuterClass.PokemonId.MISSINGNO) {
pokebank.removePokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
}
if (itemData.getItem().getItemId() != ItemIdOuterClass.ItemId.UNRECOGNIZED) {
ItemOuterClass.Item item = inventoryItem.getInventoryItemData().getItem();
itemBag.removeItem(inventoryItem.getInventoryItemData().getItem().getItemId(), item.getCount());
}
if (itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.UNRECOGNIZED) {
candyjar.removeCandy(
inventoryItem.getInventoryItemData().getPokemonFamily().getFamilyId(),
inventoryItem.getInventoryItemData().getPokemonFamily().getCandy()
);
}
} else {
if (itemData.getPokemonData().getPokemonId() != PokemonIdOuterClass.PokemonId.MISSINGNO) {
pokebank.addPokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
}
if (itemData.getItem().getItemId() != ItemIdOuterClass.ItemId.UNRECOGNIZED) {
ItemOuterClass.Item item = inventoryItem.getInventoryItemData().getItem();
itemBag.addItem(new Item(item));
}
if (itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.UNRECOGNIZED) {
candyjar.addCandy(
inventoryItem.getInventoryItemData().getPokemonFamily().getFamilyId(),
inventoryItem.getInventoryItemData().getPokemonFamily().getCandy()
);
}
if (itemData.hasPlayerStats()) {
stats = inventoryItem.getInventoryItemData().getPlayerStats();
}

if (itemData.getPokemonData().getPokemonId() != PokemonIdOuterClass.PokemonId.MISSINGNO) {
pokebank.addPokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
}
if (itemData.getItem().getItemId() != ItemId.UNRECOGNIZED
&& itemData.getItem().getItemId() != ItemId.ITEM_UNKNOWN) {
ItemData item = inventoryItem.getInventoryItemData().getItem();
itemBag.addItem(new Item(item));
}
if (itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.UNRECOGNIZED
&& itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.FAMILY_UNSET) {
candyjar.setCandy(
inventoryItem.getInventoryItemData().getPokemonFamily().getFamilyId(),
inventoryItem.getInventoryItemData().getPokemonFamily().getCandy()
);
}
if (itemData.hasPlayerStats()) {
api.getPlayerProfile().setStats(inventoryItem.getInventoryItemData().getPlayerStats());
}

if (itemData.hasPokedexEntry()) {
pokedex.add(itemData.getPokedexEntry());
}

lastInventoryUpdate = System.currentTimeMillis();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/pokegoapi/api/inventory/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Inventory.ItemIdOuterClass.ItemId;

import POGOProtos.Inventory.Item.ItemDataOuterClass;
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;

public class Item {
private POGOProtos.Inventory.ItemOuterClass.Item proto;
private ItemDataOuterClass.ItemData proto;
private int count;

public Item(POGOProtos.Inventory.ItemOuterClass.Item proto) {
public Item(ItemDataOuterClass.ItemData proto) {
this.proto = proto;
this.count = proto.getCount();
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/pokegoapi/api/inventory/ItemBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Inventory.ItemIdOuterClass.ItemId;
import POGOProtos.Inventory.ItemOuterClass;
import POGOProtos.Inventory.Item.ItemDataOuterClass;
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass;
Expand Down Expand Up @@ -96,7 +97,7 @@ public Item getItem(ItemId type) {

// prevent returning null
if (!items.containsKey(type)) {
return new Item(ItemOuterClass.Item.newBuilder().setCount(0).setItemId(type).build());
return new Item(ItemData.newBuilder().setCount(0).setItemId(type).build());
}

return items.get(type);
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/pokegoapi/api/inventory/Pokeball.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import lombok.Getter;

public enum Pokeball {
POKEBALL(1),
GREATBALL(2),
ULTRABALL(3),
MASTERBALL(4);
POKEBALL(ItemId.ITEM_POKE_BALL),
GREATBALL(ItemId.ITEM_GREAT_BALL),
ULTRABALL(ItemId.ITEM_ULTRA_BALL),
MASTERBALL(ItemId.ITEM_MASTER_BALL);

@Getter
private final int balltype;
private final ItemId ballType;

Pokeball(int type) {
balltype = type;
Pokeball(ItemId type) {
ballType = type;
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/pokegoapi/api/inventory/Pokedex.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,37 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Data.PokedexEntryOuterClass.PokedexEntry;
import POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
import com.pokegoapi.api.PokemonGo;

import java.util.HashMap;
import java.util.Map;

public class Pokedex {

private final PokemonGo api;
private Map<PokemonId, PokedexEntry> pokedexMap = new HashMap();

public Pokedex(PokemonGo api) {
this.api = api;
}

/**
* Add/Update a PokdexEntry.
* @param entry The entry to add or update
*/
public void add(PokedexEntry entry) {
PokemonId id = PokemonId.forNumber(entry.getPokemonId().getNumber());
pokedexMap.put(id, entry);
}

/**
* Get a pokedex entry value.
* @param pokemonId the ID of the pokemon to get
* @return Entry if in pokedex or null if it doesn't
*/
public PokedexEntry getPokedexEntry(PokemonId pokemonId) {
return pokedexMap.get(pokemonId);
}
}
14 changes: 11 additions & 3 deletions src/main/java/com/pokegoapi/api/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package com.pokegoapi.api.map;

import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import POGOProtos.Map.Fort.FortDataOuterClass.FortData;
import POGOProtos.Map.Fort.FortTypeOuterClass.FortType;
import POGOProtos.Map.MapCellOuterClass;
Expand Down Expand Up @@ -103,6 +104,13 @@ public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException,
catchablePokemons.add(new CatchablePokemon(api, wildPokemon));
}

// TODO: Check if this code is correct; merged because this contains many other fixes
/*for (Pokestop pokestop : objects.getPokestops()) {
if (pokestop.inRange() && pokestop.hasLurePokemon()) {
catchablePokemons.add(new CatchablePokemon(api, pokestop.getFortData()));
}
}*/

return catchablePokemons;
}

Expand Down Expand Up @@ -408,7 +416,7 @@ public EncounterResponse encounterPokemon(MapPokemon catchablePokemon)
.setEncounterId(catchablePokemon.getEncounterId())
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.setSpawnpointId(catchablePokemon.getSpawnpointId())
.setSpawnPointId(catchablePokemon.getSpawnPointId())
.build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.ENCOUNTER, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest);
Expand Down Expand Up @@ -440,15 +448,15 @@ public CatchPokemonResponse catchPokemon(
double normalizedHitPosition,
double normalizedReticleSize,
double spinModifier,
int pokeball)
ItemId pokeball)
throws LoginFailedException, RemoteServerException {

CatchPokemonMessage reqMsg = CatchPokemonMessage.newBuilder()
.setEncounterId(catchablePokemon.getEncounterId())
.setHitPokemon(true)
.setNormalizedHitPosition(normalizedHitPosition)
.setNormalizedReticleSize(normalizedReticleSize)
.setSpawnPointGuid(catchablePokemon.getSpawnpointId())
.setSpawnPointGuid(catchablePokemon.getSpawnPointId())
.setSpinModifier(spinModifier)
.setPokeball(pokeball)
.build();
Expand Down
33 changes: 28 additions & 5 deletions src/main/java/com/pokegoapi/api/map/fort/Pokestop.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
public class Pokestop {

private final PokemonGo api;
@Getter
private final FortDataOuterClass.FortData fortData;
@Getter
private long cooldownCompleteTimestampMs;
Expand All @@ -52,6 +53,21 @@ public Pokestop(PokemonGo api, FortDataOuterClass.FortData fortData) {
this.cooldownCompleteTimestampMs = fortData.getCooldownCompleteTimestampMs();
}

/**
* Returns whether or not a pokestop is in range.
* @return true when in range of player
*/
public boolean inRange() {
S2LatLng pokestop = S2LatLng.fromDegrees(getLatitude(), getLongitude());
S2LatLng player = S2LatLng.fromDegrees(api.getLatitude(), api.getLongitude());
double distance = pokestop.getEarthDistance(player);
return distance < 30;
}

/**
* can user loot this from current position.
* @return true when lootable
*/
public boolean canLoot() {
return canLoot(false);
}
Expand All @@ -63,10 +79,11 @@ public boolean canLoot() {
* @return the boolean
*/
public boolean canLoot(boolean ignoreDistance) {
S2LatLng pokestop = S2LatLng.fromDegrees(getLatitude(), getLongitude());
S2LatLng player = S2LatLng.fromDegrees(api.getLatitude(), api.getLongitude());
double distance = pokestop.getEarthDistance(player);
return (ignoreDistance || distance < 30) && cooldownCompleteTimestampMs < System.currentTimeMillis();
boolean active = cooldownCompleteTimestampMs < System.currentTimeMillis();
if (!ignoreDistance) {
return active && inRange();
}
return active;
}

public String getId() {
Expand Down Expand Up @@ -134,5 +151,11 @@ public FortDetails getDetails() throws LoginFailedException, RemoteServerExcepti
return new FortDetails(response);
}


/**
* Returns whether this pokestop has an active lure.
* @return lure status
*/
public boolean hasLurePokemon() {
return fortData.hasLureInfo() && fortData.getLureInfo().getLureExpiresTimestampMs() < System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

package com.pokegoapi.api.map.fort;

import POGOProtos.Inventory.ItemAwardOuterClass;
import POGOProtos.Inventory.Item.ItemAwardOuterClass.ItemAward;
import POGOProtos.Networking.Responses.FortSearchResponseOuterClass;
import POGOProtos.Networking.Responses.FortSearchResponseOuterClass.FortSearchResponse.Result;

Expand All @@ -40,7 +40,7 @@ public Result getResult() {
return response.getResult();
}

public List<ItemAwardOuterClass.ItemAward> getItemsAwarded() {
public List<ItemAward> getItemsAwarded() {
return response.getItemsAwardedList();
}

Expand Down
Loading

0 comments on commit 4f5b715

Please sign in to comment.