Anyway of making this code optimised?
#1

Hey guys, I have two questions.

First is whenever I try adding the include y_ini it crashes my whole gamemode, and I'm really not sure why. These are my includes.

Код:
#include <a_samp>
#include <core>
#include <fixes>
#include <zcmd>
#include <sscanf2>
#include <streamer>
#include <a_mysql>
#include <foreach>
//#include <YSI\y_ini>
#include <YSI\y_iterate>
#include <YSI\y_timers>
Now, the next question is a bit of a different story.
I just finished changing this code so that it can be done whilst the player affected is offline, but it's super long and repetitive. I was wondering if there's any either simpler way of doing things or a way to optimize this code. If so, if it isn't too much to ask could you please explain why you chose to do it the method you do it, and why it works (if they aren't the same question). Thanks so much.

Код:
CMD:setvip(playerid, params[])
{
	new string[250], length, name[24], level, query[250], join[80], expire[80], PlayerID, ban, NZD;
	if(PlayerInfo[playerid][AdminLevel] < 4) return 0;
	if(sscanf(params, "s[24]dd", name, level, length)) return SendClientMessage(playerid, WHITE, "Usage: /setvip [Player Name] [Level] [Length 0, 1, 6, 12]");
	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='%s'", 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");
		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");
				mysql_format(ServerMySQL, query, sizeof(query), "SELECT `VIP` FROM `Users` WHERE Name='%s'", name);
				new Cache:c = mysql_query(ServerMySQL, query);
				if (!cache_affected_rows(ServerMySQL))
				{
					cache_delete©;
					return SendClientMessage(playerid, RED, "[Admin Notice] - Player is already VIP."); 
				}
				if(length == 0)
				{
					format(string,sizeof(string), "[Admin Log]: %s has removed %s VIP.", PlayerName(playerid), name);
					SendMessageToAdmins(COLOR_ADMIN, string, 1);
					strmid(join, "-", 0, strlen("-"), 255);
					strmid(expire, "-", 0, strlen("-"), 255);
					mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 0, join, expire, name);
					mysql_tquery(ServerMySQL, query, "", "");
				}
				else if(level == 1)
				{
					if(length == 1)
					{
			//			SendClientMessage(ID, COLOR_VIP1, "You have been promoted to VIP Level 1. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 1 for 1 month.", PlayerName(playerid), name);
						SendMessageToAdmins(COLOR_ADMIN, string, 1);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month +1, Year);
						strmid(expire, string, 0, strlen(string), 255);	
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day,1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join, expire);
						//SendClientMessage(ID, COLOR_VIP1, str);
						SendClientMessage(playerid, ORANGE, string);
						NZD += 5;
						ban += 300000;
						//Locker[ID][NZT]+=5;
						//Ownered[ID][Bank]=Ownered[ID][Bank]+300000;
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 1, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
					if(length == 6)
					{
						//if(PlayerInfo[ID][VIP] > 1)return SendClientMessage(playerid, RED, "Error: Player is already VIP.");
						//PlayerInfo[ID][VIP] = 1;
						//SendClientMessage(ID, COLOR_VIP1, "You have been promoted to VIP Level 1. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 1 for 6 months.", PlayerName(playerid), name);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month +6, Year);
						strmid(expire, string, 0, strlen(string), 255);
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day, 1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join,expire);
					//	SendClientMessage(ID, COLOR_VIP1, string);
						SendClientMessage(playerid, ORANGE, string);
						//Locker[ID][NZT]+=10;
						//Ownered[ID][Bank]=Ownered[ID][Bank]+600000;
						NZD += 10;
						ban += 600000;
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 1, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
					if(length == 12)
					{
						//if(PlayerInfo[ID][VIP] > 1)return SendClientMessage(playerid, RED, "Error: Player is already VIP.");
						//PlayerInfo[ID][VIP] = 1;
						//SendClientMessage(ID, COLOR_VIP1, "You have been promoted to VIP Level 1. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 1 for 12 months.", PlayerName(playerid), name);
						SendMessageToAdmins(COLOR_ADMIN, string, 1);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month, Year +1);
						strmid(expire, string, 0, strlen(string), 255);
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day, 1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join,expire);
					//	SendClientMessage(ID, COLOR_VIP1, string);
						SendClientMessage(playerid, ORANGE, string);
						NZD += 12;
						ban += 1200000;
						//Locker[ID][NZT]+=12;
						//Ownered[ID][Bank]=Ownered[ID][Bank];
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 1, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
				}
				else if(level == 2)
				{
					if(length == 1)
					{
						//if(PlayerInfo[ID][VIP] > 1)return SendClientMessage(playerid, RED, "Error: Player is already VIP.");
						//PlayerInfo[ID][VIP] = 2;
						//SendClientMessage(ID, COLOR_VIP2, "You have been promoted to VIP Level 2. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 2 for 1 month.", PlayerName(playerid), name);
						SendMessageToAdmins(COLOR_ADMIN, string, 1);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month +1, Year);
						strmid(expire, string, 0, strlen(string), 255);	
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day,1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join,expire);
						//SendClientMessage(ID, COLOR_VIP2, string);
						SendClientMessage(playerid, ORANGE, string);
						//Locker[ID][NZT]+=15;
						//Ownered[ID][Bank]=Ownered[ID][Bank]+750000;
						NZD += 15;
						ban += 750000;
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 2, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
					if(length == 6)
					{
						//if(PlayerInfo[ID][VIP] > 1)return SendClientMessage(playerid, RED, "Error: Player is already VIP.");
						//PlayerInfo[ID][VIP] = 2;
						//SendClientMessage(ID, COLOR_VIP2, "You have been promoted to VIP Level 2. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 2 for 6 months.", PlayerName(playerid), name);
						SendMessageToAdmins(COLOR_ADMIN, string, 1);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month +6, Year);
						strmid(expire, string, 0, strlen(string), 255);
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day, 1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join,expire);
						//SendClientMessage(ID, COLOR_VIP2, string);
						SendClientMessage(playerid, ORANGE, string);
						NZD += 20;
						ban += 1500000;
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 2, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
					if(length == 12)
					{
						//if(PlayerInfo[ID][VIP] > 1)return SendClientMessage(playerid, RED, "Error: Player is already VIP.");
						//PlayerInfo[ID][VIP] = 1;
						//SendClientMessage(ID, COLOR_VIP1, "You have been promoted to VIP Level 1. Thank you for supporting the server.");
						format(string,sizeof(string), "%s has given %s VIP Level 1 for 12 months.", PlayerName(playerid), name);
						SendMessageToAdmins(COLOR_ADMIN, string, 1);
						new Year, Month, Day;
						getdate(Year, Month, Day);
						format(string,32,"%02d/%02d/%02d", Day, Month, Year);
						strmid(join, string, 0, strlen(string), 255);
						format(string, 32, "%02d/%02d/%d", Day, Month, Year +1);
						strmid(expire, string, 0, strlen(string), 255);
						if(Month == 12)
						{
							format(expire, 32, "%02d/%02d/%d", Day, 1, Year +1);
						}
						format(string, sizeof(string), "[D/M/Y] Join Date: %s | Expire Date: %s", join, expire);
						//SendClientMessage(ID, COLOR_VIP1, string);
						SendClientMessage(playerid, ORANGE, string);
						NZD += 25;
						ban += 5000000;
						mysql_format(ServerMySQL,query,sizeof(query), "UPDATE `Users` SET `VIP`=%d, `VIPJoinDate`='%s', `VIPExpireDate`='%s' WHERE `Name`='%s'", 2, join, expire, name);
						mysql_tquery(ServerMySQL, query, "", "");
					}
				}
				foreach(new p : Player)
				{
					GetPlayerName(p, string, 24);
					if(!strcmp(string, name, true))
					{
						if(length == 0)
						{
							SendClientMessage(p, ORANGE, "Your VIP has been removed by an Administrator");
						}
						if(length == 1 && level == 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.");
						}
						if(length == 1 && level == 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.");
						}
						if(length == 6 && level == 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.");
						}
						if(length == 6 && level == 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.");
						}
						if(length == 12 && level == 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.");
						}
						if(length == 12 && level == 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.");
						}
						break;
					}
					cache_delete©;
				}
				cache_delete(b);
			}
			cache_delete(i);
		}
	}
	cache_delete(d);
	return 1;
}
Reply
#2

