瀏覽代碼

1.添加受灾情况
2.添加购买防护包操作

slambb 4 年之前
父節點
當前提交
8a1281461d

+ 2 - 0
src/main/java/com/td/boss/game/commallother/pojo/ComMallOther.java

@@ -21,6 +21,8 @@ public class ComMallOther implements Serializable {
 
     private Integer priceSnb;//
 
+    private Integer priceCnt;//新增一个cnt字段
+
     private Integer amount;//库存数量
 
     private String itemDescribe;//描述一下生产周期

+ 2 - 0
src/main/java/com/td/boss/game/commallother/vo/ComMallOtherSimpleVo.java

@@ -18,6 +18,8 @@ public class ComMallOtherSimpleVo  implements Serializable {
 
     private Integer priceSnb;//
 
+    private Integer priceCnt;//新增一个cnt字段
+
     private Integer amount;//库存数量
 
     private String itemDescribe;//描述一下生产周期

+ 2 - 0
src/main/java/com/td/boss/game/commallother/vo/ComMallOtherVo.java

@@ -16,6 +16,8 @@ public class ComMallOtherVo extends PageCondition implements Serializable {
 
     private Integer priceSnb;//
 
+    private Integer priceCnt;//新增一个cnt字段
+
     private Integer amount;//库存数量
 
     private String itemDescribe;//描述一下生产周期

+ 15 - 8
src/main/java/com/td/boss/game/complayergoods/controller/ComPlayerGoodsController.java

@@ -375,6 +375,8 @@ public class ComPlayerGoodsController extends CommonController<ComPlayerGoodsVo,
      * <p>
      * <p>
      * 收获时候,操作用户snb时候,需要加锁,防止用户收取时候遗漏计算snb
+     * 狗只有被隐藏状态下才会咬人,小偷被咬才会损失SNB或者体力;
+     * 无论是否被咬,小偷的收获不会改变
      *
      * @param otherUserId
      * @param otherLandId
@@ -411,13 +413,17 @@ public class ComPlayerGoodsController extends CommonController<ComPlayerGoodsVo,
                 if (_needConsumption > 0) {
                     //口粮足够,工作
                     _dogWork = true;
-                    //狗存在并工作,获取狗的数据
-                    ComMallOtherVo _mallDogData = comMallOtherService.get(_otherDogVo.getOtherIndex()).getData();
-                    Double _triggerPro = _mallDogData.getTriggerPro();//触发概率
-                    if (new Random().nextDouble() <= _triggerPro) {
-                        //被狗咬了
-                        _wasTheDogBitten = true;
+                    //todo 隐藏状态下才会触发咬人
+                    if (_otherDogVo.getIsShow().equals(0)) {
+                        //狗存在并工作,获取狗的数据
+                        ComMallOtherVo _mallDogData = comMallOtherService.get(_otherDogVo.getOtherIndex()).getData();
+                        Double _triggerPro = _mallDogData.getTriggerPro();//触发概率
+                        if (new Random().nextDouble() <= _triggerPro) {
+                            //被狗咬了
+                            _wasTheDogBitten = true;
+                        }
                     }
+
                 }
             }
 
@@ -431,7 +437,7 @@ public class ComPlayerGoodsController extends CommonController<ComPlayerGoodsVo,
             //todo 判断打狗棒是否到期
             long _stickRunDay = DateUtil.getDays(DateUtil.getNowDate().getTime() - _beatDogStick.getEffectiveStartTime().getTime());
             int _stickRemainingDay = _beatDogStick.getEffectiveDay() - (int) _stickRunDay;
-            if(_stickRemainingDay>0){
+            if (_stickRemainingDay > 0) {
                 //持有打狗棒
                 _holdTheStick = true;
             }
@@ -600,6 +606,7 @@ public class ComPlayerGoodsController extends CommonController<ComPlayerGoodsVo,
             comPlayerProfit.setProfitRatio(profitConfig);
             comPlayerProfit.setStealRatio(_stealRatio);
             comPlayerProfit.setFinalRatio(finallyGetRatioConfig);
+            comPlayerProfit.setLossType(0);
             ComPlayerProfitVo comPlayerProfitVo = CopyUtil.copy(comPlayerProfit, ComPlayerProfitVo.class);
             comPlayerProfitService.save(comPlayerProfitVo);
 
@@ -985,7 +992,7 @@ public class ComPlayerGoodsController extends CommonController<ComPlayerGoodsVo,
             @RequestParam(value = "amount") Integer amount) {
 
         //防止输入负数
-        if(amount <= 0){
+        if (amount <= 0) {
             return Result.of(null, false, ResultEnum.FRUIT_AMOUNT_ERROR.getMessage(), ResultEnum.FRUIT_AMOUNT_ERROR.getCode());
         }
         //

+ 89 - 0
src/main/java/com/td/boss/game/complayerland/controller/ComPlayerLandController.java

@@ -1,5 +1,7 @@
 package com.td.boss.game.complayerland.controller;
 
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.json.JSONUtil;
 import com.td.boss.common.controller.*;
 import com.td.boss.common.pojo.Result;
 import com.td.boss.config.enums.ResultEnum;
@@ -8,12 +10,14 @@ import com.td.boss.game.comexplainland.vo.ComExplainLandVo;
 import com.td.boss.game.commallseed.service.ComMallSeedService;
 import com.td.boss.game.commallseed.vo.ComMallSeedVo;
 import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
 import com.td.boss.game.complayerland.service.ComPlayerDisasterLogService;
 import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
 import com.td.boss.game.complayergoods.service.ComPlayerGoodsService;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
 import com.td.boss.game.complayerland.pojo.ComPlayerLand;
 import com.td.boss.game.complayerland.pojo.ComPlayerLandAndCanSteal;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterProtectedService;
 import com.td.boss.game.complayerland.vo.ComPlayerLandAndPlantVo;
 import com.td.boss.game.complayerland.vo.ComPlayerLandAndUserInfoVo;
 import com.td.boss.game.complayerland.vo.ComPlayerLandSimpleVo;
@@ -21,6 +25,8 @@ import com.td.boss.game.complayerland.vo.ComPlayerLandVo;
 import com.td.boss.game.complayerland.service.ComPlayerLandService;
 import com.td.boss.game.complayerlog.service.ComPlayerLogService;
 import com.td.boss.game.complayerlog.vo.ComPlayerLogVo;
+import com.td.boss.game.complayerprofit.pojo.ComPlayerProfit;
+import com.td.boss.game.complayerprofit.service.ComPlayerProfitService;
 import com.td.boss.util.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -56,6 +62,15 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
     @Autowired
     private ComPlayerDisasterLogService comPlayerDisasterLogService;
 
+    @Autowired
+    private ComPlayerDisasterProtectedService disasterProtectedService;
+
+
+    @Autowired
+    private ComPlayerProfitService comPlayerProfitService;
+
+    @Autowired
+    private RedisLock redisLock;
 
     /**
      * 获取用户土地
@@ -77,6 +92,7 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
         List<ComPlayerLandAndPlantVo> _simpleVoList = CopyUtil.copyList(comPlayerLands, ComPlayerLandAndPlantVo.class);
         List<ComPlayerLandAndPlantVo> _list = new ArrayList<>();
 
+
         try {
             _simpleVoList.stream().map(e -> {
                 ComPlayerLandAndPlantVo comPlayerLandAndPlantVo = e;
@@ -126,6 +142,8 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
                         //说明可以收获了
                         comPlayerLandAndPlantVo.setPlantDaysRemaining(0);
                         comPlayerLandAndPlantVo.setPlantHoursRemaining(0);
+                        //生成第一次受灾
+                        _InitFirstDisasterProfit(comPlayerLandAndPlantVo,comMallSeedVo);
                     } else {
                         comPlayerLandAndPlantVo.setPlantDaysRemaining(DateUtil.getDays(diff).intValue());
                         comPlayerLandAndPlantVo.setPlantHoursRemaining(DateUtil.getHours(diff).intValue());
@@ -195,6 +213,9 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
                         //说明可以收获了
                         comPlayerLandAndPlantVo.setPlantDaysRemaining(0);
                         comPlayerLandAndPlantVo.setPlantHoursRemaining(0);
+                        //生成第一次受灾
+                        _InitFirstDisasterProfit(comPlayerLandAndPlantVo,comMallSeedVo);
+
                     } else {
                         comPlayerLandAndPlantVo.setPlantDaysRemaining(DateUtil.getDays(diff).intValue());
                         comPlayerLandAndPlantVo.setPlantHoursRemaining(DateUtil.getHours(diff).intValue());
@@ -283,6 +304,8 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
             @RequestParam(value = "landId") Integer landId,
             @RequestParam(value = "seedId") Integer seedId) {
 
+        long time = System.currentTimeMillis() + RedisData.getPlayerDisastersFirstTimeout();
+
         try {
             ComPlayerLand comPlayerLand = comPlayerLandService.findByUserIdAndLandId(userId, landId);
             //土地数据不存在
@@ -410,6 +433,11 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
                 //说明可以收获了
                 comPlayerLandAndPlantVo.setPlantDaysRemaining(0);
                 comPlayerLandAndPlantVo.setPlantHoursRemaining(0);
+
+                //生成第一次受灾
+                _InitFirstDisasterProfit(comPlayerLandAndPlantVo,comMallSeedVo);
+
+
             } else {
                 comPlayerLandAndPlantVo.setPlantDaysRemaining(DateUtil.getDays(diff).intValue());
                 comPlayerLandAndPlantVo.setPlantHoursRemaining(DateUtil.getHours(diff).intValue());
@@ -445,6 +473,7 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
 
     /**
      * 获得某个用个 灾难情况
+     *
      * @param userId
      * @return
      */
@@ -454,4 +483,64 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
         List<ComPlayerDisasterLog> history = comPlayerDisasterLogService.getHistory(userId);
         return Result.of(history);
     }
+
+
+    private void _InitFirstDisasterProfit(ComPlayerLandAndPlantVo comPlayerLandAndPlantVo,ComMallSeedVo comMallSeedVo){
+        //这里处理一次判断是否发生过第一次灾难
+        //todo 添加一个灾难损失,灾难损失的时间点是种植开始到成熟阶段的一个随机时间
+        // 加个锁判断,是否在其他地方触发了这次灾难计算(自解锁)
+        long time = System.currentTimeMillis() + RedisData.getPlayerDisastersFirstTimeout();
+        String _disasterKey = RedisData.getPlayerDisastersFirstKey() + comPlayerLandAndPlantVo.getPlantFlag();
+        if (redisLock.lock(_disasterKey, String.valueOf(time))) {
+            //查询当前人员购买防护历史记录
+            List<ComPlayerDisasterProtected> protectList = disasterProtectedService.getComPlayerDisasterProtectedByUserIdOrderByProtectTimeDesc(comPlayerLandAndPlantVo.getUserId());
+
+            //灾难发生时间
+            Date endDate = com.td.boss.util.DateUtil.getOldDateAddDay(comPlayerLandAndPlantVo.getPlantStart(), comPlayerLandAndPlantVo.getPlantMature());
+            long randomDate = RandomUtil.randomLong(comPlayerLandAndPlantVo.getPlantStart().getTime(), endDate.getTime());
+            Date disasterDate = new Date(randomDate);
+            //获取损失的数据
+            List<ComPlayerProfit> comPlayerProfits = comPlayerProfitService.findByUserIdAndPlantFlagAndLandId(comPlayerLandAndPlantVo.getUserId(), comPlayerLandAndPlantVo.getPlantFlag(), comPlayerLandAndPlantVo.getConfigLandId());
+            boolean isNaturalProfit = false,isBeastProfit = false;
+            for (int j = 0; j < comPlayerProfits.size(); j++) {
+                ComPlayerProfit _profit = comPlayerProfits.get(j);
+                if(_profit.getLossType().equals(2)){
+                    isNaturalProfit = true;
+                }else if(_profit.getLossType().equals(3)){
+                    isBeastProfit = true;
+                }
+            }
+            log.info("用户编号:{},防护历史记录:{}", comPlayerLandAndPlantVo.getUserId(), JSONUtil.toJsonStr(protectList));
+            //获得对应灾难类型的防护记录    并得到这个 防护到期时间. 自然灾害类型
+            //如果没有防护或者防护到期。则直接发生灾难进行减产
+            if(!isNaturalProfit ){
+                Date _protectedDate = protectList.stream()
+                        .filter(a -> a.getDsasterType().equals(2))
+                        .sorted(Comparator.comparing(ComPlayerDisasterProtected::getProtectTime).reversed())
+                        .map(ComPlayerDisasterProtected::getProtectTime)
+                        .findFirst().orElse(cn.hutool.core.date.DateUtil.parse(null));
+                //如果没有防护或者防护到期。则直接发生灾难进行减产
+                if (_protectedDate != null && disasterDate.before(_protectedDate)) {
+                    //灾难的随机时间比防护包时间后,说明防护包到期
+                    //成熟的时候触发第一次自然灾难
+                    comPlayerProfitService.addDisasterAndLoss(CopyUtil.copy(comPlayerLandAndPlantVo, ComPlayerLand.class),
+                            comMallSeedVo, 2);
+                }
+            }
+            if(!isBeastProfit){
+                Date _protectedDate = protectList.stream()
+                        .filter(a -> a.getDsasterType().equals(3))
+                        .sorted(Comparator.comparing(ComPlayerDisasterProtected::getProtectTime).reversed())
+                        .map(ComPlayerDisasterProtected::getProtectTime)
+                        .findFirst().orElse(cn.hutool.core.date.DateUtil.parse(null));
+                //如果没有防护或者防护到期。则直接发生灾难进行减产
+                if (_protectedDate != null && disasterDate.before(_protectedDate)) {
+                    //灾难的随机时间比防护包时间后,说明防护包到期
+                    //成熟的时候触发第一次自然灾难
+                    comPlayerProfitService.addDisasterAndLoss(CopyUtil.copy(comPlayerLandAndPlantVo, ComPlayerLand.class),
+                            comMallSeedVo, 3);
+                }
+            }
+        }
+    }
 }

+ 6 - 6
src/main/java/com/td/boss/game/complayerland/scheduled/QuartzConfig.java

@@ -10,12 +10,12 @@ public class QuartzConfig {
 //    private static String JOB_GROUP_NAME = "JOBGROUP_ComPlayerDisaster";
 //    private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_ComPlayerDisaster";
 
-    private static String WeekTrigger = "0 0/1 * * * ?";
-    private static String DayTrigger = "0/5 * * * * ?";
-//    //每周日1点
-//    private static String WeekTrigger = "0 0 1 ? * SUN";
-//    //每天6点
-//    private static String DayTrigger = "0 0 6 * * ? *";
+    //private static String WeekTrigger = "0 0/1 * * * ?";
+    //private static String DayTrigger = "0/5 * * * * ?";
+    //每周日1点
+    private static String WeekTrigger = "0 0 1 ? * SUN";
+    //每天6点
+    private static String DayTrigger = "0 0 6 * * ? *";
 
     @Bean
     public JobDetail syncDisasterWeekDetail() {

+ 76 - 3
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterLogServiceImpl.java

@@ -9,13 +9,23 @@ import cn.hutool.core.util.NumberUtil;
 import cn.hutool.core.util.RandomUtil;
 import cn.hutool.json.JSONUtil;
 import com.td.boss.common.service.CommonServiceImpl;
+import com.td.boss.game.commallseed.service.ComMallSeedService;
+import com.td.boss.game.commallseed.vo.ComMallSeedVo;
 import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
 import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
 import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import com.td.boss.game.complayerland.pojo.ComPlayerLand;
 import com.td.boss.game.complayerland.repository.ComPlayerDisasterLogRepository;
 import com.td.boss.game.complayerland.scheduled.ComPlayerDisasterDateUtil;
 import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
 import com.td.boss.game.complayerland.vo.ComPlayerDisasterLogVo;
+import com.td.boss.game.complayerprofit.pojo.ComPlayerProfit;
+import com.td.boss.game.complayerprofit.service.ComPlayerProfitService;
+import com.td.boss.game.complayerprofit.vo.ComPlayerProfitVo;
+import com.td.boss.util.CopyUtil;
+import com.td.boss.util.DoubleUtil;
+import com.td.boss.util.RedisData;
+import com.td.boss.util.RedisLock;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.jpa.domain.Specification;
@@ -41,6 +51,18 @@ public class ComPlayerDisasterLogServiceImpl extends CommonServiceImpl<ComPlayer
     @Autowired
     private ComPlayerDisasterService disasterService;
 
+    @Autowired
+    private ComPlayerLandService comPlayerLandService;
+
+    @Autowired
+    private ComPlayerProfitService comPlayerProfitService;
+
+    @Autowired
+    private ComMallSeedService comMallSeedService;
+
+    @Autowired
+    private RedisLock redisLock;
+
     /**
      * 伤害 会减产50%
      */
@@ -197,9 +219,10 @@ public class ComPlayerDisasterLogServiceImpl extends CommonServiceImpl<ComPlayer
         if (protectedDate != null && now.before(protectedDate)) {
             int randomProtect = RandomUtil.randomInt(0, 101);
             log.info("灾难几率:{}", randomProtect);
-            if (randomProtect > (100 - defend)) {
-                disasterLog.setProtect(true);
-            }
+            //todo 先注释这个防护,这里触发的灾难默认是不防护的
+            //if (randomProtect > (100 - defend)) {
+            //    disasterLog.setProtect(true);
+            //}
         }
 
         log.info("灾难日志:{}", JSONUtil.toJsonStr(disasterLog));
@@ -207,6 +230,56 @@ public class ComPlayerDisasterLogServiceImpl extends CommonServiceImpl<ComPlayer
         //todo 减产接口 减产逻辑
         if (!disasterLog.isProtect()) {
             log.info("灾难结果:减产");
+            //todo 插入受灾减产数据
+            long time = System.currentTimeMillis() + RedisData.getPlayerDisastersFirstTimeout();
+            //查找用户的全部土地
+            List<ComPlayerLand> comPlayerLands = comPlayerLandService.findAllByUserIdAndPlantAndMature(disasterLog.getUserId());
+            for (int i=0;i<comPlayerLands.size();i++){
+                ComPlayerLand _comPlayerLand = comPlayerLands.get(i);
+                ComMallSeedVo comMallSeedVo = comMallSeedService.findById(_comPlayerLand.getPlantId());
+
+                //这里处理一次判断是否发生过第一次灾难
+                //todo 添加一个灾难损失,灾难损失的时间点是种植开始到成熟阶段的一个随机时间
+                // 加个锁判断,是否在其他地方触发了这次灾难计算(自解锁)
+                String _disasterKey = RedisData.getPlayerDisastersFirstKey() + _comPlayerLand.getPlantFlag();
+                if (redisLock.lock(_disasterKey, String.valueOf(time))) {
+                   //灾难发生时间
+                    Date endDate = com.td.boss.util.DateUtil.getOldDateAddDay(_comPlayerLand.getPlantStart(), _comPlayerLand.getPlantMature());
+                    long randomDate = RandomUtil.randomLong(_comPlayerLand.getPlantStart().getTime(), endDate.getTime());
+                    Date disasterDate = new Date(randomDate);
+                    //获取损失的数据
+                    List<ComPlayerProfit> comPlayerProfits = comPlayerProfitService.findByUserIdAndPlantFlagAndLandId(_comPlayerLand.getUserId(), _comPlayerLand.getPlantFlag(), _comPlayerLand.getConfigLandId());
+                    boolean isNaturalProfit = false,isBeastProfit = false;
+                    for (int j = 0; j < comPlayerProfits.size(); j++) {
+                        ComPlayerProfit _profit = comPlayerProfits.get(j);
+                       if(_profit.getLossType().equals(2)){
+                           isNaturalProfit = true;
+                       }else if(_profit.getLossType().equals(3)){
+                           isBeastProfit = true;
+                       }
+                    }
+                    log.info("用户编号:{},防护历史记录:{}", _comPlayerLand.getUserId(), JSONUtil.toJsonStr(protectList));
+                    //获得对应灾难类型的防护记录    并得到这个 防护到期时间. 自然灾害类型
+                    //如果没有防护或者防护到期。则直接发生灾难进行减产
+                    if(!isNaturalProfit && protectedDate != null && disasterDate.before(protectedDate)){
+                        //灾难的随机时间比防护包时间后,说明防护包到期
+                        //成熟的时候触发第一次自然灾难
+                        comPlayerProfitService.addDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
+                                comMallSeedVo, 2);
+                    }
+                    if(!isBeastProfit && protectedDate != null && disasterDate.before(protectedDate)){
+                        //灾难的随机时间比防护包时间后,说明防护包到期
+                        //成熟的时候触发第一次自然灾难
+                        comPlayerProfitService.addDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
+                                comMallSeedVo, 3);
+                    }
+                }
+                //todo 添加一条单独减产的数据 ,对应灾难
+                comPlayerProfitService.addDisasterAndLoss(CopyUtil.copy(_comPlayerLand, ComPlayerLand.class),
+                        comMallSeedVo, disasterLog.getDsasterType());
+            }
+
+
         } else {
             log.info("灾难结果:已防御");
         }

+ 7 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerLandService.java

@@ -14,6 +14,13 @@ public interface ComPlayerLandService extends CommonService<ComPlayerLandVo, Com
 
     List<ComPlayerLand> findAllByUserId(String userId);
 
+    /**
+     * 返回一个已种植并且成熟的列表
+     * @param userId
+     * @return
+     */
+    List<ComPlayerLand> findAllByUserIdAndPlantAndMature(String userId);
+
     //获取可以偷取的土地用户,用于首页偷菜列表显示,返回一个不包括自己的列表
     List<ComPlayerLandAndUserInfoVo> findAllByCanStealNotSelf(String userId, Pageable pageable);
 

+ 9 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerLandServiceImpl.java

@@ -41,6 +41,15 @@ public class ComPlayerLandServiceImpl extends CommonServiceImpl<ComPlayerLandVo,
     }
 
 
+    @Override
+    public List<ComPlayerLand> findAllByUserIdAndPlantAndMature(String userId) {
+        String sql = "SELECT  a.* FROM com_player_land AS a \n" +
+                "WHERE a.user_id = "+userId+" AND a.is_lease = 1 AND a.is_plant = 1 AND DATE_ADD(a.plant_start,INTERVAL a.plant_mature DAY) <= NOW() AND a.lease_time > NOW()";
+        Query nativeQuery = em.createNativeQuery(sql, ComPlayerLandAndCanSteal.class);
+        List list = nativeQuery.getResultList();
+        return list;
+    }
+
     @Override
     public List<ComPlayerLandAndUserInfoVo> findAllByCanStealNotSelf(String userId, Pageable pageable) {
         List<Object> objects = comPlayerLandRepository.findAllIsStealComPlayerLandAndUserInfo(userId, pageable);

+ 3 - 0
src/main/java/com/td/boss/game/complayerprofit/pojo/ComPlayerProfit.java

@@ -40,6 +40,9 @@ public class ComPlayerProfit implements Serializable {
 
     private Double finalRatio;//最后用户可收取的利润比例,目前是设置 10%
 
+    private Integer lossType;//损失的类型,默认是0:被偷取果实,1:自然灾难,2:野兽
+
+
     private Date createTime;//
 
     private Date updateTime;//

+ 2 - 0
src/main/java/com/td/boss/game/complayerprofit/repository/ComPlayerProfitRepository.java

@@ -26,6 +26,8 @@ public interface ComPlayerProfitRepository extends CommonRepository<ComPlayerPro
 
     Optional<ComPlayerProfit> findByUserIdAndTargetIdAndLandIdAndPlantFlag(String userId, String targetId, Integer landId, String plantFlag);
 
+    Optional<ComPlayerProfit> findByPlantFlagAndLossType(String plantFlag,Integer lossType);
+
     @Query(value = "SELECT IFNULL(sum(u.stolen), 0.0) AS sum FROM  com_player_profit u  where u.target_id=:targetId and u.plant_flag = :plantFlag", nativeQuery = true)
     Double getStolenSumByTargetIdAndPlantFlag(String targetId,String plantFlag);
 }

+ 19 - 4
src/main/java/com/td/boss/game/complayerprofit/service/ComPlayerProfitService.java

@@ -1,6 +1,8 @@
 package com.td.boss.game.complayerprofit.service;
 
 import com.td.boss.common.service.*;
+import com.td.boss.game.commallseed.pojo.ComMallSeed;
+import com.td.boss.game.commallseed.vo.ComMallSeedVo;
 import com.td.boss.game.complayerland.pojo.ComPlayerLand;
 import com.td.boss.game.complayerprofit.pojo.ComPlayerProfit;
 import com.td.boss.game.complayerprofit.vo.ComPlayerProfitVo;
@@ -11,18 +13,31 @@ public interface ComPlayerProfitService extends CommonService<ComPlayerProfitVo,
 
     /**
      * 获取被偷取的信息,表里面 targetId 对应自己的用户的 userId
+     *
      * @param userId
      * @param plantFlag
      * @param landId
      * @return
      */
-    List<ComPlayerProfit> findByUserIdAndPlantFlagAndLandId(String userId,String plantFlag,Integer landId);
+    List<ComPlayerProfit> findByUserIdAndPlantFlagAndLandId(String userId, String plantFlag, Integer landId);
 
-    ComPlayerProfit findByUserIdAndPlantFlag(String userId,String plantFlag);
+    ComPlayerProfit findByUserIdAndPlantFlag(String userId, String plantFlag);
 
-    ComPlayerProfit findByUserIdAndOtherUserIdAndPlantFlag(String userId,String otherUserId, String plantFlag);
+    ComPlayerProfit findByUserIdAndOtherUserIdAndPlantFlag(String userId, String otherUserId, String plantFlag);
+
+    ComPlayerProfit findByUserIdAndTargetIdAndLandIdAndPlantFlag(String userId, String targetId, Integer configLandId, String plantFlag);
+
+    /**
+     * 根据灾难类型进行调用,犹豫灾难只发生一次,所以lossType对于flag只有一条数据
+     * @param plantFlag
+     * @param lossType {2:自然灾难,3:野兽灾难}
+     * @return
+     */
+    ComPlayerProfit findByPlantFlagAndLossType(String plantFlag,Integer lossType);
 
-    ComPlayerProfit findByUserIdAndTargetIdAndLandIdAndPlantFlag(String userId,String targetId,Integer configLandId, String plantFlag);
 
     Double getStolenSumByOtherUserIdAndPlantFlag(String otherUserId, String plantFlag);
+
+
+    ComPlayerProfitVo addDisasterAndLoss(ComPlayerLand comPlayerLand, ComMallSeedVo comMallSeedVo, Integer lossType);
 }

+ 52 - 0
src/main/java/com/td/boss/game/complayerprofit/service/ComPlayerProfitServiceImpl.java

@@ -1,9 +1,13 @@
 package com.td.boss.game.complayerprofit.service;
 
 import com.td.boss.common.service.*;
+import com.td.boss.game.commallseed.pojo.ComMallSeed;
+import com.td.boss.game.commallseed.vo.ComMallSeedVo;
+import com.td.boss.game.complayerland.pojo.ComPlayerLand;
 import com.td.boss.game.complayerprofit.pojo.ComPlayerProfit;
 import com.td.boss.game.complayerprofit.vo.ComPlayerProfitVo;
 import com.td.boss.game.complayerprofit.repository.ComPlayerProfitRepository;
+import com.td.boss.util.CopyUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -20,6 +24,9 @@ public class ComPlayerProfitServiceImpl extends CommonServiceImpl<ComPlayerProfi
     @Autowired
     private ComPlayerProfitRepository comPlayerProfitRepository;
 
+    @Autowired
+    private ComPlayerProfitService comPlayerProfitService;
+
     @Override
     public List<ComPlayerProfit> findByUserIdAndPlantFlagAndLandId(String userId, String plantFlag, Integer landId) {
         return comPlayerProfitRepository.findByTargetIdAndPlantFlagAndLandId(userId,plantFlag,landId);
@@ -40,8 +47,53 @@ public class ComPlayerProfitServiceImpl extends CommonServiceImpl<ComPlayerProfi
         return comPlayerProfitRepository.findByUserIdAndTargetIdAndLandIdAndPlantFlag(userId,targetId,configLandId,plantFlag).orElse(null);
     }
 
+    @Override
+    public ComPlayerProfit findByPlantFlagAndLossType(String plantFlag, Integer lossType){
+       return  comPlayerProfitRepository.findByPlantFlagAndLossType(plantFlag, lossType).orElse(null);
+    }
+
     @Override
     public Double getStolenSumByOtherUserIdAndPlantFlag(String otherUserId, String plantFlag) {
         return comPlayerProfitRepository.getStolenSumByTargetIdAndPlantFlag(otherUserId,plantFlag);
     }
+
+
+    @Override
+    public ComPlayerProfitVo addDisasterAndLoss(ComPlayerLand comPlayerLand, ComMallSeedVo comMallSeedVo,Integer lossType) {
+
+
+        //计算一个偷取的收获量, 租赁倍数* 租赁日期下的产量
+        Integer _otherAmount = 0;
+        if (comPlayerLand.getLeaseDate().equals(1)) {
+            _otherAmount = comPlayerLand.getLeaseMultiple() * comMallSeedVo.getHarvest1();
+        } else if (comPlayerLand.getLeaseDate().equals(2)) {
+            _otherAmount = comPlayerLand.getLeaseMultiple() * comMallSeedVo.getHarvest2();
+        } else if (comPlayerLand.getLeaseDate().equals(3)) {
+            _otherAmount = comPlayerLand.getLeaseMultiple() * comMallSeedVo.getHarvest3();
+        }
+        //todo 拿出计算利润,收获量减去种子的成本(snb)后的百分之30 ,后面需要后台可调整
+        Integer _profit = _otherAmount - comPlayerLand.getLeaseMultiple() * comMallSeedVo.getPriceSnb();
+        //对应土地灾害损失
+        ComPlayerProfit comPlayerProfit = new ComPlayerProfit();
+        comPlayerProfit.setUserId(comPlayerLand.getUserId()); //损失记录的用户id
+        comPlayerProfit.setTargetId(comPlayerLand.getUserId()); //目标是自己
+        comPlayerProfit.setPlantFlag(comPlayerLand.getPlantFlag());
+        comPlayerProfit.setLandId(comPlayerLand.getConfigLandId());
+        comPlayerProfit.setLeaseMultiple(comPlayerLand.getLeaseMultiple());
+        comPlayerProfit.setLeaseDate(comPlayerLand.getLeaseDate());
+        comPlayerProfit.setHarvest(_otherAmount); // todo
+        comPlayerProfit.setProfit(_profit.doubleValue());//todo 目标用户可偷的初始利润值
+        comPlayerProfit.setStolen(0d);//todo 目标用户被灾害减产对应的数量
+        comPlayerProfit.setFinalSteal(0d);//
+        comPlayerProfit.setProfitAfter(0d); //可偷的减去被偷的
+        //记录相关比例
+        comPlayerProfit.setProfitRatio(0d);
+        comPlayerProfit.setStealRatio(0d);
+        comPlayerProfit.setFinalRatio(0d);
+        comPlayerProfit.setLossType(lossType);
+        ComPlayerProfitVo comPlayerProfitVo = CopyUtil.copy(comPlayerProfit, ComPlayerProfitVo.class);
+        comPlayerProfitService.save(comPlayerProfitVo);
+
+        return comPlayerProfitVo;
+    }
 }

+ 5 - 1
src/main/java/com/td/boss/game/complayerprofit/vo/ComPlayerProfitVo.java

@@ -1,6 +1,8 @@
 package com.td.boss.game.complayerprofit.vo;
 
-import com.td.boss. common.pojo.PageCondition;import lombok.Data;
+import com.td.boss.common.pojo.PageCondition;
+import lombok.Data;
+
 import java.io.Serializable;
 import java.util.Date;
 
@@ -36,6 +38,8 @@ public class ComPlayerProfitVo extends PageCondition implements Serializable {
 
     private Double finalRatio;//最后用户可收取的利润比例,目前是设置 10%
 
+    private Integer lossType;//损失的类型,默认是0:被偷取果实,1:自然灾难,2:野兽
+
     private Date createTime;//
 
     private Date updateTime;//

+ 24 - 0
src/main/java/com/td/boss/game/comsnbfreeze/controller/ComSnbFreezeController.java

@@ -1,5 +1,6 @@
 package com.td.boss.game.comsnbfreeze.controller;
 
+import cn.hutool.core.date.DateField;
 import com.td.boss.common.controller.*;
 import com.td.boss.common.pojo.Result;
 import com.td.boss.config.enums.ResultEnum;
@@ -16,7 +17,10 @@ import com.td.boss.game.complayergoods.service.ComPlayerGoodsService;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsSimpleVo;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
 import com.td.boss.game.complayerland.pojo.ComPlayerLand;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterProtectedService;
 import com.td.boss.game.complayerland.service.ComPlayerLandService;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
 import com.td.boss.game.complayerland.vo.ComPlayerLandSimpleVo;
 import com.td.boss.game.complayerland.vo.ComPlayerLandVo;
 import com.td.boss.game.comsnbfreeze.pojo.ComSnbFreeze;
@@ -69,6 +73,8 @@ public class ComSnbFreezeController extends CommonController<ComSnbFreezeVo, Com
     @Autowired
     private ComSnbTranService comSnbTranService;
 
+    @Autowired
+    private ComPlayerDisasterProtectedService comPlayerDisasterProtectedService;
 
     @Autowired
     private RedisLock redisLock;
@@ -451,8 +457,26 @@ public class ComSnbFreezeController extends CommonController<ComSnbFreezeVo, Com
 
             } else if (comCntOrderVo.getPayType().equals(2)) {
                 comCntOrderVo.setCntDescribe("自然灾害防护");
+                // todo 每购买一次防护,插入一条记录 ?
+                ComPlayerDisasterProtectedVo comPlayerDisasterProtected = new ComPlayerDisasterProtectedVo();
+                comPlayerDisasterProtected.setCreateTime(new Date());
+                comPlayerDisasterProtected.setUserId(id.toString());
+                comPlayerDisasterProtected.setDsasterType(ComPlayerDisasterEnum.ziran.getCode());
+                comPlayerDisasterProtected.setDsasterName(ComPlayerDisasterEnum.ziran.getMsg());
+                comPlayerDisasterProtected.setProtectTime(cn.hutool.core.date.DateUtil.date(new Date()).offset(DateField.DAY_OF_WEEK, 30));
+                comPlayerDisasterProtectedService.save(comPlayerDisasterProtected);
+
             } else if (comCntOrderVo.getPayType().equals(3)) {
                 comCntOrderVo.setCntDescribe("野兽防护");
+                // todo 每购买一次防护,插入一条记录 ?
+                ComPlayerDisasterProtectedVo comPlayerDisasterProtected = new ComPlayerDisasterProtectedVo();
+                comPlayerDisasterProtected = new ComPlayerDisasterProtectedVo();
+                comPlayerDisasterProtected.setCreateTime(new Date());
+                comPlayerDisasterProtected.setUserId(id.toString());
+                comPlayerDisasterProtected.setDsasterType(ComPlayerDisasterEnum.yeshou.getCode());
+                comPlayerDisasterProtected.setDsasterName(ComPlayerDisasterEnum.yeshou.getMsg());
+                comPlayerDisasterProtected.setProtectTime(cn.hutool.core.date.DateUtil.date(new Date()).offset(DateField.DAY_OF_WEEK, -30));
+                comPlayerDisasterProtectedService.save(comPlayerDisasterProtected);
             } else if (comCntOrderVo.getPayType().equals(4)) {
 
                 // 验证种子价格是否和服务器一样,不一样判定非法操作

+ 14 - 0
src/main/java/com/td/boss/util/RedisData.java

@@ -31,6 +31,20 @@ public class RedisData {
         return PLAYER_GOODS_TIMEOUT;
     }
 
+    /**
+     * 60s 第一次触发灾难锁
+     */
+    private static final int PLAYER_DISASTERS_FIRST_TIMEOUT = 60 * 1000; //超时时间 60s
+    public static int getPlayerDisastersFirstTimeout() {
+        return PLAYER_DISASTERS_FIRST_TIMEOUT;
+    }
+
+    private static final String PLAYER_DISASTERS_FIRST_KEY = "DISASTERS_FIRST";
+    public static String getPlayerDisastersFirstKey() {
+        return PLAYER_DISASTERS_FIRST_KEY;
+    }
+
+
     /**
      * 1h 灾难模块锁
      */