Compare commits
3 Commits
6f2cef9edc
...
f5ba96e225
Author | SHA1 | Date | |
---|---|---|---|
![]() |
f5ba96e225 | ||
![]() |
d7dc7d6110 | ||
![]() |
179c28020b |
|
@ -76,4 +76,11 @@ public class RoomManager {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
public static int getMag(String id){
|
||||
Room room = roomsMap.get(id);
|
||||
if (room != null) {
|
||||
return room.getMag();
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,10 +28,9 @@ 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.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.hivekion.statistic.bean.ScenarioInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
@ -62,7 +61,8 @@ public abstract class AbtParentTask implements TaskAction {
|
|||
protected final String roomId;
|
||||
//http请求
|
||||
protected WebClient webClient = WebClient.create();
|
||||
|
||||
protected final AtomicBoolean canMoved = new AtomicBoolean(true);
|
||||
protected final AtomicReference<Coordinate> coordinateReference = new AtomicReference<>();
|
||||
|
||||
/**
|
||||
* 任务相对与想定的开始时间
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.hivekion.room.bean;
|
||||
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.hivekion.Global;
|
||||
import com.hivekion.common.entity.ResponseCmdInfo;
|
||||
import com.hivekion.common.redis.RedisUtil;
|
||||
|
@ -13,6 +14,7 @@ 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.ScenarioInfo;
|
||||
import com.hivekion.statistic.bean.StatisticBean;
|
||||
import com.hivekion.statistic.service.impl.StatisticServiceImpl;
|
||||
import com.hivekion.supplier.entity.SupplierRequest;
|
||||
|
@ -71,7 +73,7 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
|
||||
@Override
|
||||
public void doSomeThing() {
|
||||
log.info("move task running:{}", scenarioTask.getResourceId());
|
||||
log.info("move task running:{},fuel::{}", scenarioTask.getResourceId(), getCurrentFuel());
|
||||
|
||||
initEnv(); //初始化环境
|
||||
initPath(); //初始化路径
|
||||
|
@ -81,10 +83,10 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
|
||||
//推送移动任务
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("duringTime",getDuringTime());
|
||||
map.put("id",scenarioTask.getResourceId());
|
||||
map.put("roomStatus",true);
|
||||
map.put("type",scenarioTask.getType());
|
||||
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,
|
||||
|
@ -100,7 +102,7 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
public String getType() {
|
||||
return "";
|
||||
}
|
||||
},null); //更新路径
|
||||
}, null); //更新路径
|
||||
fuelConsumption();//油品消耗
|
||||
}
|
||||
|
||||
|
@ -129,23 +131,30 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(
|
||||
1);
|
||||
schedule.scheduleWithFixedDelay(() -> {
|
||||
if (getRoomStatus()) {
|
||||
if (getRoomStatus() && this.canMoved.get()) {
|
||||
double currentUseUp = consumptionTaskInterval * SPEED / 1000 * fuelConsumption;
|
||||
|
||||
//更新redis中油品的消耗
|
||||
Object currentFuelObj = redis.hget(
|
||||
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"fuel");
|
||||
if (currentFuelObj != null) {
|
||||
double fuel = Double.parseDouble(currentFuelObj.toString());
|
||||
double fuel = getCurrentFuel();
|
||||
if (fuel == 0) {
|
||||
log.error("fuel is empty");
|
||||
return;
|
||||
}
|
||||
fuel = fuel - currentUseUp;
|
||||
Object statisticObj = redis.hget(
|
||||
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"scenarioInfo");
|
||||
if (statisticObj != null) {
|
||||
|
||||
setCurrentFuel(currentUseUp);
|
||||
}
|
||||
redis.hset(
|
||||
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"fuelConsume", fuel);
|
||||
|
||||
double totalFuel = statisticBean.getFuel().getTotal();
|
||||
if (fuel * 100 / totalFuel < fuelThreshold && !requestFlag.get()) {
|
||||
log.info("fuel is not enough,stop moving");
|
||||
this.canMoved.set(false);
|
||||
requestFlag.set(true);
|
||||
//需要产生需求
|
||||
produceFuelRequest();
|
||||
|
@ -153,11 +162,18 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
produceTask();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//插入消耗表
|
||||
insertConsumption(currentUseUp);
|
||||
pushStatus(scenarioTask.getResourceId());
|
||||
}
|
||||
if (!this.canMoved.get()) {
|
||||
//判断油料是否满足
|
||||
double totalFuel = statisticBean.getFuel().getTotal();
|
||||
if (Double.compare(this.getCurrentFuel(), totalFuel) >= 0) {
|
||||
this.canMoved.set(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}, 0, consumptionTaskInterval, TimeUnit.SECONDS);
|
||||
|
@ -193,8 +209,8 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
task.setResourceId(scenarioTask.getResourceId());
|
||||
task.setTaskType("6");
|
||||
task.setSupplierNum(statisticBean.getFuel().getTotal());
|
||||
task.setToLat(scenarioTask.getToLat());
|
||||
task.setToLng(scenarioTask.getToLng());
|
||||
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());
|
||||
|
@ -208,7 +224,7 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
}
|
||||
|
||||
private void insertConsumption(double num) {
|
||||
log.info("insertConsumption....{}",num);
|
||||
log.info("insertConsumption....{}", num);
|
||||
BattleConsume battleConsume = new BattleConsume();
|
||||
battleConsume.setId(IdUtils.simpleUUID());
|
||||
battleConsume.setResourceId(scenarioTask.getResourceId());
|
||||
|
@ -217,6 +233,25 @@ public class MoveTask extends AbtParentTask implements TaskAction {
|
|||
SpringUtil.getBean(BattleConsumeServiceImpl.class).save(battleConsume);
|
||||
}
|
||||
|
||||
private double getCurrentFuel() {
|
||||
Object statisticObj = redis.hget(
|
||||
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"scenarioInfo");
|
||||
if (statisticObj != null) {
|
||||
ScenarioInfo scenarioInfo = JSON.parseObject(statisticObj.toString(), ScenarioInfo.class);
|
||||
return scenarioInfo.getFuel().getCurrent();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
private void setCurrentFuel(double num) {
|
||||
Object statisticObj = redis.hget(
|
||||
scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),
|
||||
"scenarioInfo");
|
||||
if (statisticObj != null) {
|
||||
ScenarioInfo scenarioInfo = JSON.parseObject(statisticObj.toString(), ScenarioInfo.class);
|
||||
scenarioInfo.getFuel().setCurrent( scenarioInfo.getFuel().getCurrent()-num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import com.hivekion.common.utils;
|
|||
import com.hivekion.common.uuid.IdUtils;
|
||||
import com.hivekion.room.func.TaskAction;
|
||||
import com.hivekion.scenario.bean.ScenarioWsParam;
|
||||
import com.hivekion.scenario.service.impl.ScenarioResourceServiceImpl;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -220,6 +221,8 @@ public class Room implements AutoCloseable {
|
|||
if( scenarioService == null) {
|
||||
scenarioService = SpringUtil.getBean(com.hivekion.statistic.service.ScenarioService.class);
|
||||
}
|
||||
//设置资源列表
|
||||
scenario.setResourceList(SpringUtil.getBean(ScenarioResourceServiceImpl.class).getResourceListByScenarioId(scenario.getId()));
|
||||
for(ScenarioResource scenarioResource:this.scenario.getResourceList() ){
|
||||
ScenarioInfo scenarioInfo = scenarioService.listScenarioInfo(scenarioResource.getScenarioId(),roomId,scenarioResource.getId());
|
||||
if( redisUtil == null){
|
||||
|
|
|
@ -16,4 +16,5 @@ import java.util.Map;
|
|||
public interface ScenarioResourceService extends IService<ScenarioResource> {
|
||||
List<ScenarioResource> getResourceList(ScenarioResource resource);
|
||||
Map<String,ScenarioResource> resourceMap();
|
||||
List<ScenarioResource> getResourceListByScenarioId(Integer scenarioId);
|
||||
}
|
||||
|
|
|
@ -106,4 +106,11 @@ public class ScenarioResourceServiceImpl extends
|
|||
|
||||
return this.list().stream().collect(Collectors.toMap(ScenarioResource::getId, r -> r));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ScenarioResource> getResourceListByScenarioId(Integer scenarioId) {
|
||||
QueryWrapper<ScenarioResource> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("scenario_id", scenarioId);
|
||||
return this.list(queryWrapper);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user