
uno.cod.battle.client.BattleHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
The Java client for the Coduno battle
The newest version!
package uno.cod.battle.client;
import uno.cod.battle.client.exceptions.BehaviourCode;
import uno.cod.battle.client.exceptions.BehaviourException;
import uno.cod.battle.client.exceptions.CooldownException;
import uno.cod.battle.client.exceptions.InfoException;
import uno.cod.battle.client.model.*;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by vbalan on 10/22/2015.
*/
public class BattleHelper {
private final String token;
private final String serverUrl;
private ObjectMapper mapper;
private CloseableHttpClient client;
public BattleHelper(String token) {
this(token, "http://battle.cod.uno:8080");
}
public BattleHelper(String token, String serverUrl) {
this.token = token;
this.serverUrl = serverUrl;
this.mapper = new ObjectMapper();
this.client = HttpClientBuilder.create().build();
}
/**
* Get the state of your player.
*
* @return the state of your player
* @throws Exception
*/
public GameObject me() throws Exception {
CloseableHttpResponse response = get("/me");
GameObject gameObject = mapper.readValue(response.getEntity().getContent(), GameObject.class);
EntityUtils.consume(response.getEntity());
response.close();
return gameObject;
}
/**
* Get the current state of the map.
*
* @return the current state of the map.
* @throws Exception
*/
public BattleMap map() throws Exception {
CloseableHttpResponse response = get("/map");
BattleMap battleMap = new BattleMap();
List obstacles = new ArrayList<>();
List healthFountains = new ArrayList<>();
List players = new ArrayList<>();
JsonNode json = mapper.readTree(response.getEntity().getContent());
battleMap.setPlayersInfo(mapper.readValue(json.get("players"), PositionInfo.class));
battleMap.setHealthFountainsInfo(mapper.readValue(json.get("healthFountains"), PositionInfo.class));
JsonNode objects = json.get("gameObjects");
for(int i = 0;i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy