build and gather

This commit is contained in:
Guillem George 2026-02-07 16:25:00 +01:00
parent 58f5f4da8b
commit d62bd8410a
5 changed files with 49 additions and 35 deletions

View file

@ -16,11 +16,15 @@ import java.util.function.Supplier;
@NoArgsConstructor @NoArgsConstructor
public class AsyncExec<T> { public class AsyncExec<T> {
@Getter
@Setter
private static float ticksPerSecond = 1; private static float ticksPerSecond = 1;
// private CompletableFuture<T> task; // private CompletableFuture<T> task;
public static void setTicksPerSecond(float ticksPerSecond) {
AsyncExec.ticksPerSecond = ticksPerSecond;
}
public static long ticksToTime(long ticks) { public static long ticksToTime(long ticks) {
return Math.ceilDiv(ticks, (long)Math.ceil(ticksPerSecond)); return Math.ceilDiv(ticks, (long)Math.ceil(ticksPerSecond));
} }

View file

@ -4,6 +4,7 @@ import com.epita.creeps.commands.Basics;
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.Building;
import com.epita.creeps.units.Citizen; 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;
@ -60,32 +61,24 @@ public class Program {
InitResponse initResponse = Basics.connectAccount(login); InitResponse initResponse = Basics.connectAccount(login);
login = initResponse.login; // Just in case login = initResponse.login; // Just in case
AsyncExec.setTicksPerSecond((long)initResponse.setup.ticksPerSeconds); AsyncExec.setTicksPerSecond((float) initResponse.setup.ticksPerSeconds);
Citizen citizen1 = new Citizen(login, initResponse.citizen1Id); Citizen citizen1 = new Citizen(login, initResponse.citizen1Id);
Citizen citizen2 = new Citizen(login, initResponse.citizen2Id); Citizen citizen2 = new Citizen(login, initResponse.citizen2Id);
for(int i = 0; i < 3; i++) { citizen1.move(Direction.UP).waitFinished();
citizen1.move(Direction.RIGHT);
citizen2.move(Direction.UP); while (true) {
citizen1.move(Direction.UP);
citizen2.move(Direction.RIGHT);
citizen1.waitFinished(); citizen1.waitFinished();
citizen2.waitFinished(); citizen2.waitFinished();
// logger.debug("Received actions responses"); citizen1.build(Building.ROAD);
// logger.debug("citizen1.error: " + citizen1_resp.error); citizen2.gather();
// logger.debug("citizen2.error: " + citizen2_resp.error);
}
for(int i = 0; i < 3; i++) {
citizen1.move(Direction.LEFT);
citizen2.move(Direction.DOWN);
citizen1.waitFinished(); citizen1.waitFinished();
citizen2.waitFinished(); citizen2.waitFinished();
// logger.debug("Received actions responses");
// logger.debug("citizen1.error: " + citizen1_resp.error);
// logger.debug("citizen2.error: " + citizen2_resp.error);
} }

View file

@ -23,9 +23,12 @@ import java.util.concurrent.CompletableFuture;
public class Basics { public class Basics {
private static final Logger logger = LoggerFactory.getLogger(Basics.class); private static final Logger logger = LoggerFactory.getLogger(Basics.class);
@Getter @Setter
private static String srvUrl; private static String srvUrl;
public static void setSrvUrl(String srvUrl) {
Basics.srvUrl = srvUrl;
}
public static InitResponse connectAccount(String login) { public static InitResponse connectAccount(String login) {
try { try {
CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 0); CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 0);

View file

@ -23,36 +23,46 @@ public class Citizen extends Unit {
super(login, citizen_id); super(login, citizen_id);
} }
public void move(Direction direction) { public Citizen move(Direction direction) {
sendAction("move:"+direction.direction, 2); sendAction("move:"+direction.direction, 2);
return this;
} }
public void observe() { public Citizen observe() {
sendAction("observe", 1); sendAction("observe", 1);
return this;
} }
public void gather() { public Citizen gather() {
sendAction("gather", 4); sendAction("gather", 4);
return this;
} }
public void unload() { public Citizen unload() {
sendAction("unload", 3); sendAction("unload", 3);
return this;
} }
public void farm() { public Citizen farm() {
sendAction("farm", 10); sendAction("farm", 10);
return this;
} }
public void build(Building building) { public Citizen build(Building building) {
sendAction("build:" + building.name, 20); sendAction("build:" + building.name, 20);
return this;
} }
public void spawn(String unit) { public Citizen spawn(String unit) {
sendAction("spawn:" + unit, 6); sendAction("spawn:" + unit, 6);
return this;
} }
public void refine(String resource) { public Citizen refine(String resource) {
sendAction("refine:" + resource, 8); sendAction("refine:" + resource, 8);
return this;
} }
public void sendMessage(String receiver, String message) { public Citizen sendMessage(String receiver, String message) {
MessageParameter mp = new MessageParameter(receiver, message); MessageParameter mp = new MessageParameter(receiver, message);
sendActionWithBody("message:send", Json.serialize(mp), 1); sendActionWithBody("message:send", Json.serialize(mp), 1);
return this;
} }
public void fetchMessages() { public Citizen fetchMessages() {
sendAction("message:fetch", 1); sendAction("message:fetch", 1);
return this;
} }

View file

@ -15,12 +15,16 @@ import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public abstract class Unit { public abstract class Unit {
public static String srvUrl;
public static final Logger logger = LoggerFactory.getLogger(Unit.class);
protected final String id; protected final String id;
protected final String command_uri; protected final String command_uri;
protected CompletableFuture<HttpResponse<JsonNode>> pendingAction; protected CompletableFuture<HttpResponse<JsonNode>> pendingAction;
protected CompletableFuture<Report> pendingReport; protected CompletableFuture<Report> pendingReport;
public static String srvUrl; protected Report lastReport;
public static final Logger logger = LoggerFactory.getLogger(Unit.class);
public Unit(String login, String id) { public Unit(String login, String id) {
if (srvUrl == null) { if (srvUrl == null) {
@ -60,14 +64,14 @@ public abstract class Unit {
logger.warn("Tried to wait a citizen with no pending action or report"); logger.warn("Tried to wait a citizen with no pending action or report");
return false; return false;
} }
Report r = pendingReport.join(); lastReport = pendingReport.join();
if (r == null) { if (lastReport == null) {
logger.warn("Invalid report received: null value"); logger.warn("Invalid report received: null value");
return false; return false;
} }
logger.debug("Got report: " + r); logger.debug("Got report: " + lastReport);
return r.errorCode != null; return lastReport.errorCode != null;
} }