BF2Statistics.Database.StatsDatabase.DeletePlayer C# (CSharp) Method

DeletePlayer() public method

Removes a player, based on pid, from the stats database
public DeletePlayer ( int Pid, IProgress TaskProgress = null ) : void
Pid int The players Id
TaskProgress IProgress The progress object to report to.
return void
        public void DeletePlayer(int Pid, IProgress<TaskProgressUpdate> TaskProgress = null)
        {
            using (DbTransaction Transaction = BeginTransaction())
            {
                try
                {
                    // Remove the player from each player table
                    foreach (string Table in PlayerTables)
                    {
                        if (TaskProgress != null)
                            TaskProgress.Report(new TaskProgressUpdate("Removing player from \"" + Table + "\" table..."));

                        if (Table == "kills")
                            base.Execute(String.Format("DELETE FROM {0} WHERE attacker={1} OR victim={1}", Table, Pid));
                        else
                            base.Execute(String.Format("DELETE FROM {0} WHERE id={1}", Table, Pid));
                    }

                    // Commit Transaction
                    if (TaskProgress != null)
                        TaskProgress.Report(new TaskProgressUpdate("Committing Transaction"));
                    Transaction.Commit();
                }
                catch (Exception)
                {
                    // Rollback!
                    Transaction.Rollback();
                    throw;
                }
            }
        }

Usage Example

        /// <summary>
        /// Reset stats button click event
        /// </summary>
        private async void ResetStatsBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to reset players stats?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    TaskForm.Show(this, "Reset Player Stats", "Reseting Player \"" + Player["name"] + "\"'s Stats", false);
                    await Task.Run(() =>
                    {
                        // Delete the players
                        using (StatsDatabase Driver = new StatsDatabase())
                        {
                            // Delete old player statistics
                            Driver.DeletePlayer(Pid, TaskForm.Progress);

                            // Insert a new player record
                            Driver.Execute(
                                "INSERT INTO player(id, name, country, joined, clantag, permban, isbot) VALUES(@P0, @P1, @P2, @P3, @P4, @P5, @P6)",
                                Pid, Player["name"], Player["country"], Player["joined"], Player["clantag"], Player["permban"], Player["isbot"]
                            );
                        }
                    });

                    // Reload player
                    LoadPlayer();
                    Notify.Show("Player Stats Reset Successfully!", "Operation Successful", AlertType.Success);
                }
                catch (DbConnectException Ex)
                {
                    HttpServer.Stop();
                    ExceptionForm.ShowDbConnectError(Ex);
                    TaskForm.CloseForm();
                    this.Close();
                    return;
                }
                catch (Exception E)
                {
                    // Show exception error
                    using (ExceptionForm Form = new ExceptionForm(E, false))
                    {
                        Form.Message = String.Format("Failed to reset player stats!{1}{1}Error: {0}", E.Message, Environment.NewLine);
                        Form.ShowDialog();
                    }
                }
                finally
                {
                    // Close task form
                    TaskForm.CloseForm();
                }
            }
        }
All Usage Examples Of BF2Statistics.Database.StatsDatabase::DeletePlayer