Пример #1
0
 public override void Validate(BasePacket packet)
 {
     ValidationInfo info;
     _info.TryGetValue(packet.SessionToken, out info);
     if (info == null)
     {
         BaseResponse.Throw(packet.ToString(), ECGeneral.SessionError);
         return;
     }
     info.AssertAction(packet);
 }
Пример #2
0
        private BaseResponse MapData(int clientId, BasePacket packet)
        {
            PMapData pMapData = (PMapData) packet;

            PlayerDataEx playerData = World.PlayerDataGet(pMapData.SessionToken);
            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return null;
            }

            ShortRect mapWindow;
            byte[] mapData = World.MapWindowGet(playerData, out mapWindow);

            return new RMapData(pMapData)
            {
                MapData = mapData,
                MapWindow =  mapWindow
            };
        }
Пример #3
0
        private BaseResponse PlayerMove(int clientId, BasePacket packet)
        {
            PPlayerMove pPlayerMove = (PPlayerMove)packet;
            PlayerDataEx playerData = World.PlayerDataGet(pPlayerMove.SessionToken);
            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return null;
            }

            if (playerData.Player.Position != pPlayerMove.Position ||
                playerData.Player.Direction != pPlayerMove.Direction)
            {
                _vCheckPosition.Validate(packet);

                if (pPlayerMove.Direction == Direction.None)
                {
                    playerData.StopMoving(pPlayerMove.Position);
                }
                else
                {
                    playerData.StartMoving(pPlayerMove.Position, pPlayerMove.Direction);
                }

                RegisterWorldResponseForNearest(playerData, new RPlayerMove
                {
                    PlayerId = playerData.Player.Id,
                    Position = pPlayerMove.Position,
                    Direction = pPlayerMove.Direction,
                });
            }

            return null;
        }
Пример #4
0
        private BaseResponse PlayerRotate(int clientId, BasePacket packet)
        {
            PPlayerRotate pPlayerRotate = (PPlayerRotate)packet;
            PlayerDataEx playerData = World.PlayerDataGet(pPlayerRotate.SessionToken);
            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return null;
            }

            if (playerData.Player.Angle != pPlayerRotate.Angle)
            {
                playerData.Player.Angle = pPlayerRotate.Angle;

                RegisterWorldResponseForNearest(playerData, new RPlayerRotate
                {
                    PlayerId = playerData.Player.Id,
                    Angle = pPlayerRotate.Angle
                });
            }

            return null;
        }
Пример #5
0
 private BaseResponse Logout(int clientId, BasePacket packet)
 {
     return Logout(clientId, packet.SessionToken);
 }
Пример #6
0
        private BaseResponse Enter(int clientId, BasePacket packet)
        {
            PUserEnter pUserEnter = (PUserEnter) packet;

            PlayerDataEx playerData = World.PlayerDataGet(pUserEnter.SessionToken);
            if (playerData == null)
            {
                ThrowSessionError(packet.Type, packet.SessionToken);
                return null;
            }

            int maxScreenSide = Math.Max(pUserEnter.ScreenRes.Width, pUserEnter.ScreenRes.Height);
            playerData.ScreenRes = new ShortSize(maxScreenSide, maxScreenSide);

            List<Player> nearestPlayers = World.PlayersGetNearest(playerData);

            ShortRect mapWindow;
            byte[] mapData = World.MapWindowGet(playerData, out mapWindow);
            ShortSize mapSize = new ShortSize(playerData.Map.Width, playerData.Map.Height);

            ShortSize miniMapSize;
            byte[] miniMapData = World.MiniMapGet(playerData, out miniMapSize);

            RegisterWorldResponseForNearest(playerData, new RPlayerEnter
            {
                Player = playerData
            });

            return new RUserEnter(pUserEnter)
            {
                MyPlayerId = playerData.Player.Id,
                PlayersOnLocation = nearestPlayers,

                MapSize = mapSize,
                MapData = mapData,
                MapWindow = mapWindow,

                MiniMapData = miniMapData,
                MiniMapSize = miniMapSize
            };
        }
Пример #7
0
        private BaseResponse Login(int clientId, BasePacket packet)
        {
            PUserLogin pUserLogin = (PUserLogin) packet;

            User user = new User(pUserLogin.UserName, pUserLogin.Password);
            pUserLogin.SessionToken = BasePacket.NewSessionToken();

            Player player = Player.Create(user.UserName);
            ShortPoint? startPoint = World.MapFindPlayerStartPoint(player);
            if (!startPoint.HasValue)
            {
                BaseResponse.Throw("Cannot get start point for the player", ECGeneral.ServerError);
                return null;
            }
            player.Position = startPoint.Value.ToPoint(ConstMap.PIXEL_SIZE);

            lock (_loggedPlayers)
            {
                _loggedPlayers.Add(pUserLogin.SessionToken, player.Id);
            }

            lock (_tcpClients)
            {
                _tcpClients.Add(clientId, pUserLogin.SessionToken);
                _playerClients.Add(player.Id, clientId);
            }

            World.PlayerAdd(pUserLogin.SessionToken, player);

            _validators.RegisterSession(pUserLogin.SessionToken);

            return new RUserLogin(pUserLogin)
            {
                SessionToken = pUserLogin.SessionToken
            };
        }
Пример #8
0
 private BaseResponse ProcessPacket(int clientId, BasePacket packet)
 {
     try
     {
         AssertSession(packet);
         return _actions[packet.Type](clientId, packet);
     }
     catch (Exception ex)
     {
         return BaseResponse.FromException(packet, ex);
     }
 }
Пример #9
0
 private void AssertSession(BasePacket packet)
 {
     if (packet.Type != PacketType.UserLogin &&
         !_loggedPlayers.ContainsKey(packet.SessionToken))
     {
         ThrowSessionError(packet.Type, packet.SessionToken);
     }
 }
Пример #10
0
 private void TCPClientDataSend(BasePacket packet)
 {
     if (_sessionToken != 0 || packet.Type == PacketType.UserLogin)
     {
         _tcpClient.Send(packet);
     }
 }
Пример #11
0
 public abstract void Validate(BasePacket packet);
Пример #12
0
 private void AssertDistance(BasePacket packet, double dist)
 {
     double ms = DateTime.Now.Subtract(_lastMoveTime).TotalMilliseconds;
     if (ms/_lastPlayerSpeed/dist < DIST_DIFF_THRESH)
     {
         BaseResponse.Throw(packet.ToString(), ECGeneral.ActionIsImposible);
     }
 }
Пример #13
0
 public void AssertAction(BasePacket packet)
 {
     switch (packet.Type)
     {
         case PacketType.PlayerMove:
         {
             PPlayerMove p = (PPlayerMove) packet;
             AssertDistance(packet, Geometry.GetDistance(p.Position.X, p.Position.Y,
                            _lastX, _lastY));
             break;
         }
     }
 }
Пример #14
0
 protected BaseResponse(BasePacket packet)
 {
     Id = packet.Id;
 }
Пример #15
0
 public static BaseResponse FromException(BasePacket packet, Exception ex)
 {
     return(FromException(packet.Id, packet.Type, ex));
 }