浏览代码

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

xst 4 年之前
父节点
当前提交
3f9fdd624c

+ 4 - 1
pom.xml

@@ -49,7 +49,10 @@
             <artifactId>junit</artifactId>
             <scope>test</scope>
         </dependency>
-		
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-quartz</artifactId>
+        </dependency>
 		        <!--热部署工具dev-tools-->
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 2 - 0
src/main/java/com/td/boss/BossApplication.java

@@ -20,6 +20,7 @@ import org.springframework.cache.annotation.EnableCaching;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.security.core.session.SessionRegistry;
 import org.springframework.security.core.session.SessionRegistryImpl;
 import org.springframework.stereotype.Controller;
@@ -39,6 +40,7 @@ import java.util.List;
 
 @EnableAsync//开启异步调用
 @EnableCaching// 开启缓存,需要显示的指定
+@EnableScheduling
 @SpringBootApplication
 public class BossApplication {
 

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

@@ -12,4 +12,6 @@ public interface ComPlayerDisasterRepository extends CommonRepository<ComPlayerD
 
     @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);
 }

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

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

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

@@ -0,0 +1,43 @@
+package com.td.boss.game.complayerland.scheduled;
+
+import org.quartz.*;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class QuartzConfig {
+
+    private static String JOB_GROUP_NAME = "JOBGROUP_ComPlayerDisaster";
+    private static String TRIGGER_GROUP_NAME = "TRIGGERGROUP_ComPlayerDisaster";
+
+    /**
+     * 定时任务1:
+     * 同步用户信息Job(任务详情)
+     */
+    @Bean
+    public JobDetail syncUserJobDetail() {
+        JobDetail jobDetail = JobBuilder.newJob(ComPlayerDisasterTask.class)
+                .withIdentity("syncUserJobDetail", JOB_GROUP_NAME)
+                .storeDurably() //即使没有Trigger关联时,也不需要删除该JobDetail
+                .build();
+        return jobDetail;
+    }
+
+    /**
+     * 定时任务1:
+     * 同步用户信息Job(触发器)
+     */
+    @Bean
+    public Trigger syncUserJobTrigger() {
+        //每隔5秒执行一次
+        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/2 * * * ?");
+
+        //创建触发器
+        Trigger trigger = TriggerBuilder.newTrigger()
+                .forJob(syncUserJobDetail())
+                .withIdentity("syncUserJobTrigger", TRIGGER_GROUP_NAME)
+                .withSchedule(cronScheduleBuilder)
+                .build();
+        return trigger;
+    }
+}

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

@@ -12,7 +12,7 @@ public interface ComPlayerDisasterService extends CommonService<ComPlayerDisaste
      * @param userId
      * @return
      */
-    ComPlayerDisasterVo getDisaster(String userId);
+    void getDisaster(String userId);
 
     /**
      * 获取经历的灾难记录

+ 17 - 10
src/main/java/com/td/boss/game/complayerland/service/ComPlayerDisasterServiceImpl.java

@@ -49,33 +49,41 @@ public class ComPlayerDisasterServiceImpl extends CommonServiceImpl<ComPlayerDis
 
     /**
      * 获取经历的灾难记录
+     *
      * @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));
+    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) {
+    public void getDisaster(String userId) {
         //todo 需要有个定时任务去处理,定期拉取所有人,然后执行
-        log.info("灾难模式:{}",userId);
-        ComPlayerDisasterVo comPlayerDisasterVo = null;
+        log.info("灾难模式:{}", userId);
+
+        ComPlayerDisaster hasComPlayerDisaster = comPlayerDisasterRepository.findByUserId(userId);
+        if (hasComPlayerDisaster != null) {
+            log.info("灾难结果:{}", "今日已触发过灾难");
+            return;
+        }
+
         //如果本周内还有灾难
         if (getDisasterNum(userId) > 0) {
             //随机获取一种灾难方式灾难对象
-            comPlayerDisasterVo = getDisasterList(userId).get(RandomUtil.randomInt(0, 2));
+            ComPlayerDisasterVo comPlayerDisasterVo = getDisasterList(userId).get(RandomUtil.randomInt(0, 2));
             //查看该灾难是否有防护,如果有防护需要按照防护几率计算是否防御住灾害了
             if (comPlayerDisasterVo.getIsBuyProtected()) {
                 int randomProtect = RandomUtil.randomInt(0, 101);
@@ -96,7 +104,6 @@ public class ComPlayerDisasterServiceImpl extends CommonServiceImpl<ComPlayerDis
         } else {
             log.info("灾难结果:{}", "本周无灾难了");
         }
-        return comPlayerDisasterVo;
     }
 
     /**

+ 1 - 1
src/main/resources/application.yml

@@ -47,7 +47,7 @@ spring:
 
   #数据库配置
   datasource:
-    url: jdbc:mysql://123.57.252.53:6688/dragon_town?serverTimezone=GMT%2B8&characterEncoding=utf-8
+    url: jdbc:mysql://123.57.252.53:6688/dragon_town?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8
     username: root #dragon_town #root
     password:  FzCebFq6Xy0CDTXJ #zddeiBmp8c5T6TR6 #9ab8fad748dead93
     driver-class-name: com.mysql.cj.jdbc.Driver