Jump to content

[HELP] Time spent in scoreboard


Dziugasc

Recommended Posts

so hello so i tried adding days and weeks but all i got was error and i can't figure way how to make days from hours and then weeks from days

code:

exports.scoreboard:addScoreboardColumn('time')

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,
                "time",
                tostring( hour )..'h '..tostring( min )..'min.'
            )
        end
    end,
    1000, 0
)
   
function onPlayerQuit ( )
    local playeraccount = getPlayerAccount ( source )
    if ( playeraccount ) and not isGuestAccount ( playeraccount ) then
        local sValue = getElementData( source,'time' )
	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, "time-hour", tostring(hour) )
        setAccountData ( playeraccount, "time-min", tostring(min) )
        setAccountData ( playeraccount, "time-sec", tostring(sec) )
        setAccountData ( playeraccount, "time", tostring(sValue) )

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

                             t[ source ]["hour"] = tonumber(hou)
                             t[ source ]["min"] = tonumber(min)
                             t[ source ]["sec"] = tonumber(sec)
                else
            setElementData ( source, "time",0 )
            setAccountData ( playeraccount, "time",0 )
        end
    end
end

addEventHandler ( "onPlayerQuit", root, onPlayerQuit )
addEventHandler ( "onPlayerLogin", root, onPlayerLogin )

 

Link to comment

https://pastebin.com/89wg7kz9

doesn't create scoreboard collum

    exports.scoreboard:addScoreboardColumn('time')
    function createTables()
        time = executeSQLQuery("CREATE TABLE IF NOT EXISTS time ( playerUser TEXT, playerName TEXT, time TEXT)")
        if ( time == false ) then
    	    outputDebugString( "*Error Table time" )
        else
    	    outputDebugString( "*Create/Load Table time" )
        end
    end
    addEventHandler ( "onResourceStart" , resourceRoot, createTables)

    function addDataValue ( player, data, value)
        local user = getAccountName( getPlayerAccount( player ) )
    	local selectUser = executeSQLQuery("SELECT * FROM time WHERE playerUser=?", user)
    	if #selectUser == 0 then return end
    	for _, info in ipairs( selectUser ) do
    		if data == "time" then
    		    local setValue = info.time + value
    		    executeSQLQuery("UPDATE `time` SET `time`=? WHERE `playerUser`=?", setValue, user)	
    		end	
    	end
    	refreshNowDatas(player)
    end

    function getDataValue ( player, data)
        local user = getAccountName( getPlayerAccount( player ) )
    	local selectUser = executeSQLQuery("SELECT * FROM time WHERE playerUser=?", user)
    	if #selectUser == 0 then return end
    	for _, info in ipairs( selectUser ) do
    		if data == "time" then
    		    return formatValue(info.time)
    		end	
    	end
    end

    function setDataValue ( player, data, value)
        local user = getAccountName( getPlayerAccount( player ) )
    	local selectUser = executeSQLQuery("SELECT * FROM time WHERE playerUser=?", user)
    	if #selectUser == 0 then return end
    	if data == "time" then
    	    executeSQLQuery("UPDATE `time` SET `time`=? WHERE `playerUser`=?", value, user)
    	end
    	refreshNowDatas(player)
    end

    function startDataValue( player)
        if isPlayerLogged(player) then
            local user = getAccountName( getPlayerAccount( player ) )
    	    local selectUser = executeSQLQuery("SELECT * FROM time WHERE playerUser=?", user)
    	    if #selectUser == 0 then
    	        executeSQLQuery("INSERT INTO `time`(`playerUser`,`playerName`,`time`) VALUES(?,?,?)", user, getPlayerName(player),"0","0","0")
    		    outputDebugString("New Data Added by "..string.gsub(getPlayerName(player), "(#%x%x%x%x%x%x)",""))
    	    end
    		setElementData(player,"playerOnline",getTickCount())
    		setColorInLogin(player)
    		setDataValue( player, "playerName", getPlayerName(player))
    		onRecallTeam( player )
    		loginDonator( player )
    		--getPlayerTeamStatus( player )
    		refreshNowDatas(player)
    	end
    end

    local msValues = {
        week = 607800000,
    	day = 86400000,
    	hour = 3600000,
    	minute = 60000,
    }

    function msToTimeString(playedTime)
    	if playedTime then
    	    local weeks = math.floor(playedTime/msValues.week)
    		local days = math.floor(playedTime/msValues.day)
    		local playedTime = playedTime - (msValues.day*days)
    		local hours = math.floor(playedTime/msValues.hour)
    		local playedTime = playedTime - (msValues.hour*hours)
    		local minutes = math.floor(playedTime/msValues.minute)
    		return weeks.." w, "..days.." d, "..hours.." h, "..minutes.." min."
    	end
    end

    function formatValue( value)
        return tonumber(string.format("%."..(0).."f",value))
    end

    function getRankingData( player, data )
    	local select = executeSQLQuery("SELECT * FROM time ")
    	local RankingData = {}
    	for _, info in ipairs( select ) do
    	    local getData = false
    		if data == "time" then getData = info.time end
            if info.playerName and getData then
    		    getData = formatValue(getData)
    		    table.insert( RankingData,{ name = info.playerName, data = getData})
    		end
    	table.sort(RankingData,function(a,b) return(tonumber(a.data) or 0 ) > ( tonumber(b.data) or 0 ) end )
    	if data == "time" then for i,data in ipairs (RankingData) do data.data = msToTimeString(data.data) end end
    	callClientFunction(player,"refreshRanking",RankingData)
    end

    function getPlayerAllData( player , getPlayer)
    	local user = getAccountName( getPlayerAccount( getPlayer ) )
    	local selectUser = executeSQLQuery("SELECT * FROM time WHERE playerUser=?", user)
    	local RankingData = {}
    	if #selectUser == 0 then return end
    	for _, info in ipairs( selectUser ) do
    	    local name = info.playerName
    		local time = msToTimeString(info.time)
    		table.insert( RankingData,{ name, time})
    		callClientFunction(player,"refreshPlayerStats",RankingData)
           end
       end
    end

 

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