Skip to content

Commit 52d3cb3

Browse files
committed
feat(room): The room supports convenient broadcastRange.
1 parent 048dee6 commit 52d3cb3

File tree

8 files changed

+146
-33
lines changed

8 files changed

+146
-33
lines changed

widget/light-client/src/main/java/com/iohao/game/external/client/AbstractInputCommandRegion.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ protected void ofListen(CallbackDelegate callback, int subCmd, String title) {
105105
.setTitle(title);
106106
}
107107

108+
protected void ofListen(CallbackDelegate callback, CmdInfo cmd, String title) {
109+
this.ofListen(cmd.getSubCmd())
110+
.setCallback(callback)
111+
.setTitle(title);
112+
}
113+
108114
private ListenCommand ofListen(int subCmd) {
109115
CmdInfo cmdInfo = inputCommandCreate.ofCmdInfo(subCmd);
110116
ListenCommand listenCommand = new ListenCommand(cmdInfo);

widget/light-game-room/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
<groupId>com.iohao.game</groupId>
2525
<artifactId>light-domain-event</artifactId>
2626
<version>${project.parent.version}</version>
27-
<scope>provided</scope>
2827
</dependency>
2928
</dependencies>
3029

widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/Room.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package com.iohao.game.widget.light.room;
2020

21+
import com.iohao.game.action.skeleton.core.CmdInfo;
2122
import com.iohao.game.action.skeleton.core.flow.FlowContext;
2223
import com.iohao.game.action.skeleton.core.flow.FlowContextKit;
2324
import com.iohao.game.common.kit.OperationCode;
@@ -126,7 +127,7 @@ default Map<Long, Player> getRobotMap() {
126127
* get 房间状态
127128
*
128129
* @return 房间状态
129-
* @deprecated 没有代替,请开发者自行定义房间状态
130+
* @deprecated Developers should define room states themselves.
130131
*/
131132
@Deprecated
132133
RoomStatusEnum getRoomStatusEnum();
@@ -135,7 +136,7 @@ default Map<Long, Player> getRobotMap() {
135136
* set 房间状态
136137
*
137138
* @param roomStatusEnum 房间状态
138-
* @deprecated 没有代替,请开发者自行定义房间状态
139+
* @deprecated Developers should define room states themselves.
139140
*/
140141
@Deprecated
141142
void setRoomStatusEnum(RoomStatusEnum roomStatusEnum);
@@ -357,7 +358,7 @@ default void removePlayer(Player player) {
357358
*
358359
* @param roomStatusEnum 房间状态
359360
* @return true 是所指定的房间状态
360-
* @deprecated 没有代替,请开发者自行定义房间状态
361+
* @deprecated Developers should define room states themselves.
361362
*/
362363
@Deprecated
363364
default boolean isStatus(RoomStatusEnum roomStatusEnum) {
@@ -661,4 +662,55 @@ default void operation(OperationHandler operationHandler, FlowContext flowContex
661662
.setCommand(commandMessage)
662663
.execute();
663664
}
665+
666+
/**
667+
* Broadcast data to a specific user.
668+
*
669+
* @param cmdInfo cmdInfo
670+
* @param data data
671+
* @param userId userId
672+
* @since 21.28
673+
*/
674+
default void broadcastToUser(CmdInfo cmdInfo, Object data, long userId) {
675+
if (this.isRobot(userId)) {
676+
return;
677+
}
678+
679+
this.getAggregationContext().broadcast(cmdInfo, data, userId);
680+
}
681+
682+
/**
683+
* Broadcast data, excluding specified players.
684+
*
685+
* @param cmdInfo cmdInfo
686+
* @param data data
687+
* @param excludeUserId excludeUserId
688+
* @since 21.28
689+
*/
690+
default void broadcastRange(CmdInfo cmdInfo, Object data, long excludeUserId) {
691+
if (this.isEmptyRealPlayer()) {
692+
return;
693+
}
694+
695+
var playerIdList = this.listRealPlayerId();
696+
if (playerIdList.isEmpty()) {
697+
return;
698+
}
699+
700+
this.ofEmptyRangeBroadcast()
701+
.addUserId(playerIdList, excludeUserId)
702+
.setResponseMessage(cmdInfo, data)
703+
.execute();
704+
}
705+
706+
/**
707+
* Broadcast data
708+
*
709+
* @param cmdInfo cmdInfo
710+
* @param data data
711+
* @since 21.28
712+
*/
713+
default void broadcastRange(CmdInfo cmdInfo, Object data) {
714+
broadcastRange(cmdInfo, data, 0);
715+
}
664716
}

widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/RoomStatusEnum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @author 渔民小镇
2525
* @date 2022-03-31
2626
* @since 17
27-
* @deprecated 没有代替,请开发者自行定义房间状态
27+
* @deprecated Developers should define room states themselves.
2828
*/
2929
@Deprecated
3030
public enum RoomStatusEnum {

widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/flow/GameFixedService.java

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
package com.iohao.game.widget.light.room.flow;
2020

21-
import com.iohao.game.widget.light.room.Player;
22-
import com.iohao.game.widget.light.room.Room;
23-
2421
/**
2522
* 游戏流程 - 相对固定的流程。如,创建房间、创建玩家、进入房间;解散房间、退出房间、玩家准备。
2623
* <pre>
@@ -37,29 +34,7 @@
3734
* @date 2024-05-15
3835
* @since 21.8
3936
*/
40-
public interface GameFixedService {
41-
/**
42-
* 创建房间, 子类只需要关心房间配置和规则信息
43-
* <pre>
44-
* 延迟到子游戏中实现, 以便适应不同的子游戏规则
45-
* </pre>
46-
*
47-
* @param createContext 创建房间信息(及玩法规则)
48-
* @return 房间
49-
*/
50-
Room createRoom(RoomCreateContext createContext);
51-
52-
/**
53-
* 创建房间内的玩家
54-
* <pre>
55-
* 延迟到子游戏中实现, 以便适应不同的子游戏规则
56-
* </pre>
57-
*
58-
* @param gameFlowContext 游戏流程上下文
59-
* @return 玩家
60-
*/
61-
Player createPlayer(GameFlowContext gameFlowContext);
62-
37+
public interface GameFixedService extends RoomCreator, PlayerCreator {
6338
/**
6439
* 进入房间 (重连)
6540
*
@@ -71,6 +46,7 @@ public interface GameFixedService {
7146
* 解散房间
7247
*
7348
* @param gameFlowContext gameFlowContext
49+
* @deprecated non
7450
*/
7551
default void dissolveRoom(GameFlowContext gameFlowContext) {
7652
}

widget/light-game-room/src/main/java/com/iohao/game/widget/light/room/flow/GameStartService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public interface GameStartService {
4040
*
4141
* @param gameFlowContext 开始游戏上下文
4242
*/
43-
void startGameVerify(GameFlowContext gameFlowContext);
43+
default void startGameVerify(GameFlowContext gameFlowContext) {
44+
}
4445

4546
/**
4647
* 游戏开始,会在 {@link GameStartService#startGameVerify(GameFlowContext)} 校验成功后执行。
@@ -51,7 +52,8 @@ public interface GameStartService {
5152
*
5253
* @param gameFlowContext 开始游戏上下文
5354
*/
54-
void startGameVerifyAfter(GameFlowContext gameFlowContext);
55+
default void startGameVerifyAfter(GameFlowContext gameFlowContext) {
56+
}
5557

5658
/**
5759
* 执行游戏开始,内部会调用 {@link GameStartService#startGameVerify(GameFlowContext)}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* ioGame
3+
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
4+
* # iohao.com . 渔民小镇
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.iohao.game.widget.light.room.flow;
20+
21+
import com.iohao.game.widget.light.room.Player;
22+
23+
/**
24+
* @author 渔民小镇
25+
* @date 2025-06-03
26+
* @since 21.28
27+
*/
28+
public interface PlayerCreator {
29+
/**
30+
* 创建房间内的玩家
31+
* <pre>
32+
* 延迟到子游戏中实现, 以便适应不同的子游戏规则
33+
* </pre>
34+
*
35+
* @param gameFlowContext 游戏流程上下文
36+
* @return 玩家
37+
*/
38+
Player createPlayer(GameFlowContext gameFlowContext);
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* ioGame
3+
* Copyright (C) 2021 - present 渔民小镇 ([email protected][email protected]) . All Rights Reserved.
4+
* # iohao.com . 渔民小镇
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as
8+
* published by the Free Software Foundation, either version 3 of the
9+
* License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.iohao.game.widget.light.room.flow;
20+
21+
import com.iohao.game.widget.light.room.Room;
22+
23+
/**
24+
* @author 渔民小镇
25+
* @date 2025-06-03
26+
* @since 21.28
27+
*/
28+
public interface RoomCreator {
29+
/**
30+
* 创建房间, 子类只需要关心房间配置和规则信息
31+
* <pre>
32+
* 延迟到子游戏中实现, 以便适应不同的子游戏规则
33+
* </pre>
34+
*
35+
* @param createContext 创建房间信息(及玩法规则)
36+
* @return 房间
37+
*/
38+
Room createRoom(RoomCreateContext createContext);
39+
}

0 commit comments

Comments
 (0)