예제 #1
0
        private async UniTaskVoid 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;
            FindCharacterNameResp findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq()
            {
                CharacterName = characterName
            });

            if (findCharacterNameResp.FoundAmount > 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);
                await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq()
                {
                    UserId        = userPeerInfo.userId,
                    CharacterData = characterData.ToByteString()
                });
            }
            ResponseCreateCharacterMessage responseMessage = new ResponseCreateCharacterMessage();

            responseMessage.ackId        = message.ackId;
            responseMessage.responseCode = error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error;
            responseMessage.error        = error;
            ServerSendResponse(connectionId, MMOMessageTypes.ResponseCreateCharacter, responseMessage);
        }
예제 #2
0
        protected async UniTaskVoid HandleRequestCreateCharacter(
            RequestHandlerData requestHandler,
            RequestCreateCharacterMessage request,
            RequestProceedResultDelegate <ResponseCreateCharacterMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            long                  connectionId  = requestHandler.ConnectionId;
            NetDataReader         reader        = requestHandler.Reader;
            UITextKeys            message       = UITextKeys.NONE;
            string                characterName = request.characterName;
            int                   dataId        = request.dataId;
            int                   entityId      = request.entityId;
            int                   factionId     = request.factionId;
            CentralUserPeerInfo   userPeerInfo;
            FindCharacterNameResp findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq()
            {
                CharacterName = characterName
            });

            if (findCharacterNameResp.FoundAmount > 0)
            {
                message = UITextKeys.UI_ERROR_CHARACTER_NAME_EXISTED;
            }
            else if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                message = UITextKeys.UI_ERROR_NOT_LOGGED_IN;
            }
            else if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength)
            {
                message = UITextKeys.UI_ERROR_CHARACTER_NAME_TOO_SHORT;
            }
            else if (characterName.Length > maxCharacterNameLength)
            {
                message = UITextKeys.UI_ERROR_CHARACTER_NAME_TOO_LONG;
            }
            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
                message = UITextKeys.UI_ERROR_INVALID_DATA;
            }
            else
            {
                string characterId = GenericUtils.GetUniqueId();
                PlayerCharacterData characterData = new PlayerCharacterData();
                characterData.Id = characterId;
                characterData.SetNewPlayerCharacterData(characterName, dataId, entityId);
                characterData.FactionId = factionId;
                DeserializeCreateCharacterExtra(characterData, reader);
                await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq()
                {
                    UserId        = userPeerInfo.userId,
                    CharacterData = characterData,
                });
            }
            // Response
            result.Invoke(
                message == UITextKeys.NONE ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseCreateCharacterMessage()
            {
                message = message,
            });
#endif
        }
        protected async UniTaskVoid HandleRequestCreateCharacter(
            RequestHandlerData requestHandler,
            RequestCreateCharacterMessage request,
            RequestProceedResultDelegate <ResponseCreateCharacterMessage> result)
        {
            long          connectionId = requestHandler.ConnectionId;
            NetDataReader reader       = requestHandler.Reader;

            ResponseCreateCharacterMessage.Error error = ResponseCreateCharacterMessage.Error.None;
            string characterName = request.characterName;
            int    dataId        = request.dataId;
            int    entityId      = request.entityId;
            int    factionId     = request.factionId;
            CentralUserPeerInfo   userPeerInfo;
            FindCharacterNameResp findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq()
            {
                CharacterName = characterName
            });

            if (findCharacterNameResp.FoundAmount > 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, reader);
                await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq()
                {
                    UserId        = userPeerInfo.userId,
                    CharacterData = characterData.ToByteString()
                });
            }
            // Response
            result.Invoke(
                error == ResponseCreateCharacterMessage.Error.None ? AckResponseCode.Success : AckResponseCode.Error,
                new ResponseCreateCharacterMessage()
            {
                error = error,
            });
        }