login
April 19, 2024, 12:01:36 am

Author Topic: Cooldown Function.  (Read 1109 times)

0 Members and 1 Guest are viewing this topic.

Inferno69

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 761
  • Country: pk
  • 👀
    • View Profile
    • VFS
Cooldown Function.
« on: October 12, 2020, 03:44:58 am »

Intro

Quote
Cooldown functions have become a must in any server nowadays in order to prevent any spam/abuse by any player. So, here i would like to post a cooldown function I made for my server and is effective for me.

Requirement:
Quote
Requires S.L.C Extended Timers : https://forum.vc-mp.org/?topic=1487.msg10339#msg10339

OnScriptLoad

// timer callback
Code: [Select]
DecreaseCooldown <- _Timer.Create(this, DecreaseCooldown, 1000, 0);

// db to be created
dbCooldown <- ConnectSQL("Databases/Cooldown.db");
QuerySQL( dbCooldown, "CREATE TABLE IF NOT EXISTS Cooldown ( Cmd TEXT, Name TEXT, Time NUMERIC )" );




Any where in main.nut
Code: [Select]
  // Timer
 function DecreaseCooldown()
    {
        QuerySQL( dbCooldown, "UPDATE Cooldown SET Time= (Time - 1) WHERE Time > 0" );
        local q = QuerySQL( dbCooldown, "SELECT Cmd FROM Cooldown WHERE Time='0'" );
        local cmd = GetSQLColumnData( q , 0);
        local name = GetSQLColumnData( q , 1);
        if( q && cmd && name )
        {
        QuerySQL( dbCooldown, "DELETE FROM Cooldown WHERE Cmd='" + cmd + "' AND Name='"+name+"' " );
       }
    }
// checks if cooldown for a cmd for a player is set or not
function CheckCooldown( command, player )
 {
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+command+"' AND Name='"+player.Name+"' AND Time > 0" );
local atime = GetSQLColumnData( q , 2);
local name = GetSQLColumnData( q, 1 );
local cmda = GetSQLColumnData( q, 0 );
    if ( name && atime && cmda ) return 1;
    else return 0;
  }
// adds cooldown
function AddCooldown( cmd, player, time ) {
if(player) {
QuerySQL( dbCooldown, "INSERT INTO Cooldown ( Cmd, Name, Time) VALUES ( '" + cmd + "', '" + player.Name + "', '"+time+"'  )" );
}

// checks time remaining for cooldown
function CooldownTime( cmd, player ) {
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+cmd+"' AND Name='"+player.Name+"' AND Time > 0" );
local atime = GetSQLColumnData( q , 2);
local name = GetSQLColumnData( q, 1 );
local cmda = GetSQLColumnData( q, 0 );
    if ( name && atime && cmda ) return atime;
    else return 0;
}




Functions to use in commands/any other event
Code: [Select]
// add in command to return if cooldown is set
else if(CheckCooldown(cmd,player)) return MessagePlayer(""+RED+"[Cooldown] You can use this command again after "+CooldownTime(cmd,player)+" seconds ",player);

// add when command can be executed after all checks
AddCooldown( cmd, player, 5 ); // this will add cmd, player and 5 seconds cooldown, these parameters can be edited


Examples
Example 1
Code: [Select]
// Used in Keybind
function OnKeyDown(player, key) {


if(key == HKEY && player.Vehicle)
{
// by pressing H, it will flip a vehicle and add a cooldown of 2 seconds
if(CheckCooldown(key,player)) return MessagePlayer("[#ffff00][Cooldown] You can use this command again after "+CooldownTime(key,player)+" seconds ",player);
AddCooldown( key, player, 2 );
player.Vehicle.Rotation = Quaternion(0.0, 0.0, player.Vehicle.Rotation.z, player.Vehicle.Rotation.w);
ClientMessage("Flipped.", player, 176, 176, 176);
}

return 1;
}

Example 2
Code: [Select]

OnPlayerCommand(player, cmd) {


// this cmd will flip the car and add a 2s cooldown
 if ( cmd == "flip" )
{
if ( !player.Vehicle ) MessagePlayer( "[#ff1111]Error: [#FF7092]You need to be in a Vehicle to use this command." , player )
else if(CheckCooldown(cmd,player)) return MessagePlayer("[#ffff00][Cooldown] You can use this command again after "+CooldownTime(cmd,player)+" seconds ",player);
else
{
AddCooldown( cmd, player, 2 );
player.Vehicle.Rotation = Quaternion(0.0, 0.0, player.Vehicle.Rotation.z, player.Vehicle.Rotation.w);
ClientMessage("Flipped.", player, 176, 176, 176);
}
return 0;
}

// Command to set manually Cooldown to any value for a player when its existing. Added for Testing Purpose

  else if (cmd == "setcool")
{
// add your admin function here
if(!text) return MessagePlayer("[#ffff00]/setcool [player] [cmd/arg] [cooldown]",player);
else {
local plr = GetTok(text, " ", 1);
local acmd = GetTok(text, " ", 2);
local acd = GetTok(text, " ", 3);
local q = QuerySQL( dbCooldown, "SELECT* FROM Cooldown WHERE Cmd='"+acmd+"' AND Name='"+plr+"' AND Time > 0" );
if(!q) return MessagePlayer(""+RED+" That argument is not set to cooldown for that player ",player);
else if(!plr) return MessagePlayer(""+RED+" Please specify a player name ",player);
else if(!acmd) return MessagePlayer(""+RED+" Please specify a command for cd ",player);
else if(!acd) return MessagePlayer(""+RED+" Please specify a cd ",player);
else if(!IsNum(acd)) return MessagePlayer(" "+RED+" CD Values must be in numbers ",player);
else {
QuerySQL(dbCooldown, "UPDATE Cooldown SET Time = '"+acd.tointeger()+"' WHERE Name = '" + plr + "' AND Cmd='"+acmd+"'");
MessagePlayer("[#ffff00] You have set "+acd+" seconds cooldown for "+acmd+" command for player "+plr+" ",player);
}
}
return 0;


return 1;
}


Functions

Quote
This snippet can be used for any event which require Cooldown as explained in Examples

Credits
Quote
Inferno , S.L.C ( for extended timers ), Xmair ( flip command used in Examples )



Ps : wrong board. Someone kindly move it.
Winner Winner x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor





Thomas

  • Crazy Man
  • *****
  • Posts: 942
  • Country: pk
  • Death awaits you always
    • View Profile
Re: Cooldown Function.
« Reply #1 on: October 13, 2020, 08:50:06 pm »
Might not be the best idea to invoke a sql query on every event, but overall as starter.
 Good Effort!
Winner Winner x 1 (list)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor

Vice War 8 Developer
Vice War 8 Web Master
Playing VC-MP Since 2009
Representing Miami Killers.
T4iVi3R | HeisenBerg | Thomas | Tom4S

Inferno69

  • Vice Underdog
  • Crazy Man
  • *
  • *
  • *
  • Posts: 761
  • Country: pk
  • 👀
    • View Profile
    • VFS
Re: Cooldown Function.
« Reply #2 on: October 14, 2020, 12:25:32 am »
Might not be the best idea to invoke a sql query on every event, but overall as starter.
 Good Effort!
Yeah but i havent operated with raw data yet so i had to use what i have . thanks man :)
Agree Disagree Funny Winner Pwnt Informative Friendly Useful Optimistic Artistic Late Brain Donor