From bb470a8d6de9d36b5827682246f4787280989eab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=94=E6=B0=91=E5=B0=8F=E9=95=87?= <262610965@qq.com> Date: Mon, 16 Dec 2024 16:19:51 +0800 Subject: [PATCH] perf(room): Player adds isRobot method. Room adds methods to distinguish between real players and robot players. --- .../iohao/game/widget/light/room/Player.java | 10 + .../iohao/game/widget/light/room/Room.java | 182 +++++++++++++++++- .../game/widget/light/room/SimpleRoom.java | 2 + 3 files changed, 185 insertions(+), 9 deletions(-) diff --git a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Player.java b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Player.java index d4c8e45f5..024865b08 100644 --- a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Player.java +++ b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Player.java @@ -83,4 +83,14 @@ public interface Player extends Serializable { * @param ready true - 已准备 */ void setReady(boolean ready); + + /** + * 是否 Robot + * + * @return true 该玩家是 Robot + * @since 21.23 + */ + default boolean isRobot() { + return false; + } } diff --git a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java index 661165467..f200f7af1 100644 --- a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java +++ b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java @@ -26,9 +26,7 @@ import com.iohao.game.widget.light.room.operation.SimpleOperationHandler; import java.io.Serializable; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.stream.Stream; @@ -43,7 +41,7 @@ @SuppressWarnings("unchecked") public interface Room extends Serializable, RoomBroadcastEnhance { /** - * 玩家 + * 玩家,包含 Robot *
      *     key : userId
      *     value : player
@@ -53,6 +51,34 @@ public interface Room extends Serializable, RoomBroadcastEnhance {
      */
     Map getPlayerMap();
 
+    /**
+     * 所有真实的玩家
+     * 
+     *     key : userId
+     *     value : player
+     * 
+ * + * @return 真实的玩家 + * @since 21.23 + */ + default Map getRealPlayerMap() { + return Collections.emptyMap(); + } + + /** + * 所有 Robot + *
+     *     key : userId
+     *     value : player
+     * 
+ * + * @return Robot Map + * @since 21.23 + */ + default Map getRobotMap() { + return Collections.emptyMap(); + } + /** * 玩家位置 *
@@ -140,7 +166,7 @@ default boolean isCreatorUserId(long userId) {
     }
 
     /**
-     * 玩家列表: 所有玩家
+     * 玩家列表: 所有玩家,包含 Robot
      *
      * @param  玩家
      * @return 所有玩家
@@ -158,6 +184,45 @@ default  Stream streamPlayer() {
         return (Stream) this.listPlayer().stream();
     }
 
+
+    /**
+     * 真实玩家列表: 所有的真实玩家(不包含 Robot)
+     *
+     * @param  玩家
+     * @return 所有玩家
+     */
+    default  Collection listRealPlayer() {
+        return (Collection) this.getRealPlayerMap().values();
+    }
+
+    /**
+     * steam real players (真实的玩家)
+     *
+     * @return player Stream
+     */
+    default  Stream streamRealPlayer() {
+        return (Stream) listRealPlayer().stream();
+    }
+
+    /**
+     * RobotList
+     *
+     * @param  Robot
+     * @return RobotList
+     */
+    default  Collection listRobot() {
+        return (Collection) this.getRobotMap().values();
+    }
+
+    /**
+     * steam players
+     *
+     * @return player Stream
+     */
+    default  Stream streamRobot() {
+        return (Stream) this.listRobot().stream();
+    }
+
     /**
      * userId Collection
      *
@@ -167,6 +232,24 @@ default Collection listPlayerId() {
         return this.getPlayerMap().keySet();
     }
 
+    /**
+     * Robot UserId Collection
+     *
+     * @return userId
+     */
+    default Collection listRealPlayerId() {
+        return this.getRealPlayerMap().keySet();
+    }
+
+    /**
+     * Robot UserId Collection
+     *
+     * @return userId
+     */
+    default Collection listRobotPlayerId() {
+        return this.getRobotMap().keySet();
+    }
+
     /**
      * 通过 userId 查找玩家
      *
@@ -195,9 +278,32 @@ default boolean existUser(long userId) {
      */
     default void addPlayer(Player player) {
         player.setRoomId(this.getRoomId());
-        long userId = player.getUserId();
+        var userId = player.getUserId();
         this.getPlayerMap().put(userId, player);
         this.getPlayerSeatMap().put(player.getSeat(), userId);
+
+        if (notOverrideRealAndRobotMap()) {
+            return;
+        }
+
+        if (player.isRobot()) {
+            this.getRobotMap().put(userId, player);
+        } else {
+            this.getRealPlayerMap().put(userId, player);
+        }
+    }
+
+    /**
+     * 是否重写了 {@link #getRealPlayerMap()} 和 {@link #getRobotMap()} 方法
+     *
+     * @return true 表示其中一个方法没有重写
+     * @since 21.23
+     */
+    private boolean notOverrideRealAndRobotMap() {
+        Object realPlayerMap = this.getRealPlayerMap();
+        Object robotMap = this.getRobotMap();
+        Object emptyMap = Collections.emptyMap();
+        return realPlayerMap == emptyMap || robotMap == emptyMap;
     }
 
     /**
@@ -209,6 +315,16 @@ default void removePlayer(Player player) {
         long userId = player.getUserId();
         this.getPlayerMap().remove(userId);
         this.getPlayerSeatMap().remove(player.getSeat());
+
+        if (notOverrideRealAndRobotMap()) {
+            return;
+        }
+
+        if (player.isRobot()) {
+            this.getRobotMap().remove(userId);
+        } else {
+            this.getRealPlayerMap().remove(userId);
+        }
     }
 
     /**
@@ -245,7 +361,7 @@ default void ifPlayerNotExist(long userId, Runnable runnable) {
     }
 
     /**
-     * 统计房间内的玩家数量
+     * 统计房间内的玩家数量,包含机器人
      *
      * @return 玩家数量
      */
@@ -254,14 +370,62 @@ default int countPlayer() {
     }
 
     /**
-     * 房间内的是否没有玩家
+     * 统计房间内真实玩家的数量
+     *
+     * @return 真实玩家的数量
+     */
+    default int countRealPlayer() {
+        return this.getRealPlayerMap().size();
+    }
+
+    /**
+     * 统计房间内 Robot 的数量
+     *
+     * @return Robot 数量
+     */
+    default int countRobot() {
+        return this.getRobotMap().size();
+    }
+
+    /**
+     * 房间内是否没有玩家,包含 Robot
      *
-     * @return true 房间内没有玩家了
+     * @return true 房间内没有任何玩家
      */
     default boolean isEmptyPlayer() {
         return this.getPlayerMap().isEmpty();
     }
 
+    /**
+     * 房间内是否没有真实的玩家
+     *
+     * @return true 房间内没有真实玩家
+     */
+    default boolean isEmptyRealPlayer() {
+        return this.getRealPlayerMap().isEmpty();
+    }
+
+    /**
+     * 房间内是否没有 Robot
+     *
+     * @return true 房间内没有 Robot
+     */
+    default boolean isEmptyRobot() {
+        return this.getRealPlayerMap().isEmpty();
+    }
+
+    /**
+     * 是否 Robot
+     *
+     * @param userId userId
+     * @return true: Robot
+     * @since 21.23
+     */
+    default boolean isRobot(long userId) {
+        Player player = this.getPlayerById(userId);
+        return Objects.nonNull(player) && player.isRobot();
+    }
+
     /**
      * 得到一个空位置
      *
diff --git a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java
index 6df846f23..2f21b3458 100644
--- a/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java
+++ b/widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/SimpleRoom.java
@@ -52,6 +52,8 @@ public class SimpleRoom implements Room {
      * 
*/ final Map playerMap = new NonBlockingHashMap<>(); + final Map realPlayerMap = new NonBlockingHashMap<>(); + final Map robotMap = new NonBlockingHashMap<>(); /** * 玩家位置 *