simulation-backend/src/main/java/com/hivekion/room/bean/MoveRootTask.java
2025-09-19 12:14:09 +08:00

223 lines
7.1 KiB
Java

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.baseData.entity.Scenario;
import com.hivekion.baseData.service.ScenarioService;
import com.hivekion.common.MultiPointGeoPosition;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.common.redis.RedisUtil;
import com.hivekion.enums.WsCmdTypeEnum;
import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import com.hivekion.statistic.bean.StatisticBean;
import com.hivekion.statistic.service.impl.StatisticServiceImpl;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
/**
* [类的简要说明]
* <p>
* [详细描述,可选]
* <p>
*
* @author LiDongYU
* @since 2025/7/22
*/
@Slf4j
public class MoveRootTask extends AbtParentTask implements TaskAction {
/**
* 速度 换算为100Km/小时
*/
private final double SPEED = 27;
/**
* 油料消耗速率
*/
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) {
super(scenarioTask, roomId);
}
@Override
public void doSomeThing() {
log.info("move task running");
initEnv(); //初始化环境
initPath(); //初始化路径
updatePath(SPEED); //更新路径
fuelConsumption();//油品消耗
}
/**
* 初始化环境
*/
private void initEnv() {
//获取油品消耗规则
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());
}
private void fuelConsumption() {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
if (getRoomStatus()) {
double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
//更新redis中油品的消耗
Object currentFuelObj = redis.hget(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuelConsume");
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){
//产生一个需求
//insertRequest(totalFuel-fuel,getDuringTime());
}
}
// statistic();
}
}, 0, consumptionTaskInterval, TimeUnit.SECONDS);
}
// private void statistic() {
//
// Object positionObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "position");
// if (positionObj != null) {
// Coordinate coordinate = JSONObject.parseObject(positionObj.toString(), Coordinate.class);
// statisticBean.getTeam().setLat(coordinate.lat + "");
// statisticBean.getTeam().setLng(coordinate.lng + "");
//
// }
// //设置人员受伤信息
// Object deathPerson = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "deathConsume");
// if (deathPerson != null) {
// statisticBean.getPerson().setDeath(Integer.parseInt(deathPerson.toString()));
// statisticBean.getPerson().setCurrent(
// statisticBean.getPerson().getTotal() - Integer.parseInt(deathPerson.toString()));
// }
// Object injuredPerson = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "injuredConsume");
// if (injuredPerson != null) {
// statisticBean.getPerson().setInjured(Integer.parseInt(injuredPerson.toString()));
// }
// //设置弹药信息
// Object ammunitionObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "ammunitionConsume");
// if (ammunitionObj != null) {
// statisticBean.getAmmunition().setCurrent(
// statisticBean.getAmmunition().getTotal() - Integer.parseInt(ammunitionObj.toString()));
// }
// //设置食品信息
// Object foodObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "foodConsume");
// if (foodObj != null) {
// statisticBean.getFood()
// .setCurrent(statisticBean.getFood().getTotal() - Integer.parseInt(foodObj.toString()));
// }
// //设置水信息
// Object waterObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "waterConsume");
// if (waterObj != null) {
// statisticBean.getWater()
// .setCurrent(statisticBean.getWater().getTotal() - Integer.parseInt(waterObj.toString()));
// }
// //设置油料信息
// Object fuelObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "fuelConsume");
// if (fuelObj != null) {
// statisticBean.getFuel()
// .setCurrent(statisticBean.getFuel().getTotal() - Integer.parseInt(fuelObj.toString()));
// }
// //设置药品信息
// Object medicalObj = redis.hget(
// scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
// "medicalConsume");
// if (medicalObj != null) {
// statisticBean.getMedical().setCurrent(
// statisticBean.getMedical().getTotal() - Integer.parseInt(medicalObj.toString()));
// }
// Global.sendCmdInfoQueue.add(
// ResponseCmdInfo.create(WsCmdTypeEnum.STATISTIC.getCode(), roomId,
// scenarioTask.getScenarioId(), statisticBean));
// }
//插入需求表
// private void insertRequest(double num,long second){
//
//
// }
// //插入消耗表
// private void insertConsumption (double num,long second) {
//
//
// }
}