Jump to content

[HELP]Drift points database SQLite


spoty

Recommended Posts

What's the content of drft_db.lua?

current it have

addEventHandler ("onPlayerLogin",root, 
function (thePreviousAccount, theCurrentAccount, autoLogin) 
    local anterior = getAccountData (theCurrentAccount,"Last_Drift") 
    local total = getAccountData (theCurrentAccount,"Total_Drift") 
    local mejor = getAccountData (theCurrentAccount,"Best_Drift") 
    setElementData(source, "Last Drift", anterior) 
    setElementData(source, "Total Drift", total) 
    setElementData(source, "Best Drift", mejor) 
end) 
  
  
  
addEventHandler ("onPlayerLogout", root, 
function() 
    if not kickPlayer (source, nil, "Logging out is disallowed.") then -- if the player doesn't get kicked(no admin rights) 
        setElementData(source, "Last Drift", false) 
        setElementData(source, "Total Drift", false) 
        setElementData(source, "Best Drift", false) 
    end 
end) 
  
  
addEventHandler ("onPlayerQuit", root, 
function (quitType, reason, responsibleElement) 
    local account = getPlayerAccount (source) 
    if not isGuestAccount (account) then 
        local anterior = getElementData(source, "Last Drift") 
        local total = getElementData(source, "Total Drift") 
        local mejor = getElementData(source, "Best Drift") 
        setAccountData (account,"Last_Drift",anterior) 
        setAccountData (account,"Total_Drift",total) 
        setAccountData (account,"Best_Drift",mejor) 
    end 
end) 

but it wont calculate old point to the new points its just recount after rejoin

Link to comment
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite" , "Drift_DB.db" ) 
        if ( connection ) then 
            dbQuery ( connection , "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 

When you start resource check what debug says ("connected" or "not connected").

Link to comment

Did something bad, replace whole code with that

-- CHANGE IF NECCESSARY 
dataName  = { 
    ["last"] = "Last Drift", 
    ["total"] = "Total Drift", 
    ["best"] = "Best Drift" 
} 
-- type here what the elementData's names 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite", "Drift_DB.db" ) 
        if ( connection ) then 
            dbQuery ( connection, "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 
  
function saveDriftPoints ( ) 
    local account = getPlayerAccount ( source ); -- getting account 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
     
    local accName = getAccountName ( account ); -- getting name 
     
    local t = { } -- creating table for drift's data 
    for k, v in pairs ( dataName ) do 
        local data = getElementData ( source, v ); 
        if ( data ) then 
            t[v] = data; -- inserting values to table from ElementData 
        end 
    end 
     
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        for k, v in pairs ( fromJSON ( check[1]["data"] ) ) do 
            t[k] = ( tonumber ( t[k] ) + tonumber ( v ) ); 
        end 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 
  
function loadDriftPoints ( _, account ) 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
     
    local accName = getAccountName ( account ); -- getting name  
     
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if row exist 
    if ( #check > 0 ) then -- if yes 
        local d = check[1]; 
         
        local t = fromJSON ( d["data"] ); 
        if ( type ( t ) == "table" ) then 
            for k, v in pairs ( t ) do 
                setElementData ( source, k, v ); -- setting ElementData from saved drift's table in table 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, loadDriftPoints ); 

Again updated.

Link to comment

its working but it still not working fully its saving and loading but when there coming new points its recounting

example i quit the game when i have 2564 best drift, 231 last drift, 5684322 total drift

and when i join the game and make 1 drift from 6 points then it shows on scoreboard 6 best drift, 6 last drift, 6 total drift

Link to comment
-- CHANGE IF NECCESSARY 
dataName  = { 
    ["last"] = "Last Drift", 
    ["total"] = "Total Drift", 
    ["best"] = "Best Drift" 
} 
-- type here what the elementData's names 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite", "Drift_DB.db" ) 
        if ( connection ) then 
            dbQuery ( connection, "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 
  
function saveDriftPoints ( ) 
    local account = getPlayerAccount ( source ); -- getting account 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local t = { } -- creating table for drift's data 
    for k, v in pairs ( dataName ) do 
        local data = getElementData ( source, v ); 
        if ( data ) then 
            t[v] = data; -- inserting values to table from ElementData 
        end 
    end 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        for k, v in pairs ( fromJSON ( check[1]["data"] ) ) do 
            t[k] = ( tonumber ( t[k] ) + tonumber ( v ) ); 
        end 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 
  
function loadDriftPoints ( _, account ) 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if row exist 
    if ( #check > 0 ) then -- if yes 
        local d = check[1]; 
        
        local t = fromJSON ( d["data"] ); 
        if ( type ( t ) == "table" ) then 
            for k, v in pairs ( t ) do 
                setElementData ( source, k, v ); -- setting ElementData from saved drift's table in table 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, loadDriftPoints ); 

Well, it should work imo.

Link to comment
-- CHANGE IF NECCESSARY 
dataName  = { 
    ["last"] = "Last Drift", 
    ["total"] = "Total Drift", 
    ["best"] = "Best Drift" 
} 
-- type here what the elementData's names 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite", "Drift_DB.db" ) 
        if ( connection ) then 
            dbQuery ( connection, "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 
  
function saveDriftPoints ( ) 
    local account = getPlayerAccount ( source ); -- getting account 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local t = { } -- creating table for drift's data 
    for k, v in pairs ( dataName ) do 
        local data = getElementData ( source, v ); 
        if ( data ) then 
            t[v] = data; -- inserting values to table from ElementData 
        end 
    end 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        for k, v in pairs ( fromJSON ( check[1]["data"] ) ) do 
            t[k] = ( tonumber ( t[k] ) + tonumber ( v ) ); 
        end 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( t ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 
  
function loadDriftPoints ( _, account ) 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if row exist 
    if ( #check > 0 ) then -- if yes 
        local d = check[1]; 
        
        local t = fromJSON ( d["data"] ); 
        if ( type ( t ) == "table" ) then 
            for k, v in pairs ( t ) do 
                setElementData ( source, k, v ); -- setting ElementData from saved drift's table in table 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, loadDriftPoints ); 

Well, it should work imo.

no its still the same :S

this are the score when i quit

DaTD1ac.jpg?1

this are the score when i join and did 1 drift

zgfUFjz.jpg?1

Link to comment

Are you sure? Any errors? Make sure that you restarted resource. I made a simulation with tables and adding values and it should work.

Made it other way:

-- CHANGE IF NECCESSARY 
dataName  = { 
    ["last"] = "Last Drift", 
    ["total"] = "Total Drift", 
    ["best"] = "Best Drift" 
} 
-- type here what the elementData's names 
  
addEventHandler ( "onResourceStart", root, 
    function ( ) 
        -- creating connection and if true creates column if not exist 
        connection = dbConnect ( "sqlite", "Drift_DB.db" ) 
        if ( connection ) then 
            dbQuery ( connection, "CREATE TABLE IF NOT EXISTS `drift` ( `data` TEXT, `account` TEXT )" ); 
            outputDebugString ( "connected" ); 
        else 
            outputDebugString ( "not connected" ); 
        end 
        -- creating connection and if true creates column if not exist 
    end 
); 
  
function saveDriftPoints ( ) 
    local account = getPlayerAccount ( source ); -- getting account 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local t = { } -- creating table for drift's data 
    for k, v in pairs ( dataName ) do 
        local data = getElementData ( source, v ); 
        if ( data ) then 
            t[v] = data; -- inserting values to table from ElementData 
        end 
    end 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if exist row 
    if ( #check > 0 ) then 
        local data = fromJSON ( check[1]["data"] ); 
        if ( data ) then 
            for n, v in pairs ( t ) do 
                data[n] = tonumber ( data[n] ) + tonumber ( v ); 
            end 
        end 
        dbQuery ( connection, "UPDATE `drift` SET `data`=? WHERE `account`=?", toJSON ( data ), accName ); -- if yes updating 
    elseif ( #check == 0 ) then 
        dbQuery ( connection, "INSERT INTO `drift` ( `data`, `account` ) VALUES ( ?, ? )", toJSON ( t ), accName ); -- if not creating 
    end 
end 
addEventHandler ( "onPlayerQuit", root, saveDriftPoints ); 
  
function loadDriftPoints ( _, account ) 
    if ( isGuestAccount ( account ) ) then -- checking if it is valid account 
        return; 
    end 
    
    local accName = getAccountName ( account ); -- getting name 
    
    local check = dbPoll ( dbQuery ( connection, "SELECT `data` FROM `drift` WHERE `account`=?", accName ), -1 ); -- checking if row exist 
    if ( #check > 0 ) then -- if yes 
        local d = check[1]; 
        
        local t = fromJSON ( d["data"] ); 
        if ( type ( t ) == "table" ) then 
            for k, v in pairs ( t ) do 
                setElementData ( source, k, v ); -- setting ElementData from saved drift's table in table 
            end 
        end 
    end 
end 
addEventHandler ( "onPlayerLogin", root, loadDriftPoints ); 

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...