It probably crashes because you have a bad code, not because of other includes you have on.


you're using `mysql_query` which is unthreaded and slows down the code (basically, the code is held until the query completes), use pquery or tquery instead. (Difference between them is written here)
Reply
#3

So I change all my code to tquery, is there anything else I need to do to optimize it? How do I find the part in my code that is crashing the include?

With changing the code to tquery, I get the
Код:
warning 213: tag mismatch
I try adding the two "", on the end of the statement but I still get the same issue.

Код:
new Cache:c = mysql_tquery(ServerMySQL, query);
Код:
new Cache:c = mysql_tquery(ServerMySQL, query, "", "");
Both same errors.
Reply
#4

Quote:
Originally Posted by Tass007
Посмотреть сообщение
So I change all my code to tquery, is there anything else I need to do to optimize it? How do I find the part in my code that is crashing the include?
As I see, strmid only inserts into `join` and `expire` arrays the string "-" which is basically doing `join = "-";` and `expire = "-";` which is far faster.
strmid goes through a set of functions in order to achieve the same things as making an array equal to a string.

To squeeze the max. performance you can replace the `format` functions with `strcat` and create the arrays only when they're needed and not just create all the arrays at the start of the code, because if a player types the command in wrong way, the process is ran and stops at sscanf, and so you don't need most of those arrays you created before it.

