任务相关

This commit is contained in:
李玉东 2025-09-19 11:17:49 +08:00
parent f4e4aaa6fc
commit f38efce42f
3 changed files with 118 additions and 15 deletions

View File

@ -3,8 +3,13 @@ package com.hivekion.room.bean;
import com.hivekion.room.RoomManager;
import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import java.util.concurrent.*;
import com.hivekion.statistic.bean.StatisticBean;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.web.reactive.function.client.WebClient;
@ -68,14 +73,15 @@ public abstract class AbtParentTask implements TaskAction {
public long getDuringTime() {
return RoomManager.getRoomDuringTime(this.roomId);
}
//获取房间状态
public boolean getRoomStatus() {
return RoomManager.isRunning(roomId);
}
public void createBattleTaskOnTimingHandle(BizTaskOnTiming bizTaskOnTiming){
public void createBattleTaskOnTimingHandle(BizTaskOnTiming bizTaskOnTiming) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
1);
schedule.scheduleWithFixedDelay(() -> {
bizTaskOnTiming.execTask();
}, 0, 10, TimeUnit.SECONDS);
@ -83,9 +89,13 @@ public abstract class AbtParentTask implements TaskAction {
addScheduledExecutorServiceRefenceToRoom(schedule);
}
protected void pushStatistic() {
StatisticBean statisticBean = new StatisticBean();
//获取分队信息
}
}
interface BizTaskOnTiming{
interface BizTaskOnTiming {
public void execTask();
}

View File

@ -5,11 +5,17 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hivekion.Global;
import com.hivekion.baseData.entity.Scenario;
import com.hivekion.baseData.service.ScenarioService;
import com.hivekion.common.MultiPointGeoPosition;
import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.common.redis.RedisUtil;
import com.hivekion.enums.WsCmdTypeEnum;
import com.hivekion.room.func.TaskAction;
import com.hivekion.scenario.entity.ScenarioTask;
import com.hivekion.statistic.bean.StatisticBean;
import com.hivekion.statistic.service.impl.StatisticServiceImpl;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -49,6 +55,23 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
* 开始点坐标
*/
private final AtomicReference<Double> startPoint = new AtomicReference<>();
/**
* 任务相对与想定的开始时间
*/
private long taskRelativeStartTime = 0;
/**
* 油料消耗速率
*/
private double fuelConsumption = 0;
/**
* 消耗任务间隔
*/
private final int consumptionTaskInterval = 5;
/**
* redis 服务类
*/
private final RedisUtil redis = SpringUtil.getBean(RedisUtil.class);
private StatisticBean statisticBean;
public MoveRootTask(ScenarioTask scenarioTask, String roomId) {
@ -59,8 +82,28 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
@Override
public void doSomeThing() {
log.info("move task running");
initEnv(); //初始化环境
initPath(); //初始化路径
updatePath(); //更新路径
fuelConsumption();//油品消耗
}
/**
* 初始化环境
*/
private void initEnv() {
//获取任务相对于想定的开始时间
Scenario scenario = SpringUtil.getBean(ScenarioService.class)
.getScenarioById(scenarioTask.getScenarioId());
taskRelativeStartTime = Math.abs(
Duration.between(scenario.getStartTime(), scenarioTask.getStartTime()).getSeconds());
//获取油品消耗规则
String fuelConsumptionStr = SpringUtil.getBean(Environment.class)
.getProperty("vehicle.fuel.consumption.per.km");
fuelConsumption = Double.parseDouble(fuelConsumptionStr == null ? "0" : fuelConsumptionStr);
statisticBean = SpringUtil.getBean(StatisticServiceImpl.class).statistic(scenarioTask.getResourceId());
}
/**
@ -98,7 +141,11 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_INIT.getCode(), roomId,
scenarioTask.getScenarioId(), dataMap));
log.info("init::{}", JSON.toJSONString(coordinates));
redis.hset(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"init_path", JSON.toJSONString(coordinates));
//计算各个点的累计距离和坐标的对应关系
double beforeLng = Double.parseDouble(scenarioTask.getFromLng());
double beforeLat = Double.parseDouble(scenarioTask.getFromLat());
@ -142,7 +189,8 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
try {
if (this.getRoomStatus()) {
long duringTime = getDuringTime();
long duringTime = getDuringTime() - taskRelativeStartTime;
log.info("duringTime::{}", duringTime);
//跑动距离
double distance = duringTime * SPEED;
@ -157,7 +205,7 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
if (Double.compare(distance, endPoint.getKey()) < 0) {
//获取小于最大值的第一个key
Double lowerKey = distanceInfoMap.lowerKey(endPoint.getKey());
// log.info("distance::{},lowerKey::{},endPoint{}",distance,lowerKey,endPoint.getKey());
// log.info("distance::{},lowerKey::{},endPoint{}",distance,lowerKey,endPoint.getKey());
//获取从上一个开始节点到lowKey的数据
NavigableMap<Double, Coordinate> subPathMap = distanceInfoMap.subMap(startPoint.get(),
true, lowerKey, true);
@ -165,23 +213,23 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
Coordinate coordinate = subPathMap.get(key);
dataList.add(new double[]{coordinate.getLng(), coordinate.getLat()});
}
double diff =distance - lowerKey ;
double diff = distance - lowerKey;
//插入值
double[] insertPoints = MultiPointGeoPosition.pointAlong(
distanceInfoMap.get(lowerKey).getLat(), distanceInfoMap.get(lowerKey).getLng(),
endPoint.getValue().getLat(), endPoint.getValue().getLng(), diff);
dataList.add(new double[]{insertPoints[1], insertPoints[0]});
Coordinate coordinate = new Coordinate();
Coordinate coordinate = new Coordinate();
coordinate.setLat(insertPoints[0]);
coordinate.setLng(insertPoints[1]);
distanceInfoMap.put(distance, coordinate);
startPoint.set(distance);
redis.hset(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"position", JSON.toJSONString(coordinate));
Global.sendCmdInfoQueue.add(
ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId,
@ -208,7 +256,6 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
}
}
} catch (Exception e) {
log.error("error::", e);
@ -221,7 +268,51 @@ public class MoveRootTask extends AbtParentTask implements TaskAction {
addScheduledExecutorServiceRefenceToRoom(schedule);
}
private void fuelConsumption() {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
1);
schedule.scheduleWithFixedDelay(() -> {
if (getRoomStatus()) {
double total = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
//更新redis中油品的消耗
Object currentFuelObj = redis.hget(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuel");
if (currentFuelObj != null) {
double fuel = Double.parseDouble(currentFuelObj.toString());
if (fuel > 0) {
fuel = fuel - total < 0 ? 0 : fuel - total;
//更新值
redis.hset(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"fuel", fuel);
//推送前端
}
}
}
}, 0, consumptionTaskInterval, TimeUnit.SECONDS);
}
private void statistic() {
Object positionObj = redis.hget(
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
"position");
if (positionObj != null) {
Coordinate coordinate = JSONObject.parseObject(positionObj.toString(), Coordinate.class);
statisticBean.getTeam().setLat(coordinate.lat+"");
statisticBean.getTeam().setLng(coordinate.lng+"");
}
//设置人员受伤信息
}
}
@Data

View File

@ -3,4 +3,6 @@ battle.ammunition.warn = 3
battle.food.warn = 3
battle.water.warn = 3
battle.fuel.warn = 2
battle.medical.warn = 1
battle.medical.warn = 1
## ??????????????
vehicle.fuel.consumption.per.km: 0.08