Browse Source

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

xst 4 years ago
parent
commit
f4cae1bef9
20 changed files with 480 additions and 303 deletions
  1. 5 6
      src/main/java/com/td/boss/game/complayerland/controller/ComPlayerLandController.java
  2. 3 32
      src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisaster.java
  3. 60 0
      src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterLog.java
  4. 2 3
      src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterProtected.java
  5. 23 0
      src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterLogRepository.java
  6. 0 7
      src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterRepository.java
  7. 3 3
      src/main/java/com/td/boss/game/complayerland/scheduled/ComPlayerDisasterTask.java
  8. 26 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterLogService.java
  9. 217 0
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterLogServiceImpl.java
  10. 4 12
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterService.java
  11. 23 178
      src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterServiceImpl.java
  12. 58 0
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterLogVo.java
  13. 2 2
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterProtectedVo.java
  14. 5 48
      src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterVo.java
  15. 5 0
      src/main/java/com/td/boss/game/complayers/repository/ComPlayersRepository.java
  16. 3 1
      src/main/java/com/td/boss/game/complayers/service/ComPlayersService.java
  17. 9 2
      src/main/java/com/td/boss/game/complayers/service/ComPlayersServiceImpl.java
  18. 14 0
      src/main/resources/application.yml
  19. 5 0
      src/main/resources/logback-spring.xml
  20. 13 9
      src/test/java/com/td/boss/ComPlayerDisasterLogTests.java

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

@@ -7,9 +7,8 @@ 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.complayerland.pojo.ComPlayerDisaster;
-import com.td.boss.game.complayerland.service.ComPlayerDisasterService;
-import com.td.boss.game.complayerland.vo.ComPlayerDisasterVo;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
+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;
@@ -55,7 +54,7 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
     private ComExplainLandService comExplainLandService;
 
     @Autowired
-    private ComPlayerDisasterService comPlayerDisasterService;
+    private ComPlayerDisasterLogService comPlayerDisasterLogService;
 
 
     /**
@@ -446,8 +445,8 @@ public class ComPlayerLandController extends CommonController<ComPlayerLandVo, C
      */
     @GetMapping("getAttack")
     @Transactional(rollbackFor = Exception.class)
-    public Result<List<ComPlayerDisaster>> getAttack(@RequestParam(value = "userId") String userId) {
-        List<ComPlayerDisaster> history = comPlayerDisasterService.getHistory(userId);
+    public Result<List<ComPlayerDisasterLog>> getAttack(@RequestParam(value = "userId") String userId) {
+        List<ComPlayerDisasterLog> history = comPlayerDisasterLogService.getHistory(userId);
         return Result.of(history);
     }
 }

+ 3 - 32
src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisaster.java

@@ -6,9 +6,6 @@ import javax.persistence.*;
 import java.io.Serializable;
 import java.util.Date;
 
-/**
- * 自然灾害与防护包功能模块
- */
 @Entity
 @Table(name = "com_player_dsaster")
 @Data
@@ -16,35 +13,9 @@ public class ComPlayerDisaster implements Serializable {
     @Id
     @GeneratedValue(strategy= GenerationType.IDENTITY)
     private long id;
-    /**
-     * 灾难名称
-     */
-    private String name;
-    private Integer type;
-    /**
-     * 伤害 会减产50%
-     */
-    private int damage;
-
-    /**
-     * 防御 90%概率防御灾难
-     */
-    private double defend;
-
-    /**
-     * 防护截止时间
-     */
-    private Date protectTime;
-
-    /**
-     * 本次灾难是否激活防护
-     */
-    private boolean activityProtect;
+    private Double income;
+    private Double payment;
+    private String log;
     private String userId;
     private Date createTime;
-    private Date updateTime;
-    /**
-     * 前端 是否以显示
-     */
-    private boolean isShow;
 }

+ 60 - 0
src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterLog.java