`format` was proven to be slower than `strcat` by benchmarks. (this is not relevant to mysql_format, you still have to use it if you are dealing with unreliable strings and need to escape UNLESS you escape the unsafe string before it with mysql_escape_string, then you can use strcat, I noticed that you used in a query format the sign %s for a user nickname, which is a security vulnerability and so you should use %e for unsafe strings (strings that come from unreliable source), otherwise you're vulnerable to what so called "SQL Injections")
I assume the speed of that `format` function is slower than `strcat` because `format` is looping through the entire string in search of `%` signs and replacing them with arrays, while strcat only appends a string to its destination array.

The creation of arrays is really a minority, but again, if you want to save these microseconds you can also do this.

another thing you can do is replace sizeof() functions with actual numbers, because sizeof() is a function afterall.


pawn Код:
strmid(join, string, 0, strlen(string), 255);
This is an example of a bad code.
Instead, you could even just omit the use of `string` and use directly `join` and `expire` arrays.


Quote:
Quote:
Originally Posted by Tass007
Посмотреть сообщение
With changing the code to tquery, I get the
Код:
warning 213: tag mismatch
I try adding the two "", on the end of the statement but I still get the same issue.

Код:
new Cache:c = mysql_tquery(ServerMySQL, query);
Код:
new Cache:c = mysql_tquery(ServerMySQL, query, "", "");
Both same errors.
Because you don't use them that way, tquery/pquery are to be used with callbacks, take a look at their right syntax on the link to the MySQL R+40 wiki page.
When you use them, they launch a query and the code continues to do whatever it does - without the information from the query.
Meanwhile the thread is standing on a line to be processed, afterwards when MySQL has finished processing the queries which he received before, he processes the query which you sent, when he finishes doing all that, MySQL sends the information back and a callback is being called.
The callback at the end holds all the information which he received from MySQL plus the extra data you typed into the mysql_tquery at the end
Reply
#5

I wrote a long text with all the mistakes and how to use better ways but somehow had me to re-log in and couldn't post. Refreshing the page did not bring the text back so I won't spend another 30 minutes to write it.

- Don't use strmid to copy strings, it's only for extracting part of text.
- Set "Name" as UNIQUE INDEX to speed up the searching.
- The expire time won't always be correct.
pawn Код:
if ((Month += length) > 12) Month -= 12, Year++;
but day will still be displayed wrong if the expire month has less days than the current month.
- Use in-built mysql functions (example: https://github.com/Konstantinos-Sk/vip-expiration)

PHP код:
switch (length)
{
    case 
0:
    {
        
// demote..
        
return 1;
    }
    case 
1:
    {
        switch (
level)
        {
            case 
0:
            {
                
NZD 5;
                
ban 300000;
            }
            case 
1:
            {
                
NZD 15;
                
ban 750000;
            }
        }
    }
    case 
6:
    {
        switch (
level)
        {
            case 
0:
            {
                
NZD 10;
                
ban 600000;
            }
            case 
1:
            {
                
NZD 20;
                
ban 1500000;
            }
        }
    }
    case 
12:
    {
        switch (
level)
        {
            case 
0:
            {
                
NZD 12;
                
ban 1200000;
            }
            case 
1:
            {
                
NZD 25;
                
ban 5000000;
            }
        }
    }
}
mysql_format(ServerMySQLquerysizeof(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,u.VIPJoinDate='%s',u.VIPExpireDate='%s' WHERE u.name='%e'",
    
NZDbanleveljoinexpirename);
mysql_tquery(ServerMySQLquery""""); 
Reply
#6

Quote:
Originally Posted by Kaperstone
Посмотреть сообщение
another thing you can do is replace sizeof() functions with actual numbers, because sizeof() is a function afterall.
that is wrong, sizeof is pawn compiled to a constant. better than numbers because it is always right.
Reply
#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
#8

Quote:
Originally Posted by Tass007
Посмотреть сообщение
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?
Awful.
You're still using `mysql_query`.

You need to understand why using tquery/pquery is important and what mysql_query does to your code.
Reply
#9

Can you please use an example of my code. As I don't understand it.
Reply
#10

The whole purpose of joining tables is to update values from 3 different tables in 1 query. In other words, this means that you need to get rid of those un-threaded queries about retrieving "NZT" and "Bank" because the query takes care of them and updates accordingly.

I'd rather have another if statement at the very top of the command to check if player is currently online (you've got the name of the player so sscanf with "r" or "u" specifier) and use their user ID directly to execute 3 simple UPDATE queries.

And yeah, thanks for pointing out my silly mistake - that's what happens when you test on yourself in-game!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)