Ver código fonte

模块4:自然灾害与防护包功能模块

xst 4 anos atrás
pai
commit
4235dfbc67
17 arquivos alterados com 455 adições e 233 exclusões
  1. 0 10
      src/main/java/com/td/boss/game/complayerdog/repository/ComPlayerDisasterRepository.java
  2. 0 9
      src/main/java/com/td/boss/game/complayerdog/service/ComPlayerDisasterService.java
  3. 0 158
      src/main/java/com/td/boss/game/complayerdog/service/ComPlayerDisasterServiceImpl.java
  4. 6 12
      src/main/java/com/td/boss/game/complayerland/controller/ComPlayerLandController.java
  5. 7 3
      src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisaster.java
  6. 31 0
      src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterProtected.java
  7. 13 0
      src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterProtectedRepository.java
  8. 15 0
      src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterRepository.java
  9. 12 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterProtectedService.java
  10. 22 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterProtectedServiceImpl.java
  11. 24 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterService.java
  12. 208 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterServiceImpl.java
  13. 18 0
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterEnum.java
  14. 26 0
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterProtectedVo.java
  15. 15 13
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterVo.java
  16. 0 28
      src/test/java/com/td/boss/BossApplicationTests.java
  17. 58 0
      src/test/java/com/td/boss/ComPlayerDisasterTests.java

+ 0 - 10
src/main/java/com/td/boss/game/complayerdog/repository/ComPlayerDisasterRepository.java

@@ -1,10 +0,0 @@
-package com.td.boss.game.complayerdog.repository;
-
-import com.td.boss.common.repository.CommonRepository;
-import com.td.boss.game.complayerdog.pojo.Disaster;
-import org.springframework.stereotype.Repository;
-
-@Repository
-public interface ComPlayerDisasterRepository extends CommonRepository<Disaster, String> {
-
-}

+ 0 - 9
src/main/java/com/td/boss/game/complayerdog/service/ComPlayerDisasterService.java

@@ -1,9 +0,0 @@
-package com.td.boss.game.complayerdog.service;
-
-import com.td.boss.common.service.CommonService;
-import com.td.boss.game.complayerdog.pojo.Disaster;
-import com.td.boss.game.complayerdog.vo.DisasterVo;
-
-public interface ComPlayerDisasterService  extends CommonService<DisasterVo, Disaster, String> {
-      DisasterVo getAttack(String userId);
-}

+ 0 - 158
src/main/java/com/td/boss/game/complayerdog/service/ComPlayerDisasterServiceImpl.java

@@ -1,158 +0,0 @@
-package com.td.boss.game.complayerdog.service;
-
-import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.convert.Convert;
-import cn.hutool.core.date.DateTime;
-import cn.hutool.core.date.DateUtil;
-import cn.hutool.core.util.RandomUtil;
-import cn.hutool.json.JSONUtil;
-import com.td.boss.common.pojo.Result;
-import com.td.boss.common.service.CommonServiceImpl;
-import com.td.boss.game.complayerdog.pojo.Disaster;
-import com.td.boss.game.complayerdog.repository.ComPlayerDisasterRepository;
-import com.td.boss.game.complayerdog.vo.DisasterVo;
-import com.td.boss.util.DappUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.jpa.domain.Specification;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.Map;
-
-@Slf4j
-@Service
-@Transactional
-public class ComPlayerDisasterServiceImpl extends CommonServiceImpl<DisasterVo, Disaster, String> implements ComPlayerDisasterService {
-    @Autowired
-    private ComPlayerDisasterRepository comPlayerDisasterRepository;
-
-    @Override
-    public DisasterVo getAttack(String userId) {
-        //todo 比如1个月不上线怎么办
-        //todo 防护模式未实现
-        log.info("灾难模式");
-        DisasterVo disasterVo = null;
-        //如果本周内还有灾难
-        if (getDisasterNum(userId) > 0) {
-            //随机获取一种灾难方式灾难对象
-            disasterVo = getDisasterList(userId).get(RandomUtil.randomInt(0, 1));
-            //查看该灾难是否有防护,如果有防护需要按照防护几率计算是否防御住灾害了
-            if (disasterVo.getIsBuyProtected()) {
-                double v = RandomUtil.randomDouble(0, 1);
-                log.info("灾难几率:{}", v);
-                if (v > Convert.toDouble("0.1")) {
-                    disasterVo.setActivityProtect(true);
-                } else {
-                    disasterVo.setActivityProtect(false);
-                }
-            }
-            log.info("灾难结果:{}", JSONUtil.toJsonStr(disasterVo));
-            Disaster disaster = new Disaster();
-            BeanUtil.copyProperties(disasterVo, disaster);
-            //入库
-            //todo 应该还有个减产的接口
-            comPlayerDisasterRepository.save(disaster);
-        }
-        log.info("灾难结果:{}", "本周无灾难了");
-        return disasterVo;
-    }
-
-    private List<DisasterVo> getDisasterList(String userId) {
-        //todo 这里需要优化使用枚举获取
-        //查询有效防护时间
-        Date ziran_protected = cn.hutool.core.date.DateUtil.parse("2022-03-30");
-        Date yeshow_protected = cn.hutool.core.date.DateUtil.parse("2022-03-30");
-
-        List<DisasterVo> disasterList = new ArrayList<>();
-        disasterList.add(new DisasterVo("自然灾害", ziran_protected));
-        disasterList.add(new DisasterVo("野兽", yeshow_protected));
-
-        disasterList.forEach(a -> {
-            a.setDamage(50);
-            a.setDefend(90);
-            a.setUserId(userId);
-            a.setCreateTime(new Date());
-        });
-
-        log.info("灾难列表:{}", JSONUtil.toJsonStr(disasterList));
-        return disasterList;
-    }
-
-    /**
-     * 获得本周剩余灾难数量
-     *
-     * @param userId
-     * @return
-     */
-    private int getDisasterNum(String userId) {
-        //读取接口 获取 那种情况 这个接口实际是返回的上周数据。所以什么时候啦都不会变。
-        //本周开始时间为周日到周六,期间获取的不会变化,这周获取的是上周的数据
-        Map<String, Object> cntPayAndSwapAmount = DappUtil.getCntPayAndSwapAmount(userId);
-        log.info("接口返回支付兑换结果:{}", JSONUtil.toJsonStr(cntPayAndSwapAmount));
-        // 总支付cnt
-        Double pay_amount = Convert.toDouble(cntPayAndSwapAmount.get("pay_amount"));
-        // 总兑换cnt
-        Double swap_amount = Convert.toDouble(cntPayAndSwapAmount.get("swap_amount"));
-
-        //本周内已出现灾难次数
-        long countByWeek = getCountByWeek(userId);
-        //本周内应该出现的灾难次数
-        int disasterTimes = getDisasterTimes(pay_amount, swap_amount);
-        log.info("本周内应该出现的灾难次数:{},本周内已出现灾难次数:{}", disasterTimes, countByWeek);
-        return disasterTimes - Convert.toInt(countByWeek);
-    }
-
-    /**
-     * 本周内已出现灾难次数
-     *
-     * @param userId
-     * @return
-     */
-    public long getCountByWeek(String userId) {
-        Date now = new Date();
-        DateTime begin = DateUtil.beginOfWeek(now);
-        DateTime end = DateUtil.endOfWeek(now);
-        Specification querySpecifi = new Specification<Disaster>() {
-            @Override
-            public Predicate toPredicate(Root<Disaster> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
-                List<Predicate> predicates = new ArrayList<>();
-                predicates.add(criteriaBuilder.between(root.get("createTime"), begin, end));
-                predicates.add(criteriaBuilder.equal(root.get("userId"), userId));
-                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
-            }
-        };
-        long count = comPlayerDisasterRepository.count(querySpecifi);
-        log.info("count:{}", count);
-        return count;
-    }
-
-    /**
-     * a)当某个村长所属农场的支出小于等于收入30%时,按照正常频率,每周出现1次
-     * b)当某个村长所属农场的支出大于收入30%时,每周出现2次
-     * c)当某个村长所属农场的支出大于收入50%时,每周出现4次
-     * d)当某个村长所属农场的支出大于收入80%时,每周出现6次
-     * 支出对应pay_amount,收入对应swap_amount
-     *
-     * @param pay_amount  支出对应
-     * @param swap_amount 收入对应
-     */
-    private int getDisasterTimes(Double pay_amount, Double swap_amount) {
-        if (pay_amount > swap_amount * 0.8) {
-            return 6;
-        } else if (pay_amount > swap_amount * 0.5) {
-            return 4;
-        } else if (pay_amount > swap_amount * 0.3) {
-            return 2;
-        } else {
-            return 1;
-        }
-    }
-}

+ 6 - 12
src/main/java/com/td/boss/game/complayerland/controller/ComPlayerLandController.java

@@ -1,11 +1,5 @@
 package com.td.boss.game.complayerland.controller;
 
-import cn.hutool.core.bean.BeanUtil;
-import cn.hutool.core.convert.Convert;
-import cn.hutool.core.date.DateTime;
-import cn.hutool.core.util.RandomUtil;
-import cn.hutool.json.JSON;
-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;
@@ -13,8 +7,9 @@ import com.td.boss.game.comexplainland.service.ComExplainLandService;
 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.complayerdog.service.ComPlayerDisasterService;
-import com.td.boss.game.complayerdog.vo.DisasterVo;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterService;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterVo;
 import com.td.boss.game.complayergoods.pojo.ComPlayerGoods;
 import com.td.boss.game.complayergoods.service.ComPlayerGoodsService;
 import com.td.boss.game.complayergoods.vo.ComPlayerGoodsVo;
@@ -29,7 +24,6 @@ import com.td.boss.game.complayerlog.service.ComPlayerLogService;
 import com.td.boss.game.complayerlog.vo.ComPlayerLogVo;
 import com.td.boss.util.*;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.RandomUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Sort;
@@ -452,8 +446,8 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
      */
     @GetMapping("getAttack")
     @Transactional(rollbackFor = Exception.class)
-    public Result<DisasterVo> getAttack(@RequestParam(value = "userId") String userId) {
-        DisasterVo attack = comPlayerDisasterService.getAttack(userId);
-        return Result.of(attack, attack != null);
+    public Result<List<ComPlayerDisaster>> getAttack(@RequestParam(value = "userId") String userId) {
+        List<ComPlayerDisaster> history = comPlayerDisasterService.getHistory(userId);
+        return Result.of(history);
     }
 }

+ 7 - 3
src/main/java/com/td/boss/game/complayerdog/pojo/Disaster.java → src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisaster.java

@@ -1,4 +1,4 @@
-package com.td.boss.game.complayerdog.pojo;
+package com.td.boss.game.complayerland.pojo;
 
 import lombok.Data;
 
@@ -12,7 +12,7 @@ import java.util.Date;
 @Entity
 @Table(name = "com_player_dsaster")
 @Data
-public class Disaster implements Serializable {
+public class ComPlayerDisaster implements Serializable {
     @Id
     @GeneratedValue(strategy= GenerationType.IDENTITY)
     private long id;
@@ -34,7 +34,7 @@ public class Disaster implements Serializable {
     /**
      * 防护截止时间
      */
-    private Date buyProtectedTime;
+    private Date protectTime;
 
     /**
      * 本次灾难是否激活防护
@@ -43,4 +43,8 @@ public class Disaster implements Serializable {
     private String userId;
     private Date createTime;
     private Date updateTime;
+    /**
+     * 前端 是否以显示
+     */
+    private boolean isShow;
 }

+ 31 - 0
src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterProtected.java

@@ -0,0 +1,31 @@
+package com.td.boss.game.complayerland.pojo;
+
+import lombok.Data;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.util.Date;
+
+@Entity
+@Table(name = "com_player_dsaster_protected")
+@Data
+public class ComPlayerDisasterProtected implements Serializable {
+    @Id
+    @GeneratedValue(strategy= GenerationType.IDENTITY)
+    private long id;
+    /**
+     * 灾难名称
+     */
+    private String name;
+
+    private Integer type;
+
+    /**
+     * 保护时间
+     */
+    private Date protectTime;
+    private String userId;
+    private Date createTime;
+    private Date updateTime;
+    private String orderId;
+}

+ 13 - 0
src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterProtectedRepository.java

@@ -0,0 +1,13 @@
+package com.td.boss.game.complayerland.repository;
+
+import com.td.boss.common.repository.CommonRepository;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface ComPlayerDisasterProtectedRepository extends CommonRepository<ComPlayerDisasterProtected, String> {
+
+    public List<ComPlayerDisasterProtected> getComPlayerDisasterProtectedsByUserId(String userId);
+}

+ 15 - 0
src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterRepository.java

@@ -0,0 +1,15 @@
+package com.td.boss.game.complayerland.repository;
+
+import com.td.boss.common.repository.CommonRepository;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public interface ComPlayerDisasterRepository extends CommonRepository<ComPlayerDisaster, String> {
+
+    @Query(value = "select * from com_player_dsaster where user_id = ?1 and `is_show`=0",nativeQuery=true)
+    public List<ComPlayerDisaster> getUnShow(String userId);
+}

+ 12 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterProtectedService.java

@@ -0,0 +1,12 @@
+package com.td.boss.game.complayerland.service;
+
+import com.td.boss.common.service.CommonService;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
+
+import java.util.List;
+
+public interface ComPlayerDisasterProtectedService extends CommonService<ComPlayerDisasterProtectedVo, ComPlayerDisasterProtected, String> {
+
+    public List<ComPlayerDisasterProtected> getComPlayerDisasterProtectedByUserIdOrderByProtectTimeDesc(String userId);
+}

+ 22 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterProtectedServiceImpl.java

@@ -0,0 +1,22 @@
+package com.td.boss.game.complayerland.service;
+
+import com.td.boss.common.service.CommonServiceImpl;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import com.td.boss.game.complayerland.repository.ComPlayerDisasterProtectedRepository;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Slf4j
+@Service
+public class ComPlayerDisasterProtectedServiceImpl extends CommonServiceImpl<ComPlayerDisasterProtectedVo, ComPlayerDisasterProtected, String> implements ComPlayerDisasterProtectedService {
+    @Autowired
+    private ComPlayerDisasterProtectedRepository repository;
+
+    public List<ComPlayerDisasterProtected> getComPlayerDisasterProtectedByUserIdOrderByProtectTimeDesc(String userId) {
+        return repository.getComPlayerDisasterProtectedsByUserId(userId);
+    }
+}

+ 24 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterService.java

@@ -0,0 +1,24 @@
+package com.td.boss.game.complayerland.service;
+
+import com.td.boss.common.service.CommonService;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterVo;
+
+import java.util.List;
+
+public interface ComPlayerDisasterService extends CommonService<ComPlayerDisasterVo, ComPlayerDisaster, String> {
+    /**
+     * 灾难模块核心功能
+     * @param userId
+     * @return
+     */
+    ComPlayerDisasterVo getDisaster(String userId);
+
+    /**
+     * 获取经历的灾难记录
+     *
+     * @param userId
+     * @return
+     */
+    List<ComPlayerDisaster> getHistory(String userId);
+}

+ 208 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterServiceImpl.java

@@ -0,0 +1,208 @@
+package com.td.boss.game.complayerland.service;
+
+import cn.hutool.core.convert.Convert;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.EnumUtil;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.json.JSONUtil;
+import com.td.boss.common.service.CommonServiceImpl;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import com.td.boss.game.complayerland.repository.ComPlayerDisasterRepository;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterVo;
+import com.td.boss.util.DappUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Slf4j
+@Service
+@Transactional
+public class ComPlayerDisasterServiceImpl extends CommonServiceImpl<ComPlayerDisasterVo, ComPlayerDisaster, String> implements ComPlayerDisasterService {
+    @Autowired
+    private ComPlayerDisasterRepository comPlayerDisasterRepository;
+
+    @Autowired
+    private ComPlayerDisasterProtectedService comPlayerDisasterProtectedService;
+
+    /**
+     * 伤害 会减产50%
+     */
+    private int damage = 50;
+
+    /**
+     * 防御 90%概率防御灾难
+     */
+    private double defend = 90;
+
+    /**
+     * 获取经历的灾难记录
+     * @param userId
+     * @return
+     */
+    @Override
+    public List<ComPlayerDisaster> getHistory(String userId){
+        log.info("获取灾难记录:{}",userId);
+        List<ComPlayerDisaster> list=comPlayerDisasterRepository.getUnShow(userId).stream().sorted(Comparator.comparing(ComPlayerDisaster::getCreateTime)).collect(Collectors.toList());
+        log.info("获取灾难记录:{}",JSONUtil.toJsonStr(list));
+        list.forEach(a->a.setShow(true));
+        comPlayerDisasterRepository.saveAll(list);
+        return list;
+    }
+
+    /**
+     * 灾难模块核心功能
+     * @param userId
+     * @return
+     */
+    @Override
+    public ComPlayerDisasterVo getDisaster(String userId) {
+        //todo 需要有个定时任务去处理,定期拉取所有人,然后执行
+        log.info("灾难模式:{}",userId);
+        ComPlayerDisasterVo comPlayerDisasterVo = null;
+        //如果本周内还有灾难
+        if (getDisasterNum(userId) > 0) {
+            //随机获取一种灾难方式灾难对象
+            comPlayerDisasterVo = getDisasterList(userId).get(RandomUtil.randomInt(0, 2));
+            //查看该灾难是否有防护,如果有防护需要按照防护几率计算是否防御住灾害了
+            if (comPlayerDisasterVo.getIsBuyProtected()) {
+                int randomProtect = RandomUtil.randomInt(0, 101);
+                log.info("灾难几率:{}", randomProtect);
+                if (randomProtect > (100 - defend)) {
+                    comPlayerDisasterVo.setActivityProtect(true);
+                } else {
+                    comPlayerDisasterVo.setActivityProtect(false);
+                }
+            }
+            log.info("灾难结果:{}", JSONUtil.toJsonStr(comPlayerDisasterVo));
+            this.save(comPlayerDisasterVo);
+
+            //todo 应该还有个减产的接口,此处但是先不做
+            if (!comPlayerDisasterVo.isActivityProtect()) {
+
+            }
+        } else {
+            log.info("灾难结果:{}", "本周无灾难了");
+        }
+        return comPlayerDisasterVo;
+    }
+
+    /**
+     * 获取灾难防护列表
+     *
+     * @param userId
+     * @return
+     */
+    private List<ComPlayerDisasterVo> getDisasterList(String userId) {
+        List<ComPlayerDisasterVo> disasterList = new ArrayList<>();
+        //查询当前人员购买防护历史记录
+        ComPlayerDisasterProtectedVo comPlayerDisasterProtectedVo = new ComPlayerDisasterProtectedVo();
+        comPlayerDisasterProtectedVo.setUserId(userId);
+        List<ComPlayerDisasterProtected> protectList = comPlayerDisasterProtectedService.getComPlayerDisasterProtectedByUserIdOrderByProtectTimeDesc(userId);
+        log.info("防护历史记录:{}", JSONUtil.toJsonStr(protectList));
+
+        //获得枚举列表
+        LinkedHashMap<String, ComPlayerDisasterEnum> enumMap = EnumUtil.getEnumMap(ComPlayerDisasterEnum.class);
+        for (String key : enumMap.keySet()) {
+            Date protectedDate = protectList.stream()
+                    .filter(a -> enumMap.get(key).getCode().equals(a.getType()))
+                    .sorted(Comparator.comparing(ComPlayerDisasterProtected::getProtectTime).reversed())
+                    .map(ComPlayerDisasterProtected::getProtectTime)
+                    .findFirst().orElse(DateUtil.parse(null));
+            ComPlayerDisasterVo comPlayerDisasterVo = new ComPlayerDisasterVo();
+            comPlayerDisasterVo.setName(enumMap.get(key).getMsg());
+            comPlayerDisasterVo.setType(enumMap.get(key).getCode());
+            comPlayerDisasterVo.setProtectTime(protectedDate);
+            comPlayerDisasterVo.setUserId(userId);
+            comPlayerDisasterVo.setDamage(damage);
+            comPlayerDisasterVo.setDefend(defend);
+            comPlayerDisasterVo.setCreateTime(new Date());
+            comPlayerDisasterVo.setShow(false);
+            disasterList.add(comPlayerDisasterVo);
+        }
+        log.info("灾难列表:{}", JSONUtil.toJsonStr(disasterList));
+        return disasterList;
+    }
+
+    /**
+     * 获得本周剩余灾难数量
+     *
+     * @param userId
+     * @return
+     */
+    private int getDisasterNum(String userId) {
+        //读取接口 获取 那种情况 这个接口实际是返回的上周数据。所以什么时候啦都不会变。
+        //本周开始时间为周日到周六,期间获取的不会变化,这周获取的是上周的数据
+        Map<String, Object> cntPayAndSwapAmount = DappUtil.getCntPayAndSwapAmount(userId);
+        log.info("接口返回支付兑换结果:{}", JSONUtil.toJsonStr(cntPayAndSwapAmount));
+        // 总支付cnt
+        Double pay_amount = Convert.toDouble(cntPayAndSwapAmount.get("pay_amount"));
+        // 总兑换cnt
+        Double swap_amount = Convert.toDouble(cntPayAndSwapAmount.get("swap_amount"));
+
+        //本周内已出现灾难次数
+        long countByWeek = getCountByWeek(userId);
+        //本周内应该出现的灾难次数
+        int disasterTimes = getDisasterTimes(pay_amount, swap_amount);
+        log.info("本周内应该出现的灾难次数:{},本周内已出现灾难次数:{}", disasterTimes, countByWeek);
+        return disasterTimes - Convert.toInt(countByWeek);
+    }
+
+    /**
+     * 本周内已出现灾难次数
+     *
+     * @param userId
+     * @return
+     */
+    public long getCountByWeek(String userId) {
+        Date now = new Date();
+        DateTime begin = DateUtil.beginOfWeek(now);
+        DateTime end = DateUtil.endOfWeek(now);
+        Specification querySpecifi = new Specification<ComPlayerDisaster>() {
+            @Override
+            public Predicate toPredicate(Root<ComPlayerDisaster> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
+                List<Predicate> predicates = new ArrayList<>();
+                predicates.add(criteriaBuilder.between(root.get("createTime"), begin, end));
+                predicates.add(criteriaBuilder.equal(root.get("userId"), userId));
+                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
+            }
+        };
+        long count = comPlayerDisasterRepository.count(querySpecifi);
+        log.info("count:{}", count);
+        return count;
+    }
+
+    /**
+     * a)当某个村长所属农场的支出小于等于收入30%时,按照正常频率,每周出现1次
+     * b)当某个村长所属农场的支出大于收入30%时,每周出现2次
+     * c)当某个村长所属农场的支出大于收入50%时,每周出现4次
+     * d)当某个村长所属农场的支出大于收入80%时,每周出现6次
+     * 支出对应pay_amount,收入对应swap_amount
+     *
+     * @param pay_amount  支出对应
+     * @param swap_amount 收入对应
+     */
+    private int getDisasterTimes(Double pay_amount, Double swap_amount) {
+        if (pay_amount > swap_amount * 0.8) {
+            return 6;
+        } else if (pay_amount > swap_amount * 0.5) {
+            return 4;
+        } else if (pay_amount > swap_amount * 0.3) {
+            return 2;
+        } else {
+            return 1;
+        }
+    }
+}

+ 18 - 0
src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterEnum.java

@@ -0,0 +1,18 @@
+package com.td.boss.game.complayerland.vo;
+
+import lombok.Data;
+import lombok.Getter;
+
+@Getter
+public enum ComPlayerDisasterEnum {
+    ziran(2, "自然灾害"),
+    yeshou(3, "野兽");
+
+    ComPlayerDisasterEnum(Integer code, String msg) {
+        this.code = code;
+        this.msg = msg;
+    }
+
+    private Integer code;
+    private String msg;
+}

+ 26 - 0
src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterProtectedVo.java

@@ -0,0 +1,26 @@
+package com.td.boss.game.complayerland.vo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 灾难防护
+ */
+@Data
+public class ComPlayerDisasterProtectedVo implements Serializable {
+    /**
+     * 灾难名称
+     */
+    private String name;
+    private Integer type;
+    /**
+     * 防护截止时间
+     */
+    private Date protectTime;
+
+    private String userId;
+
+    private Date createTime;
+}

+ 15 - 13
src/main/java/com/td/boss/game/complayerdog/vo/DisasterVo.java → src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterVo.java

@@ -1,4 +1,4 @@
-package com.td.boss.game.complayerdog.vo;
+package com.td.boss.game.complayerland.vo;
 
 import lombok.Data;
 
@@ -9,19 +9,12 @@ import java.util.Date;
  * 灾难
  */
 @Data
-public class DisasterVo implements Serializable {
-    public DisasterVo() {
-    }
-
-    public DisasterVo(String name,Date buyProtectedTime) {
-        this.name = name;
-        this.buyProtectedTime=buyProtectedTime;
-    }
-
+public class ComPlayerDisasterVo implements Serializable {
     /**
      * 灾难名称
      */
     private String name;
+    private Integer type;
     /**
      * 伤害 会减产50%
      */
@@ -30,12 +23,12 @@ public class DisasterVo implements Serializable {
     /**
      * 防御 90%概率防御灾难
      */
-    private int defend;
+    private double defend;
 
     /**
      * 防护截止时间
      */
-    private Date buyProtectedTime;
+    private Date protectTime;
 
     /**
      * 本次灾难是否激活防护
@@ -45,12 +38,21 @@ public class DisasterVo implements Serializable {
     private String userId;
 
     private Date createTime;
+
+    /**
+     * 前端 是否以显示
+     */
+    private boolean isShow;
+
     /**
      * 是否购买防护并且没到期
      *
      * @return
      */
     public boolean getIsBuyProtected() {
-        return new Date().before(buyProtectedTime);
+        if (protectTime == null) {
+            return false;
+        }
+        return new Date().before(protectTime);
     }
 }

+ 0 - 28
src/test/java/com/td/boss/BossApplicationTests.java

@@ -1,28 +0,0 @@
-package com.td.boss;
-
-import com.td.boss.common.pojo.Result;
-import com.td.boss.game.complayerdog.vo.DisasterVo;
-import com.td.boss.game.complayerland.controller.ComPlayerLandController;
-import org.junit.jupiter.api.Test;
-import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.junit4.SpringRunner;
-
-@RunWith(SpringRunner.class)
-@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-class BossApplicationTests {
-
-    @Autowired
-    private ComPlayerLandController comPlayerLandController;
-
-    @Test
-    void contextLoads() {
-
-    }
-
-    @Test
-    void attack(){
-        Result<DisasterVo> attack = comPlayerLandController.getAttack("1002");
-    }
-}

+ 58 - 0
src/test/java/com/td/boss/ComPlayerDisasterTests.java

@@ -0,0 +1,58 @@
+package com.td.boss;
+
+import cn.hutool.core.date.DateUtil;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterProtectedService;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterService;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
+import org.junit.jupiter.api.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+import java.util.Date;
+import java.util.List;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+class ComPlayerDisasterTests {
+
+    @Autowired
+    private ComPlayerDisasterService comPlayerDisasterService;
+    @Autowired
+    private ComPlayerDisasterProtectedService comPlayerDisasterProtectedService;
+
+    private String userId = "1002";
+
+    /**
+     * 防护灾难
+     */
+    @Test
+    void playerAddProtected() {
+        ComPlayerDisasterProtectedVo comPlayerDisasterProtected = new ComPlayerDisasterProtectedVo();
+        comPlayerDisasterProtected.setCreateTime(new Date());
+        comPlayerDisasterProtected.setUserId(userId);
+        comPlayerDisasterProtected.setType(ComPlayerDisasterEnum.ziran.getCode());
+        comPlayerDisasterProtected.setName(ComPlayerDisasterEnum.ziran.getMsg());
+        comPlayerDisasterProtected.setProtectTime(DateUtil.parse("2022-03-30 00:00:00"));
+        comPlayerDisasterProtectedService.save(comPlayerDisasterProtected);
+    }
+
+    /**
+     * 获取灾难经历
+     */
+    @Test
+    void getHistory() {
+        List<ComPlayerDisaster> history = comPlayerDisasterService.getHistory(userId);
+        System.out.println(history);
+    }
+
+    /**
+     * 测试灾难模式
+     */
+    @Test
+    void getDisaster(){
+        comPlayerDisasterService.getDisaster(userId);
+    }
+}