Loading shops from database -
MartinSwag - 26.10.2015
Hello,
I've made a function which loads shops from the database and creates a pickup and textlabel in-game. When I delete a shop, it doesn't always delete the shop. I think it's because I use loadshops() in the createshoppoint command, so duplicatie pickups and textlabels are being created each time I add a new shop. How should I fix this? I could create the textlabel etc. as soon as you use /createshoppoint, but I don't know how I should put it in the array, because I don't know the ID. I insert NULL into the ID column, because it's auto-incremented.
Код:
CMD:createshoppoint(playerid, params[])
{
if (PlayerInfo[playerid][Admin] < 4) return Invalid_Admin(playerid);
new string[128], Float:pos[3], query[128], header[128];
if (sscanf(params, "s[128]", header)) return SendClientMessage(playerid, -1, SYNTAX"/createshoppoint <text>");
if (strlen(header) < 3) return SendClientMessage(playerid, -1, "ERROR: Name is too short.");
GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
format(string, sizeof(string), "Attempting to create a new shop point...");
SendClientMessage(playerid, -1, string);
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);
LoadShops();
format(string, sizeof(string), "%s has created a new shop.", PlayerInfo[playerid][Name]);
SQL_Log("Shop", PlayerInfo[playerid][Name], "N/A", string);
return 1;
}
--------------------------------------------------------------
CMD:deleteshoppoint(playerid, params[])
{
if (PlayerInfo[playerid][Admin] < 4) return Invalid_Admin(playerid);
new string[128], query[128], id;
if (sscanf(params, "i", id)) return SendClientMessage(playerid, -1, SYNTAX"/deleteshoppoint <id>");
if (id == 1 || id == 2 || id == 3 || id == 4) return SendClientMessage(playerid, -1, "ERROR: You cannot delete this shop.");
if (id < 0 || id > MAX_SHOPS) return SendClientMessage(playerid, -1, "ERROR: That ID doesn't exist.");
for (new i = 1; i < MAX_SHOPS; i++) //Start at 1.
{
if (ShopInfo[i][ID] == id)
{
printf("Param ID: %i", id);
printf("Shop ID: %i", ShopInfo[i][ID]);
if (IsValidDynamicPickup(ShopInfo[i][Pickup])) DestroyDynamicPickup(ShopInfo[i][Pickup]);
if (IsValidDynamic3DTextLabel(ShopInfo[i][Text])) DestroyDynamic3DTextLabel(ShopInfo[i][Text]);
mysql_format(mysql, query, sizeof(query), "DELETE FROM `shops` WHERE `ID` = %i", id);
mysql_tquery(mysql, query);
format(string, sizeof(string), "You have successfully deleted shop point %i.", id);
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "%s has deleted shop point %i.", PlayerInfo[playerid][Name], id);
SQL_Log("Shop", PlayerInfo[playerid][Name], "N/A", string);
//LoadShops(); //Necessary?
return 1;
}
}
format(string, sizeof(string), "ERROR: No shop was found with the specified ID.");
SendClientMessage(playerid, -1, string);
return 1;
}
---------------------------
LoadShops()
{
print("[MySQL] Loading shops...");
new query[128];
mysql_format(mysql, query, sizeof(query), "SELECT * FROM `shops`");
mysql_tquery(mysql, query, "OnLoadShops");
return 1;
}
forward OnLoadShops();
public OnLoadShops()
{
new rows, fields, index, string[128];
cache_get_data(rows, fields);
if (rows)
{
while (index < rows)
{
ShopInfo[index][ID] = cache_get_field_content_int(index, "ID");
cache_get_field_content(index, "Header", ShopInfo[index][Header], mysql, 128);
format(string, sizeof(string), COL_GOLD"ID{FFFFFF}: %i - {FFD700}%s{FFFFFF}", ShopInfo[index][ID], ShopInfo[index][Header]);
ShopInfo[index][Pos][0] = cache_get_field_content_float(index, "PosX");
ShopInfo[index][Pos][1] = cache_get_field_content_float(index, "PosY");
ShopInfo[index][Pos][2] = cache_get_field_content_float(index, "PosZ");
ShopInfo[index][Text] = CreateDynamic3DTextLabel(string, 0xFFD700FF, ShopInfo[index][Pos][0], ShopInfo[index][Pos][1], ShopInfo[index][Pos][2], 5.0);
ShopInfo[index][Pickup] = CreateDynamicPickup(1254, 1, ShopInfo[index][Pos][0], ShopInfo[index][Pos][1], ShopInfo[index][Pos][2]);
index++;
}
}
printf("[MySQL] Loaded %d shops.", rows);
return 1;
}
EDIT: Could I create the shop in the command by using cache_insert_id() and only use LoadShops() under OnGameModeInit?
Re: Loading shops from database -
MartinSwag - 26.10.2015
I believe I've fixed the problem.
Код:
CMD:createshoppoint(playerid, params[])
{
if (PlayerInfo[playerid][Admin] < 4) return Invalid_Admin(playerid);
new string[128], Float:pos[3], query[128], header[128];
if (sscanf(params, "s[128]", header)) return SendClientMessage(playerid, -1, SYNTAX"/createshoppoint <text>");
if (strlen(header) < 3) return SendClientMessage(playerid, -1, "ERROR: Name is too short.");
GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
format(string, sizeof(string), "Attempting to create a new shop point...");
SendClientMessage(playerid, -1, string);
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]);
//LoadShops();
format(string, sizeof(string), "%s has created a new shop.", PlayerInfo[playerid][Name]);
SQL_Log("Shop", PlayerInfo[playerid][Name], "N/A", string);
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();
printf("insert ID: %i", insertID);
ShopInfo[insertID][ID] = insertID;
printf("shop ID: %i", ShopInfo[insertID][ID]);
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;
}