0 Members and 1 Guest are viewing this topic.
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.
Requires S.L.C Extended Timers : https://forum.vc-mp.org/?topic=1487.msg10339#msg10339
DecreaseCooldown <- _Timer.Create(this, DecreaseCooldown, 1000, 0);// db to be createddbCooldown <- ConnectSQL("Databases/Cooldown.db");QuerySQL( dbCooldown, "CREATE TABLE IF NOT EXISTS Cooldown ( Cmd TEXT, Name TEXT, Time NUMERIC )" );
// 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 notfunction 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 cooldownfunction AddCooldown( cmd, player, time ) {if(player) {QuerySQL( dbCooldown, "INSERT INTO Cooldown ( Cmd, Name, Time) VALUES ( '" + cmd + "', '" + player.Name + "', '"+time+"' )" );} } // checks time remaining for cooldownfunction 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;}
// add in command to return if cooldown is setelse 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 checksAddCooldown( cmd, player, 5 ); // this will add cmd, player and 5 seconds cooldown, these parameters can be edited
// Used in Keybindfunction OnKeyDown(player, key) {if(key == HKEY && player.Vehicle) {// by pressing H, it will flip a vehicle and add a cooldown of 2 secondsif(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;}
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 hereif(!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;}
This snippet can be used for any event which require Cooldown as explained in Examples
Inferno , S.L.C ( for extended timers ), Xmair ( flip command used in Examples )
Might not be the best idea to invoke a sql query on every event, but overall as starter. Good Effort!