Conflicts:
	src/main/java/com/hivekion/room/bean/BattleRootTask.java
This commit is contained in:
wangwenhua 2025-09-21 06:24:50 +08:00
commit dbd27f27e0
13 changed files with 205 additions and 73 deletions

View File

@ -1,11 +1,13 @@
package com.hivekion.room;
import com.alibaba.fastjson2.JSON;
import com.hivekion.baseData.entity.Scenario;
import com.hivekion.room.bean.Room;
import com.hivekion.room.func.TaskAction;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import lombok.extern.slf4j.Slf4j;
/**
* [类的简要说明]
@ -16,6 +18,7 @@ import java.util.concurrent.ScheduledExecutorService;
* @author LiDongYU
* @since 2025/7/22
*/
@Slf4j
public class RoomManager {
private static final Map<String, Room> roomsMap = new ConcurrentHashMap<>();
@ -48,6 +51,7 @@ public class RoomManager {
}
public static void addAction(String id, long time, TaskAction action) {
log.info("增加任务::time::{},action::{}",time, JSON.toJSONString(action));
Room room = roomsMap.get(id);
if (room != null) {
room.addAction(time, action);

View File

@ -17,6 +17,7 @@ import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioResource;
import com.hivekion.scenario.entity.ScenarioTask;
import com.hivekion.scenario.service.impl.BattleSupplierServiceImpl;
import com.hivekion.scenario.service.impl.ScenarioResourceServiceImpl;
import com.hivekion.scenario.service.impl.ScenarioTaskServiceImpl;
import com.hivekion.statistic.bean.EditScenarioInfo;
import com.hivekion.statistic.bean.ScenarioInfo;
@ -24,6 +25,8 @@ 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 com.hivekion.team.entity.Teaminfo;
import com.hivekion.team.service.impl.TeaminfoServiceImpl;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
@ -57,6 +60,8 @@ import org.springframework.web.reactive.function.client.WebClient;
@Slf4j
public abstract class AbtParentTask implements TaskAction {
protected final AtomicBoolean taskFinishedStatus = new AtomicBoolean(false);
/**
* 油料消耗速率
*/
@ -84,16 +89,7 @@ public abstract class AbtParentTask implements TaskAction {
private final AtomicBoolean requestFlag = new AtomicBoolean(false);
private StatisticBean statisticBean;
//线程池
// protected ThreadPoolExecutor executor = new ThreadPoolExecutor(
// 5, // 核心线程数
// 10, // 最大线程数
// 60L, // 空闲线程存活时间
// TimeUnit.SECONDS, // 时间单位
// new LinkedBlockingQueue<>(100), // 任务队列
// new CustomThreadFactory("MyPool"), // 线程工厂
// new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
// );
public AbtParentTask(ScenarioTask scenarioTask, String roomId) {
this.scenarioTask = scenarioTask;
@ -256,31 +252,37 @@ public abstract class AbtParentTask implements TaskAction {
try {
if (this.getRoomStatus()) {
//自动生成的任务不需要判断油量不要在生成新的任务
log.info("{}-fromSource::{}",scenarioTask.getResourceId(),scenarioTask.getFromSource());
if(!"general".equals(scenarioTask.getFromSource())) {
double currentFuel = getCurrentFuel();
double totalFuel = statisticBean.getFuel().getTotal();
log.info("totalFuel::{}", totalFuel);
if (currentFuel <= 0 || totalFuel <= 0) {
log.error("{}:油量为零停止移动", this.scenarioTask.getResourceId());
return;
}
double currentFuel = getCurrentFuel();
double totalFuel = statisticBean.getFuel().getTotal();
if (currentFuel <= 0 || totalFuel <= 0) {
log.error("{}:油量为零停止移动", this.scenarioTask.getResourceId());
return;
log.info("{}-当前比值{},阈值{}", scenarioTask.getResourceId(),
currentFuel * 100 / totalFuel,
fuelThreshold);
if (currentFuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
log.info("{}-油料不足,需要补充,新建需求和任务", scenarioTask.getResourceId());
requestFlag.set(true);
//需要产生需求
produceFuelRequest();
//产生任务
produceTask(currentFuel);
return;
}
if (currentFuel * 100 / totalFuel < fuelThreshold) {
log.error("{}:油量不足停止移动,等待补给", this.scenarioTask.getResourceId());
return;
}
}
log.info("{}-当前比值{},阈值{}", scenarioTask.getResourceId(),
currentFuel * 100 / totalFuel,
fuelThreshold);
if (currentFuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
log.info("{}-油料不足,需要补充,新建需求和任务", scenarioTask.getResourceId());
requestFlag.set(true);
//需要产生需求
produceFuelRequest();
//产生任务
produceTask(currentFuel);
return;
}
if (currentFuel * 100 / totalFuel < fuelThreshold) {
log.error("{}:油量不足停止移动,等待补给", this.scenarioTask.getResourceId());
return;
}
if (distanceInfoMap.isEmpty()) {
return;
}
@ -288,7 +290,7 @@ public abstract class AbtParentTask implements TaskAction {
if (duringAction != null) {
duringAction.doSomeThing();
}
log.info("移动中.....");
log.info("移动中..... 放大系数{}",RoomManager.getMag(roomId));
//跑动距离
double distance = duringTime.getAndAdd(RoomManager.getMag(roomId)) * speed;
@ -366,7 +368,7 @@ public abstract class AbtParentTask implements TaskAction {
if (finishedAction != null) {
finishedAction.doSomeThing();
}
taskFinishedStatus.set(true);
//完成路径
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId,
@ -459,35 +461,42 @@ public abstract class AbtParentTask implements TaskAction {
private void produceTask(double fuel) {
try {
Map<Integer, Teaminfo> teamInfoMap = SpringUtil.getBean(TeaminfoServiceImpl.class)
.teamInfoMap();
log.info("{}-产生自动保障任务", this.scenarioTask.getResourceId());
List<ScenarioResource> resourceList = SpringUtil.getBean(BattleSupplierServiceImpl.class)
.selectSupplierResource(scenarioTask.getResourceId());
log.info("{}-可选保障分队长度{}", scenarioTask.getResourceId(), resourceList.size());
if (!resourceList.isEmpty()) {
ScenarioResource supplierResource = null;
// 找出油料保障分队
for (ScenarioResource resource : resourceList) {
ScenarioTask task = new ScenarioTask();
task.setId(IdUtils.simpleUUID());
task.setScenarioId(scenarioTask.getScenarioId());
task.setResourceId(resourceList.get(0).getId());
task.setTaskType("6");
task.setInsureResourceId(scenarioTask.getResourceId());
task.setSupplierNum(statisticBean.getFuel().getTotal() - fuel);
task.setToLat(this.coordinateReference.get().getLat() + "");
task.setToLng(this.coordinateReference.get().getLng() + "");
task.setStartTime(LocalDateTime.now());
task.setFromLat(resourceList.get(0).getLat());
task.setFromLng(resourceList.get(0).getLng());
task.setFromSource("general");
log.info("{}-保障分队id::{},from::{},to::{}", this.scenarioTask.getInsureResourceId(),
task.getResourceId(), task.getFromLat() + "," + task.getFromLng(),
task.getToLat() + "," + task.getToLng());
Teaminfo teaminfo = teamInfoMap.get(resource.getResourceId());
if ("SUPPLIER_FUEL".equals(teaminfo.getRoleCode())) {
supplierResource = resource;
break;
}
}
if (supplierResource == null) {
log.error("找不到对应的油料保障分队");
return;
}
//找出油料仓库
List<ScenarioResource> resources = SpringUtil.getBean(ScenarioResourceServiceImpl.class)
.selectResourceByRoleCode(scenarioTask.getScenarioId(), "WARE_FUEL_HOUSE");
if (resources.isEmpty()) {
log.error("找不到油料仓库");
return;
}
produceMoveTask(supplierResource, resources.get(0), this.coordinateReference.get(), fuel);
SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
//增加到房间任务
SupplierTask supplierTask = new SupplierTask(task, roomId);
//立即执行
RoomManager.addAction(roomId, 0, supplierTask);
} else {
log.error("{}-没有保障分队可以选择", scenarioTask.getResourceId());
}
@ -496,6 +505,60 @@ public abstract class AbtParentTask implements TaskAction {
}
}
private void produceMoveTask(ScenarioResource supplierResource, ScenarioResource fuelResource,
Coordinate coordinate, double minusFuel) {
ScenarioTask task = new ScenarioTask();
task.setId(IdUtils.simpleUUID());
task.setScenarioId(scenarioTask.getScenarioId());
task.setResourceId(supplierResource.getId());
task.setTaskType("1");
task.setFromLat(supplierResource.getLat());
task.setFromLng(supplierResource.getLng());
task.setToLat(fuelResource.getLat());
task.setToLng(fuelResource.getLng());
task.setStartTime(LocalDateTime.now());
task.setFromSource("general");
log.info("承担保障任务的resourceId::{}", supplierResource.getId());
SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
MoveTask moveTask = new MoveTask(task, this.roomId, new TaskAction() {
@Override
public void doSomeThing() {
//创建一个保障任务
ScenarioTask task = new ScenarioTask();
task.setId(IdUtils.simpleUUID());
task.setScenarioId(scenarioTask.getScenarioId());
task.setResourceId(supplierResource.getId());
task.setTaskType("6");
task.setInsureResourceId(scenarioTask.getResourceId());
task.setSupplierNum(statisticBean.getFuel().getTotal() - minusFuel);
task.setToLat(coordinate.getLat() + "");
task.setToLng(coordinate.getLng() + "");
task.setStartTime(LocalDateTime.now());
task.setFromLat(fuelResource.getLat());
task.setFromLng(fuelResource.getLng());
task.setFromSource("general");
SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
SupplierTask supplierTask = new SupplierTask(task, roomId);
RoomManager.addAction(roomId, 0, supplierTask);
}
@Override
public String getId() {
return "";
}
@Override
public String getType() {
return "";
}
});
//立即执行
RoomManager.addAction(roomId, 0, moveTask);
}
}
interface BizTaskOnTiming {

View File

@ -44,11 +44,12 @@ public class MoveTask extends AbtParentTask implements TaskAction {
*/
private final int consumptionTaskInterval = 5;
private final TaskAction finishedAction;
public MoveTask(ScenarioTask scenarioTask, String roomId) {
public MoveTask(ScenarioTask scenarioTask, String roomId,TaskAction finishedAction) {
super(scenarioTask, roomId);
this.finishedAction = finishedAction;
}
@ -83,7 +84,7 @@ public class MoveTask extends AbtParentTask implements TaskAction {
public String getType() {
return "";
}
}, null); //更新路径
}, this.finishedAction); //更新路径
fuelConsumption();//油品消耗
}
@ -97,7 +98,7 @@ public class MoveTask extends AbtParentTask implements TaskAction {
schedule.scheduleWithFixedDelay(() -> {
if (getRoomStatus()) {
double currentFuel = getCurrentFuel();
if(currentFuel > 0) {
if(currentFuel > 0&&!"general".equals(scenarioTask.getFromSource())&&!taskFinishedStatus.get()) {
double currentUseUp = consumptionTaskInterval*RoomManager.getMag(roomId) * SPEED / 1000 * fuelConsumption;
log.info("{}-当前消耗油料::{},当前剩余油料::{}", scenarioTask.getResourceId(),

View File

@ -110,6 +110,7 @@ public class Room implements AutoCloseable {
startTask();
//初始化系统资源 物资人员等信息
initRoomParam();
pushRoomInfo();
}
/**
@ -117,6 +118,7 @@ public class Room implements AutoCloseable {
*/
public void stop() {
status.set(false);
pushRoomInfo();
cancelTask();
futures.forEach((key, value) -> {
try {
@ -133,12 +135,15 @@ public class Room implements AutoCloseable {
* 暂停
*/
public void pause() {
status.set(false);
pushRoomInfo();
cancelTask();
}
public void resume() {
status.set(true);
pushRoomInfo();
startTask();
}
@ -272,4 +277,15 @@ public class Room implements AutoCloseable {
public Map<String, ScenarioResource> getScenarioResourceMap() {
return scenarioResourceMap;
}
private void pushRoomInfo(){
ResponseCmdInfo<Object> respObj = new ResponseCmdInfo<>();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("mag", this.getMag());
dataMap.put("status", this.getStatus());
respObj.setData(dataMap);
respObj.setRoom(this.getRoomId());
respObj.setScenarioId(this.getScenario().getId());
respObj.setCmdType("room_info");
Global.sendCmdInfoQueue.add(respObj);
}
}

View File

@ -5,11 +5,13 @@ import com.hivekion.baseData.controller.BaseController;
import com.hivekion.baseData.entity.Scenario;
import com.hivekion.baseData.service.ScenarioService;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.common.entity.ResponseData;
import com.hivekion.common.enums.ResultCodeEnum;
import com.hivekion.common.security.SecurityUtils;
import com.hivekion.common.uuid.IdUtils;
import com.hivekion.enums.ScenarioRoomStatusEnum;
import com.hivekion.room.RoomManager;
import com.hivekion.scenario.bean.ScenarioWsParam;
import com.hivekion.scenario.entity.RoomLog;
import com.hivekion.scenario.entity.ScenarioRoom;
@ -18,6 +20,7 @@ import com.hivekion.scenario.service.ScenarioRoomService;
import com.hivekion.scenario.service.ScenarioTaskService;
import io.swagger.annotations.ApiOperation;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
@ -143,17 +146,30 @@ public class ScenarioRoomController extends BaseController {
SecurityUtils.getCurrentLoginUser().getUsername()));
if (Global.roomParamMap.get(room.getScenarioId() + "_" + room.getId()) == null) {
Global.roomParamMap.put(room.getScenarioId() + "_" + room.getId(), new ScenarioWsParam(room.getMag()));
}else{
ScenarioWsParam magValue = Global.roomParamMap.get(room.getScenarioId() + "_" + room.getId());
Global.roomParamMap.put(room.getScenarioId() + "_" + room.getId(),
new ScenarioWsParam(room.getMag()));
} else {
ScenarioWsParam magValue = Global.roomParamMap.get(
room.getScenarioId() + "_" + room.getId());
magValue.setMag(room.getMag());
}
log.info("magValue:{}",Global.roomParamMap.get(room.getScenarioId() + "_" + room.getId()));
pushRoomInfo(room.getMag(), room);
log.info("magValue:{}", Global.roomParamMap.get(room.getScenarioId() + "_" + room.getId()));
}
return ResponseData.success(null);
}
private void pushRoomInfo(Integer mag, ScenarioRoom room) {
ResponseCmdInfo<Object> respObj = new ResponseCmdInfo<>();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("mag", mag);
dataMap.put("status", RoomManager.getRoom(room.getId()).getStatus());
respObj.setData(dataMap);
respObj.setRoom(room.getId());
respObj.setScenarioId(room.getScenarioId());
respObj.setCmdType("room_info");
Global.sendCmdInfoQueue.add(respObj);
}
@PostMapping("/stop")
public ResponseData<Void> stop(@RequestBody ScenarioRoom room) {
@ -173,13 +189,13 @@ public class ScenarioRoomController extends BaseController {
room.setScenario(scenario);
room.setScenarioName(scenario.getName());
room.setRoomLogs(roomLogService.roomLogListLimit10(id));
if (Global.roomParamMap.get(scenario.getId()+"_"+id) == null) {
if (Global.roomParamMap.get(scenario.getId() + "_" + id) == null) {
ScenarioWsParam scenarioWsParam = new ScenarioWsParam(1);
scenarioWsParam.setMag(1);
Global.roomParamMap.put(scenario.getId()+"_"+id, scenarioWsParam);
Global.roomParamMap.put(scenario.getId() + "_" + id, scenarioWsParam);
room.setMag(1);
} else {
room.setMag(Global.roomParamMap.get(scenario.getId()+"_"+id).getMag());
room.setMag(Global.roomParamMap.get(scenario.getId() + "_" + id).getMag());
}
}
return ResponseData.success(room);

View File

@ -2,6 +2,8 @@ package com.hivekion.scenario.mapper;
import com.hivekion.scenario.entity.ScenarioResource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
/**
* <p>
@ -12,5 +14,5 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
* @since 2025-09-09
*/
public interface ScenarioResourceMapper extends BaseMapper<ScenarioResource> {
List<ScenarioResource> selectResourceByRoleCode(@Param("scenarioId") Integer scenarioId,@Param("roleCode") String roleCode );
}

View File

@ -4,6 +4,7 @@ import com.hivekion.scenario.entity.ScenarioResource;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
/**
* <p>
@ -17,4 +18,5 @@ public interface ScenarioResourceService extends IService<ScenarioResource> {
List<ScenarioResource> getResourceList(ScenarioResource resource);
Map<String,ScenarioResource> resourceMap();
List<ScenarioResource> getResourceListByScenarioId(Integer scenarioId);
List<ScenarioResource> selectResourceByRoleCode( Integer scenarioId, String roleCode );
}

View File

@ -113,4 +113,9 @@ public class ScenarioResourceServiceImpl extends
queryWrapper.eq("scenario_id", scenarioId);
return this.list(queryWrapper);
}
@Override
public List<ScenarioResource> selectResourceByRoleCode(Integer scenarioId, String roleCode) {
return this.baseMapper.selectResourceByRoleCode(scenarioId, roleCode);
}
}

View File

@ -157,8 +157,8 @@ public class ScenarioTaskServiceImpl extends
switch (task.getTaskType()) {
//移动任务
case "1":
log.info("move task::{}",diff);
MoveTask moveRootTask = new MoveTask(task, roomId);
MoveTask moveRootTask = new MoveTask(task, roomId,null);
RoomManager.addAction(roomId, diff, moveRootTask);
respObj.setCmdType("moveTask");
respObj.setData(JSON.toJSONString(moveRootTask));

View File

@ -59,6 +59,9 @@ public class HandleReceiveRunnable implements Runnable {
case "get_init_path":
handleGetInitPath(requestCmdInfo);
break;
case "get_room_info":
handleGetRootInfo(requestCmdInfo);
break;
default:
break;
@ -69,7 +72,21 @@ public class HandleReceiveRunnable implements Runnable {
log.error("error::", e);
}
}
private void handleGetRootInfo(RequestCmdInfo requestCmdInfo){
log.info("接收到获取到房间信息::{}", JSON.toJSONString(requestCmdInfo));
Room room = RoomManager.getRoom(requestCmdInfo.getRoom());
if (room != null) {
ResponseCmdInfo<Object> respObj = new ResponseCmdInfo<>();
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("mag", room.getMag());
dataMap.put("status", room.getStatus());
respObj.setData(dataMap);
respObj.setRoom(requestCmdInfo.getRoom());
respObj.setScenarioId(requestCmdInfo.getScenarioId());
respObj.setCmdType("room_info");
Global.sendCmdInfoQueue.add(respObj);
}
}
private void handleGetInitPath(RequestCmdInfo requestCmdInfo) {
log.info("接收到请求路线信息::{}", JSON.toJSONString(requestCmdInfo));
Room room = RoomManager.getRoom(requestCmdInfo.getRoom());

View File

@ -3,7 +3,7 @@ death.warn = 56
ammunition.warn = 3
food.warn = 3
water.warn = 3
fuel.warn = 80.6
fuel.warn = 99.99
medical.warn = 1
death.spreed = 3
injured.spreed = 3

View File

@ -3,7 +3,7 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hivekion.scenario.mapper.BattleSupplierMapper">
<select id="selectSupplierResource" resultType="com.hivekion.scenario.entity.ScenarioResource">
select id ,lng,lat,scenario_Id as scenarioId, resource_name as resourceName
select id ,lng,lat,scenario_Id as scenarioId, resource_name as resourceName,resource_id as resourceId
from tbl_scenario_resource
where id in
(select supplier_resource_id from tbl_battle_supplier where battle_resource_id = #{battleResourceId})

View File

@ -1,5 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hivekion.scenario.mapper.ScenarioResourceMapper">
<select id="selectResourceByRoleCode" resultType="com.hivekion.scenario.entity.ScenarioResource">
SELECT
id,SCENARIO_ID as scenarioId,RESOURCE_TYPE as resourceType,RESOURCE_ID as resourceId,lng,lat
FROM
TBL_SCENARIO_RESOURCE t where t.SCENARIO_ID = #{scenarioId}
and t.RESOURCE_ID in (select id from TBL_TEAMINFO t2 where t2.role_code=#{roleCode})
</select>
</mapper>