Exemplo n.º 1
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;
            string              characterName = request.characterName.Trim();
            int                 dataId        = request.dataId;
            int                 entityId      = request.entityId;
            int                 factionId     = request.factionId;
            CentralUserPeerInfo userPeerInfo;
            if (!NameValidating.ValidateCharacterName(characterName))
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_CHARACTER_NAME
                });
                return;
            }
            // Validate character name
            AsyncResponseData <FindCharacterNameResp> findCharacterNameResp = await DbServiceClient.FindCharacterNameAsync(new FindCharacterNameReq()
            {
                CharacterName = characterName
            });

            if (!findCharacterNameResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            if (findCharacterNameResp.Response.FoundAmount > 0)
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_CHARACTER_NAME_EXISTED,
                });
                return;
            }
            if (!userPeers.TryGetValue(connectionId, out userPeerInfo))
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }
            if (string.IsNullOrEmpty(characterName) || characterName.Length < minCharacterNameLength)
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_CHARACTER_NAME_TOO_SHORT,
                });
                return;
            }
            if (characterName.Length > maxCharacterNameLength)
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_CHARACTER_NAME_TOO_LONG,
                });
                return;
            }
            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
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_DATA,
                });
                return;
            }
            string characterId = GenericUtils.GetUniqueId();
            PlayerCharacterData characterData = new PlayerCharacterData();
            characterData.Id = characterId;
            characterData.SetNewPlayerCharacterData(characterName, dataId, entityId);
            characterData.FactionId = factionId;
            DeserializeCreateCharacterExtra(characterData, reader);
            AsyncResponseData <CharacterResp> createResp = await DbServiceClient.CreateCharacterAsync(new CreateCharacterReq()
            {
                UserId        = userPeerInfo.userId,
                CharacterData = characterData,
            });

            if (!createResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateCharacterMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            // Response
            result.InvokeSuccess(new ResponseCreateCharacterMessage());
#endif
        }
Exemplo n.º 2
0
        public async UniTaskVoid HandleRequestCreateGuild(RequestHandlerData requestHandler, RequestCreateGuildMessage request, RequestProceedResultDelegate <ResponseCreateGuildMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            await UniTask.Yield();

            IPlayerCharacterData playerCharacter;
            if (!GameInstance.ServerUserHandlers.TryGetPlayerCharacter(requestHandler.ConnectionId, out playerCharacter))
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_NOT_LOGGED_IN,
                });
                return;
            }
            ValidateGuildRequestResult validateResult = playerCharacter.CanCreateGuild(request.guildName);
            if (!validateResult.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = validateResult.GameMessage,
                });
                return;
            }
            string guildName = request.guildName.Trim();
            if (!NameValidating.ValidateGuildName(guildName))
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_GUILD_NAME
                });
                return;
            }
            AsyncResponseData <FindGuildNameResp> findGuildNameResp = await DbServiceClient.FindGuildNameAsync(new FindGuildNameReq()
            {
                GuildName = guildName,
            });

            if (!findGuildNameResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            if (findGuildNameResp.Response.FoundAmount > 0)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_GUILD_NAME_EXISTED,
                });
                return;
            }
            AsyncResponseData <GuildResp> createGuildResp = await DbServiceClient.CreateGuildAsync(new CreateGuildReq()
            {
                LeaderCharacterId = playerCharacter.Id,
                GuildName         = guildName,
            });

            if (!createGuildResp.IsSuccess)
            {
                result.InvokeError(new ResponseCreateGuildMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            // Update cache
            GuildData guild = createGuildResp.Response.GuildData;
            GameInstance.Singleton.SocialSystemSetting.DecreaseCreateGuildResource(playerCharacter);
            GameInstance.ServerGuildHandlers.SetGuild(guild.id, guild);
            playerCharacter.GuildId        = guild.id;
            playerCharacter.GuildRole      = guild.GetMemberRole(playerCharacter.Id);
            playerCharacter.SharedGuildExp = 0;
            // Broadcast via chat server
            if (ClusterClient.IsNetworkActive)
            {
                ClusterClient.SendCreateGuild(MMOMessageTypes.UpdateGuild, guild.id, guildName, playerCharacter.Id);
                ClusterClient.SendAddSocialMember(MMOMessageTypes.UpdateGuildMember, guild.id, playerCharacter.Id, playerCharacter.CharacterName, playerCharacter.DataId, playerCharacter.Level);
            }
            GameInstance.ServerGameMessageHandlers.SendSetGuildData(requestHandler.ConnectionId, guild);
            GameInstance.ServerGameMessageHandlers.SendAddGuildMembersToOne(requestHandler.ConnectionId, guild);
            result.InvokeSuccess(new ResponseCreateGuildMessage());
#endif
        }
        protected async UniTaskVoid HandleRequestUserRegister(
            RequestHandlerData requestHandler,
            RequestUserRegisterMessage request,
            RequestProceedResultDelegate <ResponseUserRegisterMessage> result)
        {
#if UNITY_STANDALONE && !CLIENT_BUILD
            if (disableDefaultLogin)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_SERVICE_NOT_AVAILABLE
                });
                return;
            }
            string username = request.username.Trim();
            string password = request.password.Trim();
            string email    = request.email.Trim();
            if (!NameValidating.ValidateUsername(username))
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_INVALID_USERNAME
                });
                return;
            }
            if (requireEmail)
            {
                if (string.IsNullOrEmpty(email) || !Email.IsValid(email))
                {
                    result.InvokeError(new ResponseUserRegisterMessage()
                    {
                        message = UITextKeys.UI_ERROR_INVALID_EMAIL,
                    });
                    return;
                }
                AsyncResponseData <FindEmailResp> findEmailResp = await DbServiceClient.FindEmailAsync(new FindEmailReq()
                {
                    Email = email
                });

                if (!findEmailResp.IsSuccess)
                {
                    result.InvokeError(new ResponseUserRegisterMessage()
                    {
                        message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                    });
                    return;
                }
                if (findEmailResp.Response.FoundAmount > 0)
                {
                    result.InvokeError(new ResponseUserRegisterMessage()
                    {
                        message = UITextKeys.UI_ERROR_EMAIL_ALREADY_IN_USE,
                    });
                    return;
                }
            }
            AsyncResponseData <FindUsernameResp> findUsernameResp = await DbServiceClient.FindUsernameAsync(new FindUsernameReq()
            {
                Username = username
            });

            if (!findUsernameResp.IsSuccess)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            if (findUsernameResp.Response.FoundAmount > 0)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_USERNAME_EXISTED,
                });
                return;
            }
            if (string.IsNullOrEmpty(username) || username.Length < minUsernameLength)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_USERNAME_TOO_SHORT,
                });
                return;
            }
            if (username.Length > maxUsernameLength)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_USERNAME_TOO_LONG,
                });
                return;
            }
            if (string.IsNullOrEmpty(password) || password.Length < minPasswordLength)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_PASSWORD_TOO_SHORT,
                });
                return;
            }
            AsyncResponseData <EmptyMessage> createResp = await DbServiceClient.CreateUserLoginAsync(new CreateUserLoginReq()
            {
                Username = username,
                Password = password,
                Email    = email,
            });

            if (!createResp.IsSuccess)
            {
                result.InvokeError(new ResponseUserRegisterMessage()
                {
                    message = UITextKeys.UI_ERROR_INTERNAL_SERVER_ERROR,
                });
                return;
            }
            // Response
            result.InvokeSuccess(new ResponseUserRegisterMessage());
#endif
        }