战斗任务消耗资源代码

This commit is contained in:
wangwenhua 2025-09-18 21:03:36 +08:00
parent b6dde358d4
commit 897f2ecc82
3 changed files with 58 additions and 35 deletions

View File

@ -1,5 +1,6 @@
package com.hivekion; package com.hivekion;
import cn.hutool.extra.spring.EnableSpringUtil;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -13,8 +14,9 @@ import java.time.LocalDate;
import java.time.LocalTime; import java.time.LocalTime;
@SpringBootApplication @SpringBootApplication
@EnableSpringUtil
@MapperScan({"com.hivekion.**.mapper"}) @MapperScan({"com.hivekion.**.mapper"})
@ComponentScan(basePackages = { "com.hivekion.*"}) @ComponentScan(basePackages = {"cn.hutool.extra.spring","com.hivekion.*"})
public class DemoApplication extends SpringBootServletInitializer { public class DemoApplication extends SpringBootServletInitializer {
private static Logger log = LoggerFactory.getLogger(DemoApplication.class); private static Logger log = LoggerFactory.getLogger(DemoApplication.class);

View File

@ -6,6 +6,7 @@ import com.alibaba.fastjson2.JSONObject;
import com.hivekion.Global; import com.hivekion.Global;
import com.hivekion.common.entity.ResponseCmdInfo; import com.hivekion.common.entity.ResponseCmdInfo;
import com.hivekion.common.redis.RedisUtil; import com.hivekion.common.redis.RedisUtil;
import com.hivekion.common.uuid.IdUtils;
import com.hivekion.scenario.entity.ScenarioTask; import com.hivekion.scenario.entity.ScenarioTask;
import com.hivekion.statistic.bean.*; import com.hivekion.statistic.bean.*;
import com.hivekion.statistic.service.StatisticService; import com.hivekion.statistic.service.StatisticService;
@ -40,7 +41,7 @@ public class BattleRootTask extends AbtParentTask {
private ISupplierRequestService supplierRequestService; private ISupplierRequestService supplierRequestService;
private static final Double TEAM_SPREED = 1.2D; private static final Double TEAM_SPREED = 1.2D;
private static final Integer PERSON_SPREED = 3; private static final Double PERSON_SPREED = 3D;
private static final Double AMMUNITION_SPREED = 2.6D; private static final Double AMMUNITION_SPREED = 2.6D;
private static final Double FOOD_SPREED = 2.3D; private static final Double FOOD_SPREED = 2.3D;
private static final Double WATER_SPREED = 3.6D; private static final Double WATER_SPREED = 3.6D;
@ -55,32 +56,43 @@ public class BattleRootTask extends AbtParentTask {
private void initBean(){ private void initBean(){
if(statisticService == null) { if(statisticService == null) {
statisticService = SpringUtil.getBean("statisticService"); statisticService = SpringUtil.getBean(StatisticService.class);
} }
if(redisUtil == null) { if(redisUtil == null) {
redisUtil = SpringUtil.getBean("redisUtil"); redisUtil = SpringUtil.getBean(RedisUtil.class);
} }
if(supplierRequestService == null){ if(supplierRequestService == null){
supplierRequestService = SpringUtil.getBean("supplierRequestService"); supplierRequestService = SpringUtil.getBean(ISupplierRequestService.class);
} }
} }
//执行一次 //执行一次
@Override @Override
public void doSomeThing() { public void doSomeThing() {
log.info("===============begin BattleRootTask=========={}==========",this.getRoomStatus());
this.initBean(); this.initBean();
if(this.getRoomStatus()) { if(this.getRoomStatus()) {
long initDuringTime = this.getDuringTime(); long initDuringTime = this.getDuringTime();
StatisticBean statisticBean = statisticService.statistic(scenarioTask.getResourceId()); StatisticBean statisticBean = null;
try {
statisticBean = statisticService.statistic(scenarioTask.getResourceId());
}catch (Exception ex){
ex.printStackTrace();
}
HashMap<String,Object> battleParams = new HashMap<>();
// statisticBean.getAmmunition().getCurrent() // statisticBean.getAmmunition().getCurrent()
//初始化本次战斗任务各种资源数 //初始化本次战斗任务各种资源数
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"ammunition",statisticBean.getAmmunition().getCurrent());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"food",statisticBean.getFood().getCurrent()); battleParams.put("ammunition",Double.valueOf(statisticBean.getAmmunition().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"fuel",statisticBean.getFuel().getCurrent()); battleParams.put("food",Double.valueOf(statisticBean.getFood().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"medical",statisticBean.getMedical().getCurrent()); battleParams.put("fuel",Double.valueOf(statisticBean.getFuel().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"water",statisticBean.getWater().getCurrent()); battleParams.put("medical",Double.valueOf(statisticBean.getMedical().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"person",statisticBean.getPerson().getCurrent()); battleParams.put("water",Double.valueOf(statisticBean.getWater().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"team",statisticBean.getPerson().getCurrent()); battleParams.put("person",Double.valueOf(statisticBean.getPerson().getCurrent()).toString());
redisUtil.hset(scenarioTask.getScenarioId()+"-"+roomId+"-"+scenarioTask.getResourceId(),"duringTime",initDuringTime); battleParams.put("team",Double.valueOf(statisticBean.getTeam().getLat()).toString());
battleParams.put("duringTime",Long.valueOf(initDuringTime).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),battleParams);
log.info("===============================初始化本次战斗任务各种资源数===================================="); log.info("===============================初始化本次战斗任务各种资源数====================================");
//定时检查统计各种资源消耗量 //定时检查统计各种资源消耗量
@ -89,7 +101,7 @@ public class BattleRootTask extends AbtParentTask {
public void execTask() { public void execTask() {
log.info("===============================定时检查统计各种资源消耗量 begin===================================="); log.info("===============================定时检查统计各种资源消耗量 begin====================================");
Double teamConsume = null; Double teamConsume = null;
Long personConsume = null; Double personConsume = null;
Double ammunitionConsume = null; Double ammunitionConsume = null;
Double foodConsume = null; Double foodConsume = null;
Double waterConsume = null; Double waterConsume = null;
@ -97,16 +109,17 @@ public class BattleRootTask extends AbtParentTask {
Double medicalConsume = null; Double medicalConsume = null;
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
try { try {
HashMap<String,Object> battleParams = (HashMap<String, Object>) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId());
long duringTime = getDuringTime(); long duringTime = getDuringTime();
long lastDuringTime = (long) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "duringTime"); long lastDuringTime = Long.valueOf(battleParams.get("duringTime").toString());
long intervalDuringTime = duringTime - lastDuringTime; long intervalDuringTime = duringTime - lastDuringTime;
double ammunition = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "ammunition"); double ammunition = Double.valueOf(battleParams.get("ammunition").toString());
double food = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "food"); double food = Double.valueOf(battleParams.get("food").toString());
double fuel = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "fuel"); double fuel = Double.valueOf(battleParams.get("fuel").toString());
double medical = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "medical"); double medical = Double.valueOf(battleParams.get("medical").toString());
double water = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "water"); double water = Double.valueOf(battleParams.get("water").toString());
int person = (int) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "person"); int person = Integer.valueOf(battleParams.get("person").toString());
double team = (double) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "team"); double team = Double.valueOf(battleParams.get( "team").toString());
// //
teamConsume = intervalDuringTime * TEAM_SPREED; teamConsume = intervalDuringTime * TEAM_SPREED;
@ -117,14 +130,15 @@ public class BattleRootTask extends AbtParentTask {
fuelConsume = intervalDuringTime * FUEL_SPREED; fuelConsume = intervalDuringTime * FUEL_SPREED;
medicalConsume = intervalDuringTime * MEDICAL_SPREED; medicalConsume = intervalDuringTime * MEDICAL_SPREED;
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "ammunition", ammunition - ammunitionConsume); battleParams.put("ammunition",Double.valueOf(ammunition - ammunitionConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "food", food - foodConsume); battleParams.put("food",Double.valueOf(food - foodConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "fuel", fuel - fuelConsume); battleParams.put("fuel",Double.valueOf(fuel - fuelConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "medical", medical - medicalConsume); battleParams.put("medical",Double.valueOf(medical - medicalConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "water", water - waterConsume); battleParams.put("water",Double.valueOf(water - waterConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "person", person - personConsume); battleParams.put("person",Double.valueOf(person - personConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "team", team - teamConsume); battleParams.put("team",Double.valueOf(team - teamConsume).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), "duringTime", duringTime); battleParams.put("duringTime",Long.valueOf(duringTime).toString());
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(), battleParams);
}catch (Exception ex){ }catch (Exception ex){
log.error("==============================设置消耗信息失败=============================================",ex.getMessage()); log.error("==============================设置消耗信息失败=============================================",ex.getMessage());
} }
@ -150,14 +164,16 @@ public class BattleRootTask extends AbtParentTask {
Set<Map.Entry<String, Object>> consumeSet = jsonObject.entrySet(); Set<Map.Entry<String, Object>> consumeSet = jsonObject.entrySet();
for (Map.Entry<String, Object> consumeEntry : consumeSet) { for (Map.Entry<String, Object> consumeEntry : consumeSet) {
SupplierRequest supplierRequest = new SupplierRequest(); SupplierRequest supplierRequest = new SupplierRequest();
supplierRequest.setId(IdUtils.simpleUUID());
supplierRequest.setFromResourceId(scenarioTask.getResourceId()); supplierRequest.setFromResourceId(scenarioTask.getResourceId());
supplierRequest.setSupplierNum((Double) consumeEntry.getValue()); supplierRequest.setSupplierNum(Double.valueOf(consumeEntry.getValue().toString()));
supplierRequest.setSupplierType(consumeEntry.getKey()); supplierRequest.setSupplierType(consumeEntry.getKey());
supplierRequest.setGeneralTime(currentDateTime); supplierRequest.setGeneralTime(currentDateTime);
requestList.add(supplierRequest); requestList.add(supplierRequest);
} }
supplierRequestService.saveBatch(requestList); supplierRequestService.saveBatch(requestList);
}catch (Exception ex){ }catch (Exception ex){
ex.printStackTrace();
log.error("===========BattleRootTask supplierRequestService.saveBatch error====================",ex.getMessage()); log.error("===========BattleRootTask supplierRequestService.saveBatch error====================",ex.getMessage());
} }
log.info("===============================定时检查统计各种资源消耗量 end===================================="); log.info("===============================定时检查统计各种资源消耗量 end====================================");

View File

@ -1,5 +1,6 @@
package com.hivekion.supplier.entity; package com.hivekion.supplier.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@ -20,18 +21,22 @@ public class SupplierRequest implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@TableField(value="ID")
private String id; private String id;
@TableField(value="GENERAL_TIME")
private LocalDateTime generalTime; private LocalDateTime generalTime;
@TableField(value="FROM_RESOURCE_ID")
private String fromResourceId; private String fromResourceId;
@TableField(value="SUPPLIER_TYPE")
private String supplierType; private String supplierType;
@TableField(value="SUPPLIER_NUM")
private Double supplierNum; private Double supplierNum;
@TableField(value="LAT")
private String lat; private String lat;
@TableField(value="LNG")
private String lng; private String lng;
public String getId() { public String getId() {