Skip to content

Commit 7c98480

Browse files
authored
Merge pull request #2 from leancloud/feat/hook-test
add game hook test
2 parents e7eadfd + 02f64ce commit 7c98480

File tree

26 files changed

+1395
-23
lines changed

26 files changed

+1395
-23
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea/
2+
.vscode/
3+
__pycache__/
24
# Compiled class file
35
*.class
46

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

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
[local]
3+
APPID=hahahahaha-long-long-long-long-local-appid
4+
APPKEY=keykeykeykey
5+
APP_MASTER_KEY=master-key
6+
GAME_ROUTER_URL=http://localhost:8081/v1
7+
GAME_ROUTER_INTERNAL_URL=http://localhost:8081
8+
GAME_ROUTER_AUTH_KEY=key-auth-key
9+
TEST_WITH_HOOK=True
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
RED='\033[0;31m'
4+
NC='\033[0m'
5+
6+
CONFIG_TAG="local"
7+
8+
export PYTHONPATH=../testing-tools
9+
10+
function execute_test
11+
{
12+
local TEST_FILE=$1
13+
14+
echo -e "\n${RED}start run test: $TEST_FILE${NC}\n"
15+
python3 $TEST_FILE $CONFIG_TAG
16+
if [ "$?" -ne 0 ]; then
17+
echo -e "\n${RED}failed finishing test: $TEST_FILE${NC}\n"
18+
break
19+
else
20+
echo -e "\n${RED}finishing test: $TEST_FILE${NC}\n"
21+
fi
22+
}
23+
24+
if [ "$#" == 1 ]; then
25+
CONFIG_TAG=$1
26+
fi
27+
28+
if [ "$#" == 2 ]; then
29+
CONFIG_TAG=$1
30+
execute_test $2
31+
else
32+
for i in test_*.py; do
33+
execute_test $i
34+
sleep 0.5
35+
done
36+
37+
fi
38+
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()

0 commit comments

Comments
 (0)