parce qu'il faut backup a un moment
This commit is contained in:
parent
9d4b6c15d4
commit
b81edecd41
4 changed files with 65 additions and 7 deletions
27
src/main/java/com/epita/creeps/AsyncExec.java
Normal file
27
src/main/java/com/epita/creeps/AsyncExec.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.epita.creeps;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
// Handles server operations to respect commands delays
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class AsyncExec {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@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 void justWait(long time) {
|
||||||
|
CompletableFuture.supplyAsync(() -> 0, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS)).join();
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/main/java/com/epita/creeps/Citizen.java
Normal file
26
src/main/java/com/epita/creeps/Citizen.java
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package com.epita.creeps;
|
||||||
|
|
||||||
|
import com.epita.creeps.given.vo.geometry.Direction;
|
||||||
|
import kong.unirest.core.Unirest;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
|
||||||
|
public class Citizen {
|
||||||
|
private String id;
|
||||||
|
private String command_uri;
|
||||||
|
|
||||||
|
public Citizen(String login, String citizen_id ) {
|
||||||
|
id = citizen_id;
|
||||||
|
command_uri = "/command/" + login + "/" + citizen_id + "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void move(Direction direction) {
|
||||||
|
AsyncExec.asyncExec(() -> {
|
||||||
|
Unirest.post(command_uri + "move:"+direction.direction);
|
||||||
|
return null;
|
||||||
|
}, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void noop() {
|
||||||
|
// AsyncExec.asyncExec()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.epita.creeps;
|
package com.epita.creeps;
|
||||||
|
|
||||||
|
import com.epita.creeps.commands.Basics;
|
||||||
import kong.unirest.core.HttpResponse;
|
import kong.unirest.core.HttpResponse;
|
||||||
import kong.unirest.core.JsonNode;
|
import kong.unirest.core.JsonNode;
|
||||||
import kong.unirest.core.Unirest;
|
import kong.unirest.core.Unirest;
|
||||||
|
|
@ -7,7 +8,7 @@ import kong.unirest.core.UnirestException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.net.ConnectException;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
|
|
||||||
|
|
@ -44,8 +45,7 @@ public class Program {
|
||||||
logger.error("Cannot connect to the server. Aborting...");
|
logger.error("Cannot connect to the server. Aborting...");
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
// JsonNode jsonNode = response.getBody();
|
AsyncExec.justWait(3);
|
||||||
// System.out.println(jsonNode);
|
|
||||||
|
|
||||||
logger.info("Creating account");
|
logger.info("Creating account");
|
||||||
basics.connectAccount(login);
|
basics.connectAccount(login);
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.epita.creeps;
|
package com.epita.creeps.commands;
|
||||||
|
|
||||||
|
import com.epita.creeps.AsyncExec;
|
||||||
import kong.unirest.core.HttpResponse;
|
import kong.unirest.core.HttpResponse;
|
||||||
import kong.unirest.core.JsonNode;
|
import kong.unirest.core.JsonNode;
|
||||||
import kong.unirest.core.Unirest;
|
import kong.unirest.core.Unirest;
|
||||||
|
|
@ -9,6 +10,8 @@ import lombok.Getter;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public class Basics {
|
public class Basics {
|
||||||
|
|
@ -16,13 +19,15 @@ public class Basics {
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
private String srvUrl;
|
private String srvUrl;
|
||||||
|
|
||||||
public void connectAccount(String login) {
|
public JsonNode connectAccount(String login) {
|
||||||
HttpResponse<JsonNode> response;
|
|
||||||
try {
|
try {
|
||||||
response = Unirest.post(srvUrl + "/init/" + login).asJson();
|
CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 10);
|
||||||
|
HttpResponse<JsonNode> response = (HttpResponse<JsonNode>) resp.join();
|
||||||
logger.debug(response.getBody().toPrettyString());
|
logger.debug(response.getBody().toPrettyString());
|
||||||
|
return response.getBody();
|
||||||
} catch (UnirestException e) {
|
} catch (UnirestException e) {
|
||||||
logger.error("Cannot create account.");
|
logger.error("Cannot create account.");
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue