Skip to content

Commit 05fc286

Browse files
authored
Merge pull request #3 from leancloud/fix/hook
hook 1.3
2 parents e89f39e + 060c2c9 commit 05fc286

21 files changed

+95
-76
lines changed

game-hook-base/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>cn.leancloud.play</groupId>
88
<artifactId>game-hook-base</artifactId>
9-
<version>1.3-SNAPSHOT</version>
9+
<version>1.3</version>
1010

1111
<properties>
1212
<clojure.version>1.10.0</clojure.version>

game-hook-base/src/main/java/cn/leancloud/play/hook/AbstractGameHook.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
import cn.leancloud.play.hook.context.*;
44

55
public abstract class AbstractGameHook implements GameHook{
6+
private final HookedRoom room;
7+
8+
public AbstractGameHook(HookedRoom room) {
9+
this.room = room;
10+
}
11+
12+
@Override
13+
public HookedRoom getHookedRoom() {
14+
return room;
15+
}
16+
617
@Override
718
public void onCreateRoom(CreateRoomContext ctx) {
819
ctx.continueProcess();

game-hook-base/src/main/java/cn/leancloud/play/hook/GameHook.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import cn.leancloud.play.hook.context.*;
44

55
public interface GameHook {
6+
/**
7+
* 获取与当前 Game Hook 绑定的 Room 实例
8+
*
9+
* @return 绑定的 Room 实例
10+
*/
11+
HookedRoom getHookedRoom();
12+
613
/**
714
* 创建房间前调用
815
*

game-hook-base/src/main/java/cn/leancloud/play/hook/HookedRoom.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@
66

77
import java.util.List;
88
import java.util.Map;
9+
import java.util.concurrent.ScheduledExecutorService;
910

1011
/**
1112
* 与 Hook 绑定的房间实例,用于获取房间信息以及对房间进行操作。
1213
*/
1314
public interface HookedRoom {
15+
/**
16+
* 获取一个 ScheduledExecutorService 用于将某个任务交给与当前房间绑定的线程来执行。在该线程上能保证任务运行期间,房间
17+
* 属性不会发生变化,但需要任务尽快执行完以避免过长时间的阻塞线程。
18+
*
19+
* 因为性能原因,返回的 ScheduledExecutorService 时间精度为 20ms,比如布置一个任务 30ms 后执行则实际
20+
* 执行时间在 30ms ~ 50ms 之间,即不会早于预期执行时间且与预期执行时间最大偏差为 20ms。
21+
*
22+
* 返回的 ScheduledExecutorService 不能被 shutdown,执行 shutdown 没有任何作用。
23+
*
24+
* @return ScheduledExecutorService
25+
*/
26+
ScheduledExecutorService getScheduler();
27+
1428
/**
1529
* 获取房间名称
1630
*
@@ -100,24 +114,24 @@ public interface HookedRoom {
100114
* 更改房间玩家自定义属性
101115
*
102116
* @param targetActorId 目标玩家 Actor Id
103-
* @param valuesToSet 待修改的玩家自定义属性
104-
* @param expectedValues 设置 CAS 操作用于匹配的玩家自定义属性。设置了用于匹配的玩家自定义属性后,只有当玩家自定义属性符合
117+
* @param valuesToSet 待修改的玩家自定义属性,不能为 null
118+
* @param expectedValues 不能为 null。设置 CAS 操作用于匹配的玩家自定义属性。设置了用于匹配的玩家自定义属性后,只有当玩家自定义属性符合
105119
* 匹配的值后更新玩家自定义属性操作才会生效。
106120
*/
107121
void updatePlayerProperty(int targetActorId, Map<String, Object> valuesToSet, Map<String, Object> expectedValues);
108122

109123
/**
110124
* 更改房间自定义属性
111125
*
112-
* @param valuesToSet 待修改的房间自定义属性
113-
* @param expectedValues 进行 CAS 操作时使用,用于匹配的房间自定义属性。设置了用于匹配的自定义属性后,只有当房间自定义属性符合
126+
* @param valuesToSet 待修改的房间自定义属性, 不能为 null
127+
* @param expectedValues 不能为 null。进行 CAS 操作时使用,用于匹配的房间自定义属性。设置了用于匹配的自定义属性后,只有当房间自定义属性符合
114128
* 匹配的值后更新房间自定义属性操作才会生效。
115129
*/
116130
void updateRoomProperty(Map<String, Object> valuesToSet, Map<String, Object> expectedValues);
117131

118132
/**
119133
* 更改房间系统属性
120-
* @param property 待修改的系统属性
134+
* @param property 待修改的系统属性,不能为 null
121135
* @param <K> 具体要改的系统属性
122136
* @param <V> 待修改后的值
123137
*/
@@ -133,20 +147,20 @@ public interface HookedRoom {
133147
/**
134148
* 发送事件给目标 Actor。
135149
*
136-
* @param toActorsIds 目标 Actor Id 列表
137-
* @param fromActorId 发送事件的 Actor Id
138-
* @param data 事件数据
139-
* @param options 发送事件配置选项
150+
* @param toActorsIds 目标 Actor Id 列表,不能为 null
151+
* @param fromActorId 发送事件的 Actor Id,为 0 表示由系统发出
152+
* @param data 事件数据,不能为 null
153+
* @param options 发送事件配置选项,不能为 null
140154
*/
141155
void raiseRpcToActors(List<Integer> toActorsIds, int fromActorId, byte[] data, RaiseRpcOptions options);
142156

143157
/**
144158
* 发送事件给目标接收组
145159
*
146-
* @param toReceiverGroup 目标接收组
147-
* @param fromActorId 发送事件的 Actor Id
148-
* @param data 事件数据
149-
* @param options 发送事件配置选项
160+
* @param toReceiverGroup 目标接收组,不能为 null
161+
* @param fromActorId 发送事件的 Actor Id,为 0 表示由系统发出
162+
* @param data 事件数据,不能为 null
163+
* @param options 发送事件配置选项,不能为 null
150164
*/
151165
void raiseRpcToReceiverGroup(ReceiverGroup toReceiverGroup, int fromActorId, byte[] data, RaiseRpcOptions options);
152166
}

game-hook-base/src/main/java/cn/leancloud/play/hook/context/AbstractDeferrableContext.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
54
import cn.leancloud.play.hook.request.RoomRequest;
65

76
import java.util.concurrent.CompletableFuture;
@@ -10,8 +9,8 @@ abstract class AbstractDeferrableContext<T extends RoomRequest>
109
extends AbstractOperationContext<T>
1110
implements DeferableContext<T>, SkipableContext<T>{
1211

13-
AbstractDeferrableContext(T req, HookedRoom hookedRoom, CompletableFuture<HookResponse<T>> future) {
14-
super(req, hookedRoom, future);
12+
AbstractDeferrableContext(T req, CompletableFuture<HookResponse<T>> future) {
13+
super(req, future);
1514
}
1615

1716
@Override

game-hook-base/src/main/java/cn/leancloud/play/hook/context/AbstractOperationContext.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
54
import cn.leancloud.play.hook.Reason;
65
import cn.leancloud.play.hook.request.RoomRequest;
76

@@ -11,18 +10,16 @@
1110
abstract class AbstractOperationContext<T extends RoomRequest> implements Context<T> {
1211
private final CompletableFuture<HookResponse<T>> future;
1312
private final T req;
14-
private final HookedRoom hookedRoom;
1513

1614
private ContextStatus status;
1715

18-
AbstractOperationContext(T req, HookedRoom hookedRoom, CompletableFuture<HookResponse<T>> future) {
16+
AbstractOperationContext(T req, CompletableFuture<HookResponse<T>> future) {
1917
Objects.requireNonNull(req);
2018
Objects.requireNonNull(future);
2119

2220
this.req = req;
2321
this.future = future;
2422
this.status = ContextStatus.NEW;
25-
this.hookedRoom = hookedRoom;
2623
}
2724

2825
@Override
@@ -35,11 +32,6 @@ public T getRequest() {
3532
return req;
3633
}
3734

38-
@Override
39-
public HookedRoom getHookedRoom() {
40-
return hookedRoom;
41-
}
42-
4335
@Override
4436
public void continueProcess() {
4537
updateStatusWhenNotProcessed(ContextStatus.CONTINUED);
@@ -52,11 +44,16 @@ public void rejectProcess(Reason reason) {
5244
completeHookFuture(HookResponse.reject(reason));
5345
}
5446

47+
@Override
48+
public boolean isProcessed(){
49+
return status.getCode() > ContextStatus.DEFERRED.getCode();
50+
}
51+
5552
void updateStatusWhenNotProcessed(ContextStatus newStatus) {
56-
if (status == ContextStatus.NEW || status == ContextStatus.DEFERRED) {
57-
status = newStatus;
58-
} else {
53+
if (isProcessed()) {
5954
throw new IllegalStateException("request already processed");
55+
} else {
56+
status = newStatus;
6057
}
6158
}
6259

game-hook-base/src/main/java/cn/leancloud/play/hook/context/BeforeJoinRoomContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
5-
import cn.leancloud.play.hook.Reason;
64
import cn.leancloud.play.hook.request.JoinRoomRequest;
7-
import cn.leancloud.play.utils.Log;
85

96
import java.util.concurrent.CompletableFuture;
107

118
public final class BeforeJoinRoomContext extends AbstractOperationContext<JoinRoomRequest> {
129
public BeforeJoinRoomContext(JoinRoomRequest req,
13-
HookedRoom hookedRoom,
1410
CompletableFuture<HookResponse<JoinRoomRequest>> future) {
15-
super(req, hookedRoom, future);
11+
super(req, future);
1612
}
1713

1814
@Override

game-hook-base/src/main/java/cn/leancloud/play/hook/context/BeforeLeaveRoomContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
54
import cn.leancloud.play.hook.request.LeaveRoomRequest;
65

76
import java.util.concurrent.CompletableFuture;
87

98
public final class BeforeLeaveRoomContext extends AbstractOperationContext<LeaveRoomRequest> {
109
public BeforeLeaveRoomContext(LeaveRoomRequest req,
11-
HookedRoom hookedRoom,
1210
CompletableFuture<HookResponse<LeaveRoomRequest>> future) {
13-
super(req, hookedRoom, future);
11+
super(req, future);
1412
}
1513

1614
@Override

game-hook-base/src/main/java/cn/leancloud/play/hook/context/BeforeRaiseRpcContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
54
import cn.leancloud.play.hook.request.RaiseRpcRequest;
65

76
import java.util.concurrent.CompletableFuture;
87

98
public final class BeforeRaiseRpcContext extends AbstractDeferrableContext<RaiseRpcRequest> {
109
public BeforeRaiseRpcContext(RaiseRpcRequest req,
11-
HookedRoom hookedRoom,
1210
CompletableFuture<HookResponse<RaiseRpcRequest>> future) {
13-
super(req, hookedRoom, future);
11+
super(req, future);
1412
}
1513

1614
@Override

game-hook-base/src/main/java/cn/leancloud/play/hook/context/BeforeSetPlayerPropertiesContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package cn.leancloud.play.hook.context;
22

33
import cn.leancloud.play.hook.HookResponse;
4-
import cn.leancloud.play.hook.HookedRoom;
54
import cn.leancloud.play.hook.request.SetPlayerPropertiesRequest;
65

76
import java.util.concurrent.CompletableFuture;
87

98
public final class BeforeSetPlayerPropertiesContext extends AbstractDeferrableContext<SetPlayerPropertiesRequest> {
109
public BeforeSetPlayerPropertiesContext(SetPlayerPropertiesRequest req,
11-
HookedRoom hookedRoom,
1210
CompletableFuture<HookResponse<SetPlayerPropertiesRequest>> future) {
13-
super(req, hookedRoom, future);
11+
super(req, future);
1412
}
1513

1614
@Override

0 commit comments

Comments
 (0)