diff --git a/src/main/java/com/hivekion/room/bean/AbtParentTask.java b/src/main/java/com/hivekion/room/bean/AbtParentTask.java index 5e17ca8..e54634a 100644 --- a/src/main/java/com/hivekion/room/bean/AbtParentTask.java +++ b/src/main/java/com/hivekion/room/bean/AbtParentTask.java @@ -28,9 +28,7 @@ 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.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import com.hivekion.statistic.bean.ScenarioInfo; @@ -65,12 +63,7 @@ public abstract class AbtParentTask implements TaskAction { //http请求 protected WebClient webClient = WebClient.create(); - private RedisUtil redisUtil; - private com.hivekion.statistic.service.ScenarioService scenarioService; - //是否可以移动 - protected final AtomicBoolean canMoved = new AtomicBoolean(true); - protected final AtomicReference coordinateReference = new AtomicReference<>(new Coordinate()); /** * 任务相对与想定的开始时间 */ @@ -119,9 +112,7 @@ public abstract class AbtParentTask implements TaskAction { public long getDuringTime() { return RoomManager.getRoomDuringTime(this.roomId); } - protected int getMag(){ - return RoomManager.getMag(this.roomId); - } + //获取房间状态 public boolean getRoomStatus() { return RoomManager.isRunning(roomId); @@ -211,14 +202,13 @@ public abstract class AbtParentTask implements TaskAction { } protected void updatePath(double speed,TaskAction duringAction, TaskAction finishedAction) { - AtomicLong runSeconds = new AtomicLong(0); + ScheduledExecutorService schedule = Executors.newScheduledThreadPool( 1); schedule.scheduleWithFixedDelay(() -> { log.info("task is running...."); try { - - if (this.getRoomStatus()&&canMoved.get()) { + if (this.getRoomStatus()) { if (distanceInfoMap.isEmpty()) { return; } @@ -229,9 +219,8 @@ public abstract class AbtParentTask implements TaskAction { if(duringAction!=null){ duringAction.doSomeThing(); } - runSeconds.getAndAdd(getMag()); //跑动距离 - double distance = runSeconds.get() * speed; + double distance = duringTime * speed; //获取大与此距离的第一个路线点key Entry endPoint = distanceInfoMap.ceilingEntry(distance); @@ -271,7 +260,6 @@ public abstract class AbtParentTask implements TaskAction { Coordinate coordinate = new Coordinate(); coordinate.setLat(insertPoints[0]); coordinate.setLng(insertPoints[1]); - coordinateReference.set(coordinate); distanceInfoMap.put(distance, coordinate); startPoint.set(distance); SpringUtil.getBean(RedisUtil.class).hset( @@ -282,7 +270,6 @@ public abstract class AbtParentTask implements TaskAction { ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId, scenarioTask.getScenarioId(), dataMap)); - } else if (Double.compare(distance, endPoint.getKey()) == 0) { NavigableMap subPathMap = distanceInfoMap.subMap(startPoint.get(), true, endPoint.getKey(), true); @@ -290,7 +277,7 @@ public abstract class AbtParentTask implements TaskAction { Coordinate coordinate = subPathMap.get(key); dataList.add(new double[]{coordinate.getLng(), coordinate.getLat()}); } - coordinateReference.set(endPoint.getValue()); + startPoint.set(endPoint.getKey()); Global.sendCmdInfoQueue.add( ResponseCmdInfo.create(WsCmdTypeEnum.PATH_UPDATE.getCode(), roomId, @@ -300,7 +287,6 @@ public abstract class AbtParentTask implements TaskAction { if (finishedAction != null) { finishedAction.doSomeThing(); } - coordinateReference.set(endPoint.getValue()); //完成路径 Global.sendCmdInfoQueue.add( ResponseCmdInfo.create(WsCmdTypeEnum.PATH_FINISHED.getCode(), roomId, @@ -311,7 +297,6 @@ public abstract class AbtParentTask implements TaskAction { } - } catch (Exception e) { log.error("error::", e); } @@ -322,19 +307,19 @@ public abstract class AbtParentTask implements TaskAction { //房间统一管理定时器;房间关闭后,定时器销毁 addScheduledExecutorServiceRefenceToRoom(schedule); } + private RedisUtil redisUtil; //统一推送方法 protected void pushStatus(String resourceId) { - if( scenarioService == null) { - scenarioService = SpringUtil.getBean(com.hivekion.statistic.service.ScenarioService.class); - } - ScenarioInfo scenarioInfo = scenarioService.listScenarioInfo(this.scenarioTask.getScenarioId(),roomId,resourceId); - - if( redisUtil == null){ + if( redisUtil == null) { redisUtil = SpringUtil.getBean(RedisUtil.class); - redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"scenarioInfo",JSON.toJSONString(scenarioInfo)); - }else{ - redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"scenarioInfo",JSON.toJSONString(scenarioInfo)); } + String jsonStr= (String) redisUtil.hget(this.scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"scenarioInfo"); + ResponseCmdInfo respObj = new ResponseCmdInfo<>(); + respObj.setData(jsonStr); + respObj.setRoom(roomId); + respObj.setScenarioId(scenarioTask.getScenarioId()); + respObj.setCmdType("scenarioInfo"); + Global.sendCmdInfoQueue.add(respObj); } } diff --git a/src/main/java/com/hivekion/room/bean/BattleRootTask.java b/src/main/java/com/hivekion/room/bean/BattleRootTask.java index ded8903..2492101 100644 --- a/src/main/java/com/hivekion/room/bean/BattleRootTask.java +++ b/src/main/java/com/hivekion/room/bean/BattleRootTask.java @@ -82,14 +82,8 @@ public class BattleRootTask extends AbtParentTask { this.initBean(); if(this.getRoomStatus()) { long initDuringTime = this.getDuringTime(); - StatisticBean statisticBean = null; - try { - statisticBean = statisticService.statistic(scenarioTask.getResourceId()); - }catch (Exception ex){ - ex.printStackTrace(); - } + redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"duringTime",String.valueOf(initDuringTime)); String jsonStr = (String)redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"scenarioInfo"); - ScenarioInfo scenarioInfo =JSONObject.parseObject(jsonStr,ScenarioInfo.class); HashMap battleParams = new HashMap<>(); //初始化本次战斗任务各种资源数 @@ -105,9 +99,9 @@ public class BattleRootTask extends AbtParentTask { battleParams.put("teamLng",scenarioInfo.getTeam().getLng().toString()); battleParams.put("duringTime",Long.valueOf(initDuringTime).toString()); log.info("===============================初始化本次战斗任务各种资源数===================================="); - double suppleAmount =statisticBean.getAmmunition().getTotal(); - int suppleDeath =statisticBean.getPerson().getDeath(); - int suppleInjured =statisticBean.getPerson().getInjured(); + double suppleAmount =scenarioInfo.getAmmunition().getTotal(); + int suppleDeath =scenarioInfo.getPerson().getDeath(); + int suppleInjured =scenarioInfo.getPerson().getInjured(); final Map suppleFlagMap = new HashMap<>(); suppleFlagMap.put("ammunition",false); suppleFlagMap.put("death",false); @@ -129,9 +123,8 @@ public class BattleRootTask extends AbtParentTask { String teamLng = null; JSONObject jsonObject = new JSONObject(); try { - HashMap battleParams = (HashMap) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId()); long duringTime = getDuringTime(); - long lastDuringTime = Long.valueOf(battleParams.get("duringTime").toString()); + long lastDuringTime = Long.valueOf(redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"duringTime").toString()); long intervalDuringTime = duringTime - lastDuringTime; double ammunition = Double.valueOf(battleParams.get("ammunition").toString()); double food = Double.valueOf(battleParams.get("food").toString()); @@ -165,10 +158,10 @@ public class BattleRootTask extends AbtParentTask { scenarioInfo.getFuel().setCurrent(Double.valueOf(fuel - fuelConsume)); scenarioInfo.getMedical().setCurrent(Double.valueOf(medical - medicalConsume)); scenarioInfo.getWater().setCurrent(Double.valueOf(water - waterConsume)); - battleParams.put("death",Long.valueOf(death+deathConsume).toString()); - battleParams.put("injured",Long.valueOf(injured +injuredConsume).toString()); - battleParams.put("duringTime",Long.valueOf(duringTime).toString()); - redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), battleParams); + scenarioInfo.getPerson().setDeath(Long.valueOf(death+deathConsume).intValue()); + scenarioInfo.getPerson().setInjured(Long.valueOf(injured +injuredConsume).intValue()); + redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),"duringTime",String.valueOf(duringTime)); + redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "scenarioInfo",JSONObject.toJSONString(scenarioInfo)); }catch (Exception ex){ log.error("==============================设置消耗信息失败=============================================",ex.getMessage()); } diff --git a/src/main/java/com/hivekion/room/bean/Room.java b/src/main/java/com/hivekion/room/bean/Room.java index 87b7b31..c76d5c4 100644 --- a/src/main/java/com/hivekion/room/bean/Room.java +++ b/src/main/java/com/hivekion/room/bean/Room.java @@ -1,8 +1,11 @@ package com.hivekion.room.bean; +import cn.hutool.extra.spring.SpringUtil; +import com.alibaba.fastjson2.JSON; import com.hivekion.Global; import com.hivekion.baseData.entity.Scenario; import com.hivekion.common.entity.ResponseCmdInfo; +import com.hivekion.common.redis.RedisUtil; import com.hivekion.common.utils; import com.hivekion.common.uuid.IdUtils; import com.hivekion.room.func.TaskAction; @@ -22,6 +25,9 @@ import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; + +import com.hivekion.scenario.entity.ScenarioResource; +import com.hivekion.statistic.bean.ScenarioInfo; import lombok.Data; import lombok.extern.slf4j.Slf4j; @@ -52,6 +58,10 @@ public class Room implements AutoCloseable { * 想定信息 */ private Scenario scenario; + + private RedisUtil redisUtil; + + private com.hivekion.statistic.service.ScenarioService scenarioService; /** * 任务容器 */ @@ -92,9 +102,8 @@ public class Room implements AutoCloseable { status.set(true); totalTime.set(time); startTask(); - //初始化系统资源 初始化resource下物资信息 - //1. - + //初始化系统资源 物资人员等信息 + initRoomParam(); } /** @@ -207,8 +216,21 @@ public class Room implements AutoCloseable { return status.get(); } - public int getMag() { - return this.mag; + public void initRoomParam(){ + if( scenarioService == null) { + scenarioService = SpringUtil.getBean(com.hivekion.statistic.service.ScenarioService.class); + } + for(ScenarioResource scenarioResource:this.scenario.getResourceList() ){ + ScenarioInfo scenarioInfo = scenarioService.listScenarioInfo(scenarioResource.getScenarioId(),roomId,scenarioResource.getId()); + if( redisUtil == null){ + redisUtil = SpringUtil.getBean(RedisUtil.class); + redisUtil.hset(scenarioResource.getScenarioId() + "-" + roomId + "-" + scenarioResource.getId(),"scenarioInfo", JSON.toJSONString(scenarioInfo)); + }else{ + redisUtil.hset(scenarioResource.getScenarioId() + "-" + roomId + "-" + scenarioResource.getId(),"scenarioInfo",JSON.toJSONString(scenarioInfo)); + } + } + } + }