SA-MP Forums Archive
Need help to make code short and better.. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Need help to make code short and better.. (/showthread.php?tid=562641)



Need help to make code short and better.. - MBilal - 10.02.2015

Код:
public OnPlayerUpdate(playerid)
{
	UpdatePlayerMoney(playerid);
	Anticheat(playerid);
    return 1;
}
stock Anticheat(playerid)
{
   	new Float:Armour;
	GetPlayerArmour(playerid,Armour);
	new a[16];
	GetPlayerIp(playerid, a, sizeof(a));
	new year, month, day, hour, minuite, second;
    getdate(year, month, day);
  	gettime(hour,minuite,second);
    if(Player[playerid][pAdmin] < 1 )
    {
 	if(Armour > 99.0 && hacker[playerid] == 0){
	SCM(playerid, COLOR_YELLOW, "You have been banned from the server. Reason: Armour Hack");
 	format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Armour Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(playerid),day,month,year,hour,minuite);
 	SCMToAll(COLOR_KRED,Message);
 	format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(playerid),a);
	SaveIn("BanLog.txt",Message);
	hacker[playerid] =1;
    SetTimerEx("Banned",10,false,"d", playerid);
 	return 1;
}
	if(GetPlayerAnimationIndex(playerid))
	{ new animlib[32], animname[32]; GetAnimationName(GetPlayerAnimationIndex(playerid), animlib, sizeof(animlib), animname, sizeof(animname));
	 if(!strcmp(animlib, "PARACHUTE", true) && !strcmp(animname, "FALL_SkyDive_Accel", true))
	 { if(GetPlayerWeapon(playerid) != 46 && hacker[playerid] == 0)  {
	SCM(playerid, COLOR_YELLOW, "You have been banned from the server. Reason: Fly Hack");
 	format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Fly Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(playerid),day,month,year,hour,minuite);
 	SCMToAll(COLOR_KRED,Message);
 	format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(playerid),a);
	SaveIn("BanLog.txt",Message);
	hacker[playerid] =1;
 	SetTimerEx("Banned",10,false,"d", playerid);
 	return 1;
  }}}

	if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER && hacker[playerid] == 0)
	{
	if(GetVehicleSpeed(GetPlayerVehicleID(playerid)) > 250 && !IsVehicleFLYVehicle(GetPlayerVehicleID(playerid))){
	SCM(playerid, COLOR_YELLOW, "You have been Kicked from the server. Reason: Sobiet Speed Hack");
 	format(Message,sizeof(Message),"SERVER : %s has been kick from the server. Reason: Speed Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(playerid),day,month,year,hour,minuite);
 	SCMToAll(COLOR_KRED,Message);
 	format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(playerid),a);
	SaveIn("BanLog.txt",Message);
	hacker[playerid] =1;
    SetTimerEx("KickPlayer",10,false,"d", playerid);
	 return 1;
	}}

		new Float:Pos_x,Float:Pos_y,Float:Pos_z;
        new anim = GetPlayerAnimationIndex(playerid);
        GetPlayerVelocity(playerid,Pos_x,Pos_y,Pos_z);
        if((Pos_x <= -0.800000  || Pos_y <= -0.800000 || Pos_z <= -0.800000) && (anim == 1008 || anim == 1539))
        {
        if(hacker[playerid] == 0)
        {
		SCM(playerid, COLOR_YELLOW, "You have been banned from the server. Reason: Sobiet Fly Hack");
	 	format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Sobiet fly Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(playerid),day,month,year,hour,minuite);
	 	SCMToAll(COLOR_KRED,Message);
	 	format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(playerid),a);
		SaveIn("BanLog.txt",Message);
		hacker[playerid] =1;
	    SetTimerEx("Banned",10,false,"d", playerid);
		}
		}
}
return 1;
}
I'm using this Anti cheat code Under OnPlayerUpdate ! I want to know ! how i can make these all code short and more Efficient to reduce the lag .. Thanks.


Re: Need help to make code short and better.. - Sime30 - 10.02.2015

Remove your anticheat from OnPlayerUpdate!!!!!!!
Make a 2 sec timer for that


Re: Need help to make code short and better.. - MBilal - 10.02.2015

I was using the Timer before for each player under OnPlayerConnect like this SetTimerEx("AntiCheat", 2500, true, "i", playerid);

It was also making lag for players..!


Re: Need help to make code short and better.. - Sime30 - 10.02.2015

I created this fast, correct my mistakes (brackets etc..)
pawn Код:
SetTimer("AntiCheatUpdate",2000,1); // OnGameModeInit

forward AntiCheatUpdate();
public AntiCheatUpdate()
{
    foreach(Player,i)
    {
        new Float:Armour;
        GetPlayerArmour(i,Armour);
        new a[16];
        GetPlayerIp(i, a, sizeof(a));
        new year, month, day, hour, minuite, second;
        getdate(year, month, day);
        gettime(hour,minuite,second);
        if(Player[i][pAdmin] < 1 )
        {
            if(Armour > 99.0 && hacker[i] == 0){
            SCM(i, COLOR_YELLOW, "You have been banned from the server. Reason: Armour Hack");
            format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Armour Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(i),day,month,year,hour,minuite);
            SCMToAll(COLOR_KRED,Message);
            format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(i),a);
            SaveIn("BanLog.txt",Message);
            hacker[i] =1;
            SetTimerEx("Banned",10,false,"d", i);
            return 1;
        }
        if(GetPlayerAnimationIndex(i))
        {
            new animlib[32], animname[32]; GetAnimationName(GetPlayerAnimationIndex(i), animlib, sizeof(animlib), animname, sizeof(animname));
            if(!strcmp(animlib, "PARACHUTE", true) && !strcmp(animname, "FALL_SkyDive_Accel", true))
            {
                if(GetPlayerWeapon(i) != 46 && hacker[i] == 0)
                {
                    SCM(i, COLOR_YELLOW, "You have been banned from the server. Reason: Fly Hack");
                    format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Fly Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(i),day,month,year,hour,minuite);
                    SCMToAll(COLOR_KRED,Message);
                    format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(i),a);
                    SaveIn("BanLog.txt",Message);
                    hacker[i] =1;
                    SetTimerEx("Banned",10,false,"d", i);
                    return 1;
                }
            }
        }

        if(GetPlayerState(i) == PLAYER_STATE_DRIVER && hacker[i] == 0)
        {
            if(GetVehicleSpeed(GetPlayerVehicleID(i)) > 250 && !IsVehicleFLYVehicle(GetPlayerVehicleID(i)))
            {
                SCM(i, COLOR_YELLOW, "You have been Kicked from the server. Reason: Sobiet Speed Hack");
                format(Message,sizeof(Message),"SERVER : %s has been kick from the server. Reason: Speed Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(i),day,month,year,hour,minuite);
                SCMToAll(COLOR_KRED,Message);
                format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(i),a);
                SaveIn("BanLog.txt",Message);
                hacker[i] =1;
                SetTimerEx("KickPlayer",10,false,"d", i);
                return 1;
            }
        }

        new Float:Pos_x,Float:Pos_y,Float:Pos_z;
        new anim = GetPlayerAnimationIndex(i);
        GetPlayerVelocity(i,Pos_x,Pos_y,Pos_z);
        if((Pos_x <= -0.800000  || Pos_y <= -0.800000 || Pos_z <= -0.800000) && (anim == 1008 || anim == 1539))
        {
            if(hacker[i] == 0)
            {
                SCM(i, COLOR_YELLOW, "You have been banned from the server. Reason: Sobiet Fly Hack");
                format(Message,sizeof(Message),"SERVER : %s has been banned from the server. Reason: Sobiet fly Hack <Date: %d/%d/%d> <Time: %d:%d>",GetName(i),day,month,year,hour,minuite);
                SCMToAll(COLOR_KRED,Message);
                format(Message,sizeof(Message),"SERVER : %s and his ip %s",GetName(i),a);
                SaveIn("BanLog.txt",Message);
                hacker[i] =1;
                SetTimerEx("Banned",10,false,"d", i);
            }
        }
    }
    return 1;
}



Re: Need help to make code short and better.. - MBilal - 10.02.2015

I'll try that.


Re: Need help to make code short and better.. - danish007 - 10.02.2015

use timer it will be good!

include foreach
pawn Код:
#include <foreach>
pawn Код:
SetTimer("Anticheat", 1000, 1);
pawn Код:
forward Anticheat();
pawn Код:
public Anticheat()
{
    foreach (new i : Player)
    {
        //use your code here
    }
}



Re: Need help to make code short and better.. - MBilal - 10.02.2015

Код:
Now I got 5 Timers Under OnGameMod

	SetTimer("AntiCheatUpdate",2000,true); //this is for anti cheat
	SetTimer("GlobalAnnouncement",50000,true);//this is for showing random messages
	SetTimer("TimeOnServer",60000,1);//this is to get total time and update his time on server.
	SetTimer("ZoneTimer", 1000, true);// this is zone check taking timer
	SetTimer("settime",1000,true);//this is for global time and date texdraw updater.
How i can reduce some timer?
Is they make Lag?


Re: Need help to make code short and better.. - Sime30 - 10.02.2015

I don't think so.
Also, you could put stuff from "ZoneTimer" public to your "settime" public as they are both repeating at the same time. That way you would only have 4 global timers which is excellent.


Re: Need help to make code short and better.. - MBilal - 10.02.2015

Thanks Rep+1


Re: Need help to make code short and better.. - DanishHaq - 10.02.2015

I'd just like to explain something too. Use OnPlayerUpdate for the really vital stuff. For example for me I only have SetPlayerHealth and SetPlayerArmour to their server-side health & armour (anti hp/armour hacks). Nothing else. OnPlayerUpdate is called a few times every seconds for players. Of course if you take a look at what you've got there I'm sure you'll know now that calling all that code a few times within a second will start to cause lag, and some code will not be complete before it starts again.