MySQL House System - Problems
#1

Hey,

I've been recently re-scripting my house system as there were many bugs in the previous system. I have came across one bug so far. The problem is, when you create a house (/createhouse) the House ID's get messed up and the house ID will just show as 0 on the house dialog instead of its usual number. And, when you restart the server, the house which had the problem will go back to normal. So I'm assuming there is something wrong with the /createhouse command.

Код:
CMD:createhouse(playerid,params[])
{
	if( pInfo[playerid][pAdmin] < 6 )
	    return PlayerIsNotAdmin(playerid);
	
	new
		price,
		interior,
		Float:x,
		Float:y,
		Float:z,
		id = Iter_Free(Houses);
		
 	if( sscanf(params, "ii", price, interior ) )
	    return SendUsageError( playerid, "/createhouse [Price] [Interior]" );

	if( interior >= 10 || interior <= 0 )
		return SendClientMessage( playerid, RED, "ERROR: {FFFFFF}Interior ID's range from 1 - 9");
		
 	if( Iter_Contains(Houses, MAX_HOUSES-1) )
        return Error(playerid, "Max House Limit has been exceeded (500)");

 	GetPlayerPos(playerid, x, y, z);
	//=============================[Lets set it up]=============================

	format(hInfo[id][Owner], MAX_PLAYER_NAME, "None");

	hInfo[id][Locked] = 0;
	hInfo[id][World] = id;
	hInfo[id][InteriorID] = interior;
	hInfo[id][Price] = price;
	hInfo[id][OX] = x;
	hInfo[id][OY] = y;
	hInfo[id][OZ] = z;

	hInfo[id][Label] = CreateDynamic3DTextLabel("This House is on Sale\n{FFFFFF}Press [F] to buy", 0xFF9900FF, hInfo[id][OX], hInfo[id][OY], hInfo[id][OZ]+0.5, 36.0, .testlos = 1);
	hInfo[id][Icon] = CreateDynamicMapIcon(hInfo[id][OX], hInfo[id][OY], hInfo[id][OZ], 31, -1);
	hInfo[id][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[id][OX], hInfo[id][OY], hInfo[id][OZ], -1, -1, -1, 36.0);


	new query[500];

	mysql_format(mysql, query, sizeof(query), "INSERT INTO `"HouseTable"` (`ID`, `Owner`, `Owned`, `Locked`, `InteriorID`, `World`, `Price`, `OX`, `OY`, `OZ`) VALUES('%i', '%s', '%i', '%i', '%i', '%i', '%i', '%f', '%f', '%f')", hInfo[id][HID], hInfo[id][Owner], hInfo[id][Owned], hInfo[id][Locked], hInfo[id][InteriorID], hInfo[id][World], hInfo[id][Price], hInfo[id][OX], hInfo[id][OY], hInfo[id][OZ]);
	mysql_tquery(mysql, query, "", "");

	SendClientMessage(playerid, INFO, "[HOUSE] You have successfully created a house");

	Iter_Add(Houses, id);
	return 1;
}
Here is the loadhouse function

Код:
forward OnLoadHouses();
public OnLoadHouses()
{
    new i, loaded;
	new rows = cache_num_rows();
 	if(rows)
    {
        while(loaded < rows)
		{
			i = cache_get_field_content_int(loaded, "ID");
			cache_get_field_content(loaded, "Owner", hInfo[i][Owner], mysql, 128);


			hInfo[i][Owned] = cache_get_field_content_int(loaded, "Owned");
			hInfo[i][Locked] = cache_get_field_content_int(loaded, "Locked");
			hInfo[i][Price] = cache_get_field_content_int(loaded, "Price");

			hInfo[i][OX] = cache_get_field_content_float(loaded, "OX");
			hInfo[i][OY] = cache_get_field_content_float(loaded, "OY");
			hInfo[i][OZ] = cache_get_field_content_float(loaded, "OZ");


			hInfo[i][World] = cache_get_field_content_int(loaded, "World");
			hInfo[i][OnSale] = cache_get_field_content_int(loaded, "OnSale");


			hInfo[i][InteriorID] = cache_get_field_content_int(loaded, "InteriorID");

	 	    new lString[500];
	 		if(hInfo[i][OnSale] == 0 )
			{
				switch(hInfo[i][Owned])
				{
					case 0:
	    			{
	    				hInfo[i][Label] = CreateDynamic3DTextLabel("This House is on Sale\n{FFFFFF}Press [F] to Buy", 0xFF9900FF, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ]+0.5, 36.0, .testlos = 1);
	   					hInfo[i][Icon] = CreateDynamicMapIcon(hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ], 31, -1);
						hInfo[i][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ], -1, -1, -1, 36.0);
	                    loaded++;
	                    Iter_Add(Houses, loaded);
					}
					case 1:
					{
					    switch(hInfo[i][Locked])
					    {
					        case 0:
					        {
					            format(lString, sizeof(lString), "%s's House\n{FFFFFF}Press [F] to Enter", hInfo[i][Owner]);
			 					hInfo[i][Label] = CreateDynamic3DTextLabel(lString, 0xFF9900FF, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ]+0.5, 36.0, .testlos = 1);
			 					hInfo[i][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[i][OX],hInfo[i][OY], hInfo[i][OZ], -1, -1, -1, 36.0);
					        }
					        case 1:
					        {
					            format(lString, sizeof(lString), "%s's House\n{E00B0B}Locked\n{FFFFFF}Press [F] to Enter", hInfo[i][Owner]);
			 					hInfo[i][Label] = CreateDynamic3DTextLabel(lString, 0xFF9900FF, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ]+0.5, 36.0, .testlos = 1);
			 					hInfo[i][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[i][OX],hInfo[i][OY], hInfo[i][OZ], -1, -1, -1, 36.0);
					        }
						}
						loaded++;
	                    Iter_Add(Houses, loaded);
					}
				}
			}
			if(hInfo[i][OnSale] == 1)
			{
				format(lString, sizeof(lString),"House Owned: Yes\nHouse Owner: %s\nHouse Price: $%i\nHouse On Sale: Yes", hInfo[i][Owner], hInfo[i][Price]);
				hInfo[i][Label] = CreateDynamic3DTextLabel(lString, 0xD65418FF, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ]+0.5, 36.0, .testlos = 1);
				hInfo[i][Icon] = CreateDynamicMapIcon(hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ], 32, -1);
				hInfo[i][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[i][OX],hInfo[i][OY], hInfo[i][OZ], -1, -1, -1, 36.0);
	  			loaded++;
      			Iter_Add(Houses, loaded);
			}
		}
	}

	if(loaded > 1) printf("[House System] %i houses were created...", loaded);
	return 1;
}
Please help me. Thanks
Reply
#2

hInfo[id][HID], what is the value of this? I don't see you to assign it's value anywhere, something like hInfo[id][HID] = id; probably or you should read it from the database and then assign it like you do in OnLoadHouses because you insert ID into the table from the value of hInfo[id][HID].
Reply
#3

Try this:
Код:
forward OnLoadHouses();
public OnLoadHouses()
{
    new index;
	new rows = cache_num_rows();
 	if(rows)
    {
        while(index < rows)
		{
			hInfo[index][ID] = cache_get_field_content_int(index, "ID");
			cache_get_field_content(index, "Owner", hInfo[index][Owner], mysql, 128);


			hInfo[index][Owned] = cache_get_field_content_int(index, "Owned");
			hInfo[index][Locked] = cache_get_field_content_int(index, "Locked");
			hInfo[index][Price] = cache_get_field_content_int(index, "Price");

			hInfo[index][OX] = cache_get_field_content_float(index, "OX");
			hInfo[index][OY] = cache_get_field_content_float(index, "OY");
			hInfo[index][OZ] = cache_get_field_content_float(index, "OZ");


			hInfo[index][World] = cache_get_field_content_int(index, "World");
			hInfo[index][OnSale] = cache_get_field_content_int(index, "OnSale");


			hInfo[index][InteriorID] = cache_get_field_content_int(index, "InteriorID");

	 	    new lString[500];
	 		if(hInfo[index][OnSale] == 0 )
			{
				switch(hInfo[index][Owned])
				{
					case 0:
	    			{
	    				hInfo[index][Label] = CreateDynamic3DTextLabel("This House is on Sale\n{FFFFFF}Press [F] to Buy", 0xFF9900FF, hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ]+0.5, 36.0, .testlos = 1);
	   					hInfo[index][Icon] = CreateDynamicMapIcon(hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ], 31, -1);
						hInfo[index][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ], -1, -1, -1, 36.0);
	                    loaded++;
	                    Iter_Add(Houses, loaded);
					}
					case 1:
					{
					    switch(hInfo[index][Locked])
					    {
					        case 0:
					        {
					            format(lString, sizeof(lString), "%s's House\n{FFFFFF}Press [F] to Enter", hInfo[index][Owner]);
			 					hInfo[index][Label] = CreateDynamic3DTextLabel(lString, 0xFF9900FF, hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ]+0.5, 36.0, .testlos = 1);
			 					hInfo[index][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[index][OX],hInfo[index][OY], hInfo[index][OZ], -1, -1, -1, 36.0);
					        }
					        case 1:
					        {
					            format(lString, sizeof(lString), "%s's House\n{E00B0B}Locked\n{FFFFFF}Press [F] to Enter", hInfo[i][Owner]);
			 					hInfo[index][Label] = CreateDynamic3DTextLabel(lString, 0xFF9900FF, hInfo[i][OX], hInfo[i][OY], hInfo[i][OZ]+0.5, 36.0, .testlos = 1);
			 					hInfo[index][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[index][OX],hInfo[index][OY], hInfo[index][OZ], -1, -1, -1, 36.0);
					        }
						}
						loaded++;
	                    Iter_Add(Houses, loaded);
					}
				}
			}
			if(hInfo[index][OnSale] == 1)
			{
				format(lString, sizeof(lString),"House Owned: Yes\nHouse Owner: %s\nHouse Price: $%i\nHouse On Sale: Yes", hInfo[index][Owner], hInfo[index][Price]);
				hInfo[index][Label] = CreateDynamic3DTextLabel(lString, 0xD65418FF, hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ]+0.5, 36.0, .testlos = 1);
				hInfo[index][Icon] = CreateDynamicMapIcon(hInfo[index][OX], hInfo[index][OY], hInfo[index][OZ], 32, -1);
				hInfo[index][EnterPickup] = CreateDynamicPickup(1273, 23, hInfo[index][OX],hInfo[index][OY], hInfo[index][OZ], -1, -1, -1, 36.0);
	  			loaded++;
      			Iter_Add(Houses, loaded);
			}
			
			index++;
		}
	}

	if(index > 1) printf("[House System] %i houses were created...", index);
	return 1;
}
About the /createhouse command, I've had a similar problem. What I did was I inserted a new record in the shops table and then called another method, OnCreateShop. This allowed me to use cache_insert_id() to retrieve the ID of the auto generated field ID.

Example:
Код:
	mysql_format(mysql, query, sizeof(query), "INSERT INTO `shops` (`ID`, `Header`, `PosX`, `PosY`, `PosZ`) VALUES (NULL, '%e', %f, %f, %f)", header, pos[0], pos[1], pos[2]);
	mysql_tquery(mysql, query, "OnCreateShop", "sfff", header, pos[0], pos[1], pos[2]);
	
	return 1;
}

forward OnCreateShop(header[], Float:posX, Float:posY, Float:posZ);
public OnCreateShop(header[], Float:posX, Float:posY, Float:posZ)
{
	new insertID, string[128];
	insertID = cache_insert_id();
	ShopInfo[insertID][ID] = insertID;
	
	format(ShopInfo[insertID][Header], 128, header);
	
	format(string, sizeof(string), COL_GOLD"ID{FFFFFF}: %i - {FFD700}%s{FFFFFF}", ShopInfo[insertID][ID], ShopInfo[insertID][Header]);
    ShopInfo[insertID][Pos][0] = posX;
    ShopInfo[insertID][Pos][1] = posY;
    ShopInfo[insertID][Pos][2] = posZ;
    ShopInfo[insertID][Text] = CreateDynamic3DTextLabel(string, 0xFFD700FF, ShopInfo[insertID][Pos][0], ShopInfo[insertID][Pos][1], ShopInfo[insertID][Pos][2], 5.0);
    ShopInfo[insertID][Pickup] = CreateDynamicPickup(1254, 1, ShopInfo[insertID][Pos][0], ShopInfo[insertID][Pos][1], ShopInfo[insertID][Pos][2]);
	
	return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)