212 lines
6.6 KiB
Java
212 lines
6.6 KiB
Java
package com.hivekion.scenario.service.impl;
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
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;
|
|
import com.hivekion.baseData.entity.WeatherResource;
|
|
import com.hivekion.baseData.service.IWeatherResourceService;
|
|
import com.hivekion.baseData.service.ScenarioService;
|
|
import com.hivekion.common.entity.ResponseCmdInfo;
|
|
import com.hivekion.room.RoomManager;
|
|
import com.hivekion.room.bean.BattleRootTask;
|
|
import com.hivekion.room.bean.MoveTask;
|
|
import com.hivekion.room.bean.SupplierTask;
|
|
import com.hivekion.room.func.TaskAction;
|
|
import com.hivekion.scenario.entity.ScenarioTask;
|
|
import com.hivekion.scenario.mapper.ScenarioTaskMapper;
|
|
import com.hivekion.scenario.service.ScenarioTaskService;
|
|
import java.time.Duration;
|
|
import java.util.List;
|
|
import javax.annotation.Resource;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
/**
|
|
* <p>
|
|
* 服务实现类
|
|
* </p>
|
|
*
|
|
* @author liDongYu
|
|
* @since 2025-09-13
|
|
*/
|
|
@Service
|
|
@Slf4j
|
|
public class ScenarioTaskServiceImpl extends
|
|
ServiceImpl<ScenarioTaskMapper, ScenarioTask> implements
|
|
ScenarioTaskService {
|
|
|
|
|
|
@Resource
|
|
private ScenarioService scenarioService;
|
|
|
|
@Resource
|
|
private IWeatherResourceService weatherResourceService;
|
|
|
|
|
|
@Override
|
|
public void start(Integer scenarioId, String roomId) {
|
|
//查询想定的持续时间
|
|
Scenario scenario = scenarioService.getScenarioById(scenarioId);
|
|
if (scenario != null) {
|
|
//查看想定的持续时间
|
|
long duringTime = Duration.between(scenario.getStartTime(),scenario.getEndTime() )
|
|
.getSeconds();
|
|
RoomManager.startRoom(roomId, scenario, duringTime);
|
|
addWeatherEvent(scenario, roomId);
|
|
addTaskEvent(scenario, roomId);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void stop(Integer id, String roomId) {
|
|
RoomManager.stopRoom(roomId);
|
|
}
|
|
|
|
@Override
|
|
public void sleepWhile(Integer id, String roomId) {
|
|
RoomManager.stopRoom(roomId);
|
|
}
|
|
|
|
@Override
|
|
public void wakeup(Integer id, String roomId) {
|
|
RoomManager.resumeRoom(roomId);
|
|
}
|
|
|
|
|
|
@Override
|
|
public List<ScenarioTask> queryTaskList(ScenarioTask task) {
|
|
QueryWrapper<ScenarioTask> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("scenario_id", task.getScenarioId());
|
|
if (StringUtils.isNotBlank(task.getResourceId())) {
|
|
queryWrapper.eq("resource_id", task.getResourceId());
|
|
}
|
|
return this.list(queryWrapper);
|
|
}
|
|
|
|
/**
|
|
* 增加天气任务
|
|
*/
|
|
private void addWeatherEvent(Scenario scenario, String roomId) {
|
|
List<WeatherResource> weatherList = weatherResourceService.list();
|
|
|
|
for (WeatherResource weatherResource : weatherList) {
|
|
long diff = Duration.between(weatherResource.getLastBegTime(), scenario.getStartTime())
|
|
.getSeconds();
|
|
//开始
|
|
TaskAction startAction = new TaskAction() {
|
|
@Override
|
|
public void doSomeThing() {
|
|
Global.sendCmdInfoQueue.add(
|
|
create("start_" + weatherResource.getWeatherType(), scenario, roomId));
|
|
}
|
|
|
|
@Override
|
|
public String getId() {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
public String getType() {
|
|
return "";
|
|
}
|
|
};
|
|
RoomManager.addAction(roomId, diff, startAction);
|
|
//结束
|
|
long duringTime = Duration.between(weatherResource.getLastBegTime(),
|
|
weatherResource.getLastEndTime())
|
|
.getSeconds();
|
|
//开始
|
|
TaskAction endAction = new TaskAction() {
|
|
@Override
|
|
public void doSomeThing() {
|
|
Global.sendCmdInfoQueue.add(
|
|
create("stop_" + weatherResource.getWeatherType(), scenario, roomId));
|
|
}
|
|
|
|
@Override
|
|
public String getId() {
|
|
return "";
|
|
}
|
|
|
|
@Override
|
|
public String getType() {
|
|
return "";
|
|
}
|
|
};
|
|
RoomManager.addAction(roomId, diff + duringTime, endAction);
|
|
}
|
|
}
|
|
|
|
//增加任务
|
|
private void addTaskEvent(Scenario scenario, String roomId) {
|
|
ScenarioTask scenarioTask = new ScenarioTask();
|
|
scenarioTask.setScenarioId(scenario.getId());
|
|
List<ScenarioTask> taskList = this.queryTaskList(scenarioTask);
|
|
log.info("taskList.size ::{}", taskList.size());
|
|
for (ScenarioTask task : taskList) {
|
|
try {
|
|
long diff = Duration.between(scenario.getStartTime(),task.getStartTime())
|
|
.getSeconds();
|
|
ResponseCmdInfo<String> respObj = new ResponseCmdInfo<>();
|
|
switch (task.getTaskType()) {
|
|
//移动任务
|
|
case "1":
|
|
log.info("move task::{}",diff);
|
|
MoveTask moveRootTask = new MoveTask(task, roomId);
|
|
RoomManager.addAction(roomId, diff, moveRootTask);
|
|
respObj.setCmdType("moveTask");
|
|
respObj.setData(JSON.toJSONString(moveRootTask));
|
|
respObj.setRoom(roomId);
|
|
respObj.setScenarioId(scenarioTask.getScenarioId());
|
|
Global.sendCmdInfoQueue.add(respObj);
|
|
break;
|
|
//战斗任务
|
|
case "2":
|
|
BattleRootTask battleRootTask = new BattleRootTask(task, roomId);
|
|
RoomManager.addAction(roomId, diff, battleRootTask);
|
|
respObj.setCmdType("battleTask");
|
|
respObj.setData(JSON.toJSONString(battleRootTask));
|
|
respObj.setRoom(roomId);
|
|
respObj.setScenarioId(scenarioTask.getScenarioId());
|
|
Global.sendCmdInfoQueue.add(respObj);
|
|
break;
|
|
//补充保障任务
|
|
case "4":
|
|
case "5":
|
|
case "6":
|
|
case "7":
|
|
case "8":
|
|
log.info("supplier task::{}",diff);
|
|
SupplierTask supplierTask = new SupplierTask(task, roomId);
|
|
RoomManager.addAction(roomId, diff, supplierTask);
|
|
respObj.setCmdType("supplierTask");
|
|
respObj.setData(JSON.toJSONString(supplierTask));
|
|
respObj.setRoom(roomId);
|
|
respObj.setScenarioId(scenarioTask.getScenarioId());
|
|
Global.sendCmdInfoQueue.add(respObj);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
|
|
} catch (Exception e) {
|
|
log.error("error::", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
private ResponseCmdInfo<Void> create(String type, Scenario scenario, String roomId) {
|
|
ResponseCmdInfo<Void> responseCmdInfo = new ResponseCmdInfo<>();
|
|
responseCmdInfo.setScenarioId(scenario.getId());
|
|
responseCmdInfo.setRoom(roomId);
|
|
responseCmdInfo.setCmdType(type);
|
|
responseCmdInfo.setScenarioId(scenario.getId());
|
|
responseCmdInfo.setRoom(roomId);
|
|
return responseCmdInfo;
|
|
}
|
|
}
|