예제 #1
0
        /// <summary>
        /// Whenever the "newuser" command is recieved, this method is called to
        /// add the new users information into the database
        /// </summary>
        /// <param name="Recv">Array of parms sent by the server</param>
        private void CreateNewUser(Dictionary <string, string> Recv)
        {
            // Make sure the user doesnt exist already
            try
            {
                using (GamespyDatabase Database = new GamespyDatabase())
                {
                    // Check to see if user exists
                    if (Database.UserExists(Recv["nick"]))
                    {
                        Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\This account name is already in use!\id\1\final\");
                        Disconnect(DisconnectReason.CreateFailedUsernameExists);
                        return;
                    }

                    // We need to decode the Gamespy specific encoding for the password
                    string Password = GamespyUtils.DecodePassword(Recv["passwordenc"]);
                    string Cc       = (RemoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
                        ? GeoIP.GetCountryCode(RemoteEndPoint.Address)
                        : "US";

                    // Attempt to create account. If Pid is 0, then we couldnt create the account
                    if ((PlayerId = Database.CreateUser(Recv["nick"], Password, Recv["email"], Cc)) == 0)
                    {
                        Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\Error creating account!\id\1\final\");
                        Disconnect(DisconnectReason.CreateFailedDatabaseError);
                        return;
                    }

                    Stream.SendAsync(@"\nur\\userid\{0}\profileid\{0}\id\1\final\", PlayerId);
                }
            }
            catch (Exception e)
            {
                // Check for invalid query params
                if (e is KeyNotFoundException)
                {
                    Stream.SendAsync(@"\error\\err\0\fatal\\errmsg\Invalid Query!\id\1\final\");
                }
                else
                {
                    Stream.SendAsync(@"\error\\err\516\fatal\\errmsg\Error creating account!\id\1\final\");
                    Program.ErrorLog.Write("ERROR: [GpcmClient.CreateNewUser] An error occured while trying to create a new User account :: " + e.Message);
                }

                Disconnect(DisconnectReason.GeneralError);
                return;
            }
        }
        /// <summary>
        /// Event fired when the Submit button is pushed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpdateBtn_Click(object sender, EventArgs e)
        {
            int Pid = (int)PlayerID.Value;

            using (GamespyDatabase Database = new GamespyDatabase())
            {
                // Make sure there is no empty fields!
                if (AccountNick.Text.Trim().Length < 3)
                {
                    MessageBox.Show("Please enter a valid account name", "Error");
                    return;
                }
                else if (!Validator.IsValidEmail(AccountEmail.Text))
                {
                    MessageBox.Show("Please enter a valid account email", "Error");
                    return;
                }
                else if (Pid != AccountId)
                {
                    if (!Validator.IsValidPID(Pid.ToString()))
                    {
                        MessageBox.Show("Invalid PID Format. A PID must be 8 or 9 digits in length", "Error");
                        return;
                    }
                    // Make sure the PID doesnt exist!
                    else if (Database.UserExists(Pid))
                    {
                        MessageBox.Show("Battlefield 2 PID is already taken. Please try a different PID.", "Error");
                        return;
                    }
                }

                Database.UpdateUser(AccountId, Pid, AccountNick.Text, AccountPass.Text, AccountEmail.Text);
            }
            this.Close();
        }
예제 #3
0
        /// <summary>
        /// Reads the next input in the console (Blocking Method)
        /// </summary>
        private static void CheckInput()
        {
            // Get user input [Blocking]
            string Input = Console.ReadLine();

            // Make sure input is not empty
            if (String.IsNullOrWhiteSpace(Input))
            {
                Console.WriteLine("Please enter a command");
                Console.WriteLine();
                Console.Write("Cmd > ");
                return;
            }

            // Split input into an array by whitespace, empty entries are removed
            string[] InParts = Input.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
            Dictionary <string, object> user = new Dictionary <string, object>();

            // Process Task
            try
            {
                switch (InParts[0].ToLowerInvariant())
                {
                case "stop":
                case "quit":
                case "exit":
                    IsRunning = false;     // Setting to false will stop program loop
                    break;

                case "connections":
                    Console.WriteLine("Total Connections: {0}", ServerManager.NumConnections());
                    break;

                case "accounts":
                    using (GamespyDatabase Db = new GamespyDatabase())
                        Console.WriteLine("Total Accounts: {0}", Db.GetNumAccounts());
                    break;

                case "fetch":
                    // Prevent an out of range exception
                    if (InParts.Length < 2)
                    {
                        Console.WriteLine("Incorrect command format. Please type 'help' to see list of available commands.");
                        break;
                    }

                    // Make sure we have a nick
                    if (String.IsNullOrEmpty(InParts[1]))
                    {
                        Console.WriteLine("No account named provided. Please make sure you are providing an account name, and not a space");
                        break;
                    }

                    // Fetch user account info
                    using (GamespyDatabase Db = new GamespyDatabase())
                        user = Db.GetUser(InParts[1]);

                    if (user == null)
                    {
                        Console.WriteLine("Account '{0}' does not exist in the gamespy database.", InParts[1]);
                        break;
                    }

                    // Get BF2 PID
                    Console.WriteLine(" - PlayerID: " + user["id"].ToString());
                    Console.WriteLine(" - Email: " + user["email"].ToString());
                    Console.WriteLine(" - Country: " + user["country"].ToString());
                    break;

                case "create":
                    // Prevent an out of range exception
                    if (InParts.Length < 4)
                    {
                        Console.WriteLine("Incorrect command format. Please type 'help' to see list of available commands.");
                        break;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(InParts[1]) || String.IsNullOrEmpty(InParts[2]) || String.IsNullOrEmpty(InParts[3]))
                    {
                        Console.WriteLine("Account name, password, or email was not provided. Please try again with the correct format.");
                        break;
                    }

                    // Disposible connection
                    using (GamespyDatabase Db = new GamespyDatabase())
                    {
                        // Make sure the account exists!
                        if (Db.UserExists(InParts[1]))
                        {
                            Console.WriteLine("Account '{0}' already exists in the gamespy database.", InParts[1]);
                            break;
                        }

                        bool r = Db.CreateUser(InParts[1], InParts[2], InParts[3], "00") > 0;
                        Console.WriteLine((r == true) ? "Account created successfully" : "Error creating account!");
                    }
                    break;

                case "delete":
                    // Prevent an out of range exception
                    if (InParts.Length < 2)
                    {
                        Console.WriteLine("Incorrect command format. Please type 'help' to see list of available commands.");
                        break;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(InParts[1]))
                    {
                        Console.WriteLine("Account name was not provided. Please try again with the correct format.");
                        break;
                    }

                    // Disposible connection
                    using (GamespyDatabase Db = new GamespyDatabase())
                    {
                        // Make sure the account exists!
                        if (!Db.UserExists(InParts[1]))
                        {
                            break;
                        }

                        // Do a confimration
                        Console.Write("Are you sure you want to delete account '{0}'? <y/n>: ", InParts[1]);
                        string v = Console.ReadLine().ToLower().Trim();

                        // If no, stop here
                        if (v == "n" || v == "no" || v == "na" || v == "nope")
                        {
                            Console.WriteLine("Command cancelled.");
                            break;
                        }

                        // Process any command other then no
                        if (v == "y" || v == "yes" || v == "ya" || v == "yep")
                        {
                            if (Db.DeleteUser(InParts[1]) == 1)
                            {
                                Console.WriteLine("Account deleted successfully");
                            }
                            else
                            {
                                Console.WriteLine("Failed to remove account from database.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Incorrect repsonse. Aborting command");
                        }
                    }

                    break;

                case "setpid":
                    // Prevent an out of range exception
                    if (InParts.Length < 3)
                    {
                        Console.WriteLine("Incorrect command format. Please type 'help' to see list of available commands.");
                        break;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(InParts[1]) || String.IsNullOrEmpty(InParts[2]))
                    {
                        Console.WriteLine("Account name or PID not provided. Please try again with the correct format.");
                        break;
                    }

                    // Disposible connection
                    using (GamespyDatabase Db = new GamespyDatabase())
                    {
                        // Make sure the account exists!
                        user = Db.GetUser(InParts[1]);
                        if (user == null)
                        {
                            Console.WriteLine("Account '{0}' does not exist in the gamespy database.", InParts[1]);
                            break;
                        }

                        // Try to make a PID out of parts 2
                        int newpid;
                        if (!Int32.TryParse(InParts[2], out newpid))
                        {
                            Console.WriteLine("Player ID must be an numeric only!");
                            break;
                        }

                        // try and set the PID
                        int    result  = Db.SetPID(InParts[1], newpid);
                        string message = "";
                        switch (result)
                        {
                        case 1:
                            message = "New PID is set!";
                            break;

                        case 0:
                            message = "Error setting PID";
                            break;

                        case -1:
                            message = String.Format("Account '{0}' does not exist in the gamespy database.", InParts[1]);
                            break;

                        case -2:
                            message = String.Format("PID {0} is already in use.", newpid);
                            break;
                        }
                        Console.WriteLine(" - " + message);
                    }
                    break;

                case "?":
                case "help":
                    Console.Write(Environment.NewLine +
                                  "stop/quit/exit          - Stops the server" + Environment.NewLine +
                                  "connections             - Displays the current number of connected clients" + Environment.NewLine +
                                  "accounts                - Displays the current number accounts in the DB." + Environment.NewLine +
                                  "create {nick} {password} {email}  - Create a new Gamespy account." + Environment.NewLine +
                                  "delete {nick}           - Deletes a user account." + Environment.NewLine +
                                  "fetch {nick}            - Displays the account information" + Environment.NewLine +
                                  "setpid {nick} {newpid}  - Sets the BF2 Player ID of the givin account name" + Environment.NewLine
                                  );
                    break;

                case "enter":
                    // Insert a break point here in Visual Studio to and enter this command to "Enter" the program during debugging
                    IsRunning = true;
                    break;

                default:
                    Console.WriteLine("Unrecognized input '{0}'", Input);
                    break;
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }

            // Await a new command
            if (IsRunning)
            {
                Console.WriteLine();
                Console.Write("Cmd > ");
            }
        }
예제 #4
0
        public void CheckInput()
        {
            // Get user input
            Console.Write("cmd > ");
            string Line = Console.ReadLine();

            // Make sure input is not empty
            if (string.IsNullOrWhiteSpace(Line))
            {
                return;
            }

            // Define some base vars
            Dictionary <string, object> user = new Dictionary <string, object>();

            string command = Line.Trim();

            string[] parts = command.Split(' ');
            try
            {
                switch (parts[0])
                {
                case "stop":
                case "quit":
                case "exit":
                    isRunning = false;
                    break;

                case "connections":
                    Console.WriteLine(" - Total Connections: {0}" + Environment.NewLine, CmServer.NumClients());
                    break;

                case "accounts":
                    Console.WriteLine(" - Total Accounts: {0}" + Environment.NewLine, Database.GetNumAccounts());
                    break;

                case "fetch":
                    // Prevent an out of range exception
                    if (parts.Length < 2)
                    {
                        Console.WriteLine(" - Incorrect command format. Please type 'help' to see list of available commands.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure we have a nick
                    if (String.IsNullOrEmpty(parts[1]))
                    {
                        Console.WriteLine(" - No account named provided. Please make sure you are providing an account name, and not a space");
                        Console.WriteLine("");
                        return;
                    }

                    // Fetch user account info
                    user = Database.GetUser(parts[1]);
                    if (user == null)
                    {
                        Console.WriteLine(
                            " - Account '{0}' does not exist in the gamespy database." + Environment.NewLine,
                            parts[1]);
                        return;
                    }

                    // Get BF2 PID
                    Console.Write(
                        " - Account ID: " + user["id"].ToString() + Environment.NewLine +
                        " - Email: " + user["email"].ToString() + Environment.NewLine +
                        " - Country: " + user["country"].ToString() + Environment.NewLine
                        + Environment.NewLine
                        );
                    break;

                case "create":
                    // Prevent an out of range exception
                    if (parts.Length < 4)
                    {
                        Console.WriteLine(" - Incorrect command format. Please type 'help' to see list of available commands.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(parts[1]) || String.IsNullOrEmpty(parts[2]) || String.IsNullOrEmpty(parts[3]))
                    {
                        Console.WriteLine(" - Account name, password, or email was not provided. Please try again with the correct format.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure the account exists!
                    if (Database.UserExists(parts[1]))
                    {
                        Console.WriteLine(" - Account '{0}' already exists in the gamespy database.", parts[1]);
                        return;
                    }

                    bool   r = Database.CreateUser(parts[1], parts[2], parts[3], "00");
                    string m = (r == true) ? " - Account created successfully" : " - Error creating account!";
                    Console.WriteLine(m + Environment.NewLine);
                    break;

                case "delete":
                    // Prevent an out of range exception
                    if (parts.Length < 2)
                    {
                        Console.WriteLine(" - Incorrect command format. Please type 'help' to see list of available commands.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(parts[1]))
                    {
                        Console.WriteLine(" - Account name was not provided. Please try again with the correct format.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure the account exists!
                    if (!Database.UserExists(parts[1]))
                    {
                        Console.WriteLine(" - Account '{0}' doesnt exist in the gamespy database.", parts[1]);
                        Console.WriteLine("");
                        return;
                    }

                    // Do a confimration
                    Console.Write(" - Are you sure you want to delete account '{0}'? <y/n>: ", parts[1]);
                    string v = Console.ReadLine().ToLower();

                    // If no, stop here
                    if (v == "n" || v == "no")
                    {
                        Console.WriteLine(" - Command cancelled." + Environment.NewLine);
                        return;
                    }

                    // Process any command other then no
                    if (v == "y" || v == "yes")
                    {
                        string output = "";
                        if (Database.DeleteUser(parts[1]) == 1)
                        {
                            output = " - Account deleted successfully";
                        }
                        else
                        {
                            output = " - Failed to remove account from database.";
                        }

                        Console.WriteLine(output + Environment.NewLine);
                    }
                    else
                    {
                        Console.WriteLine(" - Incorrect repsonse. Aborting command" + Environment.NewLine);
                    }

                    break;

                case "setpid":
                    // Prevent an out of range exception
                    if (parts.Length < 3)
                    {
                        Console.WriteLine(" - Incorrect command format. Please type 'help' to see list of available commands.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure our strings are not empty!
                    if (String.IsNullOrEmpty(parts[1]) || String.IsNullOrEmpty(parts[2]))
                    {
                        Console.WriteLine(" - Account name or PID not provided. Please try again with the correct format.");
                        Console.WriteLine("");
                        return;
                    }

                    // Make sure the account exists!
                    user = Database.GetUser(parts[1]);
                    if (user == null)
                    {
                        Console.WriteLine(" - Account '{0}' does not exist in the gamespy database.", parts[1]);
                        return;
                    }

                    // Try to make a PID out of parts 2
                    int newpid;
                    if (!Int32.TryParse(parts[2], out newpid))
                    {
                        Console.WriteLine(" - Player ID must be an numeric only!");
                        Console.WriteLine("");
                        return;
                    }

                    // try and set the PID
                    int    result  = Database.SetPID(parts[1], newpid);
                    string message = "";
                    switch (result)
                    {
                    case 1:
                        message = "New PID is set!";
                        break;

                    case 0:
                        message = "Error setting PID";
                        break;

                    case -1:
                        message = String.Format("Account '{0}' does not exist in the gamespy database.", parts[1]);
                        break;

                    case -2:
                        message = String.Format("PID {0} is already in use.", newpid);
                        break;
                    }
                    Console.WriteLine(" - " + message);
                    Console.WriteLine("");
                    break;

                case "help":
                    Console.Write(Environment.NewLine +
                                  "stop/quit/exit          - Stops the server" + Environment.NewLine +
                                  "connections             - Displays the current number of connected clients" + Environment.NewLine +
                                  "accounts                - Displays the current number accounts in the DB." + Environment.NewLine +
                                  "create {nick} {password} {email}  - Create a new Gamespy account." + Environment.NewLine +
                                  "delete {nick}           - Deletes a user account." + Environment.NewLine +
                                  "fetch {nick}            - Displays the account information" + Environment.NewLine +
                                  "setpid {nick} {newpid}  - Sets the BF2 Player ID of the givin account name" + Environment.NewLine
                                  + Environment.NewLine
                                  );
                    break;

                default:
                    lock (Console.Out)
                    {
                        Console.WriteLine("Unrecognized input '{0}'", Line);
                    }
                    break;
                }
            }
            catch {}

            Thread.Sleep(100);
        }
        private void CreateBtn_Click(object sender, EventArgs e)
        {
            int Pid = (int)PidBox.Value;

            using (GamespyDatabase Database = new GamespyDatabase())
            {
                // Make sure there is no empty fields!
                if (AccountName.Text.Trim().Length < 3)
                {
                    MessageBox.Show("Please enter a valid account name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (AccountPass.Text.Trim().Length < 3)
                {
                    MessageBox.Show("Please enter a valid account password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else if (!Validator.IsValidEmail(AccountEmail.Text))
                {
                    MessageBox.Show("Please enter a valid account email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                // Check if PID exists (for changing PID)
                if (PidSelect.SelectedIndex == 1)
                {
                    if (!Validator.IsValidPID(Pid.ToString()))
                    {
                        MessageBox.Show("Invalid PID Format. A PID must be 8 or 9 digits in length", "Error",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else if (Database.UserExists(Pid))
                    {
                        MessageBox.Show("PID is already in use. Please enter a different PID.", "Error",
                                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }

                // Check if the user exists
                if (Database.UserExists(AccountName.Text))
                {
                    MessageBox.Show("Account name is already in use. Please select a different Account Name.", "Error",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                try
                {
                    // Attempt to create the account
                    if (PidSelect.SelectedIndex == 1)
                    {
                        Database.CreateUser(Pid, AccountName.Text, AccountPass.Text, AccountEmail.Text, "00");
                    }
                    else
                    {
                        Database.CreateUser(AccountName.Text, AccountPass.Text, AccountEmail.Text, "00");
                    }

                    Notify.Show("Account Created Successfully!", AccountName.Text, AlertType.Success);
                }
                catch (Exception E)
                {
                    MessageBox.Show(E.Message, "Account Create Error");
                }
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }