Jump to content

Safe player data management with MySQL


Rudy

Recommended Posts

Hey guys,

I know, this isn't the shortest question ever, but please read over and if you can, help for me. Thank you.

How I can manage players' data with MySQL? I haven't problems with MySQL, or with MySQL in MTA's scripting environment. E.g.: my login system works very well. But how can I manage players' data?

Can I use some server side function, e.g.: SetElementData? If I use it in this form:

setElementData(soruce, "level", "3") 

The source (or e.g. in a command the first paramter) is every time the good player? So can I store different data/player with this function in server side? If I use this function in client side, the data remains different/player? If you understand, what I mean. The player can manipluate this data (of course I think yes, only at client side)?

Or can I use some other function?

Or should I use unique IDs (E.g.: X9Dd8jSD1Z5h8p) - because the simple ID (E.g.: 1) is easily obtainable - and send a MySQL request every time (I think it's too slow)?

Or something else?

I can do all of them in LUA, this isn't the problem. The problem is: what's is the best, and what's the more safe solution (if the above solutions are good from safety standpoint)?

Thanks,

Rudy

Link to comment

So,

setElementData is synced between client & server so if you do:

setElementData(player, "level", 1) 

server-side in client will also be 1, and you can't use setElementData to store the data cause when you disconnect all data will be lost.

Also the source si the element of a event. Read more about events, The first parameter of a command is a player not source, cause a command need a player wlile source can be a vehicle a ped or anything.

About MySQL let say you'r player have:

setElementData(element, "level", 1) --Notice I put element not player/source! 

and you want to store it in MySQL

What you going to do is:

--[[saving player data.]] "onPlayerQuit" 
  
local db = dbConnect(); -- bla, bla...Read more about this function by clicking on it! 
dbExec(db, "CREATE TABLE IF NOT EXIST players_data(name, level)"); 
  
addEventHandler("onPlayerLogin", getRootElement(), function() 
    dbExec(db, "INSERT INTO players_data(name, level) VALUES(?,?)", getAccountName(getPlayerAccount(source)), getElementData(source, "level")); 
end); 
  
--[[Loading data.]] "onPlayerLogin" 
  
function exist(acc) 
    if (acc) then 
        local q = dbPoll(dbQuery(db, "SELECT * FROM players_data WHERE name='"..acc.."' LIMIT 1"), 0); 
        if (type(q) == "table") then 
            if (#q > 0) then 
                return q; 
            end 
            return false; 
        end 
    end 
    return nil; 
end 
  
addEventHandler("onPlayerLogin", getRootElement(), function() 
    local acc = getAccountName(getPlayerAccount(source)); 
    local data = exist(acc) 
    if (data and type(data) == "table") then 
        for _,v in ipairs(data) do 
            if (v["name"] == acc) then 
                setElementData(source, "level", tonumber(v["level"])) 
            end 
        end 
    end 
end); 
  

Hope you understand.

Edit: #Fixed.

Edited by Guest
Link to comment
dbQuery returns a query handle, you need to use dbPoll to fetch the data.

Your query string is wrong, it's CREATE TABLE not CREATETABLE.

Thanks, don't know why I did that I think cause I was hurrying. And about CREATETABLE was just typed bad I know is CREATE TABLE.

Updatem my topic.

Link to comment

It seems this is a big misunderstanding, and I not formulated well. But I wrote down this in the main message:

what's is the best, and what's the more safe solution (if the above solutions are good from safety standpoint)

so

  • What is the best solution to manage plyers' data?
  • The solution with ElementData functions, or any solution is enough safe?
  • The data at client side is manipulable?

Because the safe is very important; for example if I want to store a data to the SQL Query's WHERE.

---

So,

setElementData is synced between client & server so if you do:

setElementData(player, "level", 1) 

server-side in client will also be 1

I know it, but I don't know this can be manipulated on client side. If yes, this is a very big problem.

---

and you can't use setElementData to store the data cause when you disconnect all data will be lost.

I know it, too. I wrote:

I haven't problems with MySQL, or with MySQL in MTA's scripting environment. E.g.: my login system works very well.

and

I can do all of them in LUA, this isn't the problem.

but I didn't write down that I can save the data, sorry. Otherwise I've a full load/save, or login system - I wrote down this -, but I don't know what data management method is the best, and the more safe.

---

Also the source si the element of a event. Read more about events,

I know it, but I not clearly formulated, sorry for this and thanks for your help.

The first parameter of a command is a player

I know it:

(or e.g. in a command the first paramter) is every time the good player?

Yes, this is a question, but the question is: the first parameter every time the good player? Not simply a player, but thanks. Otherwise I think the answer is yes.

---

Really thank you very much that spent your time to wrote a code and to help for me, but I haven't problems with these.

---

source might not always be the "good" player. You should avoid using source when triggering data from the client to the server, and use client instead.

Thanks for you too, but I know it. However I don't know how formulate it. According to these the "good player" is a bad drafting. If I use the setElementData function at server side, after I use getElementData in client side, the server will know, what's the "good" player's data? I know, client side is client side, so in this case we can speak of one player. But the server side knows, which player sent the request, and it notes that? E.g.:

User1 Data1
User2 Data2

If I change Data1 at server side to The_Data, and I query it at client side with getElementData, this function will return with The_Data? According to my tests the answer is yes, but I still don't know:

  • What is the best solution to manage plyers' data?
  • The solution with ElementData functions, or any solution is enough safe?
  • The data at client side is manipulable?

Because the safe is very important; for example if I want to store a data to the SQL Query's WHERE.

Link to comment
BIG Quote

The setElementData() and getElementData() are safe enough and the best solution to manage player data and if you are still scared about using them you can always compile the script here or add cache="false" in meta.xml(I recommend) if the file is client-side and the file won't be stored on player PC like this:

<script src="client.lua" type="client" cache="false"/> 

And as i say'd setElementData() and getElementData() are synced and if you change the data on client will also be changed on server and reverse.

Link to comment

No. Element data is not the best way to manage player's data. You might need its sync feature for specific things but most of the time you won't need to sync each element data.

Use tables instead. Sync only what you need to sync, scoreboard data being one.

Link to comment
...

Thank you, but I won't turn off the cache, because I think the files will download every time when a player joins to the server. Otherwise I always compile my scripts. So, client side no way to manipulate these data? But it doesn't matter, eventually everybody use these functions, and use MySQL and there isn't a problem. Anyway the security more depper isn't MTA's scope.

---

Use tables instead.

But how can I use tables between all script files? If I know right the global varibles doesn't available server side and client side too. However I want to store data that I use very often, up to several times per minute, server side and client side too. Of course I've some often used data, what I want to use only at server side. So the tables are good solutions to store data to MySQL Querys, because MySQL is only server side, of course. But the tables aren't good solutions for client+server side data, I think.

If I want to use a data rarely, I use "your" tables, or I use MySQL Querys every time, or anything else?

Any other solution in addition to tables and ElementData functions (mainly therefore, because I think there are problems with the tables, and according to you the ElementData way doesn't the best way)?

Link to comment

I already mentioned that you should sync what you need whenever you need it. Using tables along with exports is fine, and you can permanently save the data in the table using SQL.

Data that needs to be always present on the client-side (scoreboard columns) can be set using element data.

Link to comment
  • 7 months later...

With the command ipconfig /all which can be executed from the prompt, you can see that the VMware Player has configured additional virtual network adapters to your network configuration.

Additionally,there are 5 tools, to help you manage your MySQL DB.

1. MySQL Command Shell

2. MySQL Workbench

3. Webmin MySQL Model

4. phpMyAdmin

5. Emma

Through these,you can try on it once.

hope you find something useful.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...