diff --git a/src/main/java/com/epita/creeps/Program.java b/src/main/java/com/epita/creeps/Program.java index 1a881ef..a4e3d29 100644 --- a/src/main/java/com/epita/creeps/Program.java +++ b/src/main/java/com/epita/creeps/Program.java @@ -1,13 +1,14 @@ package com.epita.creeps; import com.epita.creeps.commands.Basics; -import com.epita.creeps.given.extra.Cartographer; import com.epita.creeps.given.vo.geometry.Direction; import com.epita.creeps.given.vo.geometry.Point; import com.epita.creeps.given.vo.response.CommandResponse; import com.epita.creeps.given.vo.response.InitResponse; -import com.epita.creeps.given.vo.response.StatisticsResponse; -import com.epita.creeps.units.*; +import com.epita.creeps.units.Building; +import com.epita.creeps.units.Citizen; +import com.epita.creeps.units.Turret; +import com.epita.creeps.units.Unit; import kong.unirest.core.HttpResponse; import kong.unirest.core.JsonNode; import kong.unirest.core.Unirest; @@ -61,6 +62,7 @@ public class Program { logger.error("Cannot connect to the server. Aborting..."); throw e; } +// AsyncExec.justWait(0); // Create account and get init infos logger.info("Creating account"); @@ -73,11 +75,53 @@ public class Program { Unit.getUnits().add(citizen1); Unit.getUnits().add(citizen2); - // Et voilĂ , machtou pichtou + citizen1.move(Direction.UP).waitFinished(); - logger.info("Done"); + citizen1.fetchMessages(); + citizen2.move(Direction.UP); + citizen1.waitFinished(); + citizen2.waitFinished(); + + citizen2.spawn("turret"); + citizen1.move(Direction.RIGHT); + citizen1.waitFinished(); + citizen2.waitFinished(); + + List turrets = Turret.getTurretUnits(); + Turret t = (Turret) turrets.getFirst(); + t.fire(t.getPosition().plus(new Point(4, 0))).waitFinished(); + + + + +// logger.info("Done"); } +// ### Get statistics +//GET http://localhost:1664/statistics +// +//### Get status +//GET http://localhost:1664/status +// +//### Get report +//GET http://localhost:1664/report/148997e9b +// +//### Login with user login_l +//POST http://localhost:1664/init/login_l +// +//> {% +//client.global.set("baseId", response.body.baseId); +//client.global.set("probeId", response.body.probeId); +//client.global.set("login", response.body.login); +//%} +// +//### Post noop commande +//POST http://localhost:1664/command/{{login}}/{{probeId}}/noop +// +//### +//POST http://localhost:1664/command/dumeig_a/8d87eea10/inspect +// +//### } diff --git a/src/main/java/com/epita/creeps/commands/Basics.java b/src/main/java/com/epita/creeps/commands/Basics.java index 070fab9..a626b2c 100644 --- a/src/main/java/com/epita/creeps/commands/Basics.java +++ b/src/main/java/com/epita/creeps/commands/Basics.java @@ -7,8 +7,6 @@ import com.epita.creeps.given.extra.Cartographer; import com.epita.creeps.given.json.Json; import com.epita.creeps.given.vo.report.*; import com.epita.creeps.given.vo.response.InitResponse; -import com.epita.creeps.given.vo.response.StatisticsResponse; -import com.epita.creeps.units.BomberBot; import com.epita.creeps.units.Turret; import com.epita.creeps.units.Unit; import kong.unirest.core.HttpResponse; @@ -52,18 +50,6 @@ public class Basics { } } - - public static StatisticsResponse getStatistics() { - try { - CompletableFuture> resp = AsyncExec.asyncExec(() -> Unirest.get(srvUrl + "/statistics").asJson(), 0); - HttpResponse response = resp.join(); - return Json.parse(response.getBody().toString(), StatisticsResponse.class); - } catch (UnirestException e) { - logger.error("Cannot retrieve statistics."); - throw e; - } - } - // Asks the server for a certain report public static Report getReport (String reportId) { HttpResponse response = null; @@ -111,8 +97,8 @@ public class Basics { Unit.getUnits().add(turret); } else if (spawnReport.spawnedUnit.opcode.endsWith("bomber-bot")) { - BomberBot bombhero = new BomberBot(Program.getLogin(), spawnReport.spawnedUnitId, spawnReport.spawnedUnit.position); - Unit.getUnits().add(bombhero); + Turret turret = new Turret(Program.getLogin(), spawnReport.spawnedUnitId, spawnReport.spawnedUnit.position); + Unit.getUnits().add(turret); } else { logger.warn("Spawned unit is an unknown type. Ignored..."); diff --git a/src/main/java/com/epita/creeps/units/BomberBot.java b/src/main/java/com/epita/creeps/units/BomberBot.java index c499dea..9250b47 100644 --- a/src/main/java/com/epita/creeps/units/BomberBot.java +++ b/src/main/java/com/epita/creeps/units/BomberBot.java @@ -1,26 +1,10 @@ package com.epita.creeps.units; -import com.epita.creeps.given.extra.Cartographer; -import com.epita.creeps.given.json.Json; import com.epita.creeps.given.vo.geometry.Point; -import com.epita.creeps.given.vo.parameter.FireParameter; - -import java.util.List; public class BomberBot extends Unit { public BomberBot(String login, String id, Point position) { super(login, id, position); } - - // Retrieves all units that are turrets - public static List getBomberBotUnits() { - return getUnits().stream().filter(unit -> unit.getClass() == BomberBot.class).toList(); - } - - public BomberBot fire() { - FireParameter fp = new FireParameter(this.position); - sendActionWithBody("fire:bomber-bot", Json.serialize(fp), 2); - return this; - } } diff --git a/src/main/java/com/epita/creeps/units/Unit.java b/src/main/java/com/epita/creeps/units/Unit.java index d508fa4..fdff9cf 100644 --- a/src/main/java/com/epita/creeps/units/Unit.java +++ b/src/main/java/com/epita/creeps/units/Unit.java @@ -46,11 +46,7 @@ public abstract class Unit { // Sends a specific action to the server using it's HTTPS string representation // Sets pending action accordingly and asks for a report after `delay` ticks public void sendActionWithBody(String actionCode, String body, long delay) { - if (!idle) { -// logger.warn("Unit is already busy, queuing action"); - this.waitFinished(); - } - // Do + // Move idle = false; pendingAction = AsyncExec.asyncExec(() -> Unirest.post(command_uri + actionCode).body(body).asJson(), delay) .thenApplyAsync( x -> x ); @@ -87,7 +83,7 @@ public abstract class Unit { logger.warn("Invalid report received: null value"); return false; } -// logger.debug("Got report: " + lastReport); + logger.debug("Got report: " + lastReport); idle = true; return lastReport.errorCode != null; @@ -102,10 +98,8 @@ public abstract class Unit { } return AsyncExec.thenAsyncExec(pendingAction, x -> { CommandResponse cr = Json.parse(x.getBody().toString(), CommandResponse.class); - if (cr.error != null) { - logger.debug("Server reponse: " + cr.toString()); + if (cr.error != null) throw new ServerReponseException("Error retrieving the report id"); - } String reportId = cr.reportId; return Basics.getReport(reportId); }, 0);