[Release]Another report system

05/27/2010 00:46 xScott#1
I did this for educational purposes and thought i'd release it.
It works uses ini's, When a player is reported for a certain reason, it +1's to total number of reports

Atm it still can be spammed, by making new reasons up, mispelling the name/reason etc. you'll see what i mean alter; im gonna change it soon.

Ok heres the class:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


namespace WorldServer
{
    public class ReportPlayer
    {

        private static int NumberOfReports;
        private static bool BeenReported;
        private string FILEPATH = @"[[B][COLOR="Red"]THE DIRECTORY YOU WANT TO SAVE THE INI IN[/COLOR][/B]].";

       
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, 
        StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// Checks if the player has been reported once before.
        /// If so the Section will need to be removed so the NumberOfReports will append
        /// a new Value for the same key.
        /// </summary>
        public bool CheckPlayerReported(string Name, string Reason)
        {
            string temps;
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Name, Reason, "", temp, 255, FILEPATH );
            temps = System.Convert.ToString(temp);

            if (temps == "")
                NumberOfReports = 0;
            else
                NumberOfReports = int.Parse(temps);


            if (NumberOfReports == 0)
                BeenReported = false;

            else
                BeenReported = true;

            return BeenReported;
        }

        /// <summary>
        /// Gets number the number of times player has been reported so
        /// the total will be NumberOfReports +1
        /// </summary>
        public int GetNumberReported(string Name, string Reason)
        {
                string temps;
                StringBuilder temp = new StringBuilder(255);

                int i = GetPrivateProfileString(Name, Reason, "", temp, 255, FILEPATH);
                temps = System.Convert.ToString(temp);
                NumberOfReports = int.Parse(temps);
                return NumberOfReports;
        }

        /// <summary>
        /// Write the account to the .ini file.
        /// </summary>


        public void WriteAccount(string Name, string Reason, int ReportsNumber)
        {
            WritePrivateProfileString(Name, Reason, Convert.ToString(ReportsNumber) , FILEPATH);

        }


    }
}
Then all you need to do is code the command, this is how i did it.
Note: this is in Elite-CoEmu & is VERY EASY to convert.

Code:
#region report
                        case "report":
                            {
                                ReportPlayer R = new ReportPlayer();
                                if (R.CheckPlayerReported(Command[1], Command[2]) == true)
                                {
                                    int TotalReports = R.GetNumberReported(Command[1], Command[2]);
                                    TotalReports += 1;
                                    R.WriteAccount(Command[1], Command[2], TotalReports);
                                }
                                else
                                {
                                    R.WriteAccount(Command[1], Command[2], 1);
                                }
                                break;
                            }
                        #endregion
Im going to eventually alter this so it checks the player being reported against accounts in the database, and wont write to the file unless the character exists.

i also need to add some sort of timer to stop mass reports.

#EDIT
Added the checking of the database, heres the method:

Code:
public bool CheckDatabase(string CharacterName)
        {

            MySqlCommand cmd = new MySqlCommand(MySqlCommandType.SELECT);
            cmd.Select("characters").Where("Name", CharacterName);
            MySqlReader r = new MySqlReader(cmd);
            if (r.Read())
            {
                if (r.ReadString("Name") == CharacterName)
                { AccountExists = true; }
            }
            return AccountExists;
        }
I used templates from how the portals were loaded etc, gonna play around with it abit more so i know abit more about using MySQL.

Your also onn need to add the following namespace to the class.
Code:
using MySqlHandler;
Your also gonna have to define AccountExists; i did it at the top
where i defined FilePath & NumberOfReports, IE:
Code:
        private static int NumberOfReports;
        private static bool BeenReported;
[B][COLOR="Red"]        private static bool AccountExists = false;[/COLOR][/B]
        private string FILEPATH = @"C:\Users\ADVENT\Documents\REPORT.ini";
Heres an example how how i added it to the Chat.cs:
05/27/2010 04:19 hunterman01#2
Glad your starting to get better scott
05/27/2010 05:16 Arcо#3
Very impressed.
Glad to see you advancing in C# scott!
05/27/2010 13:34 QuickCo#4
great ;) I like that release good luck
05/27/2010 17:14 scottdavey#5
Good work :)
05/27/2010 17:33 Basser#6
Scott, when you asked me what you were doing wrong, and told me what you were working on, I was impressed, when you solved the problem, without any of my help, I was impressed, but now that I see the actual thing, I'm even more impressed.
You are doing fantastic Scott, keep up. :)
05/27/2010 17:37 QuickCo#7
nice to see members as saying that about other people
05/27/2010 17:38 xScott#8
Thanks (: working on the mql thing now ;o
05/27/2010 18:27 xScott#9
/updated, now has method to check database character exists :P
05/30/2010 00:13 _Emme_#10
For the reports, use Assert.IsEqual after you get one of the properties, continue if nothing of the old report was found on the new one, else continue on using the IsEqual<Type> method.

Although, this only works in some Visual Studio that supports UnitTesting,

Goodluck.