diff --git a/src/main/java/com/hivekion/room/RoomManager.java b/src/main/java/com/hivekion/room/RoomManager.java index 8e7d724..1ff3416 100644 --- a/src/main/java/com/hivekion/room/RoomManager.java +++ b/src/main/java/com/hivekion/room/RoomManager.java @@ -76,4 +76,11 @@ public class RoomManager { } return false; } + public static int getMag(String id){ + Room room = roomsMap.get(id); + if (room != null) { + return room.getMag(); + } + return 1; + } } diff --git a/src/main/java/com/hivekion/room/bean/AbtParentTask.java b/src/main/java/com/hivekion/room/bean/AbtParentTask.java index 9a7a990..5e17ca8 100644 --- a/src/main/java/com/hivekion/room/bean/AbtParentTask.java +++ b/src/main/java/com/hivekion/room/bean/AbtParentTask.java @@ -28,7 +28,9 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import com.hivekion.statistic.bean.ScenarioInfo; @@ -66,6 +68,9 @@ public abstract class AbtParentTask implements TaskAction { private RedisUtil redisUtil; private com.hivekion.statistic.service.ScenarioService scenarioService; + //是否可以移动 + protected final AtomicBoolean canMoved = new AtomicBoolean(true); + protected final AtomicReference coordinateReference = new AtomicReference<>(new Coordinate()); /** * 任务相对与想定的开始时间 */ @@ -114,7 +119,9 @@ public abstract class AbtParentTask implements TaskAction { public long getDuringTime() { return RoomManager.getRoomDuringTime(this.roomId); } - + protected int getMag(){ + return RoomManager.getMag(this.roomId); + } //获取房间状态 public boolean getRoomStatus() { return RoomManager.isRunning(roomId); @@ -204,13 +211,14 @@ public abstract class AbtParentTask implements TaskAction { } protected void updatePath(double speed,TaskAction duringAction, TaskAction finishedAction) { - + AtomicLong runSeconds = new AtomicLong(0); ScheduledExecutorService schedule = Executors.newScheduledThreadPool( 1); schedule.scheduleWithFixedDelay(() -> { log.info("task is running...."); try { - if (this.getRoomStatus()) { + + if (this.getRoomStatus()&&canMoved.get()) { if (distanceInfoMap.isEmpty()) { return; } @@ -221,8 +229,9 @@ public abstract class AbtParentTask implements TaskAction { if(duringAction!=null){ duringAction.doSomeThing(); } + runSeconds.getAndAdd(getMag()); //跑动距离 - double distance = duringTime * speed; + double distance = runSeconds.get() * speed; //获取大与此距离的第一个路线点key Entry endPoint = distanceInfoMap.ceilingEntry(distance); @@ -262,6 +271,7 @@ public abstract class AbtParentTask implements TaskAction { Coordinate coordinate = new Coordinate(); coordinate.setLat(insertPoints[0]); coordinate.setLng(insertPoints[1]); + coordinateReference.set(coordinate); distanceInfoMap.put(distance, coordinate); startPoint.set(distance); SpringUtil.getBean(RedisUtil.class).hset( @@ -272,6 +282,7 @@ public abstract class AbtParentTask implements TaskAction { ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId, scenarioTask.getScenarioId(), dataMap)); + } else if (Double.compare(distance, endPoint.getKey()) == 0) { NavigableMap subPathMap = distanceInfoMap.subMap(startPoint.get(), true, endPoint.getKey(), true); @@ -279,7 +290,7 @@ public abstract class AbtParentTask implements TaskAction { Coordinate coordinate = subPathMap.get(key); dataList.add(new double[]{coordinate.getLng(), coordinate.getLat()}); } - + coordinateReference.set(endPoint.getValue()); startPoint.set(endPoint.getKey()); Global.sendCmdInfoQueue.add( ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId, @@ -289,6 +300,7 @@ public abstract class AbtParentTask implements TaskAction { if (finishedAction != null) { finishedAction.doSomeThing(); } + coordinateReference.set(endPoint.getValue()); //完成路径 Global.sendCmdInfoQueue.add( ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId, @@ -299,6 +311,7 @@ public abstract class AbtParentTask implements TaskAction { } + } catch (Exception e) { log.error("error::", e); } diff --git a/src/main/java/com/hivekion/room/bean/MoveTask.java b/src/main/java/com/hivekion/room/bean/MoveTask.java index 6bd4a78..24f9a71 100644 --- a/src/main/java/com/hivekion/room/bean/MoveTask.java +++ b/src/main/java/com/hivekion/room/bean/MoveTask.java @@ -71,7 +71,7 @@ public class MoveTask extends AbtParentTask implements TaskAction { @Override public void doSomeThing() { - log.info("move task running:{}", scenarioTask.getResourceId()); + log.info("move task running:{},fuel::{}", scenarioTask.getResourceId(), getCurrentFuel()); initEnv(); //初始化环境 initPath(); //初始化路径 @@ -81,10 +81,10 @@ public class MoveTask extends AbtParentTask implements TaskAction { //推送移动任务 Map map = new HashMap<>(); - map.put("duringTime",getDuringTime()); - map.put("id",scenarioTask.getResourceId()); - map.put("roomStatus",true); - map.put("type",scenarioTask.getType()); + map.put("duringTime", getDuringTime()); + map.put("id", scenarioTask.getResourceId()); + map.put("roomStatus", true); + map.put("type", scenarioTask.getType()); Global.sendCmdInfoQueue.add( ResponseCmdInfo.create("moveTask", roomId, @@ -100,7 +100,7 @@ public class MoveTask extends AbtParentTask implements TaskAction { public String getType() { return ""; } - },null); //更新路径 + }, null); //更新路径 fuelConsumption();//油品消耗 } @@ -129,35 +129,43 @@ public class MoveTask extends AbtParentTask implements TaskAction { ScheduledExecutorService schedule = Executors.newScheduledThreadPool( 1); schedule.scheduleWithFixedDelay(() -> { - if (getRoomStatus()) { + if (getRoomStatus() && this.canMoved.get()) { double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption; - //更新redis中油品的消耗 - Object currentFuelObj = redis.hget( - scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), - "fuel"); - if (currentFuelObj != null) { - double fuel = Double.parseDouble(currentFuelObj.toString()); - fuel = fuel - currentUseUp; - - redis.hset( - scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), - "fuelConsume", fuel); - - double totalFuel = statisticBean.getFuel().getTotal(); - if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) { - requestFlag.set(true); - //需要产生需求 - produceFuelRequest(); - //产生任务 - produceTask(); - - } + double fuel = getCurrentFuel(); + if (fuel == 0) { + log.error("fuel is empty"); + return; } + fuel = fuel - currentUseUp; + + redis.hset( + scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), + "fuelConsume", fuel); + + double totalFuel = statisticBean.getFuel().getTotal(); + if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) { + log.info("fuel is not enough,stop moving"); + this.canMoved.set(false); + requestFlag.set(true); + //需要产生需求 + produceFuelRequest(); + //产生任务 + produceTask(); + + } + //插入消耗表 insertConsumption(currentUseUp); pushStatus(scenarioTask.getResourceId()); } + if (!this.canMoved.get()) { + //判断油料是否满足 + double totalFuel = statisticBean.getFuel().getTotal(); + if (Double.compare(this.getCurrentFuel(), totalFuel) >= 0) { + this.canMoved.set(true); + } + } }, 0, consumptionTaskInterval, TimeUnit.SECONDS); @@ -193,8 +201,8 @@ public class MoveTask extends AbtParentTask implements TaskAction { task.setResourceId(scenarioTask.getResourceId()); task.setTaskType("6"); task.setSupplierNum(statisticBean.getFuel().getTotal()); - task.setToLat(scenarioTask.getToLat()); - task.setToLng(scenarioTask.getToLng()); + task.setToLat(this.coordinateReference.get().getLat() + ""); + task.setToLng(this.coordinateReference.get().getLng() + ""); task.setStartTime(LocalDateTime.now()); task.setFromLat(resourceList.get(0).getLat()); task.setFromLng(resourceList.get(0).getLng()); @@ -208,7 +216,7 @@ public class MoveTask extends AbtParentTask implements TaskAction { } private void insertConsumption(double num) { - log.info("insertConsumption....{}",num); + log.info("insertConsumption....{}", num); BattleConsume battleConsume = new BattleConsume(); battleConsume.setId(IdUtils.simpleUUID()); battleConsume.setResourceId(scenarioTask.getResourceId()); @@ -217,6 +225,15 @@ public class MoveTask extends AbtParentTask implements TaskAction { SpringUtil.getBean(BattleConsumeServiceImpl.class).save(battleConsume); } + private double getCurrentFuel() { + Object currentFuelObj = redis.hget( + scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), + "fuel"); + if (currentFuelObj != null) { + return Double.parseDouble(currentFuelObj.toString()); + } + return 0; + } } diff --git a/src/main/java/com/hivekion/room/bean/Room.java b/src/main/java/com/hivekion/room/bean/Room.java index 9d364b1..87b7b31 100644 --- a/src/main/java/com/hivekion/room/bean/Room.java +++ b/src/main/java/com/hivekion/room/bean/Room.java @@ -92,7 +92,8 @@ public class Room implements AutoCloseable { status.set(true); totalTime.set(time); startTask(); - //初始化系统资源 物资人员等信息 + //初始化系统资源 初始化resource下物资信息 + //1. } @@ -206,5 +207,8 @@ public class Room implements AutoCloseable { return status.get(); } + public int getMag() { + return this.mag; + } }