mauve
This commit is contained in:
parent
b81edecd41
commit
8836ea44c5
4 changed files with 86 additions and 24 deletions
|
|
@ -1,10 +1,14 @@
|
|||
package com.epita.creeps;
|
||||
|
||||
import com.epita.creeps.given.vo.report.Report;
|
||||
import com.epita.creeps.given.vo.response.CommandResponse;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
|
@ -17,10 +21,10 @@ public class AsyncExec {
|
|||
@Setter
|
||||
private static long ticksPerSecond = 1;
|
||||
|
||||
public static CompletableFuture asyncExec(Supplier supplier, long time) {
|
||||
return CompletableFuture.supplyAsync(supplier, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS));
|
||||
public static <T> CompletableFuture<T> asyncExec(Supplier<T> supplier, long time) {
|
||||
return CompletableFuture.supplyAsync(supplier).thenApplyAsync(x -> x,
|
||||
CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
public static void justWait(long time) {
|
||||
CompletableFuture.supplyAsync(() -> 0, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS)).join();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,42 @@
|
|||
package com.epita.creeps;
|
||||
|
||||
import com.epita.creeps.given.json.Json;
|
||||
import com.epita.creeps.given.vo.geometry.Direction;
|
||||
import com.epita.creeps.given.vo.response.CommandResponse;
|
||||
import kong.unirest.core.HttpResponse;
|
||||
import kong.unirest.core.JsonNode;
|
||||
import kong.unirest.core.Unirest;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
//import java.util.logging.Logger;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class Citizen {
|
||||
private String id;
|
||||
private String command_uri;
|
||||
private final String id;
|
||||
private final String command_uri;
|
||||
private CompletableFuture<HttpResponse<JsonNode>> pendingAction;
|
||||
public static String srvUrl;
|
||||
public static Logger logger;
|
||||
|
||||
public Citizen(String login, String citizen_id ) {
|
||||
if (srvUrl == null || logger == null) {
|
||||
throw new RuntimeException("Tried to create a citizen without properly initializing static fields");
|
||||
}
|
||||
id = citizen_id;
|
||||
command_uri = "/command/" + login + "/" + citizen_id + "/";
|
||||
command_uri = srvUrl + "/command/" + login + "/" + citizen_id + "/";
|
||||
}
|
||||
|
||||
public void move(Direction direction) {
|
||||
AsyncExec.asyncExec(() -> {
|
||||
Unirest.post(command_uri + "move:"+direction.direction);
|
||||
return null;
|
||||
}, 2);
|
||||
pendingAction = AsyncExec.asyncExec(() -> Unirest.post(command_uri + "move:"+direction.direction).asJson(), 2);
|
||||
}
|
||||
|
||||
public CommandResponse waitFinished() {
|
||||
if (pendingAction == null) {
|
||||
logger.warn("Tried to wait a citizen with no pending action");
|
||||
return null;
|
||||
}
|
||||
JsonNode response = ((HttpResponse<JsonNode>) pendingAction.join()).getBody();
|
||||
return Json.parse(response.toString(), CommandResponse.class);
|
||||
}
|
||||
|
||||
public void noop() {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
package com.epita.creeps;
|
||||
|
||||
import com.epita.creeps.commands.Basics;
|
||||
import com.epita.creeps.given.json.Json;
|
||||
import com.epita.creeps.given.vo.geometry.Direction;
|
||||
import com.epita.creeps.given.vo.response.CommandResponse;
|
||||
import com.epita.creeps.given.vo.response.InitResponse;
|
||||
import kong.unirest.core.HttpResponse;
|
||||
import kong.unirest.core.JsonNode;
|
||||
import kong.unirest.core.Unirest;
|
||||
|
|
@ -8,6 +12,7 @@ import kong.unirest.core.UnirestException;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Program {
|
||||
|
|
@ -24,19 +29,26 @@ public class Program {
|
|||
srvUrl = "http://localhost:1664";
|
||||
login = "marion.mavie";
|
||||
|
||||
// Classes
|
||||
logger = LoggerFactory.getLogger(Program.class);
|
||||
Basics basics = new Basics(logger, srvUrl);
|
||||
logger.info("Initializing");
|
||||
|
||||
// Arguments
|
||||
boolean default_values = true;
|
||||
if (args.length == 3) {
|
||||
srvUrl = "http://" + args[0] + ":" + args[1];
|
||||
login = args[2];
|
||||
} else {
|
||||
logger.warn("No given args: using the default values");
|
||||
default_values = false;
|
||||
}
|
||||
|
||||
// Classes
|
||||
logger = LoggerFactory.getLogger(Program.class);
|
||||
Basics basics = new Basics(logger, srvUrl);
|
||||
logger.info("Initializing");
|
||||
Citizen.srvUrl = srvUrl;
|
||||
Citizen.logger = logger;
|
||||
logger.debug("Using server " + srvUrl + " with player " + login);
|
||||
|
||||
if (default_values)
|
||||
logger.warn("No given args: using the default values");
|
||||
|
||||
// Test
|
||||
HttpResponse<JsonNode> response;
|
||||
try {
|
||||
|
|
@ -45,12 +57,32 @@ public class Program {
|
|||
logger.error("Cannot connect to the server. Aborting...");
|
||||
throw e;
|
||||
}
|
||||
AsyncExec.justWait(3);
|
||||
// AsyncExec.justWait(0);
|
||||
|
||||
// Create account and get init infos
|
||||
logger.info("Creating account");
|
||||
basics.connectAccount(login);
|
||||
InitResponse initResponse = basics.connectAccount(login);
|
||||
|
||||
logger.info("Done");
|
||||
login = initResponse.login; // Just in case
|
||||
AsyncExec.setTicksPerSecond((long)initResponse.setup.ticksPerSeconds);
|
||||
Citizen citizen1 = new Citizen(login, initResponse.citizen1Id);
|
||||
Citizen citizen2 = new Citizen(login, initResponse.citizen2Id);
|
||||
|
||||
while (true) {
|
||||
citizen1.move(Direction.RIGHT);
|
||||
citizen2.move(Direction.UP);
|
||||
|
||||
CommandResponse citizen1_resp = citizen1.waitFinished();
|
||||
CommandResponse citizen2_resp = citizen2.waitFinished();
|
||||
|
||||
// logger.debug("Received actions responses");
|
||||
// logger.debug("citizen1.error: " + citizen1_resp.error);
|
||||
// logger.debug("citizen2.error: " + citizen2_resp.error);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// logger.info("Done");
|
||||
}
|
||||
|
||||
// ### Get statistics
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package com.epita.creeps.commands;
|
||||
|
||||
import com.epita.creeps.AsyncExec;
|
||||
import com.epita.creeps.given.json.Json;
|
||||
import com.epita.creeps.given.vo.response.InitResponse;
|
||||
import kong.unirest.core.HttpResponse;
|
||||
import kong.unirest.core.JsonNode;
|
||||
import kong.unirest.core.Unirest;
|
||||
|
|
@ -19,12 +21,17 @@ public class Basics {
|
|||
private Logger logger;
|
||||
private String srvUrl;
|
||||
|
||||
public JsonNode connectAccount(String login) {
|
||||
public InitResponse connectAccount(String login) {
|
||||
try {
|
||||
CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 10);
|
||||
CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 0);
|
||||
HttpResponse<JsonNode> response = (HttpResponse<JsonNode>) resp.join();
|
||||
logger.debug(response.getBody().toPrettyString());
|
||||
return response.getBody();
|
||||
// logger.debug(response.getBody().toPrettyString());
|
||||
InitResponse initResponse = Json.parse(response.getBody().toString(), InitResponse.class);
|
||||
if (initResponse.error != null) {
|
||||
logger.error("Error in server response");
|
||||
throw new RuntimeException("Cannot create account");
|
||||
}
|
||||
return initResponse;
|
||||
} catch (UnirestException e) {
|
||||
logger.error("Cannot create account.");
|
||||
throw e;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue