Browse Source

优化游戏服发送中控服消息发送

johnclot69 4 months ago
parent
commit
ec8a55e2f2

+ 8 - 9
incubator-game/src/main/java/com/incubator/game/listener/CenterClientListener.java

@@ -3,11 +3,13 @@ package com.incubator.game.listener;
 import com.incubator.core.net.ws.*;
 import com.incubator.game.GGame;
 import com.incubator.common.net.Connection;
-import com.incubator.message.GameRegMsg;
 import com.incubator.message.proto.CommonProto;
 import com.incubator.core.net.handler.listener.GameClientConnectionListener;
 import io.netty.buffer.ByteBuf;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * 中控服消息
  * @author johnc
@@ -19,17 +21,14 @@ public class CenterClientListener extends GameClientConnectionListener {
 	public void connectionOpened(Connection conn) {
 		logger.info("中控服链路打开发送注册,注册码:{}", CommonProto.Cmd.S2S_GAME_REG_VALUE);
         try {
-            GameRegMsg.GameRegG2C gameRegG2C = new GameRegMsg.GameRegG2C();
-            gameRegG2C.setServerId(GGame.SERVER_ID);
-            gameRegG2C.setHost(GGame.BIND_HOST);
-            gameRegG2C.setPort(GGame.BIND_PORT);
-//          Message msg = new Message(ProtoCommon.MsgId.S2S_GAME_REG, gameRegG2C.toJson());
-//          conn.writeAndFlush(super.packMessage(msg));
+            Map<String, Object> data = new HashMap<>();
+			data.put("serverId", GGame.SERVER_ID);
+			data.put("host", GGame.BIND_HOST);
+			data.put("port", GGame.BIND_PORT);
+			conn.writeAndFlush(new WSResponse(CommonProto.Cmd.S2S_GAME_REG, data).toBytes());
         } catch (Exception e) {
 			logger.info("游戏服注册出错:{}", e.getMessage());
         }
-
-//		conn.writeAndFlush(new TextWebSocketFrame(msg.toJson()));
 	}
 
 	@Override

+ 6 - 4
incubator-game/src/main/java/com/incubator/game/listener/PublicListener.java

@@ -34,19 +34,21 @@ public class PublicListener extends GameServerConnectionListener {
 
 	@Override
 	public void connectionClosed(Connection connection) {
-		//连接关闭操作
 		try {
-			GPlayer player = GGame.onlinePlayers.getOrDefault(connection.getPlayerId(), null);
+			GPlayer player = GGame.onlinePlayers.get(connection.getPlayerId());
 			if (player != null && player.getSession() == connection) {
+				// 执行玩家登出操作
 				player.doLogout();
 			}
 		} catch (Exception e) {
-			logger.error("玩家退出...");
+			logger.error("处理玩家退出时发生异常: ", e);
 		} finally {
+			// 从公共客户端和在线玩家列表中移除连接
 			GGame.publicClients.remove(connection);
-			if (connection.getPlayerId() != null && !GGame.onlinePlayers.isEmpty()) {
+			if (connection.getPlayerId() != null) {
 				GGame.onlinePlayers.remove(connection.getPlayerId());
 			}
+			logger.info("连接关闭: {}", connection);
 		}
 	}
 

+ 1 - 0
incubator-game/src/main/java/com/incubator/game/room/JDGDRoom.java

@@ -329,6 +329,7 @@ public class JDGDRoom extends Room implements GRoomInterface {
 
     /**
      * 判断玩家出的牌是否可以压过上一手牌
+     *
      * @param disCardsList 当前玩家出的牌
      * @param disCardsType 当前玩家出的牌型
      * @param currentLevel 当前级数

+ 3 - 3
incubator-game/src/main/java/com/incubator/game/util/JDGDUtils.java

@@ -57,10 +57,10 @@ public final class JDGDUtils {
      * @param previousCards 上一手牌
      * @param disCardList 当前玩家出的牌
      * @param curLevel 当前级数
-     * @param cardType 牌型
+     * @param disCardsType 出牌牌型
      * @return 正数表示当前牌较大,负数表示上一手牌较大,0表示相等
      */
-    public static int compareCardStrength(int[] previousCards, int[] disCardList, int curLevel, CardType cardType) {
+    public static int compareCardStrength(int[] previousCards, int[] disCardList, int curLevel, CardType disCardsType) {
         List<Integer> previousPoints = Arrays.stream(previousCards)
                 .map(card -> getCardPoint(card, curLevel))
                 .sorted()
@@ -73,7 +73,7 @@ public final class JDGDUtils {
                 .boxed()
                 .collect(Collectors.toList());
 
-        switch (cardType) {
+        switch (disCardsType) {
             case SINGLE:
             case PAIR:
             case TRIPLE: