03.03.2018, 18:09
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:
Code for loading:
Code for deletion:
deletehouse Function:
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(playerid, params[])
{
new hprice, hinterior, hlevel, Float:PlayerPosition[3], string[512], query[2000];
if(!IsPlayerAdmin(playerid))
return SendClientMessage(playerid, 0xE74C3CFF, "You are not authorized to use this command!");
if(sscanf(params, "iii", hprice, hinterior, hlevel))
return SendClientMessage(playerid, 0xE74C3CFF, "Usage: /createhouse [price] [interior] [level]");
if(hprice < 0)
return SendClientMessage(playerid, 0xE74C3CFF, "Price cannot be less than 0!");
if(hinterior < 1 || hinterior > 6)
return SendClientMessage(playerid, 0xE74C3CFF, "Interior must be from 1 to 6!");
if(hlevel < 1)
return SendClientMessage(playerid, 0xE74C3CFF, "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(playerid, 0xE74C3CFF, "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(playerid, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2]);
House[houses][HouseEntranceX] = PlayerPosition[0];
House[houses][HouseEntranceY] = PlayerPosition[1];
House[houses][HouseEntranceZ] = PlayerPosition[2];
format(string, sizeof(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", houses, hlevel, hprice, houses);
House[houses][HouseText] = CreateDynamic3DTextLabel(string, 0xffffff, PlayerPosition[0], PlayerPosition[1], PlayerPosition[2], 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 20.0);
mysql_format(g_SQL, query, sizeof(query), "INSERT INTO `houses` (`HouseID`, `HouseOwner`, `HousePrice`, `HouseInterior`, `HouseEntranceX`, `HouseEntranceY`, `HouseEntranceZ`, `isLocked`, `HouseLevel`) VALUES ('%d', '%e', '%d', '%d', '%f', '%f', '%f', '%d', '%d')", houses, House[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_SQL, query, "", "");
mysql_format(g_SQL, query, sizeof(query), "UPDATE `houses` SET `HouseStatus` = 1 WHERE `HouseID` = %d", houses);
mysql_tquery(g_SQL, query, "", "");
format(string, sizeof(string), "House %d created!", houses);
SendClientMessage(playerid, 0xE74C3CFF, string);
houses++;
return 1;
}
PHP Code:
stock LoadHouses() // load houses from database
{
new query[256];
mysql_format(g_SQL, query, sizeof(query), "SELECT * FROM `houses` WHERE `HouseStatus` = 1");
mysql_tquery(g_SQL, query, "LoadHousesCb", "");
}
public LoadHousesCb( )
{
new rows, string[512];
cache_get_row_count(rows);
printf("%d", rows);
if(rows)
{
print("This is executing.");
new hid, x;
while(x < rows)
{
printf("This is executing");
cache_get_value_int(x, "HouseID", hid);
House[hid][HouseID] = hid;
cache_get_value_name( x, "HouseOwner", House[hid][ HouseOwner ], .max_len = 48 );
cache_get_value_int( x, "HousePrice", House[hid][ HousePrice ] );
cache_get_value_int( x, "HouseInterior", House[hid][ HouseInterior ] );
cache_get_value_int( x, "isLocked", House[hid][ isLocked ] );
cache_get_value_int( x, "HouseLevel", House[hid][ HouseLevel ] );
cache_get_value_int( x, "HouseStatus", House[hid][ HouseStatus ] );
cache_get_value_float( x, "HouseEntranceX", House[hid][ HouseEntranceX ] );
cache_get_value_float( x, "HouseEntranceY", House[hid][ HouseEntranceY ] );
cache_get_value_float( x, "HouseEntranceZ", House[hid][ HouseEntranceZ ] );
if(!strcmp(House[hid][HouseOwner], "The State", true) && House[hid][HousePrice] != 0)
{
format(string, sizeof(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", hid, House[hid][HouseLevel], House[hid][HousePrice], hid);
House[hid][HouseText] = CreateDynamic3DTextLabel(string, 0xFFFFFF, House[hid][HouseEntranceX], House[hid][HouseEntranceY], House[hid][HouseEntranceZ], 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 20.0);
}
else
{
if(House[hid][HousePrice] == 0)
{
format(string, sizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}Price: {ffffff}$%d\n", hid, House[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice]);
}
else
{
format(string, sizeof(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", hid, House[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice], houses);
}
}
if(!strcmp(House[hid][HouseOwner], "The State", true) && House[hid][HousePrice] != 0)
{
format(string, sizeof(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", houses, House[hid][HouseLevel], House[hid][HousePrice], houses);
House[hid][HouseText] = CreateDynamic3DTextLabel(string, 0xFFFFFF, House[hid][HouseEntranceX], House[hid][HouseEntranceY], House[hid][HouseEntranceZ], 20.0, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 0, -1, -1, -1, 20.0);
}
else
{
if(House[hid][HousePrice] == 0)
{
format(string, sizeof(string), "{12900B}House %d\nLevel: {ffffff}%d\n{12900B}Owner: {ffffff}%e\n{12900B}Price: {ffffff}$%d\n", houses, House[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice]);
}
else
{
format(string, sizeof(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", houses, House[hid][HouseLevel], House[hid][HouseOwner], House[hid][HousePrice], houses);
}
}
x++;
}
printf("Loaded %d houses.", x);
houses = x;
}
return 1;
}
PHP Code:
CMD:deletehouse(playerid, params[])
{
new hid, string[128];
if(!IsPlayerAdmin(playerid))
return SendClientMessage(playerid, 0xE74C3CFF, "You are not authorized to use this command!");
if(sscanf(params, "i", hid))
return SendClientMessage(playerid, 0xE74C3CFF, "Usage: /deletehouse [houseid]");
DestroyDynamic3DTextLabel(House[hid][HouseText]);
House[hid][HouseStatus] = 0;
DeleteHouse(hid);
format(string, sizeof(string), "House %d deleted!", hid);
SendClientMessage(playerid, 0xE74C3CFF, string);
return 1;
}
PHP Code:
public DeleteHouse(hid)
{
new query[512];
mysql_format(g_SQL, query, sizeof(query), "UPDATE `houses` SET `HouseStatus` = 0 WHERE `HouseID` = %d", hid);
mysql_tquery(g_SQL, query, "", "");
mysql_format(g_SQL, query, sizeof(query), "UPDATE `accounts` SET `HouseID` = -1 WHERE `HouseID` = %d", hid);
mysql_tquery(g_SQL, query, "", "");
}