public async static ETTask LinkByFaceBook(Player player, LinkInfo info, L2C_Link response)
        {
            string fbToken      = info.Secret;
            bool   isValidToken = await FacebookHelper.ValidateFacebookToken(fbToken);

            if (!isValidToken)
            {
                response.Error = ErrorCode.ERR_LinkFailed;
                return;
            }
            ThirdPartyInfo fbInfo = await FacebookHelper.GetFacebookUserInfo(fbToken);

            if (fbInfo == null)
            {
                response.Error = ErrorCode.ERR_LinkFailed;
                return;
            }
            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(fbInfo.id, UserDataHelper.tagFB);

            if (thirdPartyUser == null)
            {
                long uid  = player.uid;
                User user = await UserDataHelper.FindOneUser(uid);

                if (user == null)
                {
                    response.Error = ErrorCode.ERR_LinkFailed;
                    return;
                }

                //綁定第三方-FB
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagFB;
                thirdPartyUser.userId   = fbInfo.id;
                thirdPartyUser.gender   = fbInfo.gender;
                thirdPartyUser.location = fbInfo.location;
                thirdPartyUser.email    = fbInfo.email;
                thirdPartyUser.name     = fbInfo.name;
                thirdPartyUser.birthday = fbInfo.birthday;
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);

                //取得新的第三方列表
                response.LinkTypes.Clear();
                response.LinkTypes.AddRange(await GetAllLinkType(user.Id));
            }
            else
            {
                response.Error = ErrorCode.ERR_LinkIsExist;
            }
        }
        public async static ETTask <int> AuthenticationByBot(string deviceUniqueIdentifier)
        {
            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(deviceUniqueIdentifier, UserDataHelper.tagGuest);

            User user = null;

            if (thirdPartyUser == null)
            {
                user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId());
                string salt         = CryptographyHelper.GenerateRandomId();
                string password     = CryptographyHelper.GenerateRandomId(16);
                string hashPassword = CryptographyHelper.MD5Encoding(password, salt);
                user.salt                          = salt;
                user.hashPassword                  = hashPassword;
                user.createAt                      = now;
                user.name                          = $"{user.Id}";
                user.playerCharSetting             = new PlayerCharSetting();
                user.playerCharSetting.CharacterId = 1L;
                user.playerRideTotalInfo           = new RideTotalInfo();
                user.language                      = 10;
                user.identity                      = (int)User.Identity.TestPlayer;
                user.userBagCapacity               = EquipmentDataHelper.GetDefaultUserBag();
                await UserDataHelper.SinUserUp(user);

                //註冊第三方-Guest
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagGuest;
                thirdPartyUser.userId   = deviceUniqueIdentifier;
                thirdPartyUser.name     = "";
                thirdPartyUser.gender   = "";
                thirdPartyUser.location = "";
                thirdPartyUser.email    = "";
                thirdPartyUser.birthday = "";
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);

                return(ErrorCode.ERR_Success);
            }
            else
            {
                return(ErrorCode.ERR_DeviceUniqueIdentifierIsExist);
            }
        }
        public async static ETTask LinkByAppleId(Player player, LinkInfo info, L2C_Link response)
        {
            string         appleId   = CryptographyHelper.AESDecrypt(info.Secret);
            ThirdPartyInfo appleInfo = new ThirdPartyInfo
            {
                id = appleId,
            };
            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(appleInfo.id, UserDataHelper.tagAppleId);

            if (thirdPartyUser == null)
            {
                long uid  = player.uid;
                User user = await UserDataHelper.FindOneUser(uid);

                if (user == null)
                {
                    response.Error = ErrorCode.ERR_LinkFailed;
                    return;
                }

                // 綁定第三方-Apple
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagAppleId;
                thirdPartyUser.userId   = appleInfo.id;
                thirdPartyUser.gender   = appleInfo.gender;
                thirdPartyUser.location = appleInfo.location;
                thirdPartyUser.email    = appleInfo.email;
                thirdPartyUser.name     = appleInfo.name;
                thirdPartyUser.birthday = appleInfo.birthday;
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);

                // 取得新的第三方列表
                response.LinkTypes.Clear();
                response.LinkTypes.AddRange(await GetAllLinkType(user.Id));
            }
            else
            {
                response.Error = ErrorCode.ERR_LinkIsExist;
            }
        }
        /// <summary>
        /// 使用AppleID登入
        /// </summary>
        /// <param name="session"></param>
        /// <param name="info"></param>
        /// <param name="response"></param>
        /// <returns></returns>
        public async static ETTask AuthenticationByAppleId(Session session, AuthenticationInfo info, R2C_Authentication response)
        {
            string         appleId   = CryptographyHelper.AESDecrypt(info.Secret);
            ThirdPartyInfo appleInfo = new ThirdPartyInfo
            {
                id = appleId,
            };
            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(appleInfo.id, UserDataHelper.tagAppleId);

            User user = null;

            if (thirdPartyUser == null)
            {
                // 用AppleId註冊帳號
                user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId());
                string salt         = CryptographyHelper.GenerateRandomId();
                string password     = CryptographyHelper.GenerateRandomId(16);
                string hashPassword = CryptographyHelper.MD5Encoding(password, salt);
                user.salt                          = salt;
                user.hashPassword                  = hashPassword;
                user.email                         = appleInfo.email;
                user.name                          = user.Id.ToString();
                user.gender                        = appleInfo.genderCode;
                user.location                      = appleInfo.locationCode;
                user.birthday                      = appleInfo.birthdayCode;
                user.createAt                      = now;
                user.playerCharSetting             = new PlayerCharSetting();
                user.playerCharSetting.CharacterId = 1L;
                user.playerRideTotalInfo           = new RideTotalInfo();
                user.language                      = info.Language;
                user.identity                      = (int)User.Identity.Player;
                user.userBagCapacity               = EquipmentDataHelper.GetDefaultUserBag();
                await UserDataHelper.SinUserUp(user);

                // 註冊第三方-AppleId
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagAppleId;
                thirdPartyUser.userId   = appleInfo.id;
                thirdPartyUser.gender   = appleInfo.gender;
                thirdPartyUser.location = appleInfo.location;
                thirdPartyUser.email    = appleInfo.email;
                thirdPartyUser.name     = appleInfo.name;
                thirdPartyUser.birthday = appleInfo.birthday;
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);

                // 註冊第三方-Guest
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagGuest;
                thirdPartyUser.userId   = info.DeviceId;
                thirdPartyUser.name     = "";
                thirdPartyUser.gender   = "";
                thirdPartyUser.location = "";
                thirdPartyUser.email    = "";
                thirdPartyUser.birthday = "";
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);
            }
            else
            {
                user = await UserDataHelper.FindOneUser(thirdPartyUser.uid);
            }

            await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagAppleId);
        }
        public async static ETTask AuthenticationByFaceBook(Session session, AuthenticationInfo info, R2C_Authentication response)
        {
            string fbToken      = info.Secret;
            bool   isValidToken = await FacebookHelper.ValidateFacebookToken(fbToken);

            if (!isValidToken)
            {
                response.Error = ErrorCode.ERR_FBSignInFailed;
                return;
            }
            ThirdPartyInfo fbInfo = await FacebookHelper.GetFacebookUserInfo(fbToken);

            if (fbInfo == null)
            {
                response.Error = ErrorCode.ERR_FBSignInFailed;
                return;
            }
            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(fbInfo.id, UserDataHelper.tagFB);

            User user = null;

            if (thirdPartyUser == null)
            {
                // 用FB註冊帳號
                user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId());
                string salt         = CryptographyHelper.GenerateRandomId();
                string password     = CryptographyHelper.GenerateRandomId(16);
                string hashPassword = CryptographyHelper.MD5Encoding(password, salt);
                user.salt                          = salt;
                user.hashPassword                  = hashPassword;
                user.email                         = fbInfo.email;
                user.name                          = fbInfo.name;
                user.gender                        = fbInfo.genderCode;
                user.location                      = fbInfo.locationCode;
                user.birthday                      = fbInfo.birthdayCode;
                user.createAt                      = now;
                user.playerCharSetting             = new PlayerCharSetting();
                user.playerCharSetting.CharacterId = 1L;
                user.playerRideTotalInfo           = new RideTotalInfo();
                user.language                      = info.Language;
                user.identity                      = (int)User.Identity.Player;
                user.userBagCapacity               = EquipmentDataHelper.GetDefaultUserBag();
                await UserDataHelper.SinUserUp(user);

                //註冊第三方-FB
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagFB;
                thirdPartyUser.userId   = fbInfo.id;
                thirdPartyUser.gender   = fbInfo.gender;
                thirdPartyUser.location = fbInfo.location;
                thirdPartyUser.email    = fbInfo.email;
                thirdPartyUser.name     = fbInfo.name;
                thirdPartyUser.birthday = fbInfo.birthday;
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);

                //註冊第三方-Guest
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagGuest;
                thirdPartyUser.userId   = info.DeviceId;
                thirdPartyUser.name     = "";
                thirdPartyUser.gender   = "";
                thirdPartyUser.location = "";
                thirdPartyUser.email    = "";
                thirdPartyUser.birthday = "";
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);
            }
            else
            {
                user = await UserDataHelper.FindOneUser(thirdPartyUser.uid);
            }

            await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagFB);
        }
        public async static ETTask AuthenticationByGuest(Session session, AuthenticationInfo info, R2C_Authentication response)
        {
            string deviceUniqueIdentifier = string.Empty;

            try
            {
                deviceUniqueIdentifier = CryptographyHelper.AESDecrypt(info.Secret);
            }
            catch (Exception)
            {
                response.Error = ErrorCode.ERR_InvalidDeviceUniqueIdentifier;
                return;
            }

            if (string.IsNullOrEmpty(deviceUniqueIdentifier))
            {
                response.Error = ErrorCode.ERR_DeviceUniqueIdentifierIsNull;
                return;
            }

            long           now            = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            ThirdPartyUser thirdPartyUser = await UserDataHelper.FindOneThirdPartyUser(deviceUniqueIdentifier, UserDataHelper.tagGuest);

            User user = null;

            if (thirdPartyUser == null)
            {
                user = ComponentFactory.CreateWithId <User>(IdGenerater.GenerateId());
                string salt         = CryptographyHelper.GenerateRandomId();
                string password     = CryptographyHelper.GenerateRandomId(16);
                string hashPassword = CryptographyHelper.MD5Encoding(password, salt);
                user.salt                          = salt;
                user.hashPassword                  = hashPassword;
                user.createAt                      = now;
                user.name                          = $"{user.Id}";
                user.email                         = "";
                user.playerCharSetting             = new PlayerCharSetting();
                user.playerCharSetting.CharacterId = 1L;
                user.playerRideTotalInfo           = new RideTotalInfo();
                user.language                      = info.Language;
                user.identity                      = (int)User.Identity.Player;
                user.userBagCapacity               = EquipmentDataHelper.GetDefaultUserBag();
                await UserDataHelper.SinUserUp(user);

                //註冊第三方-Guest
                thirdPartyUser          = ComponentFactory.CreateWithId <ThirdPartyUser>(IdGenerater.GenerateId());
                thirdPartyUser.uid      = user.Id;
                thirdPartyUser.party    = UserDataHelper.tagGuest;
                thirdPartyUser.userId   = deviceUniqueIdentifier;
                thirdPartyUser.name     = "";
                thirdPartyUser.gender   = "";
                thirdPartyUser.location = "";
                thirdPartyUser.email    = "";
                thirdPartyUser.birthday = "";
                thirdPartyUser.createAt = now;
                await UserDataHelper.UpsertThirdPartyUser(thirdPartyUser);
            }
            else
            {
                user = await UserDataHelper.FindOneUser(thirdPartyUser.uid);
            }

            await SignInByUid(session, user, response, info.FirebaseDeviceToken, true, UserDataHelper.tagGuest);
        }