SA-MP Forums Archive
is there a better way of doing this ? - 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: is there a better way of doing this ? (/showthread.php?tid=627102)



is there a better way of doing this ? - TheAngryBird - 23.01.2017

hi all.
i havent been coding PAWN for a long time but i started again as i have some free time.

is there a better way of doing this ? basically what i'm doing is a sell / accept command.
the command verify if the item the player want to sell is valid ( so they dont sell a zippo if i dont want a zippo in the game ). i'll add other valid item at a later time as needed.
i'm using Player Inventory fromJoe Staff
https://sampforum.blast.hk/showthread.php?tid=130436

everything seems to work , but i dont like how it look and how many lines of code it need.

Код:
YCMD:sell(playerid, params[], help)
{
    if (help)
    {
    }
    else
    {
        new targetid, itemname[20], itemprice, itemamount, HasManyItem;
        if(PlayerInfo[playerid][pJob] == 2)
        {
        	if(sscanf(params, "s[20]iiu", itemname, itemamount, itemprice, targetid)) return SendClientMessage(playerid, 0xFFFFFFF,"Syntax error.Correct usage: /sell [itemname] [amount] [price [playerid]]");
			if(!strcmp(itemname, "IRON", true) || !strcmp(itemname, "ALUMINIUM", true) || !strcmp(itemname, "COPPER", true))
			{
				for(new i, len = strlen(itemname); i < len; ++i)
				{
    				itemname[i] = toupper(itemname[i]);
				}
				if(PlayerHasItem(playerid, itemname))
			    {
			        HasManyItem = PlayerHasItem(playerid, itemname);
			        if(HasManyItem <itemamount)
			        {
						SetPVarString(playerid, "itemname", itemname);
						SetPVarInt(playerid, "amount", itemamount);
						SetPVarInt(playerid, "price", itemprice);
						Offering[playerid] = true;
        				Pending[targetid] = true;
        				SendClientMessageFormatted(targetid, COLOR_ORANGE, "%s want to sell you %i %s for $%i. type /accept to accept.", GetName(targetid), itemamount, itemname, itemprice);
        				SendClientMessageFormatted(playerid, COLOR_ORANGE, "You offered %s to %s", itemname, GetName(playerid));
        			}
        		}
        	}
			else
			{
	    		SendClientMessageFormatted(playerid, -1, "Invalid item [%s]", itemname);
			}
        }
        
        
        
        
        
        
	}
    return 1;
}
YCMD:accept(playerid, params[], help)
{
    if (help)
    {
    }
    else
    {
        new targetid, itemname[20], itemprice, itemamount;
		if(PlayerInfo[playerid][pJob] == 3)
        {
        	if(sscanf(params, "s[20]iu", itemname, itemamount, targetid)) return SendClientMessage(playerid, 0xFFFFFFF,"Syntax error.Correct usage: /sell [itemname] [amount] [price [playerid]]");
			for(new i, len = strlen(itemname); i < len; ++i)
			{
    			itemname[i] = toupper(itemname[i]);
			}
        	if(Pending[playerid] == true)
        	{
        		if(Offering[targetid] == true)
        		{
					if(GetPlayerMoney(playerid) <itemprice)
					{
        				if(sscanf(params, "%s[20]u", itemname, targetid)) SendClientMessage(playerid, 0xFFFFFFF,"Syntax error.Correct usage: /accept [itemname] [playerid]]");
						GetPVarString(targetid, "itemname", itemname, sizeof(itemname));
						itemamount = GetPVarInt(targetid, "amount");
						itemprice = GetPVarInt(targetid, "price");
        				Offering[targetid] = false;
        				Pending[playerid] = false;
        				SendClientMessageFormatted(playerid, -1, "You accepted", itemname, itemamount, itemprice, GetName(targetid));
        				RemoveItem(targetid, itemname, itemamount);
        				AddItem(playerid, itemname, itemamount);
        				GivePlayerMoney(targetid, itemprice);
        				GivePlayerMoney(playerid, -itemprice);
        				DeletePVar(targetid, "itemname");
        				DeletePVar(targetid, "amount");
        				DeletePVar(targetid, "price");
        			}
        		}
        	}
        }
        
        
        
        
        
        
        
        
        
	}
    return 1;
}



Re: is there a better way of doing this ? - GangstaSunny. - 23.01.2017

the best way is your way.
the only tip i can give is to script "one-liners". which basicly means that you should reduce line likes this;

PHP код:
for(new ilen strlen(itemname); len; ++i)
{
    
itemname[i] = toupper(itemname[i]);
}
// reduceing it to a "one-liner"
for(new ilen strlen(itemname); len; ++i){itemname[i] = toupper(itemname[i]);} 
i can not recommend to do this while working on a command or to do this to lines which need to be changed more often.


Re: is there a better way of doing this ? - TheAngryBird - 23.01.2017

ok thanks. it has no effect on performance right ?
cause i find it way easier to read and work with " multi-liner "


Re: is there a better way of doing this ? - GangstaSunny. - 25.01.2017

Quote:
Originally Posted by TheAngryBird
Посмотреть сообщение
ok thanks. it has no effect on performance right ?
cause i find it way easier to read and work with " multi-liner "
it has effect on your own performance.
if you keep doing this you will be much more professional just because of the look of your good. no joke. trust me.