|
| 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 | +} |
0 commit comments