Merge branch 'main' of http://git.hivekion.com:3000/liyudong/simulation-backend
This commit is contained in:
commit
1525954f1a
|
@ -4,12 +4,12 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
|||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.hivekion.common.entity.SearchInputVo;
|
||||
import com.hivekion.scenario.entity.ScenarioResource;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
@ -66,8 +66,12 @@ public class Scenario extends SearchInputVo {
|
|||
|
||||
@ApiModelProperty("是不是小堆")
|
||||
private Byte isXd;
|
||||
|
||||
|
||||
@TableField(value = "start_time")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime startTime;
|
||||
@TableField(value = "end_time")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime endTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
|
|
@ -57,6 +57,6 @@ public class ScenarioTask implements Serializable {
|
|||
@TableField(value = "task_type")
|
||||
private String taskType;
|
||||
@TableField(exist = false)
|
||||
private String status;
|
||||
private String status = "init";
|
||||
|
||||
}
|
||||
|
|
|
@ -17,11 +17,13 @@ import com.hivekion.scenario.entity.ScenarioTask;
|
|||
import com.hivekion.scenario.mapper.ScenarioTaskMapper;
|
||||
import com.hivekion.scenario.service.ScenarioTaskService;
|
||||
import com.hivekion.scenario.service.TaskLogicService;
|
||||
import com.hivekion.statistic.service.StatisticService;
|
||||
import com.hivekion.thread.SpringGlobalTaskManager;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -34,6 +36,7 @@ import org.springframework.stereotype.Service;
|
|||
* @since 2025-09-13
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ScenarioTaskServiceImpl extends
|
||||
ServiceImpl<ScenarioTaskMapper, ScenarioTask> implements
|
||||
ScenarioTaskService {
|
||||
|
@ -55,12 +58,16 @@ public class ScenarioTaskServiceImpl extends
|
|||
|
||||
@Override
|
||||
public void start(Integer id, String roomId) {
|
||||
log.info("id::{},roomId::{}",id,roomId);
|
||||
Scenario currentScenario = scenarioService.getScenarioById(id);
|
||||
//想定当前持续时间
|
||||
redisUtil.hset(roomId + "_" + id, "duringTime", "0");
|
||||
//想定当前状态
|
||||
redisUtil.hset(roomId + "_" + id, "states", "running");
|
||||
|
||||
Scenario currentScenario = scenarioService.getScenarioById(id);
|
||||
|
||||
|
||||
|
||||
//查询天气数据
|
||||
WeatherResource weatherList = weatherResourceService.getOne(new QueryWrapper<WeatherResource>()
|
||||
.eq("scenario_id",id));
|
||||
|
@ -74,6 +81,7 @@ public class ScenarioTaskServiceImpl extends
|
|||
new Thread(() -> {
|
||||
|
||||
springGlobalTaskManager.startPerSecondTask(roomId + "_" + id + "_task", () -> {
|
||||
|
||||
//时间累计
|
||||
increaseTime(currentScenario, roomId);
|
||||
//天气触发
|
||||
|
@ -101,6 +109,7 @@ public class ScenarioTaskServiceImpl extends
|
|||
}
|
||||
|
||||
private void increaseTime(Scenario currentScenario, String roomId) {
|
||||
try{
|
||||
int mag = Global.roomParamMap.get(currentScenario.getId() + "_" + roomId) == null ? 1
|
||||
: Global.roomParamMap.get(currentScenario.getId() + "_" + roomId).getMag();
|
||||
//获取当前状态
|
||||
|
@ -114,6 +123,10 @@ public class ScenarioTaskServiceImpl extends
|
|||
}
|
||||
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("error::",e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -151,6 +164,7 @@ public class ScenarioTaskServiceImpl extends
|
|||
}
|
||||
|
||||
private void taskTrigger(Scenario currentScenario, String roomId) {
|
||||
try{
|
||||
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
|
||||
if (statusObj != null && statusObj.toString().equals("running")) {
|
||||
Object taskListObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "taskList");
|
||||
|
@ -180,6 +194,10 @@ public class ScenarioTaskServiceImpl extends
|
|||
}
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
log.error("error::",e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,9 @@ import com.hivekion.common.redis.RedisUtil;
|
|||
import com.hivekion.scenario.TaskFinishedCall;
|
||||
import com.hivekion.scenario.entity.ScenarioTask;
|
||||
import com.hivekion.scenario.service.TaskLogicService;
|
||||
import com.hivekion.statistic.bean.StatisticBean;
|
||||
import com.hivekion.statistic.service.StatisticService;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -36,7 +39,11 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|||
@Value("${path.planning.url}")
|
||||
private String pathPlanningUrl;
|
||||
private final WebClient webClient = WebClient.create();
|
||||
|
||||
@Resource
|
||||
private StatisticService statisticService;
|
||||
private final static double fuelUseUpPerSecond = 0.1;
|
||||
private final static double medicalUseUpPerSecond = 0.1;
|
||||
private final static double ammunitionUsePerSecond = 0.1;
|
||||
|
||||
@Override
|
||||
public void handleMoveTask(ScenarioTask scenarioTask, Scenario currentScenario, String roomId,
|
||||
|
@ -46,7 +53,7 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|||
cmdInfo.setData(dataMap);
|
||||
dataMap.put("resourceId", scenarioTask.getResourceId());
|
||||
|
||||
if (!"running".equals(scenarioTask.getStatus())) {
|
||||
if ("init".equals(scenarioTask.getStatus())) {
|
||||
scenarioTask.setStatus("running");
|
||||
|
||||
String url = pathPlanningUrl + "?profile=car&point=" + scenarioTask.getFromLat() + ","
|
||||
|
@ -70,7 +77,26 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|||
scenarioTask.getId() + "_path_points", resultObject.getJSONArray("paths"));
|
||||
}
|
||||
|
||||
} else {//更新坐标
|
||||
Global.sendCmdInfoQueue.add(cmdInfo);
|
||||
//获取物资信息
|
||||
StatisticBean statistic = statisticService.statistic(scenarioTask.getResourceId());
|
||||
redisUtil.hset(roomId + "_" + currentScenario.getId(),
|
||||
"resourceId_statistic_" + scenarioTask.getResourceId(), statistic);
|
||||
} else if ("running".equals(scenarioTask.getStatus())) {
|
||||
|
||||
//消耗油料
|
||||
StatisticBean statistic = getStatistic(scenarioTask, currentScenario, roomId);
|
||||
//获取想定持续时间
|
||||
int duringTime = getCurrentDuringTime(currentScenario, roomId);
|
||||
long seconds =
|
||||
duringTime - Duration.between(scenarioTask.getStartTime(), currentScenario.getStartTime())
|
||||
.getSeconds();
|
||||
if (seconds > 0) {
|
||||
double useUp = seconds * fuelUseUpPerSecond;
|
||||
statistic.getFuel().setCurrent(statistic.getFuel().getTotal() - useUp);
|
||||
setStatistic(scenarioTask, currentScenario, roomId, statistic);
|
||||
}
|
||||
//更新坐标
|
||||
List<double[]> points = new ArrayList<>();
|
||||
cmdInfo.setCmdType("current_position");
|
||||
Object pathsObj = redisUtil.hget(roomId + "_" + currentScenario.getId(),
|
||||
|
@ -105,12 +131,27 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Global.sendCmdInfoQueue.add(cmdInfo);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleBattleTask(ScenarioTask task, Scenario scenario, String roomId) {
|
||||
StatisticBean statistic = getStatistic(task, scenario, roomId);
|
||||
int duringTime = getCurrentDuringTime(scenario, roomId);
|
||||
long seconds =
|
||||
duringTime - Duration.between(task.getStartTime(), scenario.getStartTime())
|
||||
.getSeconds();
|
||||
if (seconds > 0) {
|
||||
statistic.getMedical()
|
||||
.setCurrent(statistic.getMedical().getTotal() - seconds * medicalUseUpPerSecond);
|
||||
statistic.getAmmunition()
|
||||
.setCurrent(statistic.getAmmunition().getTotal() - seconds * ammunitionUsePerSecond);
|
||||
setStatistic(task, scenario, roomId, statistic);
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -125,4 +166,37 @@ public class TaskLogicServiceImpl implements TaskLogicService {
|
|||
|
||||
|
||||
}
|
||||
|
||||
private StatisticBean getStatistic(ScenarioTask task, Scenario scenario, String roomId) {
|
||||
synchronized (this) {
|
||||
Object statisticObj = redisUtil.hget(roomId + "_" + scenario.getId(),
|
||||
"resourceId_statistic_" + task.getResourceId());
|
||||
if (statisticObj != null) {
|
||||
return (StatisticBean) statisticObj;
|
||||
}
|
||||
return new StatisticBean();
|
||||
}
|
||||
}
|
||||
|
||||
private void setStatistic(ScenarioTask task, Scenario scenario, String roomId,
|
||||
StatisticBean bean) {
|
||||
redisUtil.hset(roomId + "_" + scenario.getId(),
|
||||
"resourceId_statistic_" + task.getResourceId(), bean);
|
||||
//推送到前端
|
||||
ResponseCmdInfo<Map<String, Object>> cmdInfo = new ResponseCmdInfo<>();
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
cmdInfo.setData(dataMap);
|
||||
dataMap.put("resourceId", task.getResourceId());
|
||||
dataMap.put("statistic", bean);
|
||||
Global.sendCmdInfoQueue.add(cmdInfo);
|
||||
}
|
||||
|
||||
private int getCurrentDuringTime(Scenario scenario, String roomId) {
|
||||
Object duringTime = redisUtil.hget(roomId + "_" + scenario.getId(), "duringTime");
|
||||
if (duringTime != null) {
|
||||
return (Integer) duringTime;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user