Anyway of making this code optimised?
#7

Alright. After switching a bit of code around (No pun intended there :P) but this is the final setvip command I've come up with. What do you guys think?

Код:
CMD:setvip(playerid, params[])
{
	if(PlayerInfo[playerid][AdminLevel] < 4) return 0;
    new name[24], level, interval, type, string[128], ban, NZD, PlayerID, query[250];
    if (sscanf(params, "s[24]iii", name, level, interval, type))
    {
        SendClientMessage(playerid, RED, "Usage: /setvip [Player Name] [Level] [Interval] [Type]");
        SendClientMessage(playerid, RED, "Types: 0 for \"day(s)\" and 1 for \"month(s)\"");
        return 1;
    }
	if(level < 0 || level > 3) return SendClientMessage(playerid, RED, "Error: Invalid VIP Level");
	mysql_format(ServerMySQL, query, sizeof(query), "SELECT `Name`, `uID` FROM `Users` WHERE Name='%e'", name); // You need to fetch player IP first
	new Cache:d = mysql_query(ServerMySQL, query);
	if(cache_num_rows())
	{
		PlayerID = cache_get_field_content_int(0, "uID");
		if (IsPlayerVip(PlayerID)) return SendClientMessage(playerid, RED, "[Admin Notice] - Player is already this VIP Level.");
		mysql_format(ServerMySQL, query,sizeof(query), "SELECT `NZT` FROM `Inventory` WHERE uID=%d",PlayerID);
		new Cache:i = mysql_query(ServerMySQL, query);
		if(cache_num_rows())
		{
			NZD = cache_get_field_content_int(0, "NZT");
			mysql_format(ServerMySQL, query,sizeof(query), "SELECT `Bank` FROM `Ownered` WHERE uID=%d",PlayerID);
			new Cache:b = mysql_query(ServerMySQL, query);
			if(cache_num_rows())
			{
				ban = cache_get_field_content_int(0, "Bank");
				SetPVarInt(playerid, "PlayerID", PlayerID);
				switch (type)
				{
					case 0:
					{
						if (!(1 <= interval <= 365)) return SendClientMessage(playerid, -1, "Error: Interval cannot be negative, equal to 0 or more than 365 days.");
						switch(level)
						{
							case 0:
							{
								format(string,sizeof(string), "%s has given %s VIP Level 1 for %d days.", PlayerName(playerid), name, interval);
								SendMessageToAdmins(COLOR_ADMIN, string, 1);
								NZD = 1;
								ban = 10000;
							}
							case 1:
							{
								format(string,sizeof(string), "%s has given %s VIP Level 2 for %d days.", PlayerName(playerid), name, interval);
								SendMessageToAdmins(COLOR_ADMIN, string, 1);
								NZD = 2;
								ban = 100000;
							}
						}
					}
					case 1:
					{
						if (!(1 <= interval <= 12)) return SendClientMessage(playerid, -1, "Error: Interval cannot be negative, equal to 0 or more than 12 months.");
						switch(interval)
						{
							case 0:
							{
								format(string,sizeof(string), "[Admin Log]: %s has removed %s VIP.", PlayerName(playerid), name);
								SendMessageToAdmins(COLOR_ADMIN, string, 1);
								OnPlayerVipStatusExpire(PlayerInfo[PlayerID][uID]);
								return 1; 
							}
							case 1 .. 5:
							{
								switch(level)
								{
									case 0:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 1 for %d months.", PlayerName(playerid), name, interval);
										SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 5;
										ban = 300000;
									}
									case 1:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 2 for %d months.", PlayerName(playerid), name, interval);
										SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 15;
										ban = 750000;
									}
								}
							}
							case 6 .. 11:
							{
								switch(level)
								{
									case 0:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 1 for %d months.", PlayerName(playerid), name, interval);SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 10;
										ban = 600000;
									}
									case 1:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 2 for %d months.", PlayerName(playerid), name, interval);SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 20;
										ban = 1500000;
									}
								}
							}
							case 12:
							{
								switch(level)
								{
									case 0:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 1 for %d months.", PlayerName(playerid), name, interval);SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 12;
										ban = 1200000;
									}
									case 1:
									{
										format(string,sizeof(string), "%s has given %s VIP Level 2 for %d months.", PlayerName(playerid), name, interval);SendMessageToAdmins(COLOR_ADMIN, string, 1);
										NZD = 25;
										ban = 5000000;
									}
								}
							}
						}						
					}
					default: return SendClientMessage(playerid, -1, "Error: Type can only be 0 or 1 for \"day(s)\" and \"month(s)\" respectively.");
				}
				SetVipStatus(PlayerID, level, interval, type);
				mysql_format(ServerMySQL, query, sizeof(query), 
				"UPDATE Users u JOIN Inventory i ON i.uID=u.uID JOIN Ownered o ON o.uID=u.uID SET i.NZT=i.NZT+%d,o.Bank=o.Bank+%d,u.VIP=%d WHERE u.name='%e'", 
				NZD, ban, level, name); 
				mysql_tquery(ServerMySQL, query, "", "");  
				foreach(new p : Player)
				{
					GetPlayerName(p, string, 24);
					if(!strcmp(string, name, true))
					{
						switch(level)
						{
							case 0:
							{
								SendClientMessage(p, ORANGE, "Your VIP has been removed by an Administrator");
							}
							case 1:
							{
								SendClientMessage(p, COLOR_VIP1, "You have been promoted to VIP Level 1. Thank you for supporting the server.");
								SendClientMessage(p, ORANGE, "Use /vipdate in order to see when your VIP is set to expire.");
							}
							case 2:
							{
								SendClientMessage(p, COLOR_VIP2, "You have been promoted to VIP Level 2. Thank you for supporting the server.");
								SendClientMessage(p, ORANGE, "Use /vipdate in order to see when your VIP is set to expire.");
							}
						}
					}
				}
				cache_delete(b);
			}
			cache_delete(i);
		}
	}
	cache_delete(d);
	return 1;
}
I'm not really sure if my code is going to work because for setting the player VIP i'm using the uID stored in the database as the playerid. So I really don't think that'll work. But how am I supposed to set the player VIP if they aren't in the server?


Also @Konstantinos I first of all want to thank you for your vip-expiration/vip system however I believe I found an error in it.

Код:
SetVipStatus(playerid, interval, type);
When you're setting the player VIP, using the CMD: setvip, at the end of the code you have ^^
Код:
SetVipStatus(playerid, interval, type);
Playerid, meaning you're going to be setting the player whom types the command VIP.
Код:
SetVipStatus(id, interval,type);
I believe is the correct code.
Reply


Messages In This Thread
Anyway of making this code optimised? - by Tass007 - 04.07.2017, 06:22
Re: Anyway of making this code optimised? - by Kaperstone - 04.07.2017, 07:26
Re: Anyway of making this code optimised? - by Tass007 - 04.07.2017, 07:30
Re: Anyway of making this code optimised? - by Kaperstone - 04.07.2017, 08:04
Re: Anyway of making this code optimised? - by Konstantinos - 04.07.2017, 11:03
Re: Anyway of making this code optimised? - by OneDay - 04.07.2017, 11:51
Re: Anyway of making this code optimised? - by Tass007 - 08.07.2017, 11:40
Re: Anyway of making this code optimised? - by Kaperstone - 10.07.2017, 16:51
Re: Anyway of making this code optimised? - by Tass007 - 11.07.2017, 10:17
Re: Anyway of making this code optimised? - by Konstantinos - 11.07.2017, 15:17

Forum Jump:


Users browsing this thread: 1 Guest(s)