2025-09-14 14:53:14 +08:00
|
|
|
package com.hivekion.scenario.service.impl;
|
|
|
|
|
|
2025-09-14 15:30:47 +08:00
|
|
|
import cn.hutool.json.JSONObject;
|
2025-09-14 23:08:37 +08:00
|
|
|
import com.alibaba.fastjson.JSON;
|
2025-09-14 23:10:15 +08:00
|
|
|
import com.alibaba.fastjson.JSONArray;
|
2025-09-14 14:53:14 +08:00
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import com.hivekion.Global;
|
|
|
|
|
import com.hivekion.baseData.entity.Scenario;
|
2025-09-14 15:30:47 +08:00
|
|
|
import com.hivekion.baseData.entity.WeatherResource;
|
|
|
|
|
import com.hivekion.baseData.service.IWeatherResourceService;
|
2025-09-14 14:53:14 +08:00
|
|
|
import com.hivekion.baseData.service.ScenarioService;
|
2025-09-14 15:30:47 +08:00
|
|
|
import com.hivekion.common.entity.ResponseCmdInfo;
|
2025-09-14 14:53:14 +08:00
|
|
|
import com.hivekion.common.redis.RedisUtil;
|
|
|
|
|
import com.hivekion.environment.entity.SimtoolWeather;
|
|
|
|
|
import com.hivekion.environment.service.SimtoolWeatherService;
|
|
|
|
|
import com.hivekion.scenario.entity.ScenarioTask;
|
|
|
|
|
import com.hivekion.scenario.mapper.ScenarioTaskMapper;
|
|
|
|
|
import com.hivekion.scenario.service.ScenarioTaskService;
|
2025-09-14 16:40:30 +08:00
|
|
|
import com.hivekion.scenario.service.TaskLogicService;
|
2025-09-14 18:01:52 +08:00
|
|
|
import com.hivekion.statistic.service.StatisticService;
|
2025-09-14 14:53:14 +08:00
|
|
|
import com.hivekion.thread.SpringGlobalTaskManager;
|
2025-09-14 23:10:15 +08:00
|
|
|
|
|
|
|
|
import java.time.ZoneId;
|
2025-09-14 15:30:47 +08:00
|
|
|
import java.time.format.DateTimeFormatter;
|
2025-09-14 14:53:14 +08:00
|
|
|
import java.util.List;
|
2025-09-14 18:33:09 +08:00
|
|
|
import javax.annotation.PostConstruct;
|
2025-09-14 14:53:14 +08:00
|
|
|
import javax.annotation.Resource;
|
2025-09-14 18:27:23 +08:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2025-09-14 14:53:14 +08:00
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* <p>
|
|
|
|
|
* 服务实现类
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @author liDongYu
|
|
|
|
|
* @since 2025-09-13
|
|
|
|
|
*/
|
|
|
|
|
@Service
|
2025-09-14 18:27:23 +08:00
|
|
|
@Slf4j
|
2025-09-14 14:53:14 +08:00
|
|
|
public class ScenarioTaskServiceImpl extends
|
|
|
|
|
ServiceImpl<ScenarioTaskMapper, ScenarioTask> implements
|
|
|
|
|
ScenarioTaskService {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private SpringGlobalTaskManager springGlobalTaskManager;
|
2025-09-14 23:51:40 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
@Resource
|
|
|
|
|
private RedisUtil redisUtil;
|
|
|
|
|
@Resource
|
|
|
|
|
private ScenarioService scenarioService;
|
|
|
|
|
|
2025-09-14 15:30:47 +08:00
|
|
|
@Resource
|
|
|
|
|
private IWeatherResourceService weatherResourceService;
|
2025-09-14 16:40:30 +08:00
|
|
|
@Resource
|
|
|
|
|
private TaskLogicService taskLogicService;
|
2025-09-14 15:30:47 +08:00
|
|
|
|
2025-09-14 23:10:15 +08:00
|
|
|
@PostConstruct
|
|
|
|
|
public void initTest(){
|
|
|
|
|
this.start(2746,"1");
|
|
|
|
|
}
|
2025-09-14 14:53:14 +08:00
|
|
|
@Override
|
2025-09-14 23:08:37 +08:00
|
|
|
public void start(Integer scenarioId, String roomId) {
|
|
|
|
|
log.info("scenarioId::{},roomId::{}",scenarioId,roomId);
|
|
|
|
|
Scenario currentScenario = scenarioService.getScenarioById(scenarioId);
|
2025-09-14 14:53:14 +08:00
|
|
|
//想定当前持续时间
|
2025-09-14 23:08:37 +08:00
|
|
|
redisUtil.hset(roomId + "_" + scenarioId, "duringTime", "0");
|
2025-09-14 14:53:14 +08:00
|
|
|
//想定当前状态
|
2025-09-14 23:08:37 +08:00
|
|
|
redisUtil.hset(roomId + "_" + scenarioId, "states", "running");
|
2025-09-14 14:53:14 +08:00
|
|
|
|
2025-09-14 18:01:52 +08:00
|
|
|
|
|
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
|
|
|
|
|
//查询天气数据
|
2025-09-14 23:10:15 +08:00
|
|
|
List<WeatherResource> weatherList = weatherResourceService.list(new QueryWrapper<WeatherResource>()
|
2025-09-14 23:08:37 +08:00
|
|
|
.eq("scenario_id",scenarioId));
|
2025-09-14 23:10:15 +08:00
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
|
|
for(WeatherResource weatherResource: weatherList) {
|
|
|
|
|
String weaherStr = JSON.toJSONString(weatherResource);
|
|
|
|
|
Long timeBegstamp = weatherResource.getLastBegTime().atZone(ZoneId.systemDefault())
|
|
|
|
|
.toInstant()
|
|
|
|
|
.toEpochMilli();
|
|
|
|
|
Long timeEndstamp = weatherResource.getLastEndTime().atZone(ZoneId.systemDefault())
|
|
|
|
|
.toInstant()
|
|
|
|
|
.toEpochMilli();
|
|
|
|
|
com.alibaba.fastjson.JSONObject weatherObj = JSON.parseObject(weaherStr);
|
|
|
|
|
weatherObj.put("weatherBegTime",timeBegstamp);
|
|
|
|
|
weatherObj.put("weatherEndTime",timeEndstamp);
|
|
|
|
|
jsonArray.add(weatherObj);
|
|
|
|
|
}
|
2025-09-14 14:53:14 +08:00
|
|
|
//放入天气数据
|
2025-09-14 23:50:40 +08:00
|
|
|
redisUtil.hset(roomId + "_" + scenarioId, "weather", JSON.toJSONString(jsonArray));
|
2025-09-14 14:53:14 +08:00
|
|
|
//查询任务
|
|
|
|
|
ScenarioTask queryTask = new ScenarioTask();
|
2025-09-14 23:08:37 +08:00
|
|
|
queryTask.setScenarioId(scenarioId);
|
|
|
|
|
redisUtil.hset(roomId + "_" + scenarioId, "taskList", JSON.toJSONString(queryTaskList(queryTask)));
|
2025-09-14 14:53:14 +08:00
|
|
|
new Thread(() -> {
|
|
|
|
|
|
2025-09-14 23:08:37 +08:00
|
|
|
springGlobalTaskManager.startPerSecondTask(roomId + "_" + scenarioId + "_task", () -> {
|
2025-09-14 18:28:51 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
//时间累计
|
|
|
|
|
increaseTime(currentScenario, roomId);
|
|
|
|
|
//天气触发
|
|
|
|
|
weatherTrigger(currentScenario, roomId);
|
|
|
|
|
//任务触发
|
|
|
|
|
taskTrigger(currentScenario, roomId);
|
2025-09-14 16:43:51 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
});
|
|
|
|
|
}).start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void stop(Integer id, String roomId) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void sleepWhile(Integer id, String roomId) {
|
|
|
|
|
redisUtil.hset(roomId + "_" + id, "states", "sleep");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void wakeup(Integer id, String roomId) {
|
|
|
|
|
redisUtil.hset(roomId + "_" + id, "states", "running");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void increaseTime(Scenario currentScenario, String roomId) {
|
2025-09-14 18:28:51 +08:00
|
|
|
try{
|
2025-09-14 23:08:37 +08:00
|
|
|
log.info("currentScenario:;{}",currentScenario);
|
2025-09-14 18:28:51 +08:00
|
|
|
int mag = Global.roomParamMap.get(currentScenario.getId() + "_" + roomId) == null ? 1
|
|
|
|
|
: Global.roomParamMap.get(currentScenario.getId() + "_" + roomId).getMag();
|
|
|
|
|
//获取当前状态
|
|
|
|
|
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
|
|
|
|
|
if (statusObj != null && statusObj.toString().equals("running")) {
|
|
|
|
|
Object duringObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "duringTime");
|
|
|
|
|
if (duringObj != null) {
|
|
|
|
|
int oldValue = duringObj instanceof Integer ? (Integer) duringObj : 0;
|
|
|
|
|
oldValue = oldValue + mag;
|
|
|
|
|
redisUtil.hset(roomId + "_" + currentScenario.getId(), "duringTime", oldValue);
|
|
|
|
|
}
|
2025-09-14 14:53:14 +08:00
|
|
|
|
2025-09-14 18:28:51 +08:00
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("error::",e);
|
2025-09-14 14:53:14 +08:00
|
|
|
}
|
2025-09-14 18:28:51 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 天气触发
|
|
|
|
|
*
|
|
|
|
|
* @param currentScenario 当前想定
|
|
|
|
|
* @param roomId 房间ID
|
|
|
|
|
*/
|
|
|
|
|
private void weatherTrigger(Scenario currentScenario, String roomId) {
|
2025-09-14 15:30:47 +08:00
|
|
|
try {
|
2025-09-14 23:10:15 +08:00
|
|
|
String weatherResources = (String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather");
|
|
|
|
|
JSONArray weatherArray = JSONArray.parseArray(weatherResources);
|
|
|
|
|
String weatherStatus = redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status") !=null?(String) redisUtil.hget(roomId + "_" + currentScenario.getId(), "weather-status"):null;
|
|
|
|
|
for(int i=0;i<weatherArray.size();i++){
|
|
|
|
|
com.alibaba.fastjson.JSONObject weatherObj = (com.alibaba.fastjson.JSONObject) weatherArray.get(i);
|
|
|
|
|
Long timeBegstamp =Long.valueOf(weatherObj.getString("weatherBegTime"));
|
|
|
|
|
Long timeEndstamp =Long.valueOf(weatherObj.getString("weatherEndTime"));
|
|
|
|
|
Long duringTime = Long.valueOf(redisUtil.hget(roomId + "_" + currentScenario.getId(), "duringTime").toString());
|
|
|
|
|
Long scenarioBegtime = currentScenario.getStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
|
|
|
|
if(scenarioBegtime+duringTime >= timeBegstamp && StringUtils.isEmpty(weatherStatus)) {
|
|
|
|
|
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
responseCmdInfo.setCmdType("start-" + weatherObj.getString("weatherType"));
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
System.out.println(responseCmdInfo.toString());
|
|
|
|
|
redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather-status","start");
|
|
|
|
|
Global.sendCmdInfoQueue.add(responseCmdInfo);
|
|
|
|
|
}
|
|
|
|
|
else if(timeBegstamp+duringTime >= timeEndstamp && StringUtils.isNotEmpty(weatherStatus)){
|
|
|
|
|
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
responseCmdInfo.setCmdType("end-" + weatherObj.getString("weatherType"));
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
System.out.println(responseCmdInfo.toString());
|
|
|
|
|
redisUtil.hset(roomId + "_" + currentScenario.getId(), "weather-status","end");
|
|
|
|
|
Global.sendCmdInfoQueue.add(responseCmdInfo);
|
|
|
|
|
}else{
|
|
|
|
|
ResponseCmdInfo<JSONObject> responseCmdInfo = new ResponseCmdInfo();
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
responseCmdInfo.setCmdType("remain-" + weatherObj.getString("weatherType"));
|
|
|
|
|
responseCmdInfo.setScenarioId(currentScenario.getId());
|
|
|
|
|
responseCmdInfo.setRoom(roomId);
|
|
|
|
|
System.out.println(responseCmdInfo.toString());
|
|
|
|
|
Global.sendCmdInfoQueue.add(responseCmdInfo);
|
|
|
|
|
}
|
2025-09-14 16:16:33 +08:00
|
|
|
}
|
2025-09-14 16:43:51 +08:00
|
|
|
|
2025-09-14 23:10:15 +08:00
|
|
|
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
ex.printStackTrace();
|
2025-09-14 15:30:47 +08:00
|
|
|
log.error(ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
|
|
|
|
|
}
|
2025-09-14 21:56:23 +08:00
|
|
|
/**
|
|
|
|
|
* 获取当前想定从开始到现在时间
|
|
|
|
|
* @param scenario
|
|
|
|
|
* @param roomId
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private int getCurrentDuringTime(Scenario scenario, String roomId) {
|
|
|
|
|
Object duringTime = redisUtil.hget(roomId + "_" + scenario.getId(), "duringTime");
|
|
|
|
|
if (duringTime != null) {
|
|
|
|
|
return (Integer) duringTime;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-09-14 14:53:14 +08:00
|
|
|
private void taskTrigger(Scenario currentScenario, String roomId) {
|
2025-09-14 18:28:51 +08:00
|
|
|
try{
|
2025-09-14 23:08:37 +08:00
|
|
|
log.info("{}",currentScenario);
|
2025-09-14 18:28:51 +08:00
|
|
|
Object statusObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "status");
|
|
|
|
|
if (statusObj != null && statusObj.toString().equals("running")) {
|
|
|
|
|
Object taskListObj = redisUtil.hget(roomId + "_" + currentScenario.getId(), "taskList");
|
|
|
|
|
if (taskListObj != null) {
|
|
|
|
|
|
|
|
|
|
if (taskListObj instanceof List<?>) {
|
|
|
|
|
List<?> taskList = (List<?>) taskListObj;
|
|
|
|
|
for (Object task : taskList) {
|
|
|
|
|
|
|
|
|
|
ScenarioTask scenarioTask = (ScenarioTask) task;
|
|
|
|
|
switch (scenarioTask.getTaskType()) {
|
|
|
|
|
case "1":
|
|
|
|
|
taskLogicService.handleMoveTask(scenarioTask, currentScenario, roomId,18.0,null);
|
|
|
|
|
break;
|
|
|
|
|
case "2":
|
|
|
|
|
taskLogicService.handleBattleTask(scenarioTask, currentScenario, roomId);
|
|
|
|
|
break;
|
|
|
|
|
case "4":
|
|
|
|
|
case "5":
|
|
|
|
|
case "6":
|
|
|
|
|
case "7":
|
|
|
|
|
default:
|
|
|
|
|
taskLogicService.supplierTask(scenarioTask, currentScenario, roomId);
|
|
|
|
|
}
|
2025-09-14 17:03:11 +08:00
|
|
|
|
2025-09-14 18:28:51 +08:00
|
|
|
}
|
2025-09-14 16:40:30 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-14 18:28:51 +08:00
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("error::",e);
|
2025-09-14 16:40:30 +08:00
|
|
|
}
|
2025-09-14 18:28:51 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
}
|
2025-09-14 16:40:30 +08:00
|
|
|
|
2025-09-14 14:53:14 +08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<ScenarioTask> queryTaskList(ScenarioTask task) {
|
|
|
|
|
QueryWrapper<ScenarioTask> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
queryWrapper.eq("scenario_id", task.getScenarioId());
|
2025-09-14 16:40:30 +08:00
|
|
|
if (StringUtils.isNotBlank(task.getResourceId())) {
|
2025-09-14 14:53:14 +08:00
|
|
|
queryWrapper.eq("resource_id", task.getResourceId());
|
|
|
|
|
}
|
|
|
|
|
return this.list(queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|