Punishment System

Started by Maximiliano, Oct 03, 2019, 07:21 PM

Previous topic - Next topic

Maximiliano

Requeriments: S.L.C Extended Timers ( http://forum.vc-mp.org/?topic=1487.0 )

Commands: /ban, /banick, /banip, /tempban, /mute, /unban, /unbanip, /unmute

Code:

[spoiler]
Put in OnScriptLoad:
[noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae]db <- ConnectSQL( "DataBase.db" );
dofile("scripts/_PunishmentSystem.nut");
Punishment.LoadSystem()
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]

Put in OnPlayerJoin:
[noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae]Punishment.CheckMute(player, 1);
Punishment.CheckBan(player);
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]

Put in OnPlayerPart:
[noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae]if ( Muted[ player.ID ] ) Punishment.CheckMute(player, 2);
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]

Put in OnPlayerChat:
[noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae][noae]if ( Muted[ player.ID ] && Muted[ player.ID ].Time.tointeger() > 0 )
{
MessagePlayer("You are muted. In "+ Muted[ player.ID ].Time +" seconds you can speak again", player );
}
else ClientMessageToAll( player.Name + "[#FFFFFF]: " + text, player.Color.r, player.Color.g, player.Color.b );
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]

Put in onPlayerCommand:
[noae][noae][noae][noae][noae][noae][noae][noae][noae]if(cmd == "ban" || cmd == "banip" || cmd == "banick" || cmd == "unban"|| cmd == "unbanip" || cmd == "mute" || cmd == "unmute" || cmd == "tempban" ) Punishment.Command(cmd, text, player);[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]

Put at the end of the main.nut:
[spoiler][noae][noae][noae][noae][noae][noae][noae][noae][noae]
function GetTok(string, separator, n, ...)
{
local m = vargv.len() > 0 ? vargv[0] : n,
  tokenized = split(string, separator),
  text = "";
if (n > tokenized.len() || n < 1) return null;
for (; n <= m; n++)
{
text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
}
return text;
}

function rotateRight(val, sbits)
{
return (val >> sbits) | (val << (0x20 - sbits));
}

function e( string )
{
local hp = [
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
];
local k = [
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
];
local
w          = array( 64 ),
i          = 0,
s          = 0,
len        = string.len( ),
word_array = array( 0 );
for( i = 0; i < len - 3; i += 4 )
{
word_array.push( string[i] << 0x18 | string[i + 1] << 0x10 | string[i + 2] << 0x08 | string[i + 3] );
}
switch( len % 4 )
{
case 0:
i = 0x80000000;
break;
case 1:
i = string[len - 1] << 0x18 | 0x80000000;
break;
case 2:
i = string[len - 2] << 0x18 | string[len - 1] << 0x10 | 0x08000;
break;
case 3:
i = string[len - 3] << 0x18 | string[len - 2] << 0x10 | string[len - 1] << 0x08 | 0x80;
break;
}
word_array.push( i );
while( ( word_array.len() % 0x10 ) != 0x0E )
word_array.push( 0 );
word_array.push( len >> 0x10 );
word_array.push( ( len << 0x03 ) & 0xFFFFFFFF );
local s0, s1;
for( s = 0; s < word_array.len(); s += 0x10 )
{
for( i = 0x00; i < 0x10; i++ )
w[i] = word_array[s + i];

for( i = 0x10; i < 0x40; i++ )
{
s0   = rotateRight( w[i - 15], 7 ) ^ rotateRight( w[i - 15], 18 ) ^ ( w[i - 15] >> 3 );
s1   = rotateRight( w[i - 2], 17 ) ^ rotateRight( w[i - 2], 19 ) ^ ( w[i - 2] >> 10 );
w[i] = w[i - 0x10] + s0 + w[i - 7] + s1;
}
local a = hp[0],
      b = hp[1],
      c = hp[2],
      d = hp[3],
      e = hp[4],
      f = hp[5],
      g = hp[6],
      h = hp[7];
for( i = 0x00; i < 0x40; i++ )
{
s0        = ( rotateRight( a, 2 ) ^ rotateRight( a, 13 ) ^ rotateRight( a, 22 ) );
local maj = ( ( a & b ) ^ ( a & c ) ^ ( b & c ) );
local t2  = ( s0 + maj );
s1        = ( rotateRight( e, 6 ) ^ rotateRight( e, 11) ^ rotateRight( e, 25 ) );
local ch  = ( ( e & f ) ^ ( ( ~e ) & g ) );
local t1  = ( h + s1 + ch + k[i] + w[i] );

h = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
hp[0] += a;
hp[1] += b;
hp[2] += c;
hp[3] += d;
hp[4] += e;
hp[5] += f;
hp[6] += g;
hp[7] += h;
}

local hash = format(
"%08x%08x%08x%08x%08x%08x%08x%08x",
hp[0],
hp[1],
hp[2],
hp[3],
hp[4],
hp[5],
hp[6],
hp[7]
);
return hash;
}

function GetPlayer( plr )
{
if ( plr )
{
if ( IsNum( plr ) )
{
plr = FindPlayer( plr.tointeger() );
if ( plr ) return plr;
else return false;
}
else
{
plr = FindPlayer( plr );
if ( plr ) return plr;
else return false;
}
}
else return false;
}

function CheckAllBan(){
for( local i = 0; i < GetMaxPlayers(); i++ )
    {
local plr = FindPlayer(i);
if(plr)
                {
Punishment.CheckBan(plr);
}
    }
}
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/spoiler]

Create a file .nut called _PunishmentSystem.nut with the following code:
Pastebin: https://pastebin.com/tkJjhzDJ

[noae][noae][noae][noae][noae][noae][noae][noae][noae]Muted <- array( GetMaxPlayers(), null );

Punishment <- {

    function LoadSystem()
    {
        Punishment.CreateTable();
        print("==================================");
        print(" ");
        print("  Punishment System by (ARG)Maximiliano");
        print(" ");
        Punishment.CountBans();
        print(" ");
        print("==================================");
        DecreaseBanTime <- _Timer.Create(this, Punishment.DecreaseBanTime, 1000, 0);
    }

    function CreateTable()
    {
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS Bans ( Name TEXT, Admin TEXT, Reason TEXT, Date TEXT, Type NUMERIC, UID VARCHAR(255), UID2 VARCHAR(255), IP VARCHAR(32) )" );
        //Types of Bans: Type 0 = Permanent - Type 1 = Temporary - Type 2 = Nick
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS TempBans ( Name TEXT, Time NUMERIC )" ); // This db is where you will store the time, which will decrease every second (It can be modified to minutes or hours)
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS BanIPs ( IP TEXT, Admin TEXT )" );
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS Mute ( Name TEXT, Time NUMERIC )" );
        QuerySQL( db, "CREATE TABLE IF NOT EXISTS UsersDatas ( Name TEXT, UID VARCHAR(255), UID2 VARCHAR(255), IP VARCHAR(32) )" );
    }

//-------------------------------------- Bans functions --------------------------------------//

    function AddBan(player, Admin, Reason, BanType)
    {
        QuerySQL( db, "INSERT INTO Bans ( Name, Admin, Reason, Date, Type, UID, UID2, IP ) VALUES ( '" + player.Name + "', '" + Admin + "', '" + Reason + "', '" +Punishment.GetDate()+ "', '" + BanType + "', '" + e(player.UniqueID) + "', '" + e(player.UniqueID2) + "', '" + player.IP + "' )" );
        Message("[#FF4500][ADMIN][#EEDD82] " + player.Name +" was banned by " + Admin + ". Reason: " + Reason + "." );
        CheckAllBan();
    }

    function AddBanIP(IP, Admin)
    {
        //It will store the ips and the admin that banned them as log
        QuerySQL( db, "INSERT INTO BanIPs VALUES ( '" + IP + "', '" + Admin + "' )" );
        Message("[#FF4500][ADMIN][#EEDD82] " + IP +" banned by " + Admin );
        BanIP(IP);
    }

    function AddTempBan(player, Admin, Reason, Time)
    {
        Punishment.AddBan(player, Admin, Reason, 1);
        QuerySQL( db, "INSERT INTO TempBans ( Name, Time) VALUES ( '" + player.Name + "', '" + Time + "'  )" );
    }

    function Unban(player, Admin)
    {
        QuerySQL( db, "DELETE FROM TempBans WHERE Name='" + player + "'" );
        QuerySQL( db, "DELETE FROM Bans WHERE Name='" + player + "'" );
        Message( "[#FF4500][ADMIN][#EEDD82] "+ player +" was unbanned by "+Admin+"." );
    }

    function UnbanIP(IP, Admin)
    {
        QuerySQL( db, "DELETE FROM BanIPs WHERE IP='" + IP + "'" );
        Message( "[#FF4500][ADMIN][#EEDD82] "+ IP +" unbanned by "+Admin+"." );
        UnbanIP(IP);
    }

    function CheckBan(player)
    {
        local q = QuerySQL( db, "SELECT Name,Type FROM Bans WHERE UID='" + e(player.UniqueID) + "' " );
 
        if ( q )
        {
            if ( GetSQLColumnData( q , 1) == 2)
            {
                if (GetSQLColumnData( q , 0).tolower() == player.Name.tolower())
                {
                    Message( format("[#EAEAEA][[#F0FFF0]BAN[#EAEAEA]][#EEDD82] %s was kicked. Reason: Nick banned.", player.Name) );
                    player.Kick();
                }
            }
            else
            {
                Message( format("[#EAEAEA][[#F0FFF0]BAN[#EAEAEA]][#EEDD82] %s was kicked. Reason: Banned with the account: %s", player.Name, GetSQLColumnData( q , 0)) );
                player.Kick();
            }
               
        }

        //This will save data that will be useful when giving offline users ban
    local q = QuerySQL( db, "SELECT * FROM UsersDatas WHERE Name='"+ player.Name +"'" )
    if( !q ) QuerySQL( db, "INSERT INTO UsersDatas ( Name, UID, UID2, IP ) VALUES ( '" + player + "', '" + player.UniqueID + "', '" + player.UniqueID2 + "', '" + player.IP + "' )" );
    }

    function DecreaseBanTime()
    {
        QuerySQL( db, "UPDATE Tempbans SET Time= (Time - 1) WHERE Time > 0" );
        local q = QuerySQL( db, "SELECT Name FROM TempBans WHERE Time='0'" );

        local name = GetSQLColumnData( q , 0);
        if( name )
        {
            Message( format("[#EAEAEA][[#F0FFF0]BAN[#EAEAEA]][#EEDD82] %s's ban time is over", name));
            Punishment.Unban(name, "SERVER");
        }
    }

    //------------------- Mute functions --------------------//

    function AddMute(player, admin, reason, Time)
    {
    QuerySQL( db, "INSERT INTO Mute ( Name, Time) VALUES ( '" + player.Name + "', '" + Time + "' )" );
    Message( "[#EAEAEA][[#F0FFF0]MUTE[#EAEAEA]][#EEDD82] "+ player.Name +"'s was muted by "+ admin +". Reason: "+ reason);
    Punishment.CheckMute(player, 1);
    }

    function UnMute(player, admin)
    {
    QuerySQL( db, "DELETE FROM Mute WHERE Name='" + player + "'" );
    _Timer.Destroy(Muted[ player.ID ].Timer);
    Muted[ player.ID ] = null;
    Message( format("[#EAEAEA][[#F0FFF0]MUTE[#EAEAEA]][#EEDD82] %s's was unmuted by %s.", player.Name, admin));
    }

    function CheckMute(player, type)
    {
        switch (type)
        {
            //Type 1: When a user join in the server or is silenced.
            //Type 2: When a user leaves the server

            case 1:
            local q = QuerySQL( db, "SELECT Time FROM Mute WHERE Name='" + player + "'" );   
            if ( GetSQLColumnData( q , 0) )
            {
                try{
                Muted[ player.ID ] =
                {
                    Time = GetSQLColumnData( q , 0).tointeger(),
                    Timer = _Timer.Create(this, Punishment.DecreaseMuteTime, 1000, 0, player)
                };
                } catch (e)
                {
                    print(e);
                }
            }
            break;
           
            case 2:
            QuerySQL( db, format("UPDATE Mute SET Time='"+ Muted[ player.ID ].Time +"' WHERE Name='"+ player.Name +"'" ) );
            _Timer.Destroy(Muted[ player.ID ].Timer);
            Muted[ player.ID ] = null;
            break;
        }
    }

    function DecreaseMuteTime(player)
    {
        if (Muted[ player.ID ].Time == 0)
        {
            UnMute(player, "SERVER");
        }
        else
        {
        Muted[ player.ID ].Time--;;
        }
    }

    //---------- Other functions ---------//
    function GetDate()
    {
    return format("%.2d/%.2d/%.2d - %.2d hour/s, %.2d minute/s, %.2d second/s", date().day, (date().month +1), date().year, date().hour, date().min, date().sec);
    }

    function CountBans()
    {
    local TempBans = QuerySQL( db, "SELECT count(*) from TempBans" ), Bans = QuerySQL( db, "SELECT count(*) from Bans WHERE Type='0'" ), BanNicks = QuerySQL( db, "SELECT count(*) from Bans WHERE Type='2'" ), IPBans = QuerySQL( db, "SELECT count(*) from BanIPs" );
    print("  "+ GetSQLColumnData( TempBans , 0) + " Accounts with temporary ban.");
    print("  "+ GetSQLColumnData( Bans , 0) + " Accounts with permanent ban.");
    print("  "+ GetSQLColumnData( BanNicks , 0) + " Nicks banneds.");
    print("  "+ GetSQLColumnData( IPBans , 0) + " IPs with ban.");
    }

    function Command(cmd, text, player)
    {
        switch ( cmd.tolower() )
        {
            case "ban":
            if (!text) MessagePlayer("Sintax: /ban <nick> <reason>", player)
            else
            {
                local plr = GetTok(text, " ", 1), reason = "Not specified.";
                local q = QuerySQL( db, "SELECT * FROM UsersDatas WHERE Name='"+ plr +"'");
                if (!q) MessagePlayer("User not found, verify that the name is complete and check upper and lower case.", player);
                else
                {
                     plr = {
                        Name = GetSQLColumnData( q, 0 ),
                        UniqueID = GetSQLColumnData( q, 1 ),
                        UniqueID2 = GetSQLColumnData( q, 2 ),
                        IP = GetSQLColumnData( q, 3 )
                    };

                    if (GetTok(text, " ", 2)) reason = GetTok(text, " ", 2);
                    Punishment.AddBan(plr, player.Name, reason, 0);
                }
            }
            break;

            case "banick":
            case "bannick":
            if (!text) MessagePlayer("Sintax: /ban <nick> <reason>", player)
            else
            {
                local plr = GetTok(text, " ", 1), reason = "Not specified.", q = QuerySQL( db, "SELECT * FROM UsersDatas WHERE Name='"+ plr +"'");
                if (!q) MessagePlayer("User not found, verify that the name is complete and check upper and lower case.", player);
                else
                {
                    plr = {
                        Name = GetSQLColumnData( q, 0 ),
                        UniqueID = GetSQLColumnData( q, 1 ),
                        UniqueID2 = GetSQLColumnData( q, 2 ),
                        IP = GetSQLColumnData( q, 3 )
                    }
                   
                    if (GetTok(text, " ", 2)) reason = GetTok(text, " ", 2);
                    Punishment.AddBan(plr, player.Name, reason, 2);
                }
            }
            break;

            case "tempban":
            if(!text) MessagePlayer("Sintax: /banick <nick> <days:hours:minutes> <reason>", player);
            else
            {
                local plr = GetTok(text, " ", 1),
                time = GetTok(text, " ", 2),
                reason = GetTok(text, " ", 3),
                q = QuerySQL( db, "SELECT * FROM UsersDatas WHERE Name='"+ plr +"'"),
                q1 = QuerySQL( db, "SELECT * FROM TempBans WHERE Name='"+ plr +"'");
               
                if (!time) MessagePlayer("Sintax: /tempban <nick> <days:hours:minutes> <reason>", player);
                else if (!q) MessagePlayer("User not found, verify that the name is complete and check upper and lower case.", player);
                else if (q1) MessagePlayer("Already banned.", player);
                else
                {
                    local days = (GetTok(time, ":", 1).tointeger() * 86400); //Days to seg
                    local hours = (GetTok(time, ":", 2).tointeger() * 3600); //Hours to seg
                    local mins = (GetTok(time, ":", 3).tointeger() * 60); //Minutes to seg
                    local total = (days + hours + mins);
                    plr = {
                        Name = GetSQLColumnData( q, 0 ),
                        UniqueID = GetSQLColumnData( q, 1 ),
                        UniqueID2 = GetSQLColumnData( q, 2 ),
                        IP = GetSQLColumnData( q, 3 )
                    }
                    if (!reason) reason = "Not specified.";
                    Punishment.AddTempBan(plr, player.Name, reason, total);
                }
            }
            break;

            case "banip":
            if (!text) MessagePlayer("/banip IP", player);
            else
            {
                local ip = GetTok(text, " ", 1);
                Punishment.AddBanIP(ip, player.Name);
            }
            break;

            case "mute":
            if (!text) MessagePlayer("/mute <nick> <minutes> <reason>", player);
            else
            {
                local plr = GetPlayer(GetTok(text, " ", 1)), minutes = GetTok(text, " ", 2), reason = GetTok(text, " ", 3);
               
                if (!minutes || !reason) MessagePlayer("/mute <nick> <minutes> <reason>",player);
                else if ( Muted[ plr.ID ] ) MessagePlayer("Player already silenced", player);
                else Punishment.AddMute(plr, player.Name, reason, (minutes.tointeger() * 60) );
            }
            break;
            case "unbanip":
            if (!text) MessagePlayer("/unbanip IP", player);
            else
            {
                Punishment.UnBanIP(ip, player.Name);
            }
            break;

            case "unban":
            if (!text) MessagePlayer("/unban nick", player);
            else
            {
                local plr = GetTok(text, " ", 1);
                local q = QuerySQL( db, "SELECT * FROM Bans WHERE Name='"+plr+"'");
                if (!q) MessagePlayer("Player isn't banned", player);
                else Punishment.Unban(plr, player.Name);
            }
            break;

            case "unmute":
            if (!text) MessagePlayer("/unmute nick", player);
            else
            {
                local plr = GetPlayer(GetTok(text, " ", 1));
                if ( !plr ) MessagePlayer("Player not found", player);
                else if ( !Muted[ plr.ID ] ) MessagePlayer("Player isn't muted", player);
                else Punishment.UnMute(plr, player.Name);
            }
            break;
        }
    }
};
[/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae][/noae]
[/spoiler]

Have tested it all, and it works fine, if you found some bug, tell me.



Sebastian

#1
Code looks neat, post has all the info you need.
Keep it up!

Maximiliano

Quote from: Sebastian on Oct 03, 2019, 08:33 PMCode looks neat, post has all the info you need.
Keep it up!

Thanks :D

MatheuS

if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

JackSparrow

We can't ban this tag plr =FF= but we can ban this tag plr [FF]

Maximiliano

Quote from: JackSparrow on Jan 16, 2020, 04:14 AMWe can't ban this tag plr =FF= but we can ban this tag plr [FF]
any error in the console?

=RK=MarineForce

Index Timer Does not exists wtf?

Maximiliano