Problems in House System
#1

I am making this system for the last 10 days. I am experiencing a lot of problems.

Now that, I have successfully created houses and saved them to the database, I have also achieved success in loading them.

But deletion is a problem now.

The houses that I create are deleted. But once I restart the server, the preloaded houses from the database do not delete.

Code for creation:
PHP Code:
CMD:createhouse(playeridparams[])
{
    new 
hpricehinteriorhlevelFloat:PlayerPosition[3], string[512], query[2000];
    if(!
IsPlayerAdmin(playerid))
        return 
SendClientMessage(playerid0xE74C3CFF"You are not authorized to use this command!");
    if(
sscanf(params"iii"hpricehinteriorhlevel))
        return 
SendClientMessage(playerid0xE74C3CFF"Usage: /createhouse [price] [interior] [level]");
    if(
hprice 0)
        return 
SendClientMessage(playerid0xE74C3CFF"Price cannot be less than 0!");
    if(
hinterior || hinterior 6)
        return 
SendClientMessage(playerid0xE74C3CFF"Interior must be from 1 to 6!");
    if(
hlevel 1)
        return 
SendClientMessage(playerid0xE74C3CFF"Level cannot be less than 1");
    
// if(Player[playerid][level] < hlevel) // After level system
    //     return SendClientMessage(playerid, 0xE74C3CFF, "You must have level %d to buy this house!");
    
if(houses == MAX_HOUSE_LIMIT)
        return 
SendClientMessage(playerid0xE74C3CFF"Maxmimum house limit reached!");
    
format(House[houses][HouseOwner], 512"The State");
    
House[houses][HousePrice] = hprice;    
    
House[houses][HouseLevel] = hlevel;
    
House[houses][HouseInterior] = hinterior;
    
House[houses][isLocked] = 0;
    
GetPlayerPos(playeridPlayerPosition[0], PlayerPosition[1], PlayerPosition[2]);
    
House[houses][HouseEntranceX] = PlayerPosition[0];
    
House[houses][HouseEntranceY] = PlayerPosition[1];
    
House[houses][HouseEntranceZ] = PlayerPosition[2];
    
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}The State\n{12900B}House for sale!\nPrice: {ffffff}$%d\n{12900B}To buy this house, type /buyhouse %d"houseshlevelhpricehouses);
    
House[houses][HouseText] = CreateDynamic3DTextLabel(string0xffffffPlayerPosition[0], PlayerPosition[1], PlayerPosition[2], 20.0INVALID_PLAYER_IDINVALID_VEHICLE_ID0, -1, -1, -120.0);
    
mysql_format(g_SQLquerysizeof(query), "INSERT INTO `houses` (`HouseID`, `HouseOwner`, `HousePrice`, `HouseInterior`, `HouseEntranceX`, `HouseEntranceY`, `HouseEntranceZ`, `isLocked`, `HouseLevel`) VALUES ('%d', '%e', '%d', '%d', '%f', '%f', '%f', '%d', '%d')"housesHouse[houses][HouseOwner], House[houses][HousePrice], House[houses][HouseInterior], House[houses][HouseEntranceX], House[houses][HouseEntranceY], House[houses][HouseEntranceZ], House[houses][isLocked], House[houses][HouseLevel]);
    
mysql_tquery(g_SQLquery"""");
    
mysql_format(g_SQLquerysizeof(query), "UPDATE `houses` SET `HouseStatus` = 1 WHERE `HouseID` = %d"houses);
    
mysql_tquery(g_SQLquery"""");
    
format(stringsizeof(string), "House %d created!"houses);
    
SendClientMessage(playerid0xE74C3CFFstring);
    
houses++;
    return 
1;

Code for loading:
PHP Code:
stock LoadHouses() // load houses from database
{
    new 
query[256];
    
mysql_format(g_SQLquerysizeof(query), "SELECT * FROM `houses` WHERE `HouseStatus` = 1");
    
mysql_tquery(g_SQLquery"LoadHousesCb""");
}
public 
LoadHousesCb( ) 

    new 
rowsstring[512]; 
    
cache_get_row_count(rows);
    
printf("%d"rows);
    if(
rows)
    {
        print(
"This is executing.");
        new 
hidx;
        while(
rows
        {
            
printf("This is executing");
            
cache_get_value_int(x"HouseID"hid);
            
House[hid][HouseID] = hid;
            
cache_get_value_namex"HouseOwner"House[hid][ HouseOwner ], .max_len 48 ); 
            
cache_get_value_intx"HousePrice"House[hid][ HousePrice ] ); 
            
cache_get_value_intx"HouseInterior"House[hid][ HouseInterior ] ); 
            
cache_get_value_intx"isLocked"House[hid][ isLocked ] ); 
            
cache_get_value_intx"HouseLevel"House[hid][ HouseLevel ] ); 
            
cache_get_value_intx"HouseStatus"House[hid][ HouseStatus ] ); 
            
cache_get_value_floatx"HouseEntranceX"House[hid][ HouseEntranceX ] ); 
            
cache_get_value_floatx"HouseEntranceY"House[hid][ HouseEntranceY ] ); 
            
cache_get_value_floatx"HouseEntranceZ"House[hid][ HouseEntranceZ ] ); 
            if(!
strcmp(House[hid][HouseOwner], "The State"true) && House[hid][HousePrice] != 0)
            {
                
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}The State\n{12900B}House for sale!\nPrice: {ffffff}$%d\n{12900B}To buy this house, type /buyhouse %d"hidHouse[hid][HouseLevel], House[hid][HousePrice], hid);
                
House[hid][HouseText] = CreateDynamic3DTextLabel(string0xFFFFFFHouse[hid][HouseEntranceX], House[hid][HouseEntranceY], House[hid][HouseEntranceZ], 20.0INVALID_PLAYER_IDINVALID_VEHICLE_ID0, -1, -1, -120.0);
            }
            else
            {
                if(
House[hid][HousePrice] == 0)
                {
                    
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}Price: {ffffff}$%d\n"hidHouse[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice]);
                }
                else
                {
                    
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}House for sale!\nPrice: {ffffff}$%d\n{12900B}To buy this house, type /buyhouse %d"hidHouse[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice], houses);
                }
            }
            if(!
strcmp(House[hid][HouseOwner], "The State"true) && House[hid][HousePrice] != 0)
            {
                
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}The State\n{12900B}House for sale!\nPrice: {ffffff}$%d\n{12900B}To buy this house, type /buyhouse %d"housesHouse[hid][HouseLevel], House[hid][HousePrice], houses);
                
House[hid][HouseText] = CreateDynamic3DTextLabel(string0xFFFFFFHouse[hid][HouseEntranceX], House[hid][HouseEntranceY], House[hid][HouseEntranceZ], 20.0INVALID_PLAYER_IDINVALID_VEHICLE_ID0, -1, -1, -120.0);
            }
            else
            {
                if(
House[hid][HousePrice] == 0)
                {
                    
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}Price: {ffffff}$%d\n"housesHouse[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice]);
                }
                else
                {
                    
format(stringsizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}House for sale!\nPrice: {ffffff}$%d\n{12900B}To buy this house, type /buyhouse %d"housesHouse[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice], houses);
                }
            }
            
x++;
        }
        
printf("Loaded %d houses."x);
        
houses x;
    }
    return 
1

Code for deletion:
PHP Code:
CMD:deletehouse(playeridparams[])
{
    new 
hidstring[128];
    if(!
IsPlayerAdmin(playerid))
        return 
SendClientMessage(playerid0xE74C3CFF"You are not authorized to use this command!");
    if(
sscanf(params"i"hid))
        return 
SendClientMessage(playerid0xE74C3CFF"Usage: /deletehouse [houseid]");
    
DestroyDynamic3DTextLabel(House[hid][HouseText]);
    
House[hid][HouseStatus] = 0;
    
DeleteHouse(hid);
    
format(stringsizeof(string), "House %d deleted!"hid);
    
SendClientMessage(playerid0xE74C3CFFstring);
    return 
1;

deletehouse Function:
PHP Code:
public DeleteHouse(hid)
{
    new 
query[512];
    
mysql_format(g_SQLquerysizeof(query), "UPDATE `houses` SET `HouseStatus` = 0 WHERE `HouseID` = %d"hid);
    
mysql_tquery(g_SQLquery"""");
    
mysql_format(g_SQLquerysizeof(query), "UPDATE `accounts` SET `HouseID` = -1 WHERE `HouseID` = %d"hid);
    
mysql_tquery(g_SQLquery"""");

Reply
#2

Can you show the DeleteHouse function? Does it actually delete a row from the database?

You should also check to make sure the houseID is valid before passing it under the deletehouse command.
Reply
#3

Quote:
Originally Posted by Grim_
View Post
Can you show the DeleteHouse function? Does it actually delete a row from the database?

You should also check to make sure the houseID is valid before passing it under the deletehouse command.
Added
Reply
#4

Is it your intention to keep the row in the database after deleting the house? This may be why once you restart the house are popping back up. The information is still in the database, so is getting reloaded.

Check out MySQL DELETE FROM. Also, I would still first check if the house ID passed to /deletehouse is valid before using it to delete from the database.
Reply
#5

Quote:
Originally Posted by Grim_
View Post
Is it your intention to keep the row in the database after deleting the house? This may be why once you restart the house are popping back up. The information is still in the database, so is getting reloaded.

Check out MySQL DELETE FROM. Also, I would still first check if the house ID passed to /deletehouse is valid before using it to delete from the database.
My intention is to keep the row in the database and simply change HouseStatus to 0 if it is deleted and do not load it again.

And yes I know, about valid house id. I ignored it for now. I will change it later.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)