Problem for the spawn
#1

Good evening.

So I have problems with my code:

So when I make the player spawn a query as to whether a housing.

I have a function that shows whether a player has a housing (But the application allows you to find the id of the house).

So I coded if the player has no home he spawn at different spawn (With random).

The trouble is that it does not work, the player still spawn id house to 0 when it should spawn to random coordinates imposed (I think this is the SQL query).

pawn Код:
new requete[256], rowl[128];
format(requete, sizeof(requete), "SELECT id FROM srp_players_houses WHERE owner='%s'", pName[playerid]);
MySQLCheckConnection();
mysql_query(requete);
mysql_store_result();
mysql_fetch_row(rowl);
new idbiz = strval(rowl);
mysql_free_result();
idbiz--;

if(!house_isProprio(playerid, idbiz))
{
    switch(random(2))
    {
        case 0: SetPlayerPos(playerid, 1742.7836, -1860.1030, 13.5791), SetInterior(playerid, 0);
        case 1: SetPlayerPos(playerid, 837.7275, -1341.4581, 7.1719), SetInterior(playerid, 0);
    }
}
Thanks for your help !!
Reply
#2

I see a few mistakes in here.

First, we need to normalize the tables. You have stated the house ownership depends on the player's name, but what if that player changes his nickname? We must assign a unique id for each registered player and when a player buys a house, we set the owner to the player's ID, not the player's name.

Normalized tables:
Код:
CREATE TABLE 'houses' (ID int NOT NULL AUTO_INCREMENT, NAME varchar(50) DEFAULT NULL, OWNER INT, S_X FLOAT, S_Y FLOAT, S_Z FLOAT, S_W INT, S_I INT, S_A FLOAT, PRIMARY KEY(ID));

//both the name and ID must be primary keys! We cannot have two players with the same name!
CREATE TABLE 'users' (ID int NOT NULL AUTO_INCREMENT, NAME varchar(50) NOT NULL, PRIMARY KEY(ID, NAME));

So, we need these:
Код:
enum pDB
{
Id,
name[50],
houses
}
new Player[MAX_PLAYERS][pDB];

enum hDB
{
id,
name[50],
owner,
Float:sPos[6], //used for spawn pos
}
new House[MAX_HOUSES+1][hDB];
When server is loaded:
Код:
public OnGameModeInit()
{
//loading all houses
new Cache: cache, row;
cache = mysql_query(1, "SELECT * FROM houses", true);
for(new i = 0; i<cache_get_row_count(1)+1; i++)
{
row = i - 1;
House[i][id] = cache_get_field_content_int(row, "ID");
House[i][name] = cache_get_field_content_int(row, "NAME");
House[i][owner] = cache_get_field_content_int(row, "OWNER");
House[i][sPos][0] = cache_get_field_content_float(id, "S_X");
House[i][sPos][1] = cache_get_field_content_float(id, "S_Y");
House[i][sPos][2] = cache_get_field_content_float(id, "S_Z");
House[i][sPos][3] = cache_get_field_content_float(id, "S_ANG");
House[i][sPos][4] = cache_get_field_content_float(id, "S_W");
House[i][sPos][5] = cache_get_field_content_float(id, "S_I");
}
cache_delete(cache, 1);

}
Now assigning number of houses to players on connect:
Код:
public OnPlayerConnect(playerid)
{
//loading player info
new Cache: cache, query[128], name[50], rows;
GetPlayerName(playerid, name, MAX_PLAYER_NAME);

mysql_format(1, query, sizeof query, "SELECT * FROM USERS WHERE NAME='%s' LIMIT 1",name);
cache = mysql_query(1, query, true);
rows = cache_get_row_count(1);
if(rows != 0)
{
Player[playerid][id] = cache_get_field_content_int(0, "ID");

//house number!
for(new i = 0; i<MAX_HOUSES; i++)
{
if(House[i][id] > 0 && House[i][owner] == Player[playerid][id])
Player[playerid][id]++;
}

}
else
{
// player isn't registered... 
}
cache_delete(cache, 1);

}
So when a player buys the house, we update the house table by running this query:
Код:
stock buyHouse(playerid, houseid)
{
new query[128];
format(query, sizeof query, 'UPDATE houses SET OWNER = %d WHERE ID = %d', Player[playerid][id], House[housed][id]);
mysql_query(1, query, false);
}

stock sellHouse(playerid, houseid)
{
new query[128];
format(query, sizeof query, 'UPDATE houses SET OWNER = 0 WHERE OWNER = %d AND ID = %d', Player[playerid][id], House[housed][id]);
mysql_query(1, query, false);
}
And finally, to answer your question!
Код:
public OnplayerSpawn(playerid)
{

if(Player[playerid][houses] == 0)
{

switch(random(2))    
{        
case 0: SetPlayerPos(playerid, 1742.7836, -1860.1030, 13.5791);        
case 1: SetPlayerPos(playerid, 837.7275, -1341.4581, 7.1719);    
}

}

return 1;
}
Now of course, this merely represents what your system is like. So, if you have any other questions, ask!
Reply
#3

Hello.

For the name change is not a problem, I have a function that allows you to change the name of the person in the game and in the tables (For the home owner, vehicle etc.)

In addition I use MySQL R5.

So I always look for the initial problems with my code ^ ^
Reply
#4

Hey,

Thanks for giving me a call! I'm still curious of what're trying to make, for me its unsure. Would you give me some descriptions of what're going to create? Also, if there's something wrong with your queries, just be sure to check the mysql_log; in case if there's something missing.
Reply
#5

Infact it is not my code not work I explain:

Here I make a MySQL query to see if the player has a house:

pawn Код:
new requete[256], rowl[128];
format(requete, sizeof(requete), "SELECT id FROM srp_players_houses WHERE owner='%s'", pName[playerid]);
MySQLCheckConnection();
mysql_query(requete);
mysql_store_result();
mysql_fetch_row(rowl);
new idbiz = strval(rowl);
mysql_free_result();
idbiz--;
And then I have a function (house_isproprio) which allows you to check the id of the house if the player is the owner or not.

So I said that if the player is not owner it will spawn in random positions in the data.

But the worry is that even if the player does not own it will spawn in the house id 0 instead of random positions ...

pawn Код:
if(!house_isProprio(playerid, idbiz))
{
    switch(random(2))
    {
        case 0: SetPlayerPos(playerid, 1742.7836, -1860.1030, 13.5791), SetInterior(playerid, 0);
        case 1: SetPlayerPos(playerid, 837.7275, -1341.4581, 7.1719), SetInterior(playerid, 0);
    }
}
Reply
#6

Is there any commands that executed to delete your house? Also it could be a problem with saving position of the house owner? Either try to create a new column to check if the player has owned a house before, then when the house is deleted, then if the player joins you can check if his house has been deleted, if the servee manage to find a player that got his house deleted, then set his previous house to 0 and randomly spawn him (as well give him a message that his house got deleted (for debugging).
Reply
#7

UP please.
Reply
#8

pawn Код:
new requete[256], rowl[128];
format(requete, sizeof(requete), "SELECT id FROM srp_players_houses WHERE owner='%s'", pName[playerid]);
MySQLCheckConnection();
mysql_query(requete);
mysql_store_result();

if(mysql_fetch_row(rowl))
{
    //spawn in the house using - strval(rowl)
}
else
{
    switch(random(2))
    {
        case 0: SetPlayerPos(playerid, 1742.7836, -1860.1030, 13.5791), SetInterior(playerid, 0);
        case 1: SetPlayerPos(playerid, 837.7275, -1341.4581, 7.1719), SetInterior(playerid, 0);
    }
}

printf("houseid - %d - name_player %s",strval(rowl), pName[playerid]); //this is just a debug to see what mysql gets
//if he is getting the same value has in database, i don't know where is the error
//but if doesn't get the same value, the problem is here obviously

mysql_free_result();
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)