瀏覽代碼

解决打牌中退出房间问题

johnclot69 2 月之前
父節點
當前提交
37a5b70c21

+ 1 - 1
incubator-common/build.gradle

@@ -25,7 +25,7 @@ dependencies {
     api 'org.dom4j:dom4j:2.1.4' // XML 操作工具
 
     // 网络通信和序列化
-    api 'io.netty:netty-all:4.1.116.Final' // Netty 全功能包
+    api 'io.netty:netty-all:4.1.117.Final' // Netty 全功能包
     api 'org.msgpack:msgpack-core:0.9.8' // MessagePack 序列化
     api 'com.dyuproject.protostuff:protostuff-core:1.3.1' // Protostuff 核心
     api 'com.dyuproject.protostuff:protostuff-runtime:1.3.1' // Protostuff 运行时

+ 6 - 6
incubator-game/src/main/java/com/incubator/game/handler/room/LeaveRoomHandler.java

@@ -52,12 +52,12 @@ public class LeaveRoomHandler extends NetHandler {
         }
 
         // 当前状态是否可操作
-        if (room.state != 0 || player.data.state != 0) {
-            Log.info("离开房间失败,游戏已开始...");
-            response.setCode(7);
-            response.setMessage("操作失败,游戏已开始...");
-            return;
-        }
+//        if (room.state != 0 || player.data.state != 0) {
+//            Log.info("离开房间失败,游戏已开始...");
+//            response.setCode(7);
+//            response.setMessage("操作失败,游戏已开始...");
+//            return;
+//        }
 
         // 离开房间
         room.leaveRoom(player);

+ 2 - 7
incubator-game/src/main/java/com/incubator/game/player/Player.java

@@ -139,15 +139,10 @@ public class Player extends GPlayer {
         for (ModuleManager manager : this.allManagers.values()) {
             manager.onPlayerEvent(PlayerEventType.OFFLINE);
         }
-        // 有房间解散房间
+        // 有房间就离开房间
         Room room = RoomService.getInstance().getRoomByPlayerId(this.getId());
         if (room != null) {
-            for (Player tmPlayer : room.actors.values()) {
-                if (tmPlayer != null) {
-                    tmPlayer.receive(CommonProto.Cmd.DisbandRoom_VALUE, new HashMap<>());
-                }
-            }
-            room.destroy();
+            room.leaveRoom(this);
         }
 
         WkServerStatus wkToRedis = RedisUtil.getWkToRedis(RedisKeyConstant.WK_SERVE, RedisKeyConstant.WK_SERVE_KEY);

+ 9 - 4
incubator-game/src/main/java/com/incubator/game/room/GDRoom.java

@@ -157,11 +157,15 @@ public class GDRoom extends Room {
             }
         } finally {
             lock.unlock();
-            // 房间没人直接解散
-            if (this.actors.isEmpty()) {
-                this.destroy();
+        }
+
+        // 广播
+        for (Player tmPlayer : this.actors.values()) {
+            if (tmPlayer != null) {
+                tmPlayer.receive(CommonProto.Cmd.DisbandRoom_VALUE, new HashMap<>());
             }
         }
+        this.destroy();
     }
 
     /**
@@ -1471,9 +1475,10 @@ public class GDRoom extends Room {
         if (!this.actors.isEmpty()) {
             for (Player player : this.actors.values()) {
                 if (player != null) {
-                    this.leaveRoom(player);
+                    player.resetRoom();
                 }
             }
+            this.actors.clear();
         }
 
         Log.debug("清理房间数据...");

+ 1 - 8
incubator-game/src/main/java/com/incubator/game/room/Room.java

@@ -106,19 +106,12 @@ public class Room implements GRoomInterface {
      */
     public void resetRoom() {}
 
-    /**
-     * 重置房间,用于对象池回收前清理状态
-     */
-    public void reset() {
-        this.isActive = false;
-    }
-
     /**
      * 销毁房间,释放资源
      */
     public void destroy() {
         Log.info("Room {} has been destroyed.", this.data.roomId);
-        this.reset();
+        this.isActive = false;
         this.scheduler.shutdownNow();
         this.scheduler = null;
         // 从缓存移除

+ 9 - 4
incubator-game/src/main/java/com/incubator/game/room/ZDRoom.java

@@ -147,11 +147,15 @@ public class ZDRoom extends Room {
             }
         } finally {
             lock.unlock();
-            // 房间没人直接解散
-            if (this.actors.isEmpty()) {
-                this.destroy();
+        }
+
+        // 广播
+        for (Player tmPlayer : this.actors.values()) {
+            if (tmPlayer != null) {
+                tmPlayer.receive(CommonProto.Cmd.DisbandRoom_VALUE, new HashMap<>());
             }
         }
+        this.destroy();
     }
 
     /**
@@ -1502,9 +1506,10 @@ public class ZDRoom extends Room {
         if (!this.actors.isEmpty()) {
             for (Player player : this.actors.values()) {
                 if (player != null) {
-                    this.leaveRoom(player);
+                    player.resetRoom();
                 }
             }
+            this.actors.clear();
         }
 
         Log.debug("清理房间数据...");