LimoN>w<

[AYUDA] Guardar datos del resource de tiempo en el server

Recommended Posts

Buenas, yo tengo el resource de tiempo en el servidor, el cual guarda el tiempo total que el jugador tiene en el servidor (se haya desconectado o no)

el problema es cuando se trata de darle stop o restart al resource, ya que el mismo una vez parado el resource o reiniciado borra todos los datos y se pierden

Obviamente el stop lo he hecho porque he tenido problemas con el scoreboard ya que se ha bugeado y el tiempo en el servidor no aparecía y tenia que reiniciar el resource ocasionando que las horas de los jugadores se perdieran

Si alguien me puede ayudar le agredeceria muchísimo!

Dejo el codigo lua debajo

exports.scoreboard:addScoreboardColumn('Online')

local t = { }
 
function checkValues( source,arg1,arg2)
    if (arg2 >= 60) then
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1
        t[ source ][ 'sec' ] = 0
    end
    if (arg1 >= 60) then
        t[ source ][ 'min' ] = 0
        t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1
    end
    return arg1, arg2
end
     
setTimer(
    function( )
        for _, v in pairs( getElementsByType( "player" ) ) do
            if (not t[ v ]) then
                t[ v ] = {
                            ["hour"] = 0,
                             ["min"] = 0,
                             ["sec"] = 0
                            }
            end
 
            t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1
            local min,sec = checkValues (
                    v,
                    t[ v ][ 'min' ] or 0,
                    t[ v ][ 'sec' ] or 0
						)  
	local hour = tonumber( t[ v ][ 'hour' ] or 0 )

            setElementData(
                v,
                "Online",
                tostring( hour )..':'..tostring( min )..':'..tostring( sec )
            )
        end
    end,
    1000, 0
)
   
function onPlayerQuit ( )
    local playeraccount = getPlayerAccount ( source )
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
        local sValue = getElementData( source,'Online' )
	local hour = tonumber( t[ source ][ 'hour' ] or 0 )
	local min = tonumber( t[ source ][ 'min' ] or 0 )
	local sec = tonumber( t[ source ][ 'sec' ] or 0 )
        setAccountData ( playeraccount, "Online-hour", tostring(hour) )
        setAccountData ( playeraccount, "Online-min", tostring(min) )
        setAccountData ( playeraccount, "Online-sec", tostring(sec) )
        setAccountData ( playeraccount, "Online", tostring(sValue) )

    end
    t[ source ] = nil
end
 
function onPlayerLogin (_, playeraccount )
    if ( playeraccount ) then
        local time = getAccountData ( playeraccount, "Online" )
	local hou = getAccountData ( playeraccount, "Online-hour")
	local min = getAccountData ( playeraccount, "Online-min")
	local sec = getAccountData ( playeraccount, "Online-sec")
        if ( time ) then
            setElementData ( source, "Online", time )

                             t[ source ]["hour"] = tonumber(hou)
                             t[ source ]["min"] = tonumber(min)
                             t[ source ]["sec"] = tonumber(sec)
                else
            setElementData ( source, "Online",0 )
            setAccountData ( playeraccount, "Online",0 )
        end
    end
end
addEventHandler ( "onPlayerQuit", root, onPlayerQuit )
addEventHandler ( "onPlayerLogin", root, onPlayerLogin )

Share this post


Link to post

Lo que pasa es que el evento de 'OnResourceStop' no esta controlado. Algo por el estilo debería valerte.

local function turn_off()
	local players = getElementsByType( 'player' )
  
  	for player_id = 1, #players do
    	local player = players[ player_id ]
    
    	local player_account = getPlayerAccount( player )
    
    	if ( ( player_account ) and not ( isGuestAccount( player_account ) ) ) then
      		local time_data = getElementData( player, 'Online' )
      		local online_keys = { { 'hour', 'Online-hour' }, { 'min', 'Online-min' }, { 'sec', 'Online-sec' }, 'Online' }
      		for i = 1, #online_keys do
        		if ( i == #online_keys ) then
          			setAccountData( player_account, online_keys[ i ], time_data )
          		else
          			setAccountData( player_account, online_keys[ i ][ 2 ], time_data[ i ][ 1 ] or 0 )
          		end
        	end
      	end
  	end
end
addEventHandler( "onResourceStop", resourceRoot, turn_off )

 

Share this post


Link to post
14 minutes ago, Simple01 said:

Lo que pasa es que el evento de 'OnResourceStop' no esta controlado. Algo por el estilo debería valerte.


local function turn_off()
	local players = getElementsByType( 'player' )
  
  	for player_id = 1, #players do
    	local player = players[ player_id ]
    
    	local player_account = getPlayerAccount( player )
    
    	if ( ( player_account ) and not ( isGuestAccount( player_account ) ) ) then
      		local time_data = getElementData( player, 'Online' )
      		local online_keys = { { 'hour', 'Online-hour' }, { 'min', 'Online-min' }, { 'sec', 'Online-sec' }, 'Online' }
      		for i = 1, #online_keys do
        		if ( i == #online_keys ) then
          			setAccountData( player_account, online_keys[ i ], time_data )
          		else
          			setAccountData( player_account, online_keys[ i ][ 2 ], time_data[ i ][ 1 ] or 0 )
          		end
        	end
      	end
  	end
end
addEventHandler( "onResourceStop", resourceRoot, turn_off )

 

Eso lo agrego despues de la ultima linea del lua del resource original?

Share this post


Link to post

Yo lo haría así es más fácil xD

 

exports.scoreboard:addScoreboardColumn('Online')

local t = { }
 
function checkValues( source,arg1,arg2)
    if (arg2 >= 60) then
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1
        t[ source ][ 'sec' ] = 0
    end
    if (arg1 >= 60) then
        t[ source ][ 'min' ] = 0
        t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1
    end
    return arg1, arg2
end
     
setTimer(
    function( )
        for _, v in pairs( getElementsByType( "player" ) ) do
            if (not t[ v ]) then
                t[ v ] = {
                            ["hour"] = 0,
                             ["min"] = 0,
                             ["sec"] = 0
                            }
            end
 
            t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1
            local min,sec = checkValues (
                    v,
                    t[ v ][ 'min' ] or 0,
                    t[ v ][ 'sec' ] or 0
						)  
	local hour = tonumber( t[ v ][ 'hour' ] or 0 )

            setElementData(
                v,
                "Online",
                tostring( hour )..':'..tostring( min )..':'..tostring( sec )
            )
        end
    end,
    1000, 0
)
   
function onPlayerQuit ( )
    local playeraccount = getPlayerAccount ( source )
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
        local sValue = getElementData( source,'Online' )
	local hour = tonumber( t[ source ][ 'hour' ] or 0 )
	local min = tonumber( t[ source ][ 'min' ] or 0 )
	local sec = tonumber( t[ source ][ 'sec' ] or 0 )
        setAccountData ( playeraccount, "Online-hour", tostring(hour) )
        setAccountData ( playeraccount, "Online-min", tostring(min) )
        setAccountData ( playeraccount, "Online-sec", tostring(sec) )
        setAccountData ( playeraccount, "Online", tostring(sValue) )

    end
    t[ source ] = nil
end
addEvent ("stopSave", true)
addEventHandler ("stopSave", getRootElement(), onQuit)
 
function onPlayerLogin (_, playeraccount )
    if ( playeraccount ) then
        local time = getAccountData ( playeraccount, "Online" )
	local hou = getAccountData ( playeraccount, "Online-hour")
	local min = getAccountData ( playeraccount, "Online-min")
	local sec = getAccountData ( playeraccount, "Online-sec")
        if ( time ) then
            setElementData ( source, "Online", time )

                             t[ source ]["hour"] = tonumber(hou)
                             t[ source ]["min"] = tonumber(min)
                             t[ source ]["sec"] = tonumber(sec)
                else
            setElementData ( source, "Online",0 )
            setAccountData ( playeraccount, "Online",0 )
        end
    end
end
addEventHandler ( "onPlayerQuit", root, onPlayerQuit )
addEventHandler ( "onPlayerLogin", root, onPlayerLogin )


function onStop ()
    for k, v in ipairs (getElementsByType("player")) do
        if not (isGuestAccount (getPlayerAccount (v))) then
            triggerEvent ("stopSave", v)
        end
    end
end
addEventHandler ("onResourceStop", resourceRoot, onStop)

 

Share this post


Link to post
2 hours ago, DostMaster said:

Yo lo haría así es más fácil xD

 


exports.scoreboard:addScoreboardColumn('Online')

local t = { }
 
function checkValues( source,arg1,arg2)
    if (arg2 >= 60) then
        t[ source ][ 'min' ] = tonumber( t[ source ][ 'min' ] or 0 ) + 1
        t[ source ][ 'sec' ] = 0
    end
    if (arg1 >= 60) then
        t[ source ][ 'min' ] = 0
        t[ source ][ 'hour' ] = tonumber( t[ source ][ 'hour' ] or 0 ) + 1
    end
    return arg1, arg2
end
     
setTimer(
    function( )
        for _, v in pairs( getElementsByType( "player" ) ) do
            if (not t[ v ]) then
                t[ v ] = {
                            ["hour"] = 0,
                             ["min"] = 0,
                             ["sec"] = 0
                            }
            end
 
            t[ v ][ 'sec' ] = tonumber( t[ v ][ 'sec' ] or 0 ) + 1
            local min,sec = checkValues (
                    v,
                    t[ v ][ 'min' ] or 0,
                    t[ v ][ 'sec' ] or 0
						)  
	local hour = tonumber( t[ v ][ 'hour' ] or 0 )

            setElementData(
                v,
                "Online",
                tostring( hour )..':'..tostring( min )..':'..tostring( sec )
            )
        end
    end,
    1000, 0
)
   
function onPlayerQuit ( )
    local playeraccount = getPlayerAccount ( source )
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
        local sValue = getElementData( source,'Online' )
	local hour = tonumber( t[ source ][ 'hour' ] or 0 )
	local min = tonumber( t[ source ][ 'min' ] or 0 )
	local sec = tonumber( t[ source ][ 'sec' ] or 0 )
        setAccountData ( playeraccount, "Online-hour", tostring(hour) )
        setAccountData ( playeraccount, "Online-min", tostring(min) )
        setAccountData ( playeraccount, "Online-sec", tostring(sec) )
        setAccountData ( playeraccount, "Online", tostring(sValue) )

    end
    t[ source ] = nil
end
addEvent ("stopSave", true)
addEventHandler ("stopSave", getRootElement(), onQuit)
 
function onPlayerLogin (_, playeraccount )
    if ( playeraccount ) then
        local time = getAccountData ( playeraccount, "Online" )
	local hou = getAccountData ( playeraccount, "Online-hour")
	local min = getAccountData ( playeraccount, "Online-min")
	local sec = getAccountData ( playeraccount, "Online-sec")
        if ( time ) then
            setElementData ( source, "Online", time )

                             t[ source ]["hour"] = tonumber(hou)
                             t[ source ]["min"] = tonumber(min)
                             t[ source ]["sec"] = tonumber(sec)
                else
            setElementData ( source, "Online",0 )
            setAccountData ( playeraccount, "Online",0 )
        end
    end
end
addEventHandler ( "onPlayerQuit", root, onPlayerQuit )
addEventHandler ( "onPlayerLogin", root, onPlayerLogin )


function onStop ()
    for k, v in ipairs (getElementsByType("player")) do
        if not (isGuestAccount (getPlayerAccount (v))) then
            triggerEvent ("stopSave", v)
        end
    end
end
addEventHandler ("onResourceStop", resourceRoot, onStop)

 

Eso te digo que no funcionaría, y por una razón muy sencilla.

Share this post


Link to post
4 hours ago, Simple01 said:

Eso te digo que no funcionaría, y por una razón muy sencilla.

Pues la verdad que ami me ha funcionado siempre

Share this post


Link to post
20 hours ago, Simple01 said:

Donde quieras, con que este en el resource situado en el server-side, debería ir bien.

Lo puse debajo toda la linea que me pasaron y al stopear el resource los datos siguen sin guardarse 

Share this post


Link to post
6 hours ago, LimoN>w< said:

Lo puse debajo toda la linea que me pasaron y al stopear el resource los datos siguen sin guardarse 

Asegurate de que el valor que tienen en el elementData 'Online' es el deseado. Ya que se puede deber a que dicha key almacene un valor núlo.

Share this post


Link to post

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.