Ejemplo n.º 1
0
        private IEnumerator HandleRequestCreateCharacterRoutine(LiteNetLibMessageHandler messageHandler)
        {
            long connectionId = messageHandler.connectionId;
            RequestCreateCharacterMessage message = messageHandler.ReadMessage <RequestCreateCharacterMessage>();

            ResponseCreateCharacterMessage.Error error = ResponseCreateCharacterMessage.Error.None;
            string characterName = message.characterName;
            int    dataId        = message.dataId;
            int    entityId      = message.entityId;
            int    factionId     = message.factionId;
            CentralUserPeerInfo  userPeerInfo;
            FindCharacterNameJob findCharacterNameJob = new FindCharacterNameJob(Database, characterName);

            findCharacterNameJob.Start();
            yield return(StartCoroutine(findCharacterNameJob.WaitFor()));

            if (findCharacterNameJob.result > 0)
            {
                error = ResponseCreateCharacterMessage.Error.CharacterNameAlreadyExisted;
            }
            else if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                error = ResponseCreateCharacterMessage.Error.NotLoggedin;
            }
            else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength)
            {
                error = ResponseCreateCharacterMessage.Error.TooShortCharacterName;
            }
            else if (characterName.Length > maxCharacterNameLength)
            {
                error = ResponseCreateCharacterMessage.Error.TooLongCharacterName;
            }
            else if (!GameInstance.PlayerCharacters.ContainsKey(dataId) ||
                     !GameInstance.PlayerCharacterEntities.ContainsKey(entityId) ||
                     (GameInstance.Factions.Count > 0 && !GameInstance.Factions.ContainsKey(factionId)))
            {
                // If there is factions, it must have faction with the id stored in faction dictionary
                error = ResponseCreateCharacterMessage.Error.InvalidData;
            }
            else
            {
                string characterId = GenericUtils.GetUniqueId();
                PlayerCharacterData characterData = new PlayerCharacterData();
                characterData.Id = characterId;
                characterData.SetNewPlayerCharacterData(characterName, dataId, entityId);
                characterData.FactionId = factionId;
                DeserializeCreateCharacterExtra(characterData, messageHandler.reader);
                CreateCharacterJob createCharacterJob = new CreateCharacterJob(Database, userPeerInfo.userId, characterData);
                createCharacterJob.Start();
                yield return(StartCoroutine(createCharacterJob.WaitFor()));
            }
            ResponseCreateCharacterMessage responseMessage = new ResponseCreateCharacterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            ServerSendPacket(connectionId, DeliveryMethod.ReliableOrdered, MMOMessageTypes.ResponseCreateCharacter, responseMessage);
        }
Ejemplo n.º 2
0
        private IEnumerator HandleRequestCreateCharacterRoutine(LiteNetLibMessageHandler messageHandler)
        {
            var connectionId  = messageHandler.connectionId;
            var message       = messageHandler.ReadMessage <RequestCreateCharacterMessage>();
            var error         = ResponseCreateCharacterMessage.Error.None;
            var characterName = message.characterName;
            var dataId        = message.dataId;
            var entityId      = message.entityId;
            CentralUserPeerInfo userPeerInfo;
            var findCharacterNameJob = new FindCharacterNameJob(Database, characterName);

            findCharacterNameJob.Start();
            yield return(StartCoroutine(findCharacterNameJob.WaitFor()));

            if (findCharacterNameJob.result > 0)
            {
                error = ResponseCreateCharacterMessage.Error.CharacterNameAlreadyExisted;
            }
            else if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                error = ResponseCreateCharacterMessage.Error.NotLoggedin;
            }
            else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength)
            {
                error = ResponseCreateCharacterMessage.Error.TooShortCharacterName;
            }
            else if (characterName.Length > maxCharacterNameLength)
            {
                error = ResponseCreateCharacterMessage.Error.TooLongCharacterName;
            }
            else if (!GameInstance.PlayerCharacters.ContainsKey(dataId) ||
                     !GameInstance.PlayerCharacterEntities.ContainsKey(entityId))
            {
                error = ResponseCreateCharacterMessage.Error.InvalidData;
            }
            else
            {
                var characterId   = GenericUtils.GetUniqueId();
                var characterData = new PlayerCharacterData();
                characterData.Id = characterId;
                characterData.SetNewPlayerCharacterData(characterName, dataId, entityId);
                var createCharacterJob = new CreateCharacterJob(Database, userPeerInfo.userId, characterData);
                createCharacterJob.Start();
                yield return(StartCoroutine(createCharacterJob.WaitFor()));
            }
            var responseMessage = new ResponseCreateCharacterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            ServerSendPacket(connectionId, SendOptions.ReliableOrdered, MMOMessageTypes.ResponseCreateCharacter, responseMessage);
        }