Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WIP: Gym/Battle #203

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 6 commits into from
Jul 26, 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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Include the API as jar from your own build, or use Maven/Gradle/SBT/Leiningen: h

Mostly everything is accessed through the PokemonGo class in the API package.

The constructor of PokemonGo class requires a AuthInfo object which can be obtained from GoogleLogin().login or PTCLogin().login, and a OkHttpClient object.
The constructor of PokemonGo class requires a AuthInfo object which can be obtained from GoogleLogin().login or PtcLogin().login, and a OkHttpClient object.

EG:
```java
Expand All @@ -57,11 +57,15 @@ You're running the sample code on the UI thread. Strict mode policy will throw a

This library is meant to be a Java implementation of the API. Google Volley is specific to Android and should not be introduced in this library. However, if you still want to refactor it, you should create it as a separate project.

- How can I use Android's native Google sign in with this libary?

You can't. The Google Indentity Platform uses the SHA1 fingerprint and package name to authenticate the caller of all sign in requests. This means that Niantic would need to add your app's SHA1 fingerprint and package name to their Google API Console. If you ever requested a Google Maps API key, you went through the same process. An alternative would be using a WebView to access the web based OAuth flow. This will work with the client ID and secret provided by this library.


## Contributing
- Fork it!
- Create your feature branch: `git checkout -b my-new-feature`
- Commit your changes: `git commit -am 'Usefull information about your new features'`
- Commit your changes: `git commit -am 'Useful information about your new features'`
- Push to the branch: `git push origin my-new-feature`
- Submit a pull request on the `Development` branch :D

Expand Down
257 changes: 257 additions & 0 deletions src/main/java/com/pokegoapi/api/gym/Battle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.pokegoapi.api.gym;

import POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction;
import POGOProtos.Data.Battle.BattleActionTypeOuterClass;
import POGOProtos.Data.Battle.BattlePokemonInfoOuterClass.BattlePokemonInfo;
import POGOProtos.Data.Battle.BattleStateOuterClass;
import POGOProtos.Data.Battle.BattleStateOuterClass.BattleState;
import POGOProtos.Data.PokemonDataOuterClass;
import POGOProtos.Networking.Requests.Messages.AttackGymMessageOuterClass;
import POGOProtos.Networking.Requests.Messages.AttackGymMessageOuterClass.AttackGymMessage;
import POGOProtos.Networking.Requests.Messages.StartGymBattleMessageOuterClass;
import POGOProtos.Networking.Requests.Messages.StartGymBattleMessageOuterClass.StartGymBattleMessage.Builder;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
import POGOProtos.Networking.Responses.AttackGymResponseOuterClass;
import POGOProtos.Networking.Responses.AttackGymResponseOuterClass.AttackGymResponse;
import POGOProtos.Networking.Responses.StartGymBattleResponseOuterClass.StartGymBattleResponse;
import POGOProtos.Networking.Responses.StartGymBattleResponseOuterClass.StartGymBattleResponse.Result;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.Pokemon;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class Battle {
private Gym gym;
private Pokemon[] team;
private List<BattlePokemonInfo> bteam;
private StartGymBattleResponse battleResponse;
private PokemonGo api;
private List<Integer> gymIndex;
@Getter
private boolean concluded;
@Getter
private BattleState outcome;

/**
* New battle to track the state of a battle.
*
*/
public Battle(PokemonGo api, Pokemon[] team, Gym gym) {
this.team = team;
this.gym = gym;
this.api = api;

this.bteam = new ArrayList<BattlePokemonInfo>();
this.gymIndex = new ArrayList<>();

for (int i = 0; i < team.length; i++) {
bteam.add(this.createBattlePokemon(team[i]));
}
}

/**
* Start a battle.
*
* @return Result of the attempt to start
*/
public Result start() throws LoginFailedException, RemoteServerException {

Builder builder = StartGymBattleMessageOuterClass.StartGymBattleMessage.newBuilder();

for (int i = 0; i < team.length; i++) {
builder.addAttackingPokemonIds(team[i].getId());
Copy link
Contributor

@vmarchaud vmarchaud Jul 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L#70 : You could just use addAllAttackingPokemonIds() methods

}


List<PokemonDataOuterClass.PokemonData> defenders = gym.getDefendingPokemon();
builder.setGymId(gym.getId());
builder.setPlayerLongitude(api.getLongitude());
builder.setPlayerLatitude(api.getLatitude());
builder.setDefendingPokemonId(defenders.get(0).getId()); // may need to be sorted
Copy link
Contributor

@vmarchaud vmarchaud Jul 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L#78 : they are already from what i saw, but you may check if the gym is actually defended (no team has claim it), its could lead to a NPE


ServerRequest serverRequest = new ServerRequest(RequestType.START_GYM_BATTLE, builder.build());
api.getRequestHandler().sendServerRequests(serverRequest);


try {
battleResponse = StartGymBattleResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException();
}

// need to send blank action
this.sendBlankAction();


for (BattleAction action : battleResponse.getBattleLog().getBattleActionsList()) {
gymIndex.add(action.getTargetIndex());
}

return battleResponse.getResult();
}




/**
* Attack a gym.
*
* @param times the amount of times to attack
* @return Battle
*/
public AttackGymResponse attack(int times) throws LoginFailedException, RemoteServerException {

ArrayList<BattleAction> actions = new ArrayList<BattleAction>();

for (int i = 0; i < times; i++) {
BattleAction action = BattleAction
.newBuilder()
.setType(BattleActionTypeOuterClass.BattleActionType.ACTION_ATTACK)
.setActionStartMs(System.currentTimeMillis() + (100 * times))
.setDurationMs(500)
.setTargetIndex(-1)
.build();
actions.add(action);
}

AttackGymResponse result = doActions(actions);



return result;
}


/**
* Creates a battle pokemon object to send with the request.
*
* @Param Pokemon
* @return BattlePokemonInfo
*/
private BattlePokemonInfo createBattlePokemon(Pokemon pokemon) {
BattlePokemonInfo info = BattlePokemonInfo
.newBuilder()
.setCurrentEnergy(0)
.setCurrentHealth(100)
.setPokemonData(pokemon.getDefaultInstanceForType())
.build();
return info;
}

/**
* Get the Pokemondata for the defenders.
*
* @param index of defender(0 to gym lever)
* @return Battle
*/
private PokemonDataOuterClass.PokemonData getDefender(int index) throws LoginFailedException, RemoteServerException {
return gym.getGymMembers().get(0).getPokemonData();
}

/**
* Get the last action from server.
*
* @return BattleAction
*/
private BattleAction getLastActionFromServer() {
BattleAction action;
int actionCount = battleResponse.getBattleLog().getBattleActionsCount();
action = battleResponse.getBattleLog().getBattleActions(actionCount - 1);
return action;
}

/**
* Send blank action, used for polling the state of the battle. (i think).
*
* @return AttackGymResponse
*/
private AttackGymResponse sendBlankAction() throws LoginFailedException, RemoteServerException {
AttackGymMessage message = AttackGymMessage
.newBuilder()
.setGymId(gym.getId())
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.setBattleId(battleResponse.getBattleId())
.build();

ServerRequest serverRequest = new ServerRequest(RequestType.ATTACK_GYM, message);
api.getRequestHandler().sendServerRequests(serverRequest);


try {
return AttackGymResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException();
}
}


/**
* Do Actions in battle.
*
* @param actions list of actions to send in this request
* @return AttackGymResponse
*/
private AttackGymResponse doActions(List<BattleAction> actions) throws LoginFailedException, RemoteServerException {


AttackGymMessage.Builder message = AttackGymMessage
.newBuilder()
.setGymId(gym.getId())
.setPlayerLatitude(api.getLatitude())
.setPlayerLongitude(api.getLongitude())
.setBattleId(battleResponse.getBattleId());

for (BattleAction action : actions) {
message.addAttackActions(action);
}



ServerRequest serverRequest = new ServerRequest(RequestType.ATTACK_GYM, message.build());
api.getRequestHandler().sendServerRequests(serverRequest);


try {
AttackGymResponse response = AttackGymResponse.parseFrom(serverRequest.getData());

if (response.getBattleLog().getState() == BattleState.DEFEATED
|| response.getBattleLog().getState() == BattleState.VICTORY
|| response.getBattleLog().getState() == BattleState.TIMED_OUT) {
concluded = true;
}

outcome = response.getBattleLog().getState();



return response;
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException();
}

}

}
Loading