Merge branch 'main' of http://git.hivekion.com:3000/liyudong/simulation-backend
This commit is contained in:
commit
b12d5bb3bd
|
@ -197,12 +197,12 @@ public abstract class AbtParentTask implements TaskAction {
|
|||
}
|
||||
}
|
||||
|
||||
protected void updatePath(double speed, TaskAction action) {
|
||||
protected void updatePath(double speed,TaskAction duringAction, TaskAction finishedAction) {
|
||||
|
||||
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
||||
1);
|
||||
schedule.scheduleWithFixedDelay(() -> {
|
||||
|
||||
log.info("task is running....");
|
||||
try {
|
||||
if (this.getRoomStatus()) {
|
||||
if (distanceInfoMap.isEmpty()) {
|
||||
|
@ -212,7 +212,9 @@ public abstract class AbtParentTask implements TaskAction {
|
|||
if (duringTime <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(duringAction!=null){
|
||||
duringAction.doSomeThing();
|
||||
}
|
||||
//跑动距离
|
||||
double distance = duringTime * speed;
|
||||
|
||||
|
@ -278,8 +280,8 @@ public abstract class AbtParentTask implements TaskAction {
|
|||
scenarioTask.getScenarioId(), dataMap));
|
||||
|
||||
} else {
|
||||
if (action != null) {
|
||||
action.doSomeThing();
|
||||
if (finishedAction != null) {
|
||||
finishedAction.doSomeThing();
|
||||
}
|
||||
//完成路径
|
||||
Global.sendCmdInfoQueue.add(
|
||||
|
@ -301,6 +303,10 @@ public abstract class AbtParentTask implements TaskAction {
|
|||
//房间统一管理定时器;房间关闭后,定时器销毁
|
||||
addScheduledExecutorServiceRefenceToRoom(schedule);
|
||||
}
|
||||
//统一推送方法
|
||||
protected void pushStatus(String resourceId) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
interface BizTaskOnTiming {
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
package com.hivekion.room.bean;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.hivekion.Global;
|
||||
import com.hivekion.common.entity.ResponseCmdInfo;
|
||||
import com.hivekion.common.redis.RedisUtil;
|
||||
import com.hivekion.common.uuid.IdUtils;
|
||||
import com.hivekion.room.RoomManager;
|
||||
import com.hivekion.room.func.TaskAction;
|
||||
import com.hivekion.scenario.entity.BattleConsume;
|
||||
import com.hivekion.scenario.entity.ScenarioResource;
|
||||
import com.hivekion.scenario.entity.ScenarioTask;
|
||||
import com.hivekion.scenario.service.impl.BattleConsumeServiceImpl;
|
||||
import com.hivekion.scenario.service.impl.BattleSupplierServiceImpl;
|
||||
import com.hivekion.scenario.service.impl.ScenarioTaskServiceImpl;
|
||||
import com.hivekion.statistic.bean.StatisticBean;
|
||||
|
@ -14,7 +18,9 @@ import com.hivekion.statistic.service.impl.StatisticServiceImpl;
|
|||
import com.hivekion.supplier.entity.SupplierRequest;
|
||||
import com.hivekion.supplier.service.impl.SupplierRequestServiceImpl;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -32,7 +38,7 @@ import org.springframework.core.env.Environment;
|
|||
* @since 2025/7/22
|
||||
*/
|
||||
@Slf4j
|
||||
public class MoveRootTask extends AbtParentTask implements TaskAction {
|
||||
public class MoveTask extends AbtParentTask implements TaskAction {
|
||||
|
||||
/**
|
||||
* 速度 换算为100Km/小时
|
||||
|
@ -50,7 +56,7 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
/**
|
||||
* 消耗任务间隔
|
||||
*/
|
||||
private final int consumptionTaskInterval = 5;
|
||||
private final int consumptionTaskInterval = 10;
|
||||
/**
|
||||
* redis 服务类
|
||||
*/
|
||||
|
@ -58,18 +64,42 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
private StatisticBean statisticBean;
|
||||
|
||||
|
||||
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
|
||||
public MoveTask(ScenarioTask scenarioTask, String roomId) {
|
||||
super(scenarioTask, roomId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void doSomeThing() {
|
||||
log.info("move task running:{}",scenarioTask.getResourceId());
|
||||
log.info("move task running:{}", scenarioTask.getResourceId());
|
||||
|
||||
initEnv(); //初始化环境
|
||||
initPath(); //初始化路径
|
||||
updatePath(SPEED, null); //更新路径
|
||||
updatePath(SPEED, new TaskAction() {
|
||||
@Override
|
||||
public void doSomeThing() {
|
||||
//推送移动任务
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("duringTime",getDuringTime());
|
||||
map.put("id",scenarioTask.getResourceId());
|
||||
map.put("roomStatus",true);
|
||||
map.put("type",scenarioTask.getType());
|
||||
|
||||
Global.sendCmdInfoQueue.add(
|
||||
ResponseCmdInfo.create("moveTask", roomId,
|
||||
scenarioTask.getScenarioId(), map));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "";
|
||||
}
|
||||
},null); //更新路径
|
||||
fuelConsumption();//油品消耗
|
||||
}
|
||||
|
||||
|
@ -77,7 +107,7 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
* 初始化环境
|
||||
*/
|
||||
private void initEnv() {
|
||||
try{
|
||||
try {
|
||||
//获取油品消耗规则
|
||||
String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
|
||||
.getProperty("fuel_spreed");
|
||||
|
@ -86,15 +116,15 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
.getProperty("fuel.warn ", "0"));
|
||||
statisticBean = SpringUtil.getBean(StatisticServiceImpl.class)
|
||||
.statistic(scenarioTask.getResourceId());
|
||||
}catch (Exception e){
|
||||
log.error("init env exception",e);
|
||||
} catch (Exception e) {
|
||||
log.error("init env exception", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void fuelConsumption() {
|
||||
try{
|
||||
try {
|
||||
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
||||
1);
|
||||
schedule.scheduleWithFixedDelay(() -> {
|
||||
|
@ -124,12 +154,14 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
}
|
||||
}
|
||||
//插入消耗表
|
||||
insertConsumption(currentUseUp);
|
||||
pushStatus(scenarioTask.getResourceId());
|
||||
}
|
||||
|
||||
|
||||
}, 0, consumptionTaskInterval, TimeUnit.SECONDS);
|
||||
}catch (Exception e){
|
||||
log.error("fuel consumption exception",e);
|
||||
} catch (Exception e) {
|
||||
log.error("fuel consumption exception", e);
|
||||
}
|
||||
|
||||
|
||||
|
@ -166,12 +198,22 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
|
|||
task.setFromLng(resourceList.get(0).getLng());
|
||||
task.setFromSource("general");
|
||||
SpringUtil.getBean(ScenarioTaskServiceImpl.class).save(task);
|
||||
//增加到房间任务
|
||||
//增加到房间任务
|
||||
SupplierTask supplierTask = new SupplierTask(task, roomId);
|
||||
//立即执行
|
||||
RoomManager.addAction(roomId, 0, supplierTask);
|
||||
}
|
||||
}
|
||||
|
||||
private void insertConsumption(double num) {
|
||||
BattleConsume battleConsume = new BattleConsume();
|
||||
battleConsume.setId(IdUtils.simpleUUID());
|
||||
battleConsume.setResourceId(scenarioTask.getResourceId());
|
||||
battleConsume.setFuel(num);
|
||||
battleConsume.setConsumeDate(LocalDateTime.now());
|
||||
SpringUtil.getBean(BattleConsumeServiceImpl.class).save(battleConsume);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -92,6 +92,8 @@ public class Room implements AutoCloseable {
|
|||
status.set(true);
|
||||
totalTime.set(time);
|
||||
startTask();
|
||||
//初始化系统资源 物资人员等信息
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -100,6 +102,15 @@ public class Room implements AutoCloseable {
|
|||
public void stop() {
|
||||
status.set(false);
|
||||
cancelTask();
|
||||
futures.forEach((key, value) -> {
|
||||
try {
|
||||
if (!value.isShutdown()) {
|
||||
value.shutdownNow();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("error::", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,7 +131,6 @@ public class Room implements AutoCloseable {
|
|||
}
|
||||
|
||||
|
||||
|
||||
// 启动定时任务
|
||||
private void startTask() {
|
||||
if (future == null || future.isCancelled()) {
|
||||
|
@ -195,4 +205,6 @@ public class Room implements AutoCloseable {
|
|||
public boolean isRunning() {
|
||||
return status.get();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,11 +1,15 @@
|
|||
package com.hivekion.room.bean;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.hivekion.common.entity.ResponseCmdInfo;
|
||||
import com.hivekion.common.redis.RedisUtil;
|
||||
import com.hivekion.room.func.TaskAction;
|
||||
import com.hivekion.scenario.entity.ScenarioTask;
|
||||
import com.hivekion.statistic.bean.StatisticBean;
|
||||
import com.hivekion.statistic.service.StatisticService;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* [类的简要说明]
|
||||
|
@ -16,6 +20,7 @@ import com.hivekion.statistic.service.StatisticService;
|
|||
* @author LiDongYU
|
||||
* @since 2025/7/22
|
||||
*/
|
||||
@Slf4j
|
||||
public class SupplierTask extends AbtParentTask implements TaskAction {
|
||||
|
||||
public SupplierTask(ScenarioTask scenarioTask, String roomId) {
|
||||
|
@ -28,10 +33,11 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
|
|||
StatisticBean statistic = SpringUtil.getBean(StatisticService.class)
|
||||
.statistic(scenarioTask.getResourceId());
|
||||
initPath(); //初始化路径
|
||||
updatePath(30, new TaskAction() {
|
||||
updatePath(30, null, new TaskAction() {
|
||||
|
||||
@Override
|
||||
public void doSomeThing() {
|
||||
log.info("supplier team is arrived ");
|
||||
//达到终点点后,给目标补充物资
|
||||
switch (scenarioTask.getTaskType()) {
|
||||
case "4":
|
||||
|
@ -51,7 +57,8 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
|
|||
break;
|
||||
}
|
||||
//推送最新状态信息
|
||||
|
||||
pushStatus(scenarioTask.getResourceId());
|
||||
pushStatus(scenarioTask.getResourceId());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -68,44 +75,111 @@ public class SupplierTask extends AbtParentTask implements TaskAction {
|
|||
|
||||
private void supplierMedical(StatisticBean statistic) {
|
||||
|
||||
//增加被保障分队的量
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"medical", statistic.getMedical().getTotal()+"");
|
||||
"medical", statistic.getMedical().getTotal() + "");
|
||||
|
||||
//获取保障任务的药品信息
|
||||
Object supplierObj = SpringUtil.getBean(RedisUtil.class)
|
||||
.hget(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"medical");
|
||||
//减少保障分队的量
|
||||
if (supplierObj != null) {
|
||||
double supplierMedical = Double.parseDouble(supplierObj.toString());
|
||||
|
||||
double remain = supplierMedical - statistic.getMedical().getTotal();
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"medical", remain + "");
|
||||
}
|
||||
}
|
||||
|
||||
private void supplierFuel(StatisticBean statistic) {
|
||||
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"fuel", statistic.getFuel().getTotal()+"");
|
||||
"fuel", statistic.getFuel().getTotal() + "");
|
||||
|
||||
Object supplierObj = SpringUtil.getBean(RedisUtil.class)
|
||||
.hget(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"fuel");
|
||||
//减少保障分队的量
|
||||
if (supplierObj != null) {
|
||||
double supplierMedical = Double.parseDouble(supplierObj.toString());
|
||||
|
||||
double remain = supplierMedical - statistic.getFuel().getTotal();
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"fuel", remain + "");
|
||||
}
|
||||
}
|
||||
|
||||
private void supplierAmmunition(StatisticBean statistic) {
|
||||
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"ammunition", statistic.getAmmunition().getTotal()+"");
|
||||
"ammunition", statistic.getAmmunition().getTotal() + "");
|
||||
Object supplierObj = SpringUtil.getBean(RedisUtil.class)
|
||||
.hget(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"ammunition");
|
||||
//减少保障分队的量
|
||||
if (supplierObj != null) {
|
||||
double supplierMedical = Double.parseDouble(supplierObj.toString());
|
||||
|
||||
double remain = supplierMedical - statistic.getAmmunition().getTotal();
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"ammunition", remain + "");
|
||||
}
|
||||
}
|
||||
|
||||
private void supplierWater(StatisticBean statistic) {
|
||||
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"water", statistic.getWater().getTotal()+"");
|
||||
"water", statistic.getWater().getTotal() + "");
|
||||
Object supplierObj = SpringUtil.getBean(RedisUtil.class)
|
||||
.hget(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"water");
|
||||
//减少保障分队的量
|
||||
if (supplierObj != null) {
|
||||
double supplierMedical = Double.parseDouble(supplierObj.toString());
|
||||
|
||||
double remain = supplierMedical - statistic.getWater().getTotal();
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"water", remain + "");
|
||||
}
|
||||
}
|
||||
|
||||
private void supplierFood(StatisticBean statistic) {
|
||||
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"food", statistic.getFood().getTotal()+"");
|
||||
"food", statistic.getFood().getTotal() + "");
|
||||
Object supplierObj = SpringUtil.getBean(RedisUtil.class)
|
||||
.hget(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"food");
|
||||
//减少保障分队的量
|
||||
if (supplierObj != null) {
|
||||
double supplierMedical = Double.parseDouble(supplierObj.toString());
|
||||
|
||||
double remain = supplierMedical - statistic.getFood().getTotal();
|
||||
SpringUtil.getBean(RedisUtil.class)
|
||||
.hset(scenarioTask.getScenarioId() + "-" + roomId + "-"
|
||||
+ scenarioTask.getSupplierResourceId(),
|
||||
"food", remain + "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ 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.MoveRootTask;
|
||||
import com.hivekion.room.bean.MoveTask;
|
||||
import com.hivekion.room.bean.SupplierTask;
|
||||
import com.hivekion.room.func.TaskAction;
|
||||
import com.hivekion.scenario.entity.ScenarioTask;
|
||||
|
@ -155,7 +155,7 @@ public class ScenarioTaskServiceImpl extends
|
|||
//移动任务
|
||||
case "1":
|
||||
log.info("move task::{}",diff);
|
||||
MoveRootTask moveRootTask = new MoveRootTask(task, roomId);
|
||||
MoveTask moveRootTask = new MoveTask(task, roomId);
|
||||
RoomManager.addAction(roomId, diff, moveRootTask);
|
||||
respObj.setCmdType("moveTask");
|
||||
respObj.setData(JSON.toJSONString(moveRootTask));
|
||||
|
|
Loading…
Reference in New Issue
Block a user