Ejemplo n.º 1
0
        private ICommand CompileTryCommand()
        {
            ICommand command = this.CompileSuite();

            var trycommand = new TryCommand(command);

            int indent = this.lexer.NextIndent();

            while (indent == this.indent)
            {
                if (this.TryCompile(TokenType.Name, "finally"))
                {
                    if (trycommand.Finally != null)
                    {
                        throw new SyntaxError("invalid syntax");
                    }

                    ICommand finallycommand = this.CompileSuite();
                    trycommand.SetFinally(finallycommand);
                }
                else
                {
                    break;
                }

                indent = this.lexer.NextIndent();
            }

            this.lexer.PushIndent(indent);

            return(trycommand);
        }
Ejemplo n.º 2
0
        public void CreateAndExecuteTryCommand()
        {
            BindingEnvironment environment = new BindingEnvironment();
            ICommand           body        = new SetCommand("a", new ConstantExpression(1));
            TryCommand         command     = new TryCommand(body);

            command.Execute(environment);

            Assert.AreEqual(1, environment.GetValue("a"));
        }
Ejemplo n.º 3
0
    public static void TryCommand(long roomId, long ownerId, long actorId, int commandId, int actionPointCost)
    {
        // 正规流程,应该是先向服务器申请行动点是否足够,等待服务器确认以后再真正地执行
        // 但是这样会导致服务器反应较为迟钝,而且客户端逻辑相对复杂,所有指令都要经过这样"申请/确认"的流程
        // 所以,这里先再客户端自己确认行动点是否足够以后,就先执行了,然后再发送执行消耗行动点
        // 如果服务器返回失败,则停止刚才的行为. 当然,这样做可能会导致之前的行动被打断,但是理论上服务器被驳回的几率较小,可以忽略
        TryCommand output = new TryCommand()
        {
            RoomId          = roomId,
            OwnerId         = ownerId,
            ActorId         = actorId,
            CommandId       = commandId,
            ActionPointCost = actionPointCost,
        };

        GameRoomManager.Instance.SendMsg(ROOM.TryCommand, output.ToByteArray());
    }
Ejemplo n.º 4
0
        public void ExecuteFinallyEvenWhenRaiseException()
        {
            BindingEnvironment environment = new BindingEnvironment();
            ICommand           body        = new SetCommand("a", new NameExpression("c"));
            ICommand           @finally    = new SetCommand("b", new ConstantExpression(2));
            TryCommand         command     = new TryCommand(body);

            command.SetFinally(@finally);

            try
            {
                command.Execute(environment);
                Assert.Fail("Exception expected");
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(NameError));
                Assert.AreEqual("name 'c' is not defined", ex.Message);
            }

            Assert.IsNull(environment.GetValue("a"));
            Assert.AreEqual(2, environment.GetValue("b"));
        }
Ejemplo n.º 5
0
    private void OnTryCommand(SocketAsyncEventArgs args, byte[] bytes)
    {
        TryCommand input = TryCommand.Parser.ParseFrom(bytes);

        if (input.RoomId != RoomId)
        {
            return; // 不是自己房间的消息,略过
        }
        PlayerInfoInRoom piir = GetPlayerInRoom(input.OwnerId);

        if (piir == null)
        {
            string          msg    = "在服务器没有找到该玩家!";
            TryCommandReply output = new TryCommandReply()
            {
                RoomId  = input.RoomId,
                OwnerId = input.OwnerId,
                Ret     = false,
                ErrMsg  = msg,
            };
            ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.TryCommandReply, output.ToByteArray());
            ServerRoomManager.Instance.Log("RoomLogic OnTryCommand Error - " + msg + $" - Id:{input.OwnerId}");
            return;
        }

        var csv             = CsvDataManager.Instance.GetTable("command_id");
        int actionPointCost = csv.GetValueInt(input.CommandId, "ActionPointCost");

        if (actionPointCost > 0)
        {
            bool   ret = true;
            string msg = "";
            if (actionPointCost != input.ActionPointCost)
            { // 服务器校验一下
                msg = $"行动点数服务器与客户端不一致! {input.ActionPointCost} : {actionPointCost}";
                ret = false;
            }

            if (piir.ActionPoint < input.ActionPointCost)
            {
                msg = "行动点不足, 请稍后再试!";
                ret = false;
            }

            if (!ret)
            {
                TryCommandReply output = new TryCommandReply()
                {
                    RoomId  = input.RoomId,
                    OwnerId = input.OwnerId,
                    Ret     = false,
                    ErrMsg  = msg,
                };
                ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.TryCommandReply, output.ToByteArray());
                ServerRoomManager.Instance.Log("RoomLogic OnTryCommand Error - " + msg);
                return;
            }
            // 扣除行动点数
            piir.AddActionPoint(-input.ActionPointCost);
        }

        {
            // 行动点发生变化,要通知客户端
            UpdateActionPointReply output = new UpdateActionPointReply()
            {
                RoomId         = input.RoomId,
                OwnerId        = input.OwnerId,
                ActionPoint    = piir.ActionPoint,
                ActionPointMax = piir.ActionPointMax,
                Ret            = true,
            };
            ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.UpdateActionPointReply, output.ToByteArray());
            TryCommandReply output2 = new TryCommandReply()
            {
                RoomId  = input.RoomId,
                OwnerId = input.OwnerId,
                Ret     = true,
            };
            ServerRoomManager.Instance.SendMsg(args, ROOM_REPLY.TryCommandReply, output2.ToByteArray());
            ServerRoomManager.Instance.Log($"RoomLogic OnTryCommand OK - Permission granted! - CommandId:{input.CommandId} - ActionPointCost:{input.ActionPointCost}");
        }
    }