浏览代码

添加新游戏代码

kk 1 月之前
父节点
当前提交
889b97a33b

+ 22 - 0
game-business/src/main/java/com/game/business/config/GameEightConfig.java

@@ -0,0 +1,22 @@
+package com.game.business.config;
+
+import com.game.business.util.Common;
+import com.game.business.websocket.WebSocketConnent;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.stereotype.Component;
+
+@Component
+@EnableAsync
+public class GameEightConfig implements CommandLineRunner {
+
+    public static WebSocketConnent webSocketConnent;
+
+    @Override
+    @Async
+    public void run(String... args) throws Exception {
+        webSocketConnent = new WebSocketConnent(Common.WS_GAME_EIGHT_URL, Common.WS_GAME_EIGHT_REMARK_URL, Common.GAME_EIGHT_NAME);
+        webSocketConnent.connect();
+    }
+}

+ 13 - 10
game-business/src/main/java/com/game/business/controller/AppGameBettingController.java

@@ -105,19 +105,22 @@ public class AppGameBettingController extends BaseController{
                 gameBetting.setBettingGameType(0);
             }
 
-            if(gameBetting.getMoneyId() == null){
-                return HttpRet.fail("金额ID为空。");
-            }
+//            if(gameBetting.getMoneyId() == null){
+//                return HttpRet.fail("金额ID为空。");
+//            }
 
-            if(gameBetting.getBettingGameType() == 0){
-                AppGameMoney appGameMoney = appGameMoneyService.selectAppGameMoneyById(gameBetting.getMoneyId());
+            if(gameBetting.getMoneyId() != null && gameBetting.getMoneyId() > 0){
+                if(gameBetting.getBettingGameType() == 0){
 
-                if(appGameMoney == null){
-                    return HttpRet.fail("金额对象不存在。");
-                }
+                    AppGameMoney appGameMoney = appGameMoneyService.selectAppGameMoneyById(gameBetting.getMoneyId());
 
-                if(appGameMoney.getBettingMoney().doubleValue() != gameBetting.getBettingAmount().doubleValue()){
-                    return HttpRet.fail("投注金额不匹配,无法下单。");
+                    if(appGameMoney == null){
+                        return HttpRet.fail("金额对象不存在。");
+                    }
+
+                    if(appGameMoney.getBettingMoney().doubleValue() != gameBetting.getBettingAmount().doubleValue()){
+                        return HttpRet.fail("投注金额不匹配,无法下单。");
+                    }
                 }
             }
 

+ 10 - 0
game-business/src/main/java/com/game/business/task/WebSokcetTask.java

@@ -90,5 +90,15 @@ public class WebSokcetTask {
                 session.getBasicRemote().sendText("ping");
             }
         }
+
+        if(Common.sessionMap.containsKey(Common.GAME_EIGHT_CODE)){
+            Session session = Common.sessionMap.get(Common.GAME_EIGHT_CODE);
+            if(!session.isOpen()){
+                Common.sessionMap.remove(Common.GAME_EIGHT_CODE);
+                session.close();
+            }else{
+                session.getBasicRemote().sendText("ping");
+            }
+        }
     }
 }

+ 6 - 0
game-business/src/main/java/com/game/business/util/Common.java

@@ -23,6 +23,9 @@ public class Common {
     public static final String GAME_SEVEN_NAME = "seven";
     public static final String GAME_SEVEN_CODE = "7";
 
+    public static final String GAME_EIGHT_NAME = "eight";
+    public static final String GAME_EIGHT_CODE = "8";
+
 
     public static final String [] GAME_BETTING_ITEM_LIMIT = {"0","1","6","7"};
 
@@ -59,5 +62,8 @@ public class Common {
     public static final String WS_GAME_SEVEN_URL = "ws://123.207.100.229:8000/ws";
     public static final String WS_GAME_SEVEN_REMARK_URL = "ws://43.138.141.230:8000/ws";
 
+    public static final String WS_GAME_EIGHT_URL = "ws://120.79.41.13:8000/ws";
+    public static final String WS_GAME_EIGHT_REMARK_URL = "ws://120.79.41.13:8000/ws";
+
     public static Map<String, Session> sessionMap = new HashMap<>();
 }

+ 9 - 18
game-business/src/main/java/com/game/business/websocket/WebSocketConnent.java

@@ -52,36 +52,27 @@ public class WebSocketConnent {
                 if(Common.GANME_ONE_NAME.equals(this.WS_URL_NAME)){
                     GameOneClient client = SpringContextSetting.getBean(GameOneClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GANME_TWO_NAME.equals(this.WS_URL_NAME)){
                     GameTwoClient client = SpringContextSetting.getBean(GameTwoClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GAME_THREES_NAME.equals(this.WS_URL_NAME)){
                     GameThreesClient client = SpringContextSetting.getBean(GameThreesClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GAME_FOUR_NAME.equals(this.WS_URL_NAME)){
                     GameFourClient client = SpringContextSetting.getBean(GameFourClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GAME_FIVE_NAME.equals(this.WS_URL_NAME)){
                     GameFiveClient client = SpringContextSetting.getBean(GameFiveClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_SIX_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GAME_SIX_NAME.equals(this.WS_URL_NAME)){
                     GameSixClient client = SpringContextSetting.getBean(GameSixClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
-                }
-
-                if(Common.GAME_SEVEN_NAME.equals(this.WS_URL_NAME)){
+                }else if(Common.GAME_SEVEN_NAME.equals(this.WS_URL_NAME)){
                     GameSevenClient client = SpringContextSetting.getBean(GameSevenClient.class);
                     ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
+                }else if(Common.GAME_EIGHT_NAME.equals(this.WS_URL_NAME)){
+                    GameEightClient client = SpringContextSetting.getBean(GameEightClient.class);
+                    ContainerProvider.getWebSocketContainer().connectToServer(client, new URI(this.WS_NOW_URL));
                 }
 
                 System.out.println(this.WS_URL_NAME + " 已成功连接Websocket[" + this.WS_NOW_URL + "]");

+ 71 - 0
game-business/src/main/java/com/game/business/websocket/client/GameEightClient.java

@@ -0,0 +1,71 @@
+package com.game.business.websocket.client;
+
+import com.game.business.config.GameEightConfig;
+import com.game.business.config.GameSevenConfig;
+import com.game.business.task.AppGameBettingTask;
+import com.game.business.util.Common;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.websocket.*;
+import java.util.Date;
+
+
+@Component
+@ClientEndpoint
+public class GameEightClient {
+
+    @Autowired
+    private AppGameBettingTask appGameBettingTask;
+
+    @OnOpen
+    public void onOpen(Session session) throws Exception{
+        System.out.println("game eight 游戏已连接 server");
+        session.getBasicRemote().sendText("ping");
+        Common.sessionMap.put(Common.GAME_EIGHT_CODE, session);
+    }
+
+    @OnClose
+    public void onClose(Session session) throws Exception{
+        System.out.println("game eight 游戏已断开 server:" + new Date());
+        connect();
+    }
+
+    @OnError
+    public void onError(Session session, Throwable throwable) throws Exception{
+        System.out.println("game eight 连接异常 [" + throwable.getMessage() + "]");
+        connect();
+    }
+
+    public void connect(){
+        synchronized (this){
+            try {
+                GameEightConfig.webSocketConnent.connect();
+            }catch (Exception e){
+                System.out.println("连接 game eight [socket] 异常" + e.getMessage());
+            }
+        }
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message){
+        try {
+            if(StringUtils.isBlank(message)){
+                System.out.println("game eight 数据为空");
+                return;
+            }
+
+            System.out.println("game eight 接收数据" + message);
+
+            if(message.equals("pong")){
+                return;
+            }
+
+            appGameBettingTask.gameDataTask(message, Common.GAME_EIGHT_CODE);
+        }catch (Exception e){
+            e.printStackTrace();
+            System.out.println("game eight 接收数据异常[" + e.getMessage() + "]");
+        }
+    }
+}