) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId());
+ Double restAmmunition = Double.valueOf(battleConsumeMap.get("ammunition").toString());
+ StatisticBean battleResourceStat = statisticService.statistic(scenarioTask.getResourceId());
+ Double ammunitionConsumeRate = restAmmunition/battleResourceStat.getAmmunition().getTotal()*100;
+ if(Double.valueOf(ammunitionConsumeRate) <= Double.valueOf(ammunition) && suppleFlagMap.get("ammunition") == false){
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
@@ -197,7 +222,9 @@ public class BattleRootTask extends AbtParentTask {
supplierRequestService.save(supplierRequest);
suppleFlagMap.put("ammunition",true) ;
}
- if(Long.valueOf(battleParams.get("death").toString()) <= Long.valueOf(death) && suppleFlagMap.get("death") == false){
+ Long restDeath = Long.valueOf(battleConsumeMap.get("death").toString());
+ Long deathConsumeRate = restDeath*100/battleResourceStat.getPerson().getTotal();
+ if(deathConsumeRate >= Long.valueOf(death) && suppleFlagMap.get("death") == false){
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
@@ -209,7 +236,9 @@ public class BattleRootTask extends AbtParentTask {
supplierRequestService.save(supplierRequest);
suppleFlagMap.put("death",true) ;
}
- if(Long.valueOf(battleParams.get("injured").toString()) <= Long.valueOf(injured) && suppleFlagMap.get("injured") == false){
+ Long restInjured = Long.valueOf(battleConsumeMap.get("injured").toString());
+ Long injuredConsumeRate = restInjured*100/battleResourceStat.getPerson().getTotal();
+ if(Long.valueOf(injuredConsumeRate) <= Long.valueOf(injured) && suppleFlagMap.get("injured") == false){
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
@@ -229,14 +258,11 @@ public class BattleRootTask extends AbtParentTask {
}
});
-
}
-
}
-
}
diff --git a/src/main/java/com/hivekion/room/bean/Coordinate.java b/src/main/java/com/hivekion/room/bean/Coordinate.java
new file mode 100644
index 0000000..bf656df
--- /dev/null
+++ b/src/main/java/com/hivekion/room/bean/Coordinate.java
@@ -0,0 +1,19 @@
+package com.hivekion.room.bean;
+
+import lombok.Data;
+
+/**
+ * [类的简要说明]
+ *
+ * [详细描述,可选]
+ *
+ *
+ * @author LiDongYU
+ * @since 2025/7/22
+ */
+@Data
+public class Coordinate {
+
+ private double lng;
+ private double lat;
+}
diff --git a/src/main/java/com/hivekion/room/bean/MoveRootTask.java b/src/main/java/com/hivekion/room/bean/MoveRootTask.java
index 052bf82..4c38ce2 100644
--- a/src/main/java/com/hivekion/room/bean/MoveRootTask.java
+++ b/src/main/java/com/hivekion/room/bean/MoveRootTask.java
@@ -1,27 +1,24 @@
package com.hivekion.room.bean;
import cn.hutool.extra.spring.SpringUtil;
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONArray;
-import com.alibaba.fastjson2.JSONObject;
-import com.hivekion.Global;
-import com.hivekion.common.MultiPointGeoPosition;
-import com.hivekion.common.entity.ResponseCmdInfo;
-import com.hivekion.enums.WsCmdTypeEnum;
+import com.hivekion.common.redis.RedisUtil;
+import com.hivekion.common.uuid.IdUtils;
+import com.hivekion.room.RoomManager;
import com.hivekion.room.func.TaskAction;
+import com.hivekion.scenario.entity.ScenarioResource;
import com.hivekion.scenario.entity.ScenarioTask;
-import java.util.ArrayList;
-import java.util.HashMap;
+import com.hivekion.scenario.service.impl.BattleSupplierServiceImpl;
+import com.hivekion.scenario.service.impl.ScenarioTaskServiceImpl;
+import com.hivekion.statistic.bean.StatisticBean;
+import com.hivekion.statistic.service.impl.StatisticServiceImpl;
+import com.hivekion.supplier.entity.SupplierRequest;
+import com.hivekion.supplier.service.impl.SupplierRequestServiceImpl;
+import java.time.LocalDateTime;
import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.NavigableMap;
-import java.util.TreeMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-import lombok.Data;
+import java.util.concurrent.atomic.AtomicBoolean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
@@ -42,13 +39,23 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
*/
private final double SPEED = 27;
/**
- * 距离和坐标的对应关系
+ * 需求产生标志
*/
- private final TreeMap distanceInfoMap = new TreeMap<>();
+ private final AtomicBoolean requestFlag = new AtomicBoolean(false);
/**
- * 开始点坐标
+ * 油料消耗速率
*/
- private final AtomicReference startPoint = new AtomicReference<>();
+ private double fuelConsumption = 0;
+ private double fuelThreshold = 0;
+ /**
+ * 消耗任务间隔
+ */
+ private final int consumptionTaskInterval = 5;
+ /**
+ * redis 服务类
+ */
+ private final RedisUtil redis = SpringUtil.getBean(RedisUtil.class);
+ private StatisticBean statisticBean;
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
@@ -58,176 +65,113 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
@Override
public void doSomeThing() {
- log.info("move task running");
+ log.info("move task running:{}",scenarioTask.getResourceId());
+
+ initEnv(); //初始化环境
initPath(); //初始化路径
- updatePath(); //更新路径
+ updatePath(SPEED, null); //更新路径
+ fuelConsumption();//油品消耗
}
/**
- * 初始化路径
+ * 初始化环境
*/
- private void initPath() {
- try {
+ private void initEnv() {
+ try{
+ //获取油品消耗规则
+ String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
+ .getProperty("fuel_spreed");
+ fuelConsumption = Double.parseDouble(fuelConsumptionStr == null ? "0" : fuelConsumptionStr);
+ fuelThreshold = Double.parseDouble(SpringUtil.getBean(Environment.class)
+ .getProperty("fuel.warn ", "0"));
+ statisticBean = SpringUtil.getBean(StatisticServiceImpl.class)
+ .statistic(scenarioTask.getResourceId());
+ }catch (Exception e){
+ log.error("init env exception",e);
+ }
- String url = SpringUtil.getBean(Environment.class).getProperty("path.planning.url");
- String params = url + "?"
- + "profile=car"
- + "&point=" + scenarioTask.getFromLat() + ","
- + scenarioTask.getFromLng()
- + "&point=" + scenarioTask.getToLat() + ","
- + scenarioTask.getToLng()
- + "&points_encoded=false"
- + "&algorithm=alternative_route&alternative_route.max_paths=3";
- //获取路线信息
- String result = webClient.get().uri(params)
- .retrieve()
- .bodyToMono(String.class)
- .block();
+ }
- JSONObject pointJson = JSON.parseObject(result);
- //获取路径点
- if (pointJson != null) {
- JSONObject pointsObj = pointJson.getJSONArray("paths").getJSONObject(0)
- .getJSONObject("points");
- JSONArray coordinates = pointsObj.getJSONArray("coordinates");
- //组装信息
- Map dataMap = new HashMap<>();
- dataMap.put("resourceId", scenarioTask.getResourceId());
- dataMap.put("points", coordinates);
- //推送路径任务
- Global.sendCmdInfoQueue.add(
- ResponseCmdInfo.create(WsCmdTypeEnum.PATH_INIT.getCode(), roomId,
- scenarioTask.getScenarioId(), dataMap));
- log.info("init::{}", JSON.toJSONString(coordinates));
- //计算各个点的累计距离和坐标的对应关系
- double beforeLng = Double.parseDouble(scenarioTask.getFromLng());
- double beforeLat = Double.parseDouble(scenarioTask.getFromLat());
- double total = 0;
- for (int i = 0; i < coordinates.size(); i++) {
- JSONArray coordinate = coordinates.getJSONArray(i);
- Double lng = coordinate.getDouble(0);
- Double lat = coordinate.getDouble(1);
+ private void fuelConsumption() {
+ try{
+ ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
+ 1);
+ schedule.scheduleWithFixedDelay(() -> {
+ if (getRoomStatus()) {
+ double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
- double distance = MultiPointGeoPosition.haversine(beforeLat, beforeLng, lat, lng);
- //当前总距离
- total = total + distance;
- //定义坐标对象
- Coordinate coordinateInfo = new Coordinate();
- coordinateInfo.setLat(lat);
- coordinateInfo.setLng(lng);
+ //更新redis中油品的消耗
+ Object currentFuelObj = redis.hget(
+ scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
+ "fuel");
+ if (currentFuelObj != null) {
+ double fuel = Double.parseDouble(currentFuelObj.toString());
+ fuel = fuel - currentUseUp;
- //记录距离和数组列表直接的索引关系
- distanceInfoMap.put(total, coordinateInfo);
+ redis.hset(
+ scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
+ "fuelConsume", fuel);
- beforeLng = lng;
- beforeLat = lat;
+ double totalFuel = statisticBean.getFuel().getTotal();
+ if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
+ requestFlag.set(true);
+ //需要产生需求
+ produceFuelRequest();
+ //产生任务
+ produceTask();
+ }
+ }
+ //插入消耗表
}
- //设置第一个开始位置
- startPoint.set(distanceInfoMap.firstKey());
- }
- } catch (Exception e) {
- log.error("error::", e);
+
+ }, 0, consumptionTaskInterval, TimeUnit.SECONDS);
+ }catch (Exception e){
+ log.error("fuel consumption exception",e);
+ }
+
+
+ }
+
+ private void produceFuelRequest() {
+ SupplierRequest supplierRequest = new SupplierRequest();
+ supplierRequest.setId(IdUtils.simpleUUID());
+ supplierRequest.setFromResourceId(scenarioTask.getResourceId());
+ supplierRequest.setSupplierNum(String.valueOf(statisticBean.getFuel().getTotal()));
+ supplierRequest.setSupplierType("fuel");
+ supplierRequest.setGeneralTime(LocalDateTime.now());
+ supplierRequest.setLat(scenarioTask.getToLat());
+ supplierRequest.setLng(scenarioTask.getToLng());
+ supplierRequest.setHandleFlag(1);
+ SpringUtil.getBean(SupplierRequestServiceImpl.class).save(supplierRequest);
+ }
+
+ private void produceTask() {
+
+ List resourceList = SpringUtil.getBean(BattleSupplierServiceImpl.class)
+ .selectSupplierResource(scenarioTask.getResourceId());
+ if (!resourceList.isEmpty()) {
+ ScenarioTask task = new ScenarioTask();
+ task.setId(IdUtils.simpleUUID());
+ task.setScenarioId(scenarioTask.getScenarioId());
+ task.setResourceId(scenarioTask.getResourceId());
+ task.setTaskType("6");
+ task.setSupplierNum(statisticBean.getFuel().getTotal());
+ task.setToLat(scenarioTask.getToLat());
+ task.setToLng(scenarioTask.getToLng());
+ task.setStartTime(LocalDateTime.now());
+ task.setFromLat(resourceList.get(0).getLat());
+ task.setFromLng(resourceList.get(0).getLng());
+ task.setFromSource("general");
+ SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
+ //增加到房间任务
+ SupplierTask supplierTask = new SupplierTask(task, roomId);
+ //立即执行
+ RoomManager.addAction(roomId, 0, supplierTask);
}
}
-
- private void updatePath() {
-
- ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
- 1);
- schedule.scheduleWithFixedDelay(() -> {
-
- try {
- if (this.getRoomStatus()) {
-
- long duringTime = getDuringTime();
- log.info("duringTime::{}", duringTime);
- //跑动距离
- double distance = duringTime * SPEED;
- //获取大与此距离的第一个路线点key
- Entry endPoint = distanceInfoMap.ceilingEntry(distance);
- //ws数据
- List dataList = new ArrayList<>();
- HashMap