hhhhhhhhhhh
This commit is contained in:
parent
8836ea44c5
commit
97ced313d9
4 changed files with 38 additions and 22 deletions
|
|
@ -11,20 +11,26 @@ import java.net.http.HttpResponse;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
// Handles server operations to respect commands delays
|
// Handles server operations to respect commands delays
|
||||||
@AllArgsConstructor
|
@NoArgsConstructor
|
||||||
public class AsyncExec {
|
public class AsyncExec<T> {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
private static long ticksPerSecond = 1;
|
private static long ticksPerSecond = 1;
|
||||||
|
private CompletableFuture<T> task;
|
||||||
|
|
||||||
|
|
||||||
public static <T> CompletableFuture<T> asyncExec(Supplier<T> supplier, long time) {
|
public static <T> CompletableFuture<T> asyncExec(Supplier<T> supplier, long time) {
|
||||||
return CompletableFuture.supplyAsync(supplier).thenApplyAsync(x -> x,
|
return CompletableFuture.supplyAsync(supplier).thenApplyAsync(x -> x,
|
||||||
CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS));
|
CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS));
|
||||||
}
|
}
|
||||||
|
public static <T, R> CompletableFuture<R> thenAsyncExec(CompletableFuture<T> base, Function<T, R> f, long time) {
|
||||||
|
return base.thenApplyAsync(f, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS));
|
||||||
|
}
|
||||||
public static void justWait(long time) {
|
public static void justWait(long time) {
|
||||||
CompletableFuture.supplyAsync(() -> 0, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS)).join();
|
CompletableFuture.supplyAsync(() -> 0, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS)).join();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
package com.epita.creeps;
|
package com.epita.creeps;
|
||||||
|
|
||||||
import com.epita.creeps.commands.Basics;
|
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.geometry.Direction;
|
||||||
import com.epita.creeps.given.vo.response.CommandResponse;
|
import com.epita.creeps.given.vo.response.CommandResponse;
|
||||||
import com.epita.creeps.given.vo.response.InitResponse;
|
import com.epita.creeps.given.vo.response.InitResponse;
|
||||||
|
import com.epita.creeps.units.Citizen;
|
||||||
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;
|
||||||
|
|
@ -12,9 +12,6 @@ import kong.unirest.core.UnirestException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
public class Program {
|
public class Program {
|
||||||
|
|
||||||
private static String srvUrl;
|
private static String srvUrl;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.epita.creeps;
|
package com.epita.creeps.units;
|
||||||
|
|
||||||
|
import com.epita.creeps.AsyncExec;
|
||||||
import com.epita.creeps.given.json.Json;
|
import com.epita.creeps.given.json.Json;
|
||||||
import com.epita.creeps.given.vo.geometry.Direction;
|
import com.epita.creeps.given.vo.geometry.Direction;
|
||||||
import com.epita.creeps.given.vo.response.CommandResponse;
|
import com.epita.creeps.given.vo.response.CommandResponse;
|
||||||
|
|
@ -7,27 +8,16 @@ 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;
|
||||||
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
//import java.util.logging.Logger;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
|
|
||||||
public class Citizen {
|
public class Citizen extends Unit {
|
||||||
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 ) {
|
public Citizen(String login, String citizen_id ) {
|
||||||
if (srvUrl == null || logger == null) {
|
super(login, citizen_id);
|
||||||
throw new RuntimeException("Tried to create a citizen without properly initializing static fields");
|
|
||||||
}
|
|
||||||
id = citizen_id;
|
|
||||||
command_uri = srvUrl + "/command/" + login + "/" + citizen_id + "/";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void move(Direction direction) {
|
public void move(Direction direction) {
|
||||||
pendingAction = AsyncExec.asyncExec(() -> Unirest.post(command_uri + "move:"+direction.direction).asJson(), 2);
|
pendingAction = AsyncExec.asyncExec(() -> Unirest.post(command_uri + "move:"+direction.direction).asJson(), 2)
|
||||||
|
.thenApplyAsync( x -> x );
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandResponse waitFinished() {
|
public CommandResponse waitFinished() {
|
||||||
23
src/main/java/com/epita/creeps/units/Unit.java
Normal file
23
src/main/java/com/epita/creeps/units/Unit.java
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
package com.epita.creeps.units;
|
||||||
|
|
||||||
|
import kong.unirest.core.HttpResponse;
|
||||||
|
import kong.unirest.core.JsonNode;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public abstract class Unit {
|
||||||
|
protected final String id;
|
||||||
|
protected final String command_uri;
|
||||||
|
protected CompletableFuture<HttpResponse<JsonNode>> pendingAction;
|
||||||
|
public static String srvUrl;
|
||||||
|
public static Logger logger;
|
||||||
|
|
||||||
|
public Unit(String login, String id) {
|
||||||
|
if (srvUrl == null || logger == null) {
|
||||||
|
throw new RuntimeException("Tried to create a citizen without properly initializing static fields");
|
||||||
|
}
|
||||||
|
this.id = id;
|
||||||
|
command_uri = srvUrl + "/command/" + login + "/" + id + "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue