private static bool YSFHandle_12_Unjoin(Client ThisClient, Packets.GenericPacket InPacket)
                {
                    //ThisClient.YSFServer.SendPacket(InPacket);

                    //Re-Synchronise...
                    OpenYS_Link.OYS_Link_Response Response = OpenYS_Link.Get(OpenYS_Link.Stats._id, "stats_total_flight_seconds");
                    double OldTime = 0;
                    bool   Failed  = !Double.TryParse(Response.Response, out OldTime);

                    if (!Response.Success | Failed)
                    {
                        ThisClient.SendMessage("Failed to update your flight time due to a server error! Sorry about that!");
                        return(true);
                    }



                    //Update OYS_LINK...
                    double difference = OpenYS_Link.Stats.total_flight_seconds - OldTime;

                    Response = OpenYS_Link.Set(OpenYS_Link.Stats._id, "stats_total_flight_seconds", OpenYS_Link.Stats.total_flight_seconds);
                    if (Response.Success)
                    {
                        ThisClient.SendMessage("Successfully updated your flight time by " + Math.Round(difference.AsSeconds().TotalHours, 2).ToString() + " hours.");
                        ThisClient.SendMessage("Your new total flight time is " + Math.Round(OpenYS_Link.Stats.total_flight_seconds.AsSeconds().TotalHours, 2).ToString() + " hours.");

                        return(true);
                    }
                    else
                    {
                        ThisClient.SendMessage("Failed to update your flight time due to a server error! Sorry about that!");
                        return(true);
                    }
                }
Exemplo n.º 2
0
 public static void GetAllStats()
 {
     #region total_flight_seconds
     OYS_Link_Response total_flight_seconds = OpenYS_Link.Get(Stats._id, "stats_total_flight_seconds");
     if (total_flight_seconds.Success)
     {
         bool Failed = !Double.TryParse(total_flight_seconds.Response, out Stats.total_flight_seconds);
     }
     #endregion
 }
        private void Authenticate()
        {
            //Disable all inputs!
            Username_TextBox.Enabled             = false;
            Password_TextBox.Enabled             = false;
            Authentication_Button.Enabled        = false;
            ShowPassword_CheckBox.Enabled        = false;
            AuthenticationStatus_Label.Text      = "AUTHENTICATING...";
            AuthenticationStatus_Label.ForeColor = Color.FromArgb(240, 200, 0);
            AuthenticationStatus_Label.BackColor = Color.Transparent;
            OpenYS_Link.Stats._id = 0;
            Application.DoEvents();

            using (var client = new WebClient())
            {
                var values = new NameValueCollection();
                values["username"] = Username_TextBox.Text;

                if (PasswordIsEncrypted)
                {
                    values["password"] = OYS_Cryptography.DecryptPassword(Password_TextBox.Text);
                }
                else
                {
                    values["password"] = Password_TextBox.Text;
                }
                try
                {
                    //Key Verification:
                    //https://forum.ysfhq.com/api-check.php?apikey=

                    string APIKey = Environment.GetAPIKey();

                    if (APIKey == "MISSINGNO." | APIKey == "")
                    {
                        MessageBox.Show("APIKey not supplied for this custom build of OYS!\n\nTo get your own API Key, contact YSFHQ Webmasters, then place that key inside APIKey.txt, inside OpenYS-Library/Utilities!", "API Key Not Found!");
                        Username_TextBox.Enabled             = false;
                        Password_TextBox.Enabled             = false;
                        ShowPassword_CheckBox.Enabled        = false;
                        Authentication_Button.Enabled        = false; //leave this false, unable to authenticate without reloading!
                        AuthenticationStatus_Label.Text      = "API KEY MISSING";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(128, 0, 0);
                        AuthenticationStatus_Label.BackColor = Color.FromArgb(240, 200, 0);
                        OpenYS_Link.Stats._id = 0;
                        Application.DoEvents();
                        return;
                    }

                    var response       = client.UploadValues("https://forum.ysfhq.com/api-login.php?apikey=" + APIKey, values);
                    var responseString = Encoding.Default.GetString(response);
                    System.Console.WriteLine(responseString);
                    bool Failed = !Int32.TryParse(responseString.Replace("\"", ""), out OpenYS_Link.Stats._id);

                    if (Failed | OpenYS_Link.Stats._id <= 0)
                    {
                        //NO GOOD!
                        Username_TextBox.Enabled             = true;
                        Password_TextBox.Enabled             = true;
                        ShowPassword_CheckBox.Enabled        = true;
                        Authentication_Button.Enabled        = true;
                        AuthenticationStatus_Label.Text      = "AUTHENTICATION FAIL!";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(240, 0, 0);
                        AuthenticationStatus_Label.BackColor = Color.Transparent;
                        Application.DoEvents();
                        return;
                    }
                    else
                    {
                        //ALL GOOD!
                        if (!PasswordIsEncrypted)
                        {
                            //Only update if the password has been modified!

                            SettingsHandler.LoadAll();
                            Settings.YSFHQ.Username          = Username_TextBox.Text;
                            Settings.YSFHQ.EncryptedPassword = OYS_Cryptography.EncryptPassword(Password_TextBox.Text);
                            SettingsHandler.SaveAll();
                        }
                        Username_TextBox.Enabled             = true;
                        Password_TextBox.Enabled             = true;
                        ShowPassword_CheckBox.Enabled        = true;
                        Authentication_Button.Enabled        = true;
                        AuthenticationStatus_Label.Text      = "AUTHENTICATED";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(0, 128, 0);
                        AuthenticationStatus_Label.BackColor = Color.Transparent;
                        Application.DoEvents();

                        OpenYS_Link.GetAllStats();

                        //OpenYS_Link.OYS_Link_Response Response = OpenYS_Link.Get(YSFHQ_ID, "/stats_total_flight_seconds");
                        //MessageBox.Show(Response.Response, Response.Reason);

                        //^^ WORKING!
                        return;
                    }
                }
                catch (System.Net.WebException WebError)
                {
                    HttpStatusCode ErrorCode = ((HttpWebResponse)WebError.Response).StatusCode;

                    if (ErrorCode == HttpStatusCode.BadRequest) //400
                    {
                        //Missing Username / Password
                        Username_TextBox.Enabled             = true;
                        Password_TextBox.Enabled             = true;
                        ShowPassword_CheckBox.Enabled        = true;
                        Authentication_Button.Enabled        = true;
                        AuthenticationStatus_Label.Text      = "NO USER OR PASS?";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(240, 0, 0);
                        AuthenticationStatus_Label.BackColor = Color.Transparent;
                        Application.DoEvents();
                        return;
                    }
                    if (ErrorCode == HttpStatusCode.Unauthorized) //401
                    {
                        //Bad API Key
                        MessageBox.Show("APIKey not accepted for this release of OYS!\n\nPlease confirm your API Key with YSFHQ Webmasters, then update OpenYS-Library/Utilities/APIKey.txt!", "API Key Rejected!");
                        Username_TextBox.Enabled             = false;
                        Password_TextBox.Enabled             = false;
                        ShowPassword_CheckBox.Enabled        = false;
                        Authentication_Button.Enabled        = false; //leave this false, unable to authenticate without reloading!
                        AuthenticationStatus_Label.Text      = "API KEY REJECTED";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(128, 0, 0);
                        AuthenticationStatus_Label.BackColor = Color.FromArgb(240, 200, 0);
                        Application.DoEvents();
                        return;
                    }
                    if (ErrorCode == HttpStatusCode.Forbidden) //403
                    {
                        //Bad Username / Password
                        Username_TextBox.Enabled             = true;
                        Password_TextBox.Enabled             = true;
                        ShowPassword_CheckBox.Enabled        = true;
                        Authentication_Button.Enabled        = true;
                        AuthenticationStatus_Label.Text      = "AUTHENTICATION FAIL!";
                        AuthenticationStatus_Label.ForeColor = Color.FromArgb(240, 0, 0);
                        AuthenticationStatus_Label.BackColor = Color.Transparent;
                        Application.DoEvents();
                        return;
                    }
                    //Generic Error?
                    MessageBox.Show("Generic Error Occured:\n\n" + WebError.Message, "Genereric Error!");
                    Username_TextBox.Enabled             = true;
                    Password_TextBox.Enabled             = true;
                    ShowPassword_CheckBox.Enabled        = true;
                    Authentication_Button.Enabled        = true;
                    AuthenticationStatus_Label.Text      = "GENERIC ERROR!";
                    AuthenticationStatus_Label.ForeColor = Color.FromArgb(240, 0, 0);
                    AuthenticationStatus_Label.BackColor = Color.Transparent;
                    Application.DoEvents();
                    return;
                    //System.Console.WriteLine(WebError);
                }
            }
        }