Преглед изворни кода

Merge branch 'dev' of sunkean/raex_mmo into master

sunkean пре 2 година
родитељ
комит
5a9bb39762

+ 32 - 0
src/main/java/com/izouma/meta/MetaWebsocketApplicationStartupRunner.java

@@ -0,0 +1,32 @@
+package com.izouma.meta;
+
+import com.izouma.meta.domain.MetaScheduledFuture;
+import com.izouma.meta.enums.ScheduledFutureType;
+import com.izouma.meta.repo.MetaScheduledFutureRepo;
+import com.izouma.meta.web.MMOWebSocketController;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+@AllArgsConstructor
+@Slf4j
+public class MetaWebsocketApplicationStartupRunner implements CommandLineRunner {
+
+    private MetaScheduledFutureRepo metaScheduledFutureRepo;
+    private MMOWebSocketController  mmoWebSocketController;
+
+    @Override
+    public void run(String... args) {
+        log.info(">>>>>>>>>>>>>>>服务启动执行<<<<<<<<<<<<<");
+        List<MetaScheduledFuture> metaScheduledFutures = metaScheduledFutureRepo.findAllByType(ScheduledFutureType.OBJECT_MOVE);
+        metaScheduledFutures.forEach(metaScheduledFuture -> {
+            log.info(String.format("启动任务 %s", metaScheduledFuture.getType()));
+            mmoWebSocketController.startSendingMessages(metaScheduledFuture.getTaskId());
+        });
+    }
+
+}

+ 28 - 0
src/main/java/com/izouma/meta/domain/MetaScheduledFuture.java

@@ -0,0 +1,28 @@
+package com.izouma.meta.domain;
+
+import com.izouma.meta.enums.ScheduledFutureType;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.*;
+
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Entity
+public class MetaScheduledFuture {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @ApiModelProperty("消息类型")
+    @Enumerated(EnumType.STRING)
+    private ScheduledFutureType type;
+
+    @ApiModelProperty("任务id")
+    private String taskId;
+}

+ 16 - 0
src/main/java/com/izouma/meta/enums/ScheduledFutureType.java

@@ -0,0 +1,16 @@
+package com.izouma.meta.enums;
+
+public enum ScheduledFutureType {
+
+    OBJECT_MOVE("物体移动");
+
+    private final String description;
+
+    ScheduledFutureType(String description) {
+        this.description = description;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+}

+ 18 - 0
src/main/java/com/izouma/meta/repo/MetaScheduledFutureRepo.java

@@ -0,0 +1,18 @@
+package com.izouma.meta.repo;
+
+import com.izouma.meta.domain.MetaScheduledFuture;
+import com.izouma.meta.enums.ScheduledFutureType;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+import java.util.List;
+
+public interface MetaScheduledFutureRepo extends JpaRepository<MetaScheduledFuture, Long>, JpaSpecificationExecutor<MetaScheduledFuture> {
+
+    MetaScheduledFuture findByTypeAndTaskId(ScheduledFutureType type, String taskId);
+
+    void deleteByTypeAndTaskId(ScheduledFutureType type, String taskId);
+
+    List<MetaScheduledFuture> findAllByType(ScheduledFutureType type);
+
+}

+ 17 - 3
src/main/java/com/izouma/meta/web/MMOWebSocketController.java

@@ -1,7 +1,10 @@
 package com.izouma.meta.web;
 
 import com.izouma.meta.config.Constants;
+import com.izouma.meta.domain.MetaScheduledFuture;
+import com.izouma.meta.enums.ScheduledFutureType;
 import com.izouma.meta.repo.MetaObjectMoveRepo;
+import com.izouma.meta.repo.MetaScheduledFutureRepo;
 import com.izouma.meta.websocket.MMOWebSocket;
 import lombok.AllArgsConstructor;
 import org.apache.commons.collections.CollectionUtils;
@@ -10,17 +13,19 @@ import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.*;
 import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/mmo/websocket")
 @AllArgsConstructor
-@CrossOrigin(origins = {"http://localhost:8081", "https://raex.vip/", "https://test.raex.vip/"})
+@CrossOrigin(origins = {"http://localhost:8081", "https://raex.vip/", "https://test.raex.vip/", "https://www.raex.vip/"})
 public class MMOWebSocketController {
 
-    private MMOWebSocket       mmoWebSocket;
-    private MetaObjectMoveRepo metaObjectMoveRepo;
+    private MMOWebSocket            mmoWebSocket;
+    private MetaObjectMoveRepo      metaObjectMoveRepo;
+    private MetaScheduledFutureRepo metaScheduledFutureRepo;
 
     private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
 
@@ -32,6 +37,14 @@ public class MMOWebSocketController {
         ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
         ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> sendMessage(objectId), Constants.META_ROBOT_SEND_MSG_DELAY_MS, Constants.META_ROBOT_SEND_MSG_PERIOD_MS, TimeUnit.MILLISECONDS);
         tasks.put(objectId, future);
+        // 保存任务信息
+        MetaScheduledFuture metaScheduledFuture = metaScheduledFutureRepo.findByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
+        if (Objects.isNull(metaScheduledFuture)) {
+            metaScheduledFuture = new MetaScheduledFuture();
+            metaScheduledFuture.setType(ScheduledFutureType.OBJECT_MOVE);
+            metaScheduledFuture.setTaskId(objectId);
+            metaScheduledFutureRepo.save(metaScheduledFuture);
+        }
     }
 
     @GetMapping("/{objectId}/stop")
@@ -41,6 +54,7 @@ public class MMOWebSocketController {
             future.cancel(false);
             tasks.remove(objectId);
         }
+        metaScheduledFutureRepo.deleteByTypeAndTaskId(ScheduledFutureType.OBJECT_MOVE, objectId);
     }
 
     private void sendMessage(String objectId) {