Jump to content

[QUESTION] How can I get players played time?


Tokio

Recommended Posts

i have this script:

exports.scoreboard:addScoreboardColumn('Játszott idő')

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 ][ 'hours' ] = tonumber( t[ source ][ 'hours' ] or 0 ) + 1
    end
    return arg1, arg2
end
     
setTimer(
    function( )
        for _, v in pairs( getElementsByType( "player" ) ) do
            if (not t[ v ]) then
                t[ v ] = {
                            ["hours"] = 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 hours = tonumber( t[ v ][ 'hours' ] or 0 )

            setElementData(
                v,
                "Játszott idő",
				tostring( hours )..' óra '..tostring( min )..' perc'
            )
        end
    end,
    1000, 0
)
   
function onPlayerQuit ( )
    local playeraccount = getPlayerAccount ( source )
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
        local sValue = getElementData( source,'Játszott idő' )
	local hours = tonumber( t[ source ][ 'hours' ] or 0 )
	local min = tonumber( t[ source ][ 'min' ] or 0 )
	local sec = tonumber( t[ source ][ 'sec' ] or 0 )
        setAccountData ( playeraccount, "Játszott idő-hours", tostring(hours) )
        setAccountData ( playeraccount, "Játszott idő-min", tostring(min) )
        setAccountData ( playeraccount, "Játszott idő-sec", tostring(sec) )
        setAccountData ( playeraccount, "Játszott idő", tostring(sValue) )

    end
    t[ source ] = nil
end
 
function onPlayerLogin (_, playeraccount )
    if ( playeraccount ) then
        local time = getAccountData ( playeraccount, "Játszott idő" )
	local hou = getAccountData ( playeraccount, "Játszott idő-hours")
	local min = getAccountData ( playeraccount, "Játszott idő-min")
	local sec = getAccountData ( playeraccount, "Játszott idő-sec")
        if ( time ) then
            setElementData ( source, "Játszott idő", time )

                             t[ source ]["hours"] = tonumber(hou)
                             t[ source ]["min"] = tonumber(min)
                             t[ source ]["sec"] = tonumber(sec)
                else
            setElementData ( source, "Játszott idő",0 )
            setAccountData ( playeraccount, "Játszott idő",0 )
        end
    end
end
addEventHandler ( "onPlayerQuit", root, onPlayerQuit )
addEventHandler ( "onPlayerLogin", root, onPlayerLogin )

and i want add a function, what get players played time, and when reach xy played time (for example 30), then do something (for example output a text to chat box)

How is it possible?

Link to comment
do
	exports.scoreboard:addScoreboardColumn("Játszott idő", getRootElement(), 11, 48)
end
-------------------------------------------------------------------------------------------------------------------------
local t = {}
function check_playtime()
    for i,player in pairs(getElementsByType("player")) do

        if not t[player] then
            t[player] = {["hour"] = 0,["min"] = 0,["sec"] = 0}
        end
        
        local seconds = t[player]['sec'] or 0
        local minutes = t[player]['min'] or 0
        local hours = t[player]['hour'] or 0
        do
            seconds = seconds + 1
            if seconds >= 60 then
                seconds = 0
                minutes = minutes + 1
            end

            if minutes >= 60 then
               	minutes = 0
		hours = hours + 1
		end
        end

        t[player]['sec'] = seconds
        t[player]['min'] = minutes
        t[player]['hour'] = hours
		
        local playtime_string = string.format("%d:%02d:%02d", hours, minutes, seconds)
        if playtime_string then
            setElementData(player, "Játszott idő", playtime_string)
        end
		
		if minutes == 30 then
			outputChatBox("You've played for 30 mins.", player, 255, 255, 255, true)
		end
    end
end
setTimer(check_playtime, 1000, 0)

 

Link to comment
12 minutes ago, koragg said:

do
	exports.scoreboard:addScoreboardColumn("Játszott idő", getRootElement(), 11, 48)
end
-------------------------------------------------------------------------------------------------------------------------
local t = {}
function check_playtime()
    for i,player in pairs(getElementsByType("player")) do

        if not t[player] then
            t[player] = {["hour"] = 0,["min"] = 0,["sec"] = 0}
        end
        
        local seconds = t[player]['sec'] or 0
        local minutes = t[player]['min'] or 0
        local hours = t[player]['hour'] or 0
        do
            seconds = seconds + 1
            if seconds >= 60 then
                seconds = 0
                minutes = minutes + 1
            end

            if minutes >= 60 then
               	minutes = 0
		hours = hours + 1
		end
        end

        t[player]['sec'] = seconds
        t[player]['min'] = minutes
        t[player]['hour'] = hours
		
        local playtime_string = string.format("%d:%02d:%02d", hours, minutes, seconds)
        if playtime_string then
            setElementData(player, "Játszott idő", playtime_string)
        end
		
		if minutes == 30 then
			outputChatBox("You've played for 30 mins.", player, 255, 255, 255, true)
		end
    end
end
setTimer(check_playtime, 1000, 0)

 

doesn't working :S 

Link to comment
10 hours ago, koragg said:

server-side in meta.xml

this why not working?

		local playtime_string = string.format("%d:%02d:%02d", hours, minutes, seconds)
		if playtime_string then
			setElementData(player, "Játszott idő", tostring( hours )..' óra '..tostring( minutes )..' perc')
		end

 

Link to comment
7 minutes ago, Dimos7 said:

		local playtime_string = string.format("%02d:%02d:%02d", hours, minutes, seconds)
		if playtime_string then
			setElementData(player, "Játszott idő", tostring( hours )..' óra '..tostring( minutes )..' perc')
		end

 

but the outputchatbox function still not working :S 

Link to comment

Anyway, the script I gave doesn't save playtime to account, sorry didn't scroll down to see the rest of your script xD This saves to account and you can do a simple 'if' check to get the hours, minutes, seconds and do what you want in that 'if' block.

---Playtime Account Saving
 
addEventHandler("onResourceStart" , resourceRoot ,
    function()
        for index , player in ipairs(getElementsByType("player")) do
            local pAccount = getPlayerAccount ( player )
            if not isGuestAccount(pAccount) then
				local seconds = getAccountData(pAccount , "seconds")
                local minutes = getAccountData(pAccount , "minutes")
                if minutes and seconds then
                    local hours = getAccountData(pAccount , "hours")
                    if # tostring(minutes) == 1 then
                        minutes = "0" .. minutes
                    end
                    if # tostring(seconds) == 1 then
                        seconds = "0" .. seconds
                    end
                    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
                    setElementData(player , "Online.timer" , timer)
                else
					setAccountData(pAccount , "seconds" , 0)
                    setAccountData(pAccount , "minutes" , 0)
					setAccountData(pAccount , "hours" , 0)
                    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
                    setElementData(player , "Online.timer" , timer)
                end
            end
        end
    end
)
 
addEventHandler("onResourceStop" , resourceRoot ,
    function()
        for index , player in ipairs(getElementsByType("player")) do
            local pAccount = getPlayerAccount(player)
            if not isGuestAccount(pAccount) then
                local timer = getElementData(player , "Online.timer")
                if isTimer(timer) then
                    killTimer(timer)
                end
            end
        end
    end
)
 
addEventHandler("onPlayerLogin" , root ,
    function(_ , pAccount)
		local seconds = getAccountData(pAccount , "seconds")
        local minutes = getAccountData(pAccount , "minutes")
        if minutes and seconds then
            local hours = getAccountData(pAccount , "hours")
            if # tostring(minutes) == 1 then
                minutes = "0" .. minutes
            end
            if # tostring(seconds) == 1 then
                seconds = "0" .. seconds
            end
            local timer = setTimer(actualizarJugadorOn , 1000 , 1 , source)
            setElementData(source , "Online.timer" , timer)
        else
			setAccountData(pAccount , "seconds" , 0)
            setAccountData(pAccount , "minutes" , 0)
            setAccountData(pAccount , "hours" , 0)
            local timer = setTimer(actualizarJugadorOn , 1000 , 1 , source)
            setElementData (source , "Online.timer" , timer)
        end
    end
)
 
addEventHandler("onPlayerLogout" , root ,
    function(pAccount)
        local timer = getElementData(source , "Online.timer")
        if isTimer(timer) then
            killTimer(timer)
        end
    end
)
 
addEventHandler("onPlayerQuit" , root ,
    function()
        local pAccount = getPlayerAccount(source)
        if not isGuestAccount(pAccount) then
            local timer = getElementData(source , "Online.timer")
            if isTimer(timer) then
                killTimer(timer)
            end
        end
    end
)
 
function actualizarJugadorOn(player)
    local pAccount = getPlayerAccount(player)
	local seconds = getAccountData(pAccount , "seconds")
    local minutes = getAccountData(pAccount , "minutes")
    local hours = getAccountData(pAccount , "hours")
	seconds = tostring(tonumber(seconds)+1)
	if seconds == "60" then
		minutes = tostring(tonumber(minutes)+1)
		seconds = "00"
	end
	
    if minutes == "60" then
        hours = tostring(tonumber(hours )+1)
        minutes = "00"
    end
	setAccountData(pAccount , "seconds" , tonumber(seconds))
    setAccountData(pAccount , "minutes" , tonumber(minutes))
    setAccountData(pAccount , "hours" , tonumber(hours))
    if # tostring(minutes) == 1 then minutes = "0" .. minutes end
    if # tostring(seconds) == 1 then seconds = "0" .. seconds end
    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
    setElementData(player , "Online.timer" , timer)
end

 

  • Like 1
Link to comment
8 minutes ago, koragg said:

Anyway, the script I gave doesn't save playtime to account, sorry didn't scroll down to see the rest of your script xD This saves to account and you can do a simple 'if' check to get the hours, minutes, seconds and do what you want in that 'if' block.


---Playtime Account Saving
 
addEventHandler("onResourceStart" , resourceRoot ,
    function()
        for index , player in ipairs(getElementsByType("player")) do
            local pAccount = getPlayerAccount ( player )
            if not isGuestAccount(pAccount) then
				local seconds = getAccountData(pAccount , "seconds")
                local minutes = getAccountData(pAccount , "minutes")
                if minutes and seconds then
                    local hours = getAccountData(pAccount , "hours")
                    if # tostring(minutes) == 1 then
                        minutes = "0" .. minutes
                    end
                    if # tostring(seconds) == 1 then
                        seconds = "0" .. seconds
                    end
                    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
                    setElementData(player , "Online.timer" , timer)
                else
					setAccountData(pAccount , "seconds" , 0)
                    setAccountData(pAccount , "minutes" , 0)
					setAccountData(pAccount , "hours" , 0)
                    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
                    setElementData(player , "Online.timer" , timer)
                end
            end
        end
    end
)
 
addEventHandler("onResourceStop" , resourceRoot ,
    function()
        for index , player in ipairs(getElementsByType("player")) do
            local pAccount = getPlayerAccount(player)
            if not isGuestAccount(pAccount) then
                local timer = getElementData(player , "Online.timer")
                if isTimer(timer) then
                    killTimer(timer)
                end
            end
        end
    end
)
 
addEventHandler("onPlayerLogin" , root ,
    function(_ , pAccount)
		local seconds = getAccountData(pAccount , "seconds")
        local minutes = getAccountData(pAccount , "minutes")
        if minutes and seconds then
            local hours = getAccountData(pAccount , "hours")
            if # tostring(minutes) == 1 then
                minutes = "0" .. minutes
            end
            if # tostring(seconds) == 1 then
                seconds = "0" .. seconds
            end
            local timer = setTimer(actualizarJugadorOn , 1000 , 1 , source)
            setElementData(source , "Online.timer" , timer)
        else
			setAccountData(pAccount , "seconds" , 0)
            setAccountData(pAccount , "minutes" , 0)
            setAccountData(pAccount , "hours" , 0)
            local timer = setTimer(actualizarJugadorOn , 1000 , 1 , source)
            setElementData (source , "Online.timer" , timer)
        end
    end
)
 
addEventHandler("onPlayerLogout" , root ,
    function(pAccount)
        local timer = getElementData(source , "Online.timer")
        if isTimer(timer) then
            killTimer(timer)
        end
    end
)
 
addEventHandler("onPlayerQuit" , root ,
    function()
        local pAccount = getPlayerAccount(source)
        if not isGuestAccount(pAccount) then
            local timer = getElementData(source , "Online.timer")
            if isTimer(timer) then
                killTimer(timer)
            end
        end
    end
)
 
function actualizarJugadorOn(player)
    local pAccount = getPlayerAccount(player)
	local seconds = getAccountData(pAccount , "seconds")
    local minutes = getAccountData(pAccount , "minutes")
    local hours = getAccountData(pAccount , "hours")
	seconds = tostring(tonumber(seconds)+1)
	if seconds == "60" then
		minutes = tostring(tonumber(minutes)+1)
		seconds = "00"
	end
	
    if minutes == "60" then
        hours = tostring(tonumber(hours )+1)
        minutes = "00"
    end
	setAccountData(pAccount , "seconds" , tonumber(seconds))
    setAccountData(pAccount , "minutes" , tonumber(minutes))
    setAccountData(pAccount , "hours" , tonumber(hours))
    if # tostring(minutes) == 1 then minutes = "0" .. minutes end
    if # tostring(seconds) == 1 then seconds = "0" .. seconds end
    local timer = setTimer(actualizarJugadorOn , 1000 , 1 , player)
    setElementData(player , "Online.timer" , timer)
end

 

Why not show in scoreboard?

Link to comment
1 hour ago, 50cent said:

Why not show in scoreboard?

Because it needs this:

exports.scoreboard:addScoreboardColumn("Playtime", getRootElement(), 11, 48)

I just gave you an example script which saves playtime to player account. You'll need to add the following in the script to put a scoreboard column:

function onResStart()
	exports.scoreboard:addScoreboardColumn("Playtime", root, 11, 48)
end
addEventHandler("onResourceStart", root, onResStart)

And then set the content of the newly created column with 

local playtime_string = string.format("%d:%02d:%02d", hours, minutes, seconds)
if playtime_string then
	setElementData(player, "Playtime", playtime_string)
end

at the end of the "actualizarJugadorOn" function.

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