hhhhhhhhhhh

This commit is contained in:
Guillem George 2026-02-06 23:01:51 +01:00
parent 8836ea44c5
commit 97ced313d9
4 changed files with 38 additions and 22 deletions

View file

@ -11,20 +11,26 @@ import java.net.http.HttpResponse;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;
// Handles server operations to respect commands delays
@AllArgsConstructor
public class AsyncExec {
@NoArgsConstructor
public class AsyncExec<T> {
@Getter
@Setter
private static long ticksPerSecond = 1;
private CompletableFuture<T> task;
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 <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) {
CompletableFuture.supplyAsync(() -> 0, CompletableFuture.delayedExecutor(time/ticksPerSecond + 1, TimeUnit.SECONDS)).join();
}

View file

@ -1,10 +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 com.epita.creeps.units.Citizen;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
import kong.unirest.core.Unirest;
@ -12,9 +12,6 @@ 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 {
private static String srvUrl;

View file

@ -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.vo.geometry.Direction;
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.Unirest;
import java.util.concurrent.CompletableFuture;
//import java.util.logging.Logger;
import org.slf4j.Logger;
public class Citizen {
private final String id;
private final String command_uri;
private CompletableFuture<HttpResponse<JsonNode>> pendingAction;
public static String srvUrl;
public static Logger logger;
public class Citizen extends Unit {
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 = srvUrl + "/command/" + login + "/" + citizen_id + "/";
super(login, citizen_id);
}
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() {

View 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 + "/";
}
}