2025-09-18 10:47:37 +08:00
|
|
|
|
package com.hivekion.room.bean;
|
|
|
|
|
|
|
2025-09-18 16:19:42 +08:00
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
2025-09-18 15:23:09 +08:00
|
|
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
2025-09-18 11:20:55 +08:00
|
|
|
|
import com.hivekion.Global;
|
2025-09-18 15:23:09 +08:00
|
|
|
|
import com.hivekion.common.entity.ResponseCmdInfo;
|
|
|
|
|
|
import com.hivekion.common.redis.RedisUtil;
|
2025-09-18 21:03:36 +08:00
|
|
|
|
import com.hivekion.common.uuid.IdUtils;
|
2025-09-18 10:47:37 +08:00
|
|
|
|
import com.hivekion.scenario.entity.ScenarioTask;
|
2025-09-18 15:23:09 +08:00
|
|
|
|
import com.hivekion.statistic.bean.*;
|
|
|
|
|
|
import com.hivekion.statistic.service.StatisticService;
|
|
|
|
|
|
import com.hivekion.statistic.service.impl.StatisticServiceImpl;
|
2025-09-18 16:19:42 +08:00
|
|
|
|
import com.hivekion.supplier.entity.SupplierRequest;
|
|
|
|
|
|
import com.hivekion.supplier.service.ISupplierRequestService;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
|
import java.util.*;
|
2025-09-18 15:23:09 +08:00
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2025-09-18 10:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* [类的简要说明]
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
* [详细描述,可选]
|
|
|
|
|
|
* <p>
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author LiDongYU
|
|
|
|
|
|
* @since 2025/7/22
|
|
|
|
|
|
*/
|
2025-09-18 16:19:42 +08:00
|
|
|
|
@Slf4j
|
2025-09-18 10:47:37 +08:00
|
|
|
|
public class BattleRootTask extends AbtParentTask {
|
|
|
|
|
|
|
2025-09-18 15:23:09 +08:00
|
|
|
|
private StatisticService statisticService = null;
|
|
|
|
|
|
private RedisUtil redisUtil = null;
|
|
|
|
|
|
|
2025-09-18 16:19:42 +08:00
|
|
|
|
private ISupplierRequestService supplierRequestService;
|
|
|
|
|
|
|
2025-09-18 15:23:09 +08:00
|
|
|
|
private static final Double TEAM_SPREED = 1.2D;
|
2025-09-18 21:03:36 +08:00
|
|
|
|
private static final Double PERSON_SPREED = 3D;
|
2025-09-18 15:23:09 +08:00
|
|
|
|
private static final Double AMMUNITION_SPREED = 2.6D;
|
|
|
|
|
|
private static final Double FOOD_SPREED = 2.3D;
|
|
|
|
|
|
private static final Double WATER_SPREED = 3.6D;
|
|
|
|
|
|
private static final Double FUEL_SPREED = 3.6D;
|
|
|
|
|
|
private static final Double MEDICAL_SPREED = 1.6D;
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-18 10:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
public BattleRootTask(ScenarioTask scenarioTask,String roomId) {
|
|
|
|
|
|
super(scenarioTask,roomId);
|
|
|
|
|
|
}
|
2025-09-18 15:23:09 +08:00
|
|
|
|
|
|
|
|
|
|
private void initBean(){
|
|
|
|
|
|
if(statisticService == null) {
|
2025-09-18 21:03:36 +08:00
|
|
|
|
statisticService = SpringUtil.getBean(StatisticService.class);
|
2025-09-18 15:23:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
if(redisUtil == null) {
|
2025-09-18 21:03:36 +08:00
|
|
|
|
redisUtil = SpringUtil.getBean(RedisUtil.class);
|
2025-09-18 15:23:09 +08:00
|
|
|
|
}
|
2025-09-18 16:19:42 +08:00
|
|
|
|
if(supplierRequestService == null){
|
2025-09-18 21:03:36 +08:00
|
|
|
|
supplierRequestService = SpringUtil.getBean(ISupplierRequestService.class);
|
2025-09-18 16:19:42 +08:00
|
|
|
|
}
|
2025-09-18 15:23:09 +08:00
|
|
|
|
}
|
2025-09-18 11:20:55 +08:00
|
|
|
|
//执行一次
|
2025-09-18 10:47:37 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
public void doSomeThing() {
|
2025-09-18 21:03:36 +08:00
|
|
|
|
log.info("===============begin BattleRootTask=========={}==========",this.getRoomStatus());
|
2025-09-18 15:23:09 +08:00
|
|
|
|
this.initBean();
|
|
|
|
|
|
if(this.getRoomStatus()) {
|
2025-09-18 17:30:12 +08:00
|
|
|
|
long initDuringTime = this.getDuringTime();
|
2025-09-18 21:03:36 +08:00
|
|
|
|
StatisticBean statisticBean = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
statisticBean = statisticService.statistic(scenarioTask.getResourceId());
|
|
|
|
|
|
}catch (Exception ex){
|
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HashMap<String,Object> battleParams = new HashMap<>();
|
|
|
|
|
|
|
2025-09-18 15:23:09 +08:00
|
|
|
|
// statisticBean.getAmmunition().getCurrent()
|
|
|
|
|
|
//初始化本次战斗任务各种资源数
|
2025-09-18 21:03:36 +08:00
|
|
|
|
|
|
|
|
|
|
battleParams.put("ammunition",Double.valueOf(statisticBean.getAmmunition().getCurrent()).toString());
|
|
|
|
|
|
battleParams.put("food",Double.valueOf(statisticBean.getFood().getCurrent()).toString());
|
|
|
|
|
|
battleParams.put("fuel",Double.valueOf(statisticBean.getFuel().getCurrent()).toString());
|
|
|
|
|
|
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("duringTime",Long.valueOf(initDuringTime).toString());
|
|
|
|
|
|
redisUtil.hset(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId(),battleParams);
|
2025-09-18 16:19:42 +08:00
|
|
|
|
log.info("===============================初始化本次战斗任务各种资源数====================================");
|
2025-09-18 17:30:12 +08:00
|
|
|
|
|
2025-09-18 15:23:09 +08:00
|
|
|
|
//定时检查统计各种资源消耗量
|
2025-09-18 17:30:12 +08:00
|
|
|
|
this.createBattleTaskOnTimingHandle(new BizTaskOnTiming() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void execTask() {
|
|
|
|
|
|
log.info("===============================定时检查统计各种资源消耗量 begin====================================");
|
|
|
|
|
|
Double teamConsume = null;
|
2025-09-18 21:03:36 +08:00
|
|
|
|
Double personConsume = null;
|
2025-09-18 17:30:12 +08:00
|
|
|
|
Double ammunitionConsume = null;
|
|
|
|
|
|
Double foodConsume = null;
|
|
|
|
|
|
Double waterConsume = null;
|
|
|
|
|
|
Double fuelConsume = null;
|
|
|
|
|
|
Double medicalConsume = null;
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
|
try {
|
2025-09-18 21:03:36 +08:00
|
|
|
|
HashMap<String,Object> battleParams = (HashMap<String, Object>) redisUtil.hget(scenarioTask.getScenarioId() + "-" + roomId + "-" + scenarioTask.getResourceId());
|
2025-09-18 17:30:12 +08:00
|
|
|
|
long duringTime = getDuringTime();
|
2025-09-18 21:03:36 +08:00
|
|
|
|
long lastDuringTime = Long.valueOf(battleParams.get("duringTime").toString());
|
2025-09-18 17:30:12 +08:00
|
|
|
|
long intervalDuringTime = duringTime - lastDuringTime;
|
2025-09-18 21:03:36 +08:00
|
|
|
|
double ammunition = Double.valueOf(battleParams.get("ammunition").toString());
|
|
|
|
|
|
double food = Double.valueOf(battleParams.get("food").toString());
|
|
|
|
|
|
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());
|
2025-09-18 17:30:12 +08:00
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
teamConsume = intervalDuringTime * TEAM_SPREED;
|
|
|
|
|
|
personConsume = intervalDuringTime * PERSON_SPREED;
|
|
|
|
|
|
ammunitionConsume = intervalDuringTime * AMMUNITION_SPREED;
|
|
|
|
|
|
foodConsume = intervalDuringTime * FOOD_SPREED;
|
|
|
|
|
|
waterConsume = intervalDuringTime * WATER_SPREED;
|
|
|
|
|
|
fuelConsume = intervalDuringTime * FUEL_SPREED;
|
|
|
|
|
|
medicalConsume = intervalDuringTime * MEDICAL_SPREED;
|
|
|
|
|
|
|
2025-09-18 21:03:36 +08:00
|
|
|
|
battleParams.put("ammunition",Double.valueOf(ammunition - ammunitionConsume).toString());
|
|
|
|
|
|
battleParams.put("food",Double.valueOf(food - foodConsume).toString());
|
|
|
|
|
|
battleParams.put("fuel",Double.valueOf(fuel - fuelConsume).toString());
|
|
|
|
|
|
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);
|
2025-09-18 17:30:12 +08:00
|
|
|
|
}catch (Exception ex){
|
|
|
|
|
|
log.error("==============================设置消耗信息失败=============================================",ex.getMessage());
|
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
|
sendConsumeMsg.setData(jsonObject);
|
|
|
|
|
|
Global.sendCmdInfoQueue.add(sendConsumeMsg);
|
|
|
|
|
|
}catch (Exception ex){
|
|
|
|
|
|
log.error("==================推送消耗數據 失败============================================",ex.getMessage());
|
|
|
|
|
|
}
|
2025-09-18 16:19:42 +08:00
|
|
|
|
|
2025-09-18 17:30:12 +08:00
|
|
|
|
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) {
|
|
|
|
|
|
SupplierRequest supplierRequest = new SupplierRequest();
|
2025-09-18 21:03:36 +08:00
|
|
|
|
supplierRequest.setId(IdUtils.simpleUUID());
|
2025-09-18 17:30:12 +08:00
|
|
|
|
supplierRequest.setFromResourceId(scenarioTask.getResourceId());
|
2025-09-18 21:03:36 +08:00
|
|
|
|
supplierRequest.setSupplierNum(Double.valueOf(consumeEntry.getValue().toString()));
|
2025-09-18 17:30:12 +08:00
|
|
|
|
supplierRequest.setSupplierType(consumeEntry.getKey());
|
|
|
|
|
|
supplierRequest.setGeneralTime(currentDateTime);
|
|
|
|
|
|
requestList.add(supplierRequest);
|
|
|
|
|
|
}
|
|
|
|
|
|
supplierRequestService.saveBatch(requestList);
|
|
|
|
|
|
}catch (Exception ex){
|
2025-09-18 21:03:36 +08:00
|
|
|
|
ex.printStackTrace();
|
2025-09-18 17:30:12 +08:00
|
|
|
|
log.error("===========BattleRootTask supplierRequestService.saveBatch error====================",ex.getMessage());
|
2025-09-18 16:19:42 +08:00
|
|
|
|
}
|
2025-09-18 17:30:12 +08:00
|
|
|
|
log.info("===============================定时检查统计各种资源消耗量 end====================================");
|
2025-09-18 16:19:42 +08:00
|
|
|
|
}
|
2025-09-18 17:30:12 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-09-18 15:23:09 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-18 10:47:37 +08:00
|
|
|
|
}
|
2025-09-18 11:41:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-18 10:47:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|