Beispiel #1
0
        public YingLoginPage()
        {
            InitializeComponent();

            yyregister.MouseLeftButtonDown += (ysender, yevent) =>
            {
                ylogin.Visibility    = Visibility.Collapsed;
                yregister.Visibility = Visibility.Visible;
            };

            yylogin.MouseLeftButtonDown += (ysender, yevent) =>
            {
                ylogin.Visibility    = Visibility.Visible;
                yregister.Visibility = Visibility.Collapsed;
            };


            /*
             * YingConfig.YArgs.YAuthServers.Add(new YingAuthServer { ytype = YingAuthType.YAuthLib, yname = "Ying" });
             * YingConfig.YArgs.YAuthServers.Add(new YingAuthServer { ytype = YingAuthType.Ying, yname = "Ying Ying" });
             *
             * YingConfig.YArgs.YAuthServers.Add(new YingAuthServer { ytype = YingAuthType.YAuthLib, yname = "Ying Sakura City Ying", yurl = "https://skin.prinzeugen.net/api/yggdrasil" });
             * /*YingConfig.YArgs.YAuthServers.Add(new YingAuthServer { yname =  });
             * //YingConfig.YArgs.YAuthServers.CollectionChanged += (y, yy) => {
             *  YServerList.ItemsSource = YingConfig.YArgs.YAuthServers;
             * //};
             * YingConfig.YArgs.YAuthServers.Add(new YingAuthServer { });*/

            getYEvent().Subscribe <YingPackageEvent>((yevent) =>
            {
                if (!yevent.isYSend)
                {
                    switch (yevent.yStruct.ytype)
                    {
                    case YingStruct.YingType.YLogin:
                        getYAuhenticator().YResult = JsonConvert.DeserializeObject <YingLoginStruct>(yevent.yStruct.ydata.ymessage);

                        this.Dispatcher.Invoke(() =>
                        {
                            if (getYAuhenticator().YResult.yaccessToken == null)
                            {
                                YLoginButton.IsEnabled = true;
                                YLoginButton.Content   = "  〉";
                                YLoading.Visibility    = Visibility.Collapsed;
                                YingMessageBox.Ying($"Ying: {getYAuhenticator().YResult.ymessage}", YingMessageBox.YingMessageType.yface_cry, 6040);
                            }
                            else
                            {
                                YingConfig.YArgs.YAccount = getYAuhenticator().YResult;
                                YingMessageBox.Ying($"Ying: {getYAuhenticator().YResult.ymessage}", YingMessageBox.YingMessageType.yface_smile, 6040);
                                (YingApp.Current.MainWindow as YingWindow).yframe.Navigate(new YingMainPage());

                                /*return new AuthenticationInfo
                                 * {
                                 *  AccessToken = Guid.Parse(getYAuhenticator().YResult.yaccessToken),
                                 *  UserType = "Mojang",
                                 *  DisplayName = "Ying",
                                 *  Properties = "{}",
                                 *  UUID = Guid.Parse(getYAuhenticator().YResult.yopenid)
                                 * };*/
                            }
                        });
                        break;

                    case YingStruct.YingType.YCode:
                        //if (this.ywait.Status == TaskStatus.Running) this.ywait.Dispose();
                        YingCodeStruct ycode = JsonConvert.DeserializeObject <YingCodeStruct>(yevent.yStruct.ydata.ymessage);

                        this.Dispatcher.Invoke(() =>
                        {
                            YRegisterButton.IsEnabled = true;
                            YRegisterButton.Content   = "  〉";
                            YYLoading.Visibility      = Visibility.Collapsed;
                            if (!ycode.isYSuccess)
                            {
                                YingMessageBox.Ying($"Ying: {ycode.ymessage}", YingMessageBox.YingMessageType.yface_cry, 6040);
                            }
                            else
                            {
                                this.ystep++;
                                if (this.ystep == 2)
                                {
                                    this.ystepf.Visibility = Visibility.Collapsed;
                                    this.ysteps.Visibility = Visibility.Visible;
                                }
                                YingMessageBox.Ying($"Ying: {ycode.ymessage}", YingMessageBox.YingMessageType.yface_smile, 6040);
                            }
                        });
                        break;

                    case YingStruct.YingType.YRegister:
                        //if (this.ywait.Status == TaskStatus.Running) this.ywait.Dispose();
                        YingRegisterStruct yregister = JsonConvert.DeserializeObject <YingRegisterStruct>(yevent.yStruct.ydata.ymessage);

                        this.Dispatcher.Invoke(() =>
                        {
                            YRegisterButton.IsEnabled = true;
                            YRegisterButton.Content   = "  〉";
                            YYLoading.Visibility      = Visibility.Collapsed;
                            if (!yregister.isYSuccess)
                            {
                                YingMessageBox.Ying($"Ying: {yregister.ymessage}", YingMessageBox.YingMessageType.yface_cry, 6040);
                            }
                            else
                            {
                                YingConfig.YArgs.YAccount = new YingLoginStruct {
                                    yaccessToken = yregister.yaccessToken, yclientToken = yregister.yclientToken, yuser = yregister.yuser
                                };
                                YingMessageBox.Ying($"Ying: {yregister.ymessage}", YingMessageBox.YingMessageType.yface_smile, 6040);
                                (YingApp.Current.MainWindow as YingWindow).yframe.Navigate(new YingMainPage());
                            }
                        });


                        break;

                    default:
                        break;
                    }
                }
            });



            /*YAuthType.SelectionChanged += (y, yy) =>
             * {
             *  switch(YAuthType.SelectedIndex)
             *  {
             *      case (int)YingAuthType.YLegacy:
             *          YServerList.IsEnabled = false;
             *          YServerList.Items.Clear();
             *          YServerList.Items.Add(ydefault);
             *          YServerList.SelectedIndex = 0;
             *
             *          YPassword.IsEnabled = false;
             *          break;
             *
             *      case (int)YingAuthType.YMojang:
             *          YServerList.IsEnabled = false;
             *          YServerList.Items.Clear();
             *          YServerList.Items.Add(ydefault);
             *          YServerList.SelectedIndex = 0;
             *
             *          YPassword.IsEnabled = true;
             *          break;
             *
             *      case (int)YingAuthType.YAuthLib:
             *          YServerList.IsEnabled = true;
             *          YServerList.Items.Clear();
             *
             *          YingConfig.YArgs.YAuthServers.ForEach((yserver) => {
             *              if (yserver.ytype == YingAuthType.YAuthLib)
             *              {
             *                  YServerList.Items.Add(yserver);
             *              }
             *          });
             *          if (YServerList.Items.Count != 0) YServerList.SelectedIndex = 0;
             *
             *          YPassword.IsEnabled = true;
             *          break;
             *
             *      case (int)YingAuthType.Ying:
             *          YServerList.IsEnabled = true;
             *          YServerList.Items.Clear();
             *
             *          YingConfig.YArgs.YAuthServers.ForEach((yserver) => {
             *              if(yserver.ytype == YingAuthType.Ying)
             *              {
             *                  YServerList.Items.Add(yserver);
             *              }
             *          });
             *          if(YServerList.Items.Count != 0) YServerList.SelectedIndex = 0;
             *
             *
             *          YPassword.IsEnabled = true;
             *          break;
             *
             *      default:
             *          break;
             *  }
             * };*/
        }
        protected override void OnMessage(MessageEventArgs yevent)
        {
            YingStruct ymessage = JsonConvert.DeserializeObject <YingStruct>(yevent.Data);

            if (ymessage.Ying != "颖")
            {
                return;
            }
            switch (ymessage.ytype)
            {
            case YingStruct.YingType.Ying:
                this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                {
                    ytype = YingStruct.YingType.Ying,
                    ydata = new YingStruct.YingData()
                    {
                        ycode    = 0,
                        ysender  = "Ying Server",
                        ymessage = JsonConvert.SerializeObject(new iYingStruct()
                        {
                            yservername = "Ying Server Air Plus",
                            yipaddress  = "127.0.0.1",
                            ymotd       = "so tell me your story."
                        }),
                        ydata = null
                    }
                }), ycompleted);
                break;

            case YingStruct.YingType.YUpdata:
                this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                {
                    ytype = YingStruct.YingType.YUpdata,
                    ydata = new YingStruct.YingData()
                    {
                        ycode    = 0,
                        ysender  = "Ying Server",
                        ymessage = JsonConvert.SerializeObject(new YingUpdataStruct
                        {
                        }),
                        ydata = null
                    }
                }), ycompleted);
                break;

            case YingStruct.YingType.YLogin:

                YingLoginStruct        ylogin  = JsonConvert.DeserializeObject <YingLoginStruct>(ymessage.ydata.ymessage);
                YingAuthenticateResult yresult = YingAuthenticate.yauthenticate(ylogin.yuser.yemail, ylogin.yuser.ypassword);
                YingLoginStruct        yylogin = new YingLoginStruct();
                if (ylogin.yclientToken == null)
                {
                    ylogin.yclientToken = Guid.NewGuid().ToString("N");
                }

                if (yresult.ysuccess)
                {
                    String yaccessToken = Guid.NewGuid().ToString("N");
                    getYDataBaseManager().getYConnection().Insert(new zyy_yggdrasil_tokens
                    {
                        yemail = ylogin.yuser.yemail,

                        yclientToken = ylogin.yclientToken,
                        yaccessToken = yaccessToken,

                        ytime = getYTimeStamp().TotalMilliseconds
                    });

                    zyy_users yuser = yresult.yuser;
                    yuser.ypassword = null;
                    yuser.ysalt     = null;

                    yylogin = new YingLoginStruct
                    {
                        yclientToken = ylogin.yclientToken,
                        yaccessToken = yaccessToken,
                        ymessage     = "Ying Ok",
                        yuser        = yuser
                    };
                }
                else
                {
                    yylogin = new YingLoginStruct
                    {
                        yclientToken = ylogin.yclientToken,
                        yaccessToken = null,
                        ymessage     = "Ying Error"
                    };
                }

                this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                {
                    Ying  = "颖",
                    ytype = YingStruct.YingType.YLogin,
                    ydata = new YingStruct.YingData()
                    {
                        ycode    = 0,
                        ysender  = "Ying Server",
                        ymessage = JsonConvert.SerializeObject(yylogin),
                        ydata    = null
                    }
                }), ycompleted);
                break;

            case YingStruct.YingType.YCode:
                YingCodeStruct ycode = JsonConvert.DeserializeObject <YingCodeStruct>(ymessage.ydata.ymessage);
                if (ycode.ycode == 20020604)
                {
                    if (YingAuthenticate.yauthenticate(ycode.yemail, String.Empty).yuser.yid == 0)
                    {
                        int yycode = new Random().Next(100000, 1000000);
                        try
                        {
                            getYDataBaseManager().getYConnection().Delete <zyy_verification_code>((from y in getYDataBaseManager().getYConnection().Table <zyy_verification_code>()
                                                                                                   where y.yemail == ycode.yemail
                                                                                                   select y).FirstOrDefault().yid);
                        }
                        catch { }
                        getYDataBaseManager().getYConnection().Insert(new zyy_verification_code
                        {
                            yemail = ycode.yemail,
                            ycode  = yycode,
                            ytime  = getYTimeStamp().TotalMilliseconds
                        });
                        YingMail.ysend(ycode.yemail, new System.IO.StreamReader(getYFiles().getYResource("Ying.yresources.yhtml.Ying.html")).ReadToEnd().Replace("{{yname}}", "烟雨城").Replace("{{ycode}}", Convert.ToString(yycode)));
                        ycode.isYSuccess = true;
                        ycode.ymessage   = "验证码已发送到您的邮箱";
                        this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                        {
                            Ying  = "颖",
                            ytype = YingStruct.YingType.YCode,
                            ydata = new YingStruct.YingData()
                            {
                                ycode    = 0,
                                ysender  = "Ying Server",
                                ymessage = JsonConvert.SerializeObject(ycode),
                                ydata    = null
                            }
                        }), ycompleted);
                    }
                    else
                    {
                        ycode.isYSuccess = false;
                        ycode.ymessage   = "该邮箱地址已经被使用";
                        this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                        {
                            Ying  = "颖",
                            ytype = YingStruct.YingType.YCode,
                            ydata = new YingStruct.YingData()
                            {
                                ycode    = 0,
                                ysender  = "Ying Server",
                                ymessage = JsonConvert.SerializeObject(ycode),
                                ydata    = null
                            }
                        }), ycompleted);
                    }
                }
                else
                {
                    zyy_verification_code yycode = (from y in getYDataBaseManager().getYConnection().Table <zyy_verification_code>()
                                                    where y.yemail == ycode.yemail
                                                    select y).FirstOrDefault();
                    if (ycode.ycode == yycode.ycode)
                    {
                        ycode.isYSuccess = true;
                        ycode.ymessage   = "验证码正确";
                    }
                    else
                    {
                        ycode.isYSuccess = false;
                        ycode.ymessage   = "验证码错误";
                    }
                    this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                    {
                        Ying  = "颖",
                        ytype = YingStruct.YingType.YCode,
                        ydata = new YingStruct.YingData()
                        {
                            ycode    = 0,
                            ysender  = "Ying Server",
                            ymessage = JsonConvert.SerializeObject(ycode),
                            ydata    = null
                        }
                    }), ycompleted);
                }
                break;

            case YingStruct.YingType.YRegister:
                YingRegisterStruct yregister = JsonConvert.DeserializeObject <YingRegisterStruct>(ymessage.ydata.ymessage);
                if ((from y in getYDataBaseManager().getYConnection().Table <zyy_verification_code>()
                     where y.yemail == yregister.yuser.yemail
                     select y).FirstOrDefault().ycode == yregister.ycode)
                {
                    String ypassword = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(yregister.yuser.ypassword))).Replace("-", "").ToLower();
                    String ysalt     = new YingCode(6).YCode;
                    getYDataBaseManager().getYConnection().Insert(new zyy_users {
                        yusername = yregister.yuser.yusername,
                        ypassword = BitConverter.ToString(new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(ypassword + ysalt))).Replace("-", "").ToLower(),
                        ysalt     = ysalt,
                        yopenid   = Guid.NewGuid().ToString("N"),
                        ysex      = yregister.yuser.ysex,
                        yemail    = yregister.yuser.yemail,
                        yprofiles = JsonConvert.SerializeObject(new String[] { new YingUUID(yregister.yuser.yusername).YUUID.ToString("N") })
                    });
                    getYDataBaseManager().getYConnection().Insert(new zyy_yggdrasil_profile
                    {
                        yyid       = new YingUUID(yregister.yuser.yusername).YUUID.ToString("N"),
                        yname      = yregister.yuser.yusername,
                        ytextureid = "zyy2a2a1399140968341fdedc4aa8b94"
                    });
                    if (yregister.yclientToken == null)
                    {
                        yregister.yclientToken = Guid.NewGuid().ToString("N");
                    }
                    String yaccessToken = Guid.NewGuid().ToString("N");
                    getYDataBaseManager().getYConnection().Insert(new zyy_yggdrasil_tokens
                    {
                        yemail = yregister.yuser.yemail,

                        yclientToken = yregister.yclientToken,
                        yaccessToken = yaccessToken,

                        ytime = getYTimeStamp().TotalMilliseconds
                    });
                    yregister.isYSuccess = true;
                    yregister.ymessage   = "注册成功";
                }
                else
                {
                    yregister.isYSuccess = false;
                    yregister.ymessage   = "验证码错误";
                    yregister.yuser      = new zyy_users();
                }
                this.SendAsync(JsonConvert.SerializeObject(new YingStruct()
                {
                    Ying  = "颖",
                    ytype = YingStruct.YingType.YRegister,
                    ydata = new YingStruct.YingData()
                    {
                        ycode    = 0,
                        ysender  = "Ying Server",
                        ymessage = JsonConvert.SerializeObject(yregister),
                        ydata    = null
                    }
                }), ycompleted);
                break;

            case YingStruct.YingType.YMessage:
                this.SendAsync(yevent.Data, ycompleted);
                break;

            case YingStruct.YingType.YMusic:
                this.SendAsync(yevent.Data, ycompleted);
                break;

            default:
                break;
            }
        }