Skip to content

Commit

Permalink
perf(room): Player adds isRobot method. Room adds methods to distingu…
Browse files Browse the repository at this point in the history
…ish between real players and robot players.
  • Loading branch information
iohao committed Dec 16, 2024
1 parent ba2edec commit bb470a8
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +41,7 @@
@SuppressWarnings("unchecked")
public interface Room extends Serializable, RoomBroadcastEnhance {
/**
* 玩家
* 玩家,包含 Robot
* <pre>
* key : userId
* value : player
Expand All @@ -53,6 +51,34 @@ public interface Room extends Serializable, RoomBroadcastEnhance {
*/
Map<Long, Player> getPlayerMap();

/**
* 所有真实的玩家
* <pre>
* key : userId
* value : player
* </pre>
*
* @return 真实的玩家
* @since 21.23
*/
default Map<Long, Player> getRealPlayerMap() {
return Collections.emptyMap();
}

/**
* 所有 Robot
* <pre>
* key : userId
* value : player
* </pre>
*
* @return Robot Map
* @since 21.23
*/
default Map<Long, Player> getRobotMap() {
return Collections.emptyMap();
}

/**
* 玩家位置
* <pre>
Expand Down Expand Up @@ -140,7 +166,7 @@ default boolean isCreatorUserId(long userId) {
}

/**
* 玩家列表: 所有玩家
* 玩家列表: 所有玩家,包含 Robot
*
* @param <T> 玩家
* @return 所有玩家
Expand All @@ -158,6 +184,45 @@ default <T extends Player> Stream<T> streamPlayer() {
return (Stream<T>) this.listPlayer().stream();
}


/**
* 真实玩家列表: 所有的真实玩家(不包含 Robot)
*
* @param <T> 玩家
* @return 所有玩家
*/
default <T extends Player> Collection<T> listRealPlayer() {
return (Collection<T>) this.getRealPlayerMap().values();
}

/**
* steam real players (真实的玩家)
*
* @return player Stream
*/
default <T extends Player> Stream<T> streamRealPlayer() {
return (Stream<T>) listRealPlayer().stream();
}

/**
* RobotList
*
* @param <T> Robot
* @return RobotList
*/
default <T extends Player> Collection<T> listRobot() {
return (Collection<T>) this.getRobotMap().values();
}

/**
* steam players
*
* @return player Stream
*/
default <T extends Player> Stream<T> streamRobot() {
return (Stream<T>) this.listRobot().stream();
}

/**
* userId Collection
*
Expand All @@ -167,6 +232,24 @@ default Collection<Long> listPlayerId() {
return this.getPlayerMap().keySet();
}

/**
* Robot UserId Collection
*
* @return userId
*/
default Collection<Long> listRealPlayerId() {
return this.getRealPlayerMap().keySet();
}

/**
* Robot UserId Collection
*
* @return userId
*/
default Collection<Long> listRobotPlayerId() {
return this.getRobotMap().keySet();
}

/**
* 通过 userId 查找玩家
*
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
}

/**
Expand Down Expand Up @@ -245,7 +361,7 @@ default void ifPlayerNotExist(long userId, Runnable runnable) {
}

/**
* 统计房间内的玩家数量
* 统计房间内的玩家数量,包含机器人
*
* @return 玩家数量
*/
Expand All @@ -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();
}

/**
* 得到一个空位置
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class SimpleRoom implements Room {
* </pre>
*/
final Map<Long, Player> playerMap = new NonBlockingHashMap<>();
final Map<Long, Player> realPlayerMap = new NonBlockingHashMap<>();
final Map<Long, Player> robotMap = new NonBlockingHashMap<>();
/**
* 玩家位置
* <pre>
Expand Down

0 comments on commit bb470a8

Please sign in to comment.