Skip to content

Commit c51af16

Browse files
committed
(fix) game hook test
1 parent 52498f3 commit c51af16

File tree

8 files changed

+136
-119
lines changed

8 files changed

+136
-119
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.2-SNAPSHOT</version>
9+
<version>1.3-SNAPSHOT</version>
1010

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

game-hook-template/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<dependency>
1313
<groupId>cn.leancloud.play</groupId>
1414
<artifactId>game-hook-base</artifactId>
15-
<version>1.1</version>
15+
<version>1.2</version>
1616
</dependency>
1717
</dependencies>
1818

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.leancloud.play.hook.template;
2+
3+
import cn.leancloud.play.hook.AbstractGameHook;
4+
import cn.leancloud.play.hook.Actor;
5+
import cn.leancloud.play.hook.HookedRoom;
6+
import cn.leancloud.play.hook.context.BeforeRaiseRpcContext;
7+
import cn.leancloud.play.hook.request.RaiseRpcOptions;
8+
import cn.leancloud.play.hook.request.RaiseRpcRequest;
9+
import cn.leancloud.play.hook.request.ReceiverGroup;
10+
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
public class MasterIsWatchingYouHook extends AbstractGameHook {
16+
@Override
17+
public void onBeforeRaiseRpc(BeforeRaiseRpcContext ctx) {
18+
RaiseRpcRequest req = ctx.getRequest();
19+
HookedRoom room = ctx.getHookedRoom();
20+
Actor master = room.getMaster();
21+
if (master == null) {
22+
// no master in this room
23+
// reject and swallow this request
24+
ctx.skipProcess();
25+
return;
26+
}
27+
28+
boolean masterIsInTargets = true;
29+
List<Integer> targetActors = req.getToActorIds();
30+
if (!targetActors.isEmpty() &&
31+
targetActors.stream().noneMatch(actorId -> actorId == master.getActorId())) {
32+
masterIsInTargets = false;
33+
34+
ArrayList<Integer> newTargets = new ArrayList<>(targetActors);
35+
newTargets.add(master.getActorId());
36+
req.setToActorIds(newTargets);
37+
}
38+
39+
ctx.continueProcess();
40+
41+
if (!masterIsInTargets) {
42+
String msg = String.format("actor %d is sending sneaky rpc", req.getFromActorId());
43+
room.raiseRpcToReceiverGroup(ReceiverGroup.ALL,
44+
master.getActorId(),
45+
msg.getBytes(StandardCharsets.UTF_8),
46+
RaiseRpcOptions.emptyOption);
47+
}
48+
}
49+
}

game-hook-template/src/main/java/hook/template/MyFancyGameHook.java renamed to game-hook-template/src/main/java/cn/leancloud/play/hook/template/MyFancyGameHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hook.template;
1+
package cn.leancloud.play.hook.template;
22

33
import cn.leancloud.play.hook.AbstractGameHook;
44
import cn.leancloud.play.hook.context.*;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.leancloud.play.hook.template;
2+
3+
import cn.leancloud.play.hook.HookedRoom;
4+
import cn.leancloud.play.utils.Log;
5+
import cn.leancloud.play.hook.GameHook;
6+
import cn.leancloud.play.hook.HookFactory;
7+
8+
import java.util.Map;
9+
10+
public class MyFancyHookFactory implements HookFactory {
11+
@Override
12+
public GameHook create(HookedRoom room, String hookName, Map<String, String> initConfigs) {
13+
if (hookName != null && hookName.length() > 0) {
14+
switch (hookName) {
15+
case "fancy-hook":
16+
return new MyFancyGameHook(initConfigs);
17+
case "master is watching you hook":
18+
return new MasterIsWatchingYouHook();
19+
}
20+
}
21+
22+
Log.error("unknown hook name {}", hookName);
23+
return null;
24+
}
25+
}

game-hook-template/src/main/java/hook/template/MyFancyHookFactory.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from tools.base import client
2+
from tools import utils
3+
from tools.base import lobby_shortcuts
4+
from tools.base.matcher import MATCH_ANY
5+
from tools.base import config
6+
7+
8+
@utils.integration_test()
9+
def test_game_hook():
10+
if not config.TEST_WITH_HOOK:
11+
return
12+
13+
room_id = "master_is_watching_you_hook_room"
14+
client_a, client_b, client_c = 'Alpha', 'Bravo', 'Charlie'
15+
16+
ca = utils.open_session(
17+
client_a,
18+
lambda: lobby_shortcuts.create_room(client_a, room_id=room_id))
19+
ca.send_msg_with_expect_msgs({
20+
'cmd': 'conv',
21+
'op': 'start',
22+
'cid': room_id,
23+
'maxMember': 23,
24+
'hookName': 'master is watching you hook'
25+
}, [{
26+
'cmd': 'conv',
27+
'op': 'started',
28+
'masterActorId': 1,
29+
"members": [{
30+
"pid": client_a,
31+
"actorId": 1,
32+
"attr": {}
33+
}],
34+
"open": True
35+
}])
36+
37+
cb, cc = list(
38+
map(lambda c: utils.join_room(c, room_id), [client_b, client_c]))
39+
40+
# Charlie send a secret msg to Bravo, but it is caught by master Alpha
41+
sneaky_msg = "I'm super man"
42+
future = client.add_expect_msgs_for_all(
43+
[ca, cb], [{
44+
'cmd': 'direct',
45+
'fromActorId': 3,
46+
'msg': sneaky_msg,
47+
'timestamp': MATCH_ANY
48+
}, {
49+
'cmd': 'direct',
50+
'fromActorId': 1,
51+
'msg': "actor 3 is sending sneaky rpc",
52+
'timestamp': MATCH_ANY
53+
}])
54+
cc.send_msg({'cmd': 'direct', 'msg': sneaky_msg, 'toActorIds': [2]}, )
55+
future.get()
56+
57+
58+
if __name__ == '__main__':
59+
test_game_hook()

integration-test-scripts/raise_rpc_hook.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)