Slide 41
Slide 41 text
#BSGTHEPLAN @DevFestToulouse
Worker
private void raiderWorker(Record raiderRecord, WebClient raiderClient) {
//…
vertx.setPeriodic(1000, timerID -> { // this is a worker
// get the raiders list
discovery.getRecords(r -> r.getMetadata().getString("kind").equals("raider") , ar -> {
if(ar.succeeded()) {
List raidersRecords = ar.result();
thatRaider.moveWith(raidersRecords, 300.0);
thatRaider.moveCloser(raidersRecords, 300.0);
thatRaider.moveAway(raidersRecords, 15.0);
thatRaider.move();
// === try to contact the raider and post coordinates ===
raiderClient.post("/api/coordinates").sendJsonObject(
new JsonObject()
.put("x",thatRaider.x)
.put("y",thatRaider.y)
.put("xVelocity",thatRaider.xVelocity)
.put("yVelocity",thatRaider.yVelocity),
asyncPostRes -> {
if(asyncPostRes.succeeded()) {
System.out.println(" " + asyncPostRes.result().bodyAsJsonObject().encodePrettily());
} else { // ouch
raidersCounter -= 1;
record.getMetadata().put("raiders_counter", raidersCounter);
discovery.update(record, asyncRecUpdateRes -> { vertx.cancelTimer(timerID);});
}
}
);
});
});
}