Ownership
#1

pawn Код:
if(PlayerInfo[playerid][pHouse] >= 0)
    {
        for(new idx = 0; idx < MAX_HOUSES; idx++)
        {
            if(!strcmp(Player(playerid), HouseInfo[idx][hOwner], false))
            {
                PlayerInfo[playerid][pHouse] = HouseInfo[idx][hHouseID];
            }
            else
            {
                SendClientMessage(playerid, -1, "You have been removed as ownership from your house.");
            }
        }
    }
Fixed everything else but one last problem.
Why does this send me the message that I've been removed as ownership even if I do have a house in my name?
When I remove the else part, the if part works fine..
Reply
#2

Because it loops through all houses, the player cannot own all of the houses.

Instead you could run this more effective code:

pawn Код:
if(PlayerInfo[playerid][pHouse] >= 0) {
    if(!strcmp(Player(playerid), HouseInfo[PlayerInfo[playerid][pHouse]][hOwner], false)) {
        PlayerInfo[playerid][pHouse] = HouseInfo[idx][hHouseID];
    } else {
        SendClientMessage(playerid, -1, "You have been removed as ownership from your house.");
    }
}
This eliminates the purpose of your loop, and you can also run the else statement.
Reply
#3

Quote:
Originally Posted by Calgon
Посмотреть сообщение
Because it loops through all houses, the player cannot own all of the houses.

Instead you could run this more effective code:

pawn Код:
if(PlayerInfo[playerid][pHouse] >= 0) {
    if(!strcmp(Player(playerid), HouseInfo[PlayerInfo[playerid][pHouse]][hOwner], false)) {
        PlayerInfo[playerid][pHouse] = HouseInfo[idx][hHouseID];
    } else {
        SendClientMessage(playerid, -1, "You have been removed as ownership from your house.");
    }
}
This eliminates the purpose of your loop, and you can also run the else statement.
Thanks, any other alternative way? I need the loop because when I delete houses it changes the house ID's on server restart to match MAX_HOUSES.

pawn Код:
PlayerInfo[playerid][pHouse] = HouseInfo[idx][hHouseID];
This part changes the players house ID when they log in because the loop checks through the houses to see which one they own.
Reply
#4

Quote:
Originally Posted by AphexCCFC
Посмотреть сообщение
Thanks, any other alternative way? I need the loop because when I delete houses it changes the house ID's on server restart to match MAX_HOUSES.

pawn Код:
PlayerInfo[playerid][pHouse] = HouseInfo[idx][hHouseID];
This part changes the players house ID when they log in because the loop checks through the houses to see which one they own.
This is a really messy way to deal with your records, as this breaks referential integrity, and there's the possibility the player will get the ID of a newly made house.

The neatest way to do this IMO is to change your housing system to store each house with a unique ID number which never changes.

I think to keep your current system working, just remove the else statement as it is irrelevant. The player will probably notice they no longer have a house by looking at their stats, or whatever other system you have in place.
Reply
#5

Tested the system on two accounts and it works, just got problems now when the player is in the house and the ID changes.
If I store the houses with a unique ID number they will end up like this when I remove a middle house from the database:

pawn Код:
HouseInfo[0][hHouseID] = 0;
HouseInfo[1][hHouseID] = 1;
HouseInfo[2][hHouseID] = 2;
Say if I deleted house ID 1 and restart the server, it will do this:

pawn Код:
HouseInfo[0][hHouseID] = 0;
HouseInfo[1][hHouseID] = 2;
So if I use commands for the house, it'll show that house ID 1 already exists and recreate number 2.

pawn Код:
stock CreateHouse(houseid, Float:PosX, Float:PosY, Float:PosZ, Float:PosA, playerid)
{
    new string[200], query[400];
   
    if(HouseInfo[houseid][hIsCreated] == 1)
    {
        SendClientMessage(playerid, COLOR_RED, "That house ID already exists.");
        return 1;
    }
   
    HouseInfo[houseid][hHouseID] = houseid;
    HouseInfo[houseid][hEnterPos][0] = PosX;
    HouseInfo[houseid][hEnterPos][1] = PosY;
    HouseInfo[houseid][hEnterPos][2] = PosZ;
    HouseInfo[houseid][hEnterPos][3] = PosA;
    HouseInfo[houseid][hExitPos][0] = 266.9708;
    HouseInfo[houseid][hExitPos][1] = 304.9378;
    HouseInfo[houseid][hExitPos][2] = 999.1484;
    HouseInfo[houseid][hExitPos][3] = 273.4171;
    HouseInfo[houseid][hInterior] = 2;
    HouseInfo[houseid][hVirtualWorld] = houseid+2;
    HouseInfo[houseid][hType] = 1;
    HouseInfo[houseid][hLock] = 0;
    HouseInfo[houseid][hSafe] = -1;
    format(HouseInfo[houseid][hAddress], 32, "1 San Fierro Drive");
    format(HouseInfo[houseid][hOwner], 35, "None");
    HouseInfo[houseid][hPrice] = 120000;
    HouseInfo[houseid][hIsCreated] = 1;

    format(query, sizeof(query), "INSERT INTO `Houses` (`HouseID`, `Address`, `Owner`, `Price`, `EnterX`, `EnterY`, `EnterZ`, `EnterA`, `ExitX`, `ExitY`, `ExitZ`, `ExitA`, `Interior`, `VirtualWorld`, `Type`, `Lock`, `Safe`, `IsCreated`) VALUES (%d, \'%s\', \'%s\', %d, %f, %f, %f, %f, %f, %f, %f, %f, %d, %d, %d, %d, %d, %d)",
    HouseInfo[houseid][hHouseID],
    HouseInfo[houseid][hAddress],
    HouseInfo[houseid][hOwner],
    HouseInfo[houseid][hPrice],
    HouseInfo[houseid][hEnterPos][0],
    HouseInfo[houseid][hEnterPos][1],
    HouseInfo[houseid][hEnterPos][2],
    HouseInfo[houseid][hEnterPos][3],
    HouseInfo[houseid][hExitPos][0],
    HouseInfo[houseid][hExitPos][1],
    HouseInfo[houseid][hExitPos][2],
    HouseInfo[houseid][hExitPos][3],
    HouseInfo[houseid][hInterior],
    HouseInfo[houseid][hVirtualWorld],
    HouseInfo[houseid][hType],
    HouseInfo[houseid][hLock],
    HouseInfo[houseid][hSafe],
    HouseInfo[houseid][hIsCreated]
    );

    mysql_function_query(g_Handle, query, false, "", "");

    HouseInfo[houseid][hPickup] = CreateDynamicPickup(1273, 23, PosX, PosY, PosZ, -1, -1, -1, 100.0);
    format(string, sizeof(string), "Type"White": %s\n"Aqua"Address"White": %s\n"Aqua"Owner"White": %s\n"Aqua"Price"White": $%d\n"Aqua"Status"White": Unlocked", HouseType(houseid), HouseInfo[houseid][hAddress], HouseInfo[houseid][hOwner], HouseInfo[houseid][hPrice]);
    HouseInfo[houseid][hText] = CreateDynamic3DTextLabel(string, COLOR_AQUA, PosX, PosY, PosZ, 25, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 1, -1, -1, -1, 100);
    format(string, sizeof(string), "%s %s has created house ID %d.", AdminRanks(playerid), Player(playerid), houseid);
    SendAdminMessage(string);
    houseid ++;
    return 1;
}
pawn Код:
CMD:createhouse(playerid, params[])
{
    new Float:pos[4], houseid;
    if(!IsPlayerLoggedIn(playerid))
    {
        SendClientMessage(playerid, COLOR_RED, "Error"White": You are restricted from using commands until you log in.");
        return 1;
    }
    if(PlayerInfo[playerid][pAdminLevel] < 1)
    {
        SendClientMessage(playerid, COLOR_RED, "Error"White": You do not have the authority to use this command.");
        return 1;
    }
    if(!AdminDuty[playerid])
    {
        SendClientMessage(playerid, COLOR_RED, "Error"White": You are not on duty as an Administrator (/aduty).");
        return 1;
    }
    if(sscanf(params, "i", houseid))
    {
        SendClientMessage(playerid, COLOR_GRAY, "");
        SendClientMessage(playerid, COLOR_GRAY, "Usage"White": /createhouse <house id>");
        SendClientMessage(playerid, COLOR_GRAY, "");
        return 1;
    }
    GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
    GetPlayerFacingAngle(playerid, pos[3]);
    CreateHouse(houseid, pos[0], pos[1], pos[2], pos[3], playerid);
    return 1;
}
pawn Код:
stock LoadHouses()
{
    mysql_function_query(g_Handle, "SELECT * FROM `Houses`", true, "HousesLoad", "");
    return 1;
}
pawn Код:
public HousesLoad()
{
    new temp[400], string[200], rows, fields;
    cache_get_data(rows, fields, g_Handle);

    new id = 0;
    if(rows)
    {
        while(id <= rows-1)
        {
            HouseInfo[id][hHouseID] = id;
            cache_get_row(id, 0, temp, g_Handle), HouseInfo[id][hOldID] = strval(temp);
            cache_get_row(id, 1, HouseInfo[id][hAddress], g_Handle, 32);
            cache_get_row(id, 2, HouseInfo[id][hOwner], g_Handle, 35);
            cache_get_row(id, 3, temp, g_Handle), HouseInfo[id][hPrice] = strval(temp);
            cache_get_row(id, 4, temp, g_Handle), HouseInfo[id][hEnterPos][0] = floatstr(temp);
            cache_get_row(id, 5, temp, g_Handle), HouseInfo[id][hEnterPos][1] = floatstr(temp);
            cache_get_row(id, 6, temp, g_Handle), HouseInfo[id][hEnterPos][2] = floatstr(temp);
            cache_get_row(id, 7, temp, g_Handle), HouseInfo[id][hEnterPos][3] = floatstr(temp);
            cache_get_row(id, 8, temp, g_Handle), HouseInfo[id][hExitPos][0] = floatstr(temp);
            cache_get_row(id, 9, temp, g_Handle), HouseInfo[id][hExitPos][1] = floatstr(temp);
            cache_get_row(id, 10, temp, g_Handle), HouseInfo[id][hExitPos][2] = floatstr(temp);
            cache_get_row(id, 11, temp, g_Handle), HouseInfo[id][hExitPos][3] = floatstr(temp);
            cache_get_row(id, 12, temp, g_Handle), HouseInfo[id][hInterior] = strval(temp);
            cache_get_row(id, 13, temp, g_Handle), HouseInfo[id][hVirtualWorld] = strval(temp);
            cache_get_row(id, 14, temp, g_Handle), HouseInfo[id][hType] = strval(temp);
            cache_get_row(id, 15, temp, g_Handle), HouseInfo[id][hLock] = strval(temp);
            cache_get_row(id, 16, temp, g_Handle), HouseInfo[id][hSafe] = strval(temp);
            cache_get_row(id, 17, temp, g_Handle), HouseInfo[id][hIsCreated] = strval(temp);

            SaveHouse2(id);
           
            if(!strcmp("None", HouseInfo[id][hOwner], false))
            {
                HouseInfo[id][hPickup] = CreateDynamicPickup(1273, 23, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], -1, -1, -1, 100.0);
                format(string, sizeof(string), "Type"White": %s\n"Aqua"Address"White": %s\n"Aqua"Owner"White": None\n"Aqua"Price"White": $%d\n"Aqua"Status"White": Unlocked", HouseType(id), HouseInfo[id][hAddress], HouseInfo[id][hPrice]);
                HouseInfo[id][hText] = CreateDynamic3DTextLabel(string, COLOR_AQUA, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], 25, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 1, -1, -1, -1, 100);
            }
            else
            {
                HouseInfo[id][hPickup] = CreateDynamicPickup(1273, 23, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], -1, -1, -1, 100.0);
                format(string, sizeof(string), "Type"White": %s\n"Aqua"Address"White": %s\n"Aqua"Owner"White": %s\n"Aqua"Status"White": %s", HouseType(id), HouseInfo[id][hAddress], HouseInfo[id][hOwner], HouseStatus(id));
                HouseInfo[id][hText] = CreateDynamic3DTextLabel(string, COLOR_AQUA, HouseInfo[id][hEnterPos][0], HouseInfo[id][hEnterPos][1], HouseInfo[id][hEnterPos][2], 25, INVALID_PLAYER_ID, INVALID_VEHICLE_ID, 1, -1, -1, -1, 100);
            }
            CheckObjects(id);
            id ++;
        }
        printf("Houses: %d", rows);
    }
    else
    {
        printf("Houses: 0");
    }
    return 1;
}
pawn Код:
stock SaveHouse2(id)
{
    new query[500];

    HouseInfo[id][hVirtualWorld] = id+2;

    format(query, sizeof(query), "UPDATE `Houses` SET `HouseID` = %d, `Address` = '%s', `Owner` = '%s', `Price` = %d, `EnterX` = %f, `EnterY` = %f, `EnterZ` = %f, `EnterA` = %f, `ExitX` = %f, `ExitY` = %f, `ExitZ` = %f, `ExitA` = %f, `Interior` = %d, VirtualWorld = %d, `Type` = %d, `Lock` = %d, `Safe` = %d, `IsCreated` = %d WHERE `HouseID` = %d",
    HouseInfo[id][hHouseID],
    HouseInfo[id][hAddress],
    HouseInfo[id][hOwner],
    HouseInfo[id][hPrice],
    HouseInfo[id][hEnterPos][0],
    HouseInfo[id][hEnterPos][1],
    HouseInfo[id][hEnterPos][2],
    HouseInfo[id][hEnterPos][3],
    HouseInfo[id][hExitPos][0],
    HouseInfo[id][hExitPos][1],
    HouseInfo[id][hExitPos][2],
    HouseInfo[id][hExitPos][3],
    HouseInfo[id][hInterior],
    HouseInfo[id][hVirtualWorld],
    HouseInfo[id][hType],
    HouseInfo[id][hLock],
    HouseInfo[id][hSafe],
    HouseInfo[id][hIsCreated],
    HouseInfo[id][hOldID]
    );

    mysql_function_query(g_Handle, query, false, "", "");
    return 1;
}
Here are the codes if you're able to think of a better way, but everything is working just if I delete a house, enter another one and restart server, it spawns me in the next house down.
Reply
#6

Please read what I posted, change your system so that the IDs don't change on restart. Alternatively, you could update all player rows when the server starts, checking if their current house ID is valid. But regardless, this is still a very messy system. I would strongly suggest learning more about relational databases.
Reply
#7

Thanks - not sure how to do what you've suggested except how I've done it already, one thing I cannot work out aha. How would I go about learning relational databases?
Reply
#8

Just prevent it from re-arranging IDs. Just don't change the ID, let it always remain constant after the record is made.

This may help https://www.youtube.com/watch?v=VKBindYG--8
Reply
#9

Yeah but the MAX_HOUSES ID's start from 0 again when I delete a house before the latest created and I need it to stay the same as the house ID, right? I'll check that video out now. Cheers
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)