Esempio n. 1
0
        public static void Execute(this JoinCharacterCommand command, IPEndPoint endPoint, IServerContext serverContext)
        {
            var session = serverContext.PersonsDatabase.GetSession(command.SessionId);
            if (session == null)
                throw new NotImplementedException();		// TODO: обработать

            if (serverContext.CharactersDatabase.GetCharacters(session.PersonId).All(ch => ch.Id != command.CharacterId))
                throw new NotImplementedException();		// TODO: Попытка прислать Id другого перса

            var character = serverContext.CharactersDatabase.GetCharacter(command.CharacterId);
            if (character == null)
                throw new NotImplementedException();		// TODO: обработать

            // TODO: проверить что чар принадлежит пользователю сессии

            // TODO: достаём параметры чара из БД
            var characterData = serverContext.CharactersDatabase.GetData(command.CharacterId);
            //((Character)character).Position = new Position { X = serverContext.Random.Next(200), Y = serverContext.Random.Next(200) };

            var token = (ushort)serverContext.Random.Next(ushort.MaxValue);
            serverContext.CharacterSessions.Add(new CharacterSession
            {
                Character = character,
                CharacterData = characterData,
                EndPoint = endPoint,
                Token = token
            });

            // Подтверждаем подключение
            serverContext.Connector.Send(endPoint, new CharacterJoinedCommand { Token = token });

            // Оповещаем клиентов (кроме инициатора) о появлении нового перса
            var characterDetectedCommand = new CharacterDetectedCommand { Character = character };
            foreach (var charSession in serverContext.CharacterSessions.All.Where(cs => cs.Character.Id != character.Id))
                serverContext.Connector.Send(charSession.EndPoint, characterDetectedCommand);
        }
Esempio n. 2
0
 public static void OnCharacterDetected(CharacterDetectedCommand characterDetectedCommand, ILocationControl locationControl, IClientContext clientContext)
 {
     clientContext.Characters.Add(characterDetectedCommand.Character);
     locationControl.Add(characterDetectedCommand.Character);
 }
Esempio n. 3
0
 public static CommandBase Parse(byte[] data)
 {
     using (var stream = new MemoryStream(data))
     using (var reader = new BinaryReader(stream))
     {
         CommandBase command;
         switch ((CommandType)reader.ReadUInt16())
         {
             case CommandType.JoinCharacter:
                 command = new JoinCharacterCommand();
                 break;
             case CommandType.CharacterJoined:
                 command = new CharacterJoinedCommand();
                 break;
             case CommandType.CharacterDetected:
                 command = new CharacterDetectedCommand();
                 break;
             case CommandType.KeyPressed:
                 command = new KeyPressedCommand();
                 break;
             case CommandType.PositionChanged:
                 command = new PositionChangedCommand();
                 break;
             default:
                 throw new NotImplementedException();
         }
         command.Token = reader.ReadUInt16();
         command.Deserialize(reader);
         return command;
     }
 }