Skip to content

Commit

Permalink
add query from hatched eggs + rewrite stuff around EggPokemon
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Jul 23, 2016
1 parent 2e14fab commit 1ac8fa3
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/main/java/com/pokegoapi/api/inventory/EggIncubator.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ public int getUsesRemaining() {
*/
public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg)
throws LoginFailedException, RemoteServerException {
if (!egg.getIsEgg()) {
return null;
}

UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder()
.setItemId(proto.getId())
.setPokemonId(egg.getId())
Expand Down
39 changes: 38 additions & 1 deletion src/main/java/com/pokegoapi/api/inventory/Hatchery.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@

package com.pokegoapi.api.inventory;

import POGOProtos.Networking.Requests.Messages.GetHatchedEggsMessageOuterClass.GetHatchedEggsMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
import POGOProtos.Networking.Responses.GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.EggPokemon;
import com.pokegoapi.api.pokemon.Pokemon;
import com.pokegoapi.api.pokemon.HatchedEgg;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;

import lombok.Getter;

import java.util.ArrayList;
Expand All @@ -38,5 +46,34 @@ public Hatchery(PokemonGo instance) {
public void addEgg(EggPokemon egg) {
eggs.add(egg);
}


/**
* Get if eggs has hatched.
*
* @return list of hatched eggs
* @throws RemoteServerException e
* @throws LoginFailedException e
*/
public List<HatchedEgg> queryHatchedEggs() throws RemoteServerException, LoginFailedException {
GetHatchedEggsMessage msg = GetHatchedEggsMessage.newBuilder().build();
ServerRequest serverRequest = new ServerRequest(RequestType.GET_HATCHED_EGGS, msg);
instance.getRequestHandler().sendServerRequests(serverRequest);

GetHatchedEggsResponse response = null;
try {
response = GetHatchedEggsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}
List<HatchedEgg> eggs = new ArrayList<HatchedEgg>();
for (int i = 0; i < response.getPokemonIdCount(); i++) {
eggs.add(new HatchedEgg(response.getPokemonId(i),
response.getExperienceAwarded(i),
response.getCandyAwarded(i),
response.getStardustAwarded(i)));
}
return eggs;
}

}
39 changes: 34 additions & 5 deletions src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
package com.pokegoapi.api.pokemon;

import POGOProtos.Data.PokemonDataOuterClass.PokemonData;
import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass.UseItemEggIncubatorResponse;

import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.inventory.EggIncubator;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;

import lombok.Setter;

/**
Expand All @@ -30,20 +36,39 @@ public class EggPokemon {
private PokemonData proto;

// API METHODS //
/**
* Incubate this egg.
*
* @param incubator : the incubator
* @return status of putting egg in incubator
* @throws LoginFailedException e
* @throws RemoteServerException e
*/
public UseItemEggIncubatorResponse.Result incubate(EggIncubator incubator)
throws LoginFailedException, RemoteServerException {
if (incubator.isInUse()) {
throw new IllegalArgumentException("Incubator already used");
}
return incubator.hatchEgg(this);
}

// DELEGATE METHODS BELOW //
/**
* Build a EggPokemon wrapper from the proto.
*
* @param proto : the prototype
*/
public EggPokemon(PokemonData proto) {
if (!proto.getIsEgg()) {
throw new IllegalArgumentException("You cant build a EggPokemon without a valid PokemonData.");
}
this.proto = proto;
}

public long getId() {
return proto.getId();
}

public boolean getIsEgg() {
return proto.getIsEgg();
}


public double getEggKmWalkedTarget() {
return proto.getEggKmWalkedTarget();
}
Expand All @@ -63,6 +88,10 @@ public long getCreationTimeMs() {
public String getEggIncubatorId() {
return proto.getEggIncubatorId();
}

public boolean isIncubate() {
return proto.getEggIncubatorId().length() > 0;
}

@Override
public int hashCode() {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/pokegoapi/api/pokemon/HatchedEgg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pokegoapi.api.pokemon;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class HatchedEgg {

private Long id;
private int experience;
private int candy;
private int stardust;
}

0 comments on commit 1ac8fa3

Please sign in to comment.