战斗任务消耗资源代码

This commit is contained in:
wangwenhua 2025-09-18 22:51:44 +08:00
parent 897f2ecc82
commit f69d51d7d9
5 changed files with 57 additions and 19 deletions

View File

@ -14,6 +14,7 @@ import com.hivekion.statistic.service.impl.StatisticServiceImpl;
import com.hivekion.supplier.entity.SupplierRequest;
import com.hivekion.supplier.service.ISupplierRequestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
@ -90,23 +91,30 @@ public class BattleRootTask extends AbtParentTask {
battleParams.put("medical",Double.valueOf(statisticBean.getMedical().getCurrent()).toString());
battleParams.put("water",Double.valueOf(statisticBean.getWater().getCurrent()).toString());
battleParams.put("person",Double.valueOf(statisticBean.getPerson().getCurrent()).toString());
battleParams.put("team",Double.valueOf(statisticBean.getTeam().getLat()).toString());
battleParams.put("teamLat",statisticBean.getTeam().getLat().toString());
battleParams.put("teamLng",statisticBean.getTeam().getLng().toString());
battleParams.put("duringTime",Long.valueOf(initDuringTime).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),battleParams);
log.info("===============================初始化本次战斗任务各种资源数====================================");
double suppleAmount =statisticBean.getAmmunition().getTotal();
double supplePerson =statisticBean.getPerson().getTotal();
final Map<String,Boolean> suppleFlagMap = new HashMap<>();
suppleFlagMap.put("ammunition",false);
suppleFlagMap.put("person",false);
//定时检查统计各种资源消耗量
this.createBattleTaskOnTimingHandle(new BizTaskOnTiming() {
@Override
public void execTask() {
log.info("===============================定时检查统计各种资源消耗量 begin====================================");
Double teamConsume = null;
Double personConsume = null;
Double ammunitionConsume = null;
Double foodConsume = null;
Double waterConsume = null;
Double fuelConsume = null;
Double medicalConsume = null;
String teamLat = null;
String teamLng = null;
JSONObject jsonObject = new JSONObject();
try {
HashMap<String,Object> battleParams = (HashMap<String, Object>) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId());
@ -118,11 +126,10 @@ public class BattleRootTask extends AbtParentTask {
double fuel = Double.valueOf(battleParams.get("fuel").toString());
double medical = Double.valueOf(battleParams.get("medical").toString());
double water = Double.valueOf(battleParams.get("water").toString());
int person = Integer.valueOf(battleParams.get("person").toString());
double team = Double.valueOf(battleParams.get( "team").toString());
double person = Double.valueOf(battleParams.get("person").toString());
teamLat = battleParams.get( "teamLat").toString();
teamLng = battleParams.get( "teamLng").toString();
//
teamConsume = intervalDuringTime * TEAM_SPREED;
personConsume = intervalDuringTime * PERSON_SPREED;
ammunitionConsume = intervalDuringTime * AMMUNITION_SPREED;
foodConsume = intervalDuringTime * FOOD_SPREED;
@ -136,7 +143,6 @@ public class BattleRootTask extends AbtParentTask {
battleParams.put("medical",Double.valueOf(medical - medicalConsume).toString());
battleParams.put("water",Double.valueOf(water - waterConsume).toString());
battleParams.put("person",Double.valueOf(person - personConsume).toString());
battleParams.put("team",Double.valueOf(team - teamConsume).toString());
battleParams.put("duringTime",Long.valueOf(duringTime).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), battleParams);
}catch (Exception ex){
@ -145,13 +151,14 @@ public class BattleRootTask extends AbtParentTask {
try {
//推送消耗數據
ResponseCmdInfo<JSONObject> sendConsumeMsg = new ResponseCmdInfo<>();
jsonObject.put("teamConsume", teamConsume);
jsonObject.put("personConsume", personConsume);
jsonObject.put("ammunitionConsume", ammunitionConsume);
jsonObject.put("foodConsume", foodConsume);
jsonObject.put("waterConsume", waterConsume);
jsonObject.put("fuelConsume", fuelConsume);
jsonObject.put("medicalConsume", medicalConsume);
jsonObject.put("teamLat",teamLat);
jsonObject.put("teamLng",teamLng);
sendConsumeMsg.setData(jsonObject);
Global.sendCmdInfoQueue.add(sendConsumeMsg);
}catch (Exception ex){
@ -160,18 +167,39 @@ public class BattleRootTask extends AbtParentTask {
try {
LocalDateTime currentDateTime = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
List<SupplierRequest> requestList = new ArrayList<>();
Set<Map.Entry<String, Object>> consumeSet = jsonObject.entrySet();
for (Map.Entry<String, Object> consumeEntry : consumeSet) {
String person = SpringUtil.getBean(Environment.class).getProperty("battle.person.warn");
String ammunition = SpringUtil.getBean(Environment.class).getProperty("battle.ammunition.warn");
log.info("===========person ammunition======={}====={}========",person,ammunition);
// String food = SpringUtil.getBean(Environment.class).getProperty("battle.food.warn");
// String water = SpringUtil.getBean(Environment.class).getProperty("battle.water.warn");
// String fuel = SpringUtil.getBean(Environment.class).getProperty("battle.fuel.warn");
// String medical = SpringUtil.getBean(Environment.class).getProperty("battle.medical.warn");
if(Double.valueOf(battleParams.get("ammunition").toString()) <= Double.valueOf(ammunition) && suppleFlagMap.get("ammunition") == false){
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
supplierRequest.setSupplierNum(Double.valueOf(consumeEntry.getValue().toString()));
supplierRequest.setSupplierType(consumeEntry.getKey());
supplierRequest.setSupplierNum(suppleAmount);
supplierRequest.setSupplierType("ammunition");
supplierRequest.setGeneralTime(currentDateTime);
requestList.add(supplierRequest);
supplierRequest.setLat(jsonObject.get("teamLat").toString());
supplierRequest.setLng(jsonObject.get("teamLng").toString());
supplierRequestService.save(supplierRequest);
suppleFlagMap.put("ammunition",true) ;
}
if(Double.valueOf(battleParams.get("person").toString()) <= Double.valueOf(person) && suppleFlagMap.get("person") == false){
SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
supplierRequest.setSupplierNum(supplePerson);
supplierRequest.setSupplierType("person");
supplierRequest.setGeneralTime(currentDateTime);
supplierRequest.setLat(jsonObject.get("teamLat").toString());
supplierRequest.setLng(jsonObject.get("teamLng").toString());
supplierRequestService.save(supplierRequest);
suppleFlagMap.put("person",true) ;
}
supplierRequestService.saveBatch(requestList);
}catch (Exception ex){
ex.printStackTrace();
log.error("===========BattleRootTask supplierRequestService.saveBatch error====================",ex.getMessage());

View File

@ -1,5 +1,6 @@
package com.hivekion.statistic.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.hivekion.baseData.entity.Fightpowerstaff;
import com.hivekion.baseData.entity.OrgSupplier;
import com.hivekion.baseData.service.FightpowerstaffService;
@ -61,6 +62,9 @@ public class StatisticServiceImpl implements StatisticService {
List<ScenarioOrgPost> orgPostList = scenarioOrgPostService.selectByCondition(post);
List<Integer> orgList = orgPostList.stream().map(ScenarioOrgPost::getOrgId)
.collect(Collectors.toList());
if(CollectionUtil.isEmpty(orgList)){
return new StatisticBean();
}
//获取人员信息
List<Fightpowerstaff> staffList = fightpowerstaffService.queryByOrgIds(orgList);
int sum = staffList.stream()

View File

@ -32,7 +32,7 @@ public class WsServer {
// key -> 当前key下所有会话
private static final Map<String, Map<String, Map<String, Session>>> SESSION_MAP = new ConcurrentHashMap<>();
private final Object lock = new Object();
private static final Object lock = new Object();
@OnOpen
@ -112,7 +112,7 @@ public class WsServer {
public static void sendMessage(Integer scenarioId, String room, String message) {
// log.info("send {},{},{}", message, scenarioId, room);
Object lock = lockMap.computeIfAbsent(scenarioId, k -> new Object());
synchronized (lock) {
Map<String, Map<String, Session>> roomMap = SESSION_MAP.get(String.valueOf(scenarioId));
// log.info("roomMap:{}", roomMap);

View File

@ -0,0 +1,6 @@
battle.person.warn = 2
battle.ammunition.warn = 3
battle.food.warn = 3
battle.water.warn = 3
battle.fuel.warn = 2
battle.medical.warn = 1

View File

@ -1,2 +1,2 @@
spring.profiles.active=prod
spring.profiles.active=prod,rule
#spring.profiles.active=dev