@@ -0,0 +1,60 @@
+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_log")
+@Data
+public class ComPlayerDisasterLog implements Serializable {
+    @Id
+    @GeneratedValue(strategy= GenerationType.IDENTITY)
+    private long id;
+
+    /**
+     * 主表id
+     */
+    private long dsasterId;
+    /**
+     * 灾难名称
+     */
+    private String dsasterName;
+    private Integer dsasterType;
+    /**
+     * 伤害 会减产50%
+     */
+    private int damage;
+
+    /**
+     * 防御 90%概率防御灾难
+     */
+    private double defend;
+
+    /**
+     * 防护截止时间
+     */
+    private Date protectTime;
+
+    /**
+     * 本次灾难是否激活防护
+     */
+    private boolean isProtect;
+    private String userId;
+    private Date createTime;
+    private Date updateTime;
+    /**
+     * 前端 是否以显示
+     */
+    private boolean isShow;
+    /**
+     * 是否已经使用,灾害是预先生成的
+     */
+    private boolean isEnabled;
+    private Date disasterTime;
+}

+ 2 - 3
src/main/java/com/td/boss/game/complayerland/pojo/ComPlayerDisasterProtected.java

@@ -16,9 +16,8 @@ public class ComPlayerDisasterProtected implements Serializable {
     /**
      * 灾难名称
      */
-    private String name;
-
-    private Integer type;
+    private String dsasterName;
+    private Integer dsasterType;
 
     /**
      * 保护时间

+ 23 - 0
src/main/java/com/td/boss/game/complayerland/repository/ComPlayerDisasterLogRepository.java

@@ -0,0 +1,23 @@
+package com.td.boss.game.complayerland.repository;
+
+import com.td.boss.common.repository.CommonRepository;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+import org.springframework.stereotype.Repository;
+
+import java.util.Date;
+import java.util.List;
+
+@Repository
+public interface ComPlayerDisasterLogRepository extends CommonRepository<ComPlayerDisasterLog, String> {
+
+    @Query(value = "select * from com_player_dsaster_log where user_id = ?1 and `is_show`=0",nativeQuery=true)
+    List<ComPlayerDisasterLog> getUnShow(String userId);
+//
+    //todo sql不对
+    @Query(value = "select * from com_player_dsaster_log where  `is_enabled`=0 and disaster_time between :beginTime and :endTime" ,nativeQuery=true)
+    List<ComPlayerDisasterLog> getUnEnabled(@Param("beginTime") Date beginTime,@Param("endTime") Date endTime);
+
+    ComPlayerDisasterLog findByUserId(String userId);
+}

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

@@ -2,16 +2,9 @@ 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);
-
-    public ComPlayerDisaster findByUserId(String userId);
 }

+ 3 - 3
src/main/java/com/td/boss/game/complayerland/scheduled/ComPlayerDisasterTask.java

@@ -1,7 +1,7 @@
 package com.td.boss.game.complayerland.scheduled;
 
 import cn.hutool.core.date.DateUtil;
-import com.td.boss.game.complayerland.service.ComPlayerDisasterService;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterLogService;
 import org.quartz.JobExecutionContext;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -12,11 +12,11 @@ import org.springframework.scheduling.quartz.QuartzJobBean;
 public class ComPlayerDisasterTask extends QuartzJobBean {
 
     @Autowired
-    private ComPlayerDisasterService comPlayerDisasterService;
+    private ComPlayerDisasterLogService comPlayerDisasterLogService;
 
     @Override
     protected void executeInternal(JobExecutionContext jobExecutionContext) {
-        comPlayerDisasterService.getDisaster("1002");
+//        comPlayerDisasterLogService.getDisaster("1002");
         System.out.println("测试:" + DateUtil.date());
     }
 }

+ 26 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterLogService.java

@@ -0,0 +1,26 @@
+package com.td.boss.game.complayerland.service;
+
+import com.td.boss.common.service.CommonService;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterLogVo;
+
+import java.util.List;
+
+public interface ComPlayerDisasterLogService extends CommonService<ComPlayerDisasterLogVo, ComPlayerDisasterLog, String> {
+
+    /**
+     * 灾难模式周任务:同步每个用户上周收入支出
+     */
+    void ComPlayerDisasterWeekTask();
+    /**
+     * 灾难模式天任务:触发所有用户今日灾难和保护机制
+     */
+    void ComPlayerDisasterDayTask();
+    /**
+     * 获取经历的灾难记录
+     *
+     * @param userId
+     * @return
+     */
+    List<ComPlayerDisasterLog> getHistory(String userId);
+}

+ 217 - 0
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterLogServiceImpl.java

@@ -0,0 +1,217 @@
+package com.td.boss.game.complayerland.service;
+
+import cn.hutool.core.convert.Convert;
+import cn.hutool.core.date.DateField;
+import cn.hutool.core.date.DateRange;
+import cn.hutool.core.date.DateTime;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.ArrayUtil;
+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.ComPlayerDisasterLog;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterProtected;
+import com.td.boss.game.complayerland.repository.ComPlayerDisasterLogRepository;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
+import com.td.boss.game.complayerland.vo.ComPlayerDisasterLogVo;
+import com.td.boss.game.complayers.service.ComPlayersService;
+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 ComPlayerDisasterLogServiceImpl extends CommonServiceImpl<ComPlayerDisasterLogVo, ComPlayerDisasterLog, String> implements ComPlayerDisasterLogService {
+    @Autowired
+    private ComPlayerDisasterLogRepository comPlayerDisasterLogRepository;
+
+    @Autowired
+    private ComPlayerDisasterProtectedService comPlayerDisasterProtectedService;
+
+    @Autowired
+    private ComPlayersService comPlayersService;
+
+    @Autowired
+    private ComPlayerDisasterService disasterService;
+
+    /**
+     * 伤害 会减产50%
+     */
+    private int damage = 50;
+
+    /**
+     * 防御 90%概率防御灾难
+     */
+    private double defend = 90;
+
+    /**
+     * 获取经历的灾难记录
+     *
+     * @param userId
+     * @return
+     */
+    @Override
+    public List<ComPlayerDisasterLog> getHistory(String userId) {
+        log.info("获取灾难记录:{}", userId);
+        List<ComPlayerDisasterLog> list = comPlayerDisasterLogRepository.getUnShow(userId).stream().sorted(Comparator.comparing(ComPlayerDisasterLog::getCreateTime)).collect(Collectors.toList());
+        log.info("获取灾难记录:{}", JSONUtil.toJsonStr(list));
+        list.forEach(a -> a.setShow(true));
+        comPlayerDisasterLogRepository.saveAll(list);
+        return list;
+    }
+
+    /**
+     * 灾难模式周任务:同步每个用户上周收入支出
+     */
+    @Override
+    public void ComPlayerDisasterWeekTask() {
+        List<String> userIds = new ArrayList<>();
+        userIds.add("1002");
+        //todo 测试后 改回去
+        //List<String> userIds = comPlayersService.findUserIds();
+        for (String userId : userIds) {
+            asyncDisaster(userId);
+        }
+    }
+
+    /**
+     * 同步每个用户上周收入支出
+     * @param userId
+     */
+    private void asyncDisaster(String userId) {
+        //同步上周收入支出数据
+        ComPlayerDisaster comPlayerDisaster = disasterService.save(userId);
+        //生成灾难记录记录
+        if (comPlayerDisaster != null) {
+            //本周内应该出现的灾难次数
+            int disasterTimes = getDisasterTimes(comPlayerDisaster.getPayment(), comPlayerDisaster.getIncome());
+
+            Date now = new Date();
+            DateRange dayRange = DateUtil.range(now, DateUtil.endOfWeek(now).offset(DateField.DAY_OF_WEEK,-1), DateField.DAY_OF_WEEK);
+            List<Date> hasDisasterDay = new ArrayList<>();
+            while (dayRange.hasNext()) {
+                hasDisasterDay.add(dayRange.next());
+            }
+            //发生灾难的时间点(本周具体哪天灾难)
+            List<Date> realDisasterDayList = RandomUtil.randomEleList(hasDisasterDay, disasterTimes);
+            //生成灾难对象
+            List<ComPlayerDisasterLog> disasterLogList = getComPlayerDisasterLogVoList(userId, comPlayerDisaster.getId(), realDisasterDayList);
+
+            comPlayerDisasterLogRepository.saveAll(disasterLogList);
+        }
+    }
+
+    /**
+     * 生成灾难对象
+     * @param userId 用户id
+     * @param ComPlayerDisasterTableId 主表id
+     * @param realDisasterDayList 发生灾难的时间点(本周具体哪天灾难)
+     * @return
+     */
+    private List<ComPlayerDisasterLog> getComPlayerDisasterLogVoList(String userId, long ComPlayerDisasterTableId, List<Date> realDisasterDayList) {
+        List<ComPlayerDisasterLog> list = new ArrayList<>();
+        ComPlayerDisasterLog disasterLog;
+        for (Date disasterDay : realDisasterDayList) {
+            //随机获取一种灾难方式灾难对象
+            List<String> disasterEnumFields =  (List)EnumUtil.getFieldValues(ComPlayerDisasterEnum.class, "msg");
+            ComPlayerDisasterEnum disasterEnum = EnumUtil.likeValueOf(ComPlayerDisasterEnum.class,RandomUtil.randomEle(disasterEnumFields)) ;
+
+            disasterLog = new ComPlayerDisasterLog();
+            disasterLog.setDsasterId(ComPlayerDisasterTableId);
+            disasterLog.setDsasterName(disasterEnum.getMsg());
+            disasterLog.setDsasterType(disasterEnum.getCode());
+            disasterLog.setUserId(userId);
+            disasterLog.setDamage(damage);
+            disasterLog.setDefend(defend);
+            disasterLog.setCreateTime(new Date());
+            disasterLog.setShow(false);
+            disasterLog.setEnabled(false);
+            disasterLog.setDisasterTime(disasterDay);
+            list.add(disasterLog);
+        }
+        return list;
+    }
+
+    /**
+     * 灾难模式天任务:触发所有用户今日灾难和保护机制
+     */
+    @Override
+    public void ComPlayerDisasterDayTask() {
+        Date now = new Date();
+        DateTime begin = DateUtil.beginOfDay(now);
+        DateTime end = DateUtil.endOfDay(now);
+        //把今天灾难的明细取出来。按照用户编号计算是否有防护。
+        List<ComPlayerDisasterLog> disasterLogList = comPlayerDisasterLogRepository.getUnEnabled(begin, end);
+        for (ComPlayerDisasterLog disasterLog : disasterLogList) {
+            if (!disasterLog.isEnabled()) {
+                //查询当前人员购买防护历史记录
+                List<ComPlayerDisasterProtected> protectList = comPlayerDisasterProtectedService.getComPlayerDisasterProtectedByUserIdOrderByProtectTimeDesc(disasterLog.getUserId());
+                log.info("防护历史记录:{}", JSONUtil.toJsonStr(protectList));
+                //防护到期时间
+                Date protectedDate = protectList.stream()
+                        .filter(a -> disasterLog.getDsasterType().equals(a.getDsasterType()))
+                        .sorted(Comparator.comparing(ComPlayerDisasterProtected::getProtectTime).reversed())
+                        .map(ComPlayerDisasterProtected::getProtectTime)
+                        .findFirst().orElse(DateUtil.parse(null));
+
+                disasterLog.setProtectTime(protectedDate);
+                disasterLog.setEnabled(true);
+                disasterLog.setUpdateTime(now);
+                if (protectedDate!=null&&now.before(protectedDate)) {
+                    int randomProtect = RandomUtil.randomInt(0, 101);
+                    log.info("灾难几率:{}", randomProtect);
+                    if (randomProtect > (100 - defend)) {
+                        disasterLog.setProtect(true);
+                    } else {
+                        disasterLog.setProtect(false);
+                    }
+                }
+
+                log.info("灾难结果:{}", JSONUtil.toJsonStr(disasterLog));
+                comPlayerDisasterLogRepository.save(disasterLog);
+                //todo 应该还有个减产的接口,此处但是先不做
+                if (!disasterLog.isProtect()) {
+
+                }
+            } else {
+                log.info("[{}]灾难结果:{}", disasterLog.getUserId(), "灾难不可重复出发");
+            }
+        }
+    }
+
+    /**
+     * 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;
+        }
+    }
+}

+ 4 - 12
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterService.java

@@ -4,21 +4,13 @@ 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;
+import java.util.Date;
 
 public interface ComPlayerDisasterService extends CommonService<ComPlayerDisasterVo, ComPlayerDisaster, String> {
-    /**
-     * 灾难模块核心功能
-     * @param userId
-     * @return
-     */
-    void getDisaster(String userId);
 
     /**
-     * 获取经历的灾难记录
-     *
-     * @param userId
-     * @return
+     * 收入支出比例的时间周期 这个确定是上周的数据
+     * 本周开始时间为周日到周六,期间获取的不会变化,这周获取的是上周的数据
      */
-    List<ComPlayerDisaster> getHistory(String userId);
+    ComPlayerDisaster save(String userId );
 }

+ 23 - 178
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterServiceImpl.java

@@ -1,215 +1,60 @@
 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;
+import java.util.Date;
+import java.util.Map;
 
 @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;
-    }
+    private ComPlayerDisasterRepository repository;
 
-    /**
-     * 灾难模块核心功能
-     *
-     * @param userId
-     * @return
-     */
     @Override
-    public void getDisaster(String userId) {
-        //todo 需要有个定时任务去处理,定期拉取所有人,然后执行
-        log.info("灾难模式:{}", userId);
-
-        ComPlayerDisaster hasComPlayerDisaster = comPlayerDisasterRepository.findByUserId(userId);
-        if (hasComPlayerDisaster != null) {
-            log.info("灾难结果:{}", "今日已触发过灾难");
-            return;
-        }
-
-        //如果本周内还有灾难
-        if (getDisasterNum(userId) > 0) {
-            //随机获取一种灾难方式灾难对象
-            ComPlayerDisasterVo 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("灾难结果:{}", "本周无灾难了");
-        }
-    }
-
-    /**
-     * 获取灾难防护列表
-     *
-     * @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);
+    public ComPlayerDisaster save(String userId) {
+        Date now = new Date();
+        Date begin = DateUtil.beginOfWeek(now);
+        Date end = DateUtil.endOfWeek(now);
+        if (exists(begin, end)) {
+            return null;
         }
-        log.info("灾难列表:{}", JSONUtil.toJsonStr(disasterList));
-        return disasterList;
+        ComPlayerDisaster data = getData(userId);
+        return repository.saveAndFlush(data);
     }
 
-    /**
-     * 获得本周剩余灾难数量
-     *
-     * @param userId
-     * @return
-     */
-    private int getDisasterNum(String userId) {
+    private ComPlayerDisaster getData(String userId) {
         //读取接口 获取 那种情况 这个接口实际是返回的上周数据。所以什么时候啦都不会变。
         //本周开始时间为周日到周六,期间获取的不会变化,这周获取的是上周的数据
+        //所以 支出对应pay_amount,收入对应swap_amount
         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);
+        ComPlayerDisaster comPlayerDisaster = new ComPlayerDisaster();
+        comPlayerDisaster.setCreateTime(new Date());
+        comPlayerDisaster.setUserId(userId);
+        comPlayerDisaster.setIncome(swap_amount);
+        comPlayerDisaster.setPayment(pay_amount);
+        comPlayerDisaster.setLog(JSONUtil.toJsonStr(cntPayAndSwapAmount));
+        return comPlayerDisaster;
     }
 
-    /**
-     * 本周内已出现灾难次数
-     *
-     * @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;
-        }
+    private boolean exists(Date begin, Date end) {
+        //todo 根据时间验证是否有数据
+        return false;
     }
 }

+ 58 - 0
src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterLogVo.java

@@ -0,0 +1,58 @@
+package com.td.boss.game.complayerland.vo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 灾难
+ */
+@Data
+public class ComPlayerDisasterLogVo implements Serializable {
+
+    /**
+     * 主表id
+     */
+    private long dsasterId;
+
+    /**
+     * 灾难名称
+     */
+    private String dsasterName;
+    private Integer dsasterType;
+    /**
+     * 伤害 会减产50%
+     */
+    private int damage;
+
+    /**
+     * 防御 90%概率防御灾难
+     */
+    private double defend;
+
+    /**
+     * 防护截止时间
+     */
+    private Date protectTime;
+
+    /**
+     * 本次灾难是否激活防护
+     */
+    private boolean activityProtect;
+
+    private String userId;
+
+    private Date createTime;
+
+    private Date disasterTime;
+    private Date updateTime;
+    /**
+     * 前端 是否以显示
+     */
+    private boolean isShow;
+    /**
+     * 是否已经使用,灾害是预先生成的
+     */
+    private boolean isEnabled;
+}

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

@@ -13,8 +13,8 @@ public class ComPlayerDisasterProtectedVo implements Serializable {
     /**
      * 灾难名称
      */
-    private String name;
-    private Integer type;
+    private String dsasterName;
+    private Integer dsasterType;
     /**
      * 防护截止时间
      */

+ 5 - 48
src/main/java/com/td/boss/game/complayerland/vo/ComPlayerDisasterVo.java

@@ -2,57 +2,14 @@ package com.td.boss.game.complayerland.vo;
 
 import lombok.Data;
 
-import java.io.Serializable;
 import java.util.Date;
 
-/**
- * 灾难
- */
 @Data
-public class ComPlayerDisasterVo implements Serializable {
-    /**
-     * 灾难名称
-     */
-    private String name;
-    private Integer type;
-    /**
-     * 伤害 会减产50%
-     */
-    private int damage;
-
-    /**
-     * 防御 90%概率防御灾难
-     */
-    private double defend;
-
-    /**
-     * 防护截止时间
-     */
-    private Date protectTime;
-
-    /**
-     * 本次灾难是否激活防护
-     */
-    private boolean activityProtect;
-
+public class ComPlayerDisasterVo {
+    private long id;
+    private Double income;
+    private Double payment;
+    private String log;
     private String userId;
-
     private Date createTime;
-
-    /**
-     * 前端 是否以显示
-     */
-    private boolean isShow;
-
-    /**
-     * 是否购买防护并且没到期
-     *
-     * @return
-     */
-    public boolean getIsBuyProtected() {
-        if (protectTime == null) {
-            return false;
-        }
-        return new Date().before(protectTime);
-    }
 }

+ 5 - 0
src/main/java/com/td/boss/game/complayers/repository/ComPlayersRepository.java

@@ -2,12 +2,17 @@ package com.td.boss.game.complayers.repository;
 
 import com.td.boss.common.repository.*;
 import com.td.boss.game.complayers.pojo.ComPlayers;
+import org.springframework.data.jpa.repository.Query;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
 import java.util.Optional;
 
 @Repository
 public interface ComPlayersRepository extends CommonRepository<ComPlayers, Integer> {
 
     Optional<ComPlayers> findByUserId(String userId);
+
+    @Query(value = "select user_id from com_players", nativeQuery = true)
+    List<String> findUserIds();
 }

+ 3 - 1
src/main/java/com/td/boss/game/complayers/service/ComPlayersService.java

@@ -4,6 +4,8 @@ import com.td.boss.common.service.*;
 import com.td.boss.game.complayers.pojo.ComPlayers;
 import com.td.boss.game.complayers.vo.ComPlayersVo;
 
+import java.util.List;
+
 public interface ComPlayersService extends CommonService<ComPlayersVo, ComPlayers, Integer> {
 
     /**
@@ -15,5 +17,5 @@ public interface ComPlayersService extends CommonService<ComPlayersVo, ComPlayer
 
     ComPlayers addComPlayers(ComPlayers comPlayers);
 
-
+    List<String> findUserIds();
 }

+ 9 - 2
src/main/java/com/td/boss/game/complayers/service/ComPlayersServiceImpl.java

@@ -7,12 +7,14 @@ import com.td.boss.game.complayers.repository.ComPlayersRepository;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceContext;
+import java.util.List;
 
 @Service
 @Transactional
-public class ComPlayersServiceImpl extends CommonServiceImpl<ComPlayersVo, ComPlayers, Integer> implements ComPlayersService{
+public class ComPlayersServiceImpl extends CommonServiceImpl<ComPlayersVo, ComPlayers, Integer> implements ComPlayersService {
 
     @PersistenceContext
     private EntityManager em;
@@ -27,7 +29,12 @@ public class ComPlayersServiceImpl extends CommonServiceImpl<ComPlayersVo, ComPl
     @Override
     @Transactional
     public ComPlayers addComPlayers(ComPlayers comPlayers) {
-        ComPlayers player =  comPlayersRepository.saveAndFlush(comPlayers);
+        ComPlayers player = comPlayersRepository.saveAndFlush(comPlayers);
         return player;
     }
+
+    @Override
+    public List<String> findUserIds() {
+        return comPlayersRepository.findUserIds();
+    }
 }

+ 14 - 0
src/main/resources/application.yml

@@ -33,6 +33,20 @@ spring:
   #打印SQL
   jpa:
     show-sql: true
+    properties:
+      hibernate:
+        format_sql: true
+
+logging:
+  level:
+    org:
+      hibernate:
+        type:
+          descriptor:
+            sql:
+              BasicBinder=trace:
+    root: debug
+
 
 # token 参数
 jwt:

+ 5 - 0
src/main/resources/logback-spring.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration debug="false">
+    <logger name="org.hibernate.SQL" level="DEBUG"/>
     <!--日志文件主目录:这里${user.home}为当前服务器用户主目录-->
     <property name="LOG_HOME" value="${user.home}/log"/>
     <!--日志文件名称:这里spring.application.name表示工程名称-->
@@ -68,6 +69,10 @@
         <logger level="WARN" name="com.netflix"/>
         <logger level="DEBUG" name="org.hibernate.SQL"/>
 
+        <logger name="org.hibernate.engine.QueryParameters" level="DEBUG"/>
+        <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG"/>
+        <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE"/>
+
     </springProfile>
     <springProfile name="dev">
         <!--root节点 全局日志级别,用来指定最基础的日志输出级别-->

+ 13 - 9
src/test/java/com/td/boss/ComPlayerDisasterTests.java → src/test/java/com/td/boss/ComPlayerDisasterLogTests.java

@@ -1,9 +1,9 @@
 package com.td.boss;
 
 import cn.hutool.core.date.DateUtil;
-import com.td.boss.game.complayerland.pojo.ComPlayerDisaster;
+import com.td.boss.game.complayerland.pojo.ComPlayerDisasterLog;
 import com.td.boss.game.complayerland.service.ComPlayerDisasterProtectedService;
-import com.td.boss.game.complayerland.service.ComPlayerDisasterService;
+import com.td.boss.game.complayerland.service.ComPlayerDisasterLogService;
 import com.td.boss.game.complayerland.vo.ComPlayerDisasterEnum;
 import com.td.boss.game.complayerland.vo.ComPlayerDisasterProtectedVo;
 import org.junit.jupiter.api.Test;
@@ -16,10 +16,10 @@ import java.util.List;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-class ComPlayerDisasterTests {
+class ComPlayerDisasterLogTests {
 
     @Autowired
-    private ComPlayerDisasterService comPlayerDisasterService;
+    private ComPlayerDisasterLogService comPlayerDisasterLogService;
     @Autowired
     private ComPlayerDisasterProtectedService comPlayerDisasterProtectedService;
 
@@ -33,8 +33,8 @@ class ComPlayerDisasterTests {
         ComPlayerDisasterProtectedVo comPlayerDisasterProtected = new ComPlayerDisasterProtectedVo();
         comPlayerDisasterProtected.setCreateTime(new Date());
         comPlayerDisasterProtected.setUserId(userId);
-        comPlayerDisasterProtected.setType(ComPlayerDisasterEnum.ziran.getCode());
-        comPlayerDisasterProtected.setName(ComPlayerDisasterEnum.ziran.getMsg());
+        comPlayerDisasterProtected.setDsasterType(ComPlayerDisasterEnum.ziran.getCode());
+        comPlayerDisasterProtected.setDsasterName(ComPlayerDisasterEnum.ziran.getMsg());
         comPlayerDisasterProtected.setProtectTime(DateUtil.parse("2022-03-30 00:00:00"));
         comPlayerDisasterProtectedService.save(comPlayerDisasterProtected);
     }
@@ -44,7 +44,7 @@ class ComPlayerDisasterTests {
      */
     @Test
     void getHistory() {
-        List<ComPlayerDisaster> history = comPlayerDisasterService.getHistory(userId);
+        List<ComPlayerDisasterLog> history = comPlayerDisasterLogService.getHistory(userId);
         System.out.println(history);
     }
 
@@ -52,7 +52,11 @@ class ComPlayerDisasterTests {
      * 测试灾难模式
      */
     @Test
-    void getDisaster(){
-        comPlayerDisasterService.getDisaster(userId);
+    void ComPlayerDisasterWeekTask(){
+        comPlayerDisasterLogService.ComPlayerDisasterWeekTask();
+    }
+    @Test
+    void ComPlayerDisasterDayTask(){
+        comPlayerDisasterLogService.ComPlayerDisasterDayTask();
     }
 }