A question about loading vehicles using BlueG's MySQL plugin R38
#1

Hello,

I've made this to load and save my vehicle system using BlueG's MySQL R38. My question is, is it done efficiently? If no, can someone tell me how can i make it work efficiently? The current system doesn't work properly, like vehicle's spawning coordinates are mixing up with each other and sometimes, vehicle is losing its paindjob. I want to know whats wrong and how to make it work efficiently, thanks.

pawn Код:
//Under OnPlayerDisconect, when they disconnect to save all the owned vehicles. The MAX_VEHICLES_PER_PLAYER is set to 10.
if (houseid != 0)
{
    for (new i; i<MAX_VEHICLES_PER_PLAYER; i++)
    {
        if (gHouseData[houseid][PlayerVehicles][i] != 0)
        {
            new vid = gHouseData[houseid][PlayerVehicles][i];
            Vehicles_Save(vid);
        }
    }
}

//Saving function
Vehicles_Save(vehicleid)
{
    new query[1000], components[100];
    for(new i; i < 14; i++)
    {
        format:components("%s%i%s", components, GetVehicleComponentInSlot(vehicleid, i), ((i != (14 - 1)) ? (":") : ("")));
    }
    mysql_format(mysql, query, sizeof(query), "%sUPDATE `vehicles` SET `Fuel`='%f',`SpawnX`='%f',`SpawnY`='%f',`SpawnZ`='%f',`SpawnAngle`='%f',`Color1`='%d',`Color2`='%d',`Clamped`='%d',`Paint`='%d',`Components`='%e' WHERE `vDB_ID`='%d'", query,
    gVehicleInfo[vehicleid][Fuel], gVehicleInfo[vehicleid][SpawnX], gVehicleInfo[vehicleid][SpawnY], gVehicleInfo[vehicleid][SpawnZ], gVehicleInfo[vehicleid][SpawnRot], gVehicleInfo[vehicleid][Color1],
    gVehicleInfo[vehicleid][Color2], gVehicleInfo[vehicleid][Clamped], gVehicleInfo[vehicleid][PaintJob], components, gVehicleInfo[vehicleid][DB_ID]);
    mysql_query(mysql, query, false);
}

//Loading function
LoadVehicles()
{
    new query[128];
    mysql_format(mysql, query, sizeof(query), "SELECT * FROM `vehicles`");
    mysql_tquery(mysql, query, "LoadOwnedVehicles", "", "");
}

Function:LoadOwnedVehicles()
{
    new rows, fields;
    cache_get_data(rows, fields);
    if (rows != 0)
    {
        new db_id, Float:fuel, Float:x, Float:y, Float:z, Float:a, c1, c2;
        new clamped, paint, str[100], mods[14], houseid, owner[24], model;

        for (new i; i < rows; i++)
        {
            db_id = cache_get_field_content_int(i, "vDB_ID", mysql);
            model = cache_get_field_content_int(i, "Model", mysql);
            houseid = cache_get_field_content_int(i, "LinkedHouseID", mysql);
            fuel = cache_get_field_content_float(i, "Fuel", mysql);
            x = cache_get_field_content_float(i, "SpawnX", mysql);
            y = cache_get_field_content_float(i, "SpawnY", mysql);
            z = cache_get_field_content_float(i, "SpawnZ", mysql);
            a = cache_get_field_content_float(i, "SpawnAngle", mysql);
            c1 = cache_get_field_content_int(i, "Color1", mysql);
            c2 = cache_get_field_content_int(i, "Color2", mysql);
            clamped = cache_get_field_content_int(i, "Clamped", mysql);
            paint = cache_get_field_content_int(i, "Paint", mysql);
            cache_get_field_content(i, "Owner", owner, mysql, 24);
            cache_get_field_content(i, "Components", str, mysql, 100);
            sscanf(str, "p<:>a<i>[100]", mods);

            new vehicleid = AddOwnedVehicle(houseid, owner, model, paint, mods, x, y, z, a, c1, c2);
            gVehicleInfo[vehicleid][DB_ID] = db_id;
            gVehicleInfo[vehicleid][Clamped] = clamped;
            if (gVehicleInfo[vehicleid][Fuel] == -1)
                gVehicleInfo[vehicleid][Fuel] = MAX_FUEL;
            else
                gVehicleInfo[vehicleid][Fuel] = fuel;
        }
    }
    else return 0;
    return 1;
}
Reply
#2

/Bump, anyone?
Reply
#3

The only remark I can make at this time is: don't store multiple values in one column.
Reply
#4

You mean, the way i saved the vehicle's components? Sorry i didn't exactly get what you are trying to say.

Other than that the loading and saving stuffs are done correctly?
Reply
#5

The alternative is to use 14 separate columns, just for the components, where many fields would just hold "0".
I'm also storing all vehicle-components in one column, where each component is separated by a "-".

It's also very easy to load the all at once into a string of an enum.
It only takes 1 sscanf line to load all 14 components into the enum.

What if you would remove one value from your enum?
Then your data is split up in memory.


For your saving:
I would rather update data in MySQL whenever it changes to prevent dataloss.
If your player is playing for over an hour after buying his components for example, and your server gets attacked or suddenly crashes, all that data is gone and you can't save it anymore.
When your server comes back online and the player logs in, his vehicles won't have their components anymore, but he might have paid for them already (if you save his money whenever it changes).
He'll have to buy the same stuff again and pay twice for the same thing.

That's why I'm saving data whenever it changes. Money, vehicle-data, scorepoints, statistics, anything.
If the server crashes suddenly, you won't have ANY dataloss.

And you could split up your queries into smaller ones.
It has no use to update ALL data about a vehicle when the player only adds 1 component.
You would just update the components as the other data remains unchanged.

Or if your player only changes the color of his vehicle, you would only update those fields for that specific vehicle.
Spawn-position for example won't need to be updated as changing colors doesn't change the spawn-location.

This also puts less stress on your database and your server as well.
You'll get a more efficient server in the process.

I'm not saving ANY data during OnPlayerDisconnect because I know all my data is saved whenever it changes and I don't need to send huge queries to save EVERYTHING at once when a player logs out, just to make sure everything has been saved properly.
Reply
#6

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
The alternative is to use 14 separate columns, just for the components, where many fields would just hold "0".
I'm also storing all vehicle-components in one column, where each component is separated by a "-".
The alternative, and the right way to do it, is to create a separate table for the components with two columns: vehicleid {PK, FK}, componentid {PK}. Simple.
Reply
#7

Alright, thanks for the tips. But i just want to know one last thing, the way i am loading the vehicles, is it done correctly? Or there's any other good ways to do it?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)