Jump to content

[HELP] with time count sqlite


Dziugasc

Recommended Posts

so i have this code

    exports.scoreboard:addScoreboardColumn( 'time', root, 150 ) -- Online time is the element data
    local DB = dbConnect( "sqlite", "time.db" )
    addEventHandler("onResourceStart", resourceRoot, 
    	function ( ... )
    		dbExec( DB, "CREATE TABLE IF NOT EXISTS online( playerACC, weeks,days, hours, minutes, secs )")
    		for i, player in ipairs( getElementsByType("player") ) do
    			local accountN = getAccountName( getPlayerAccount( player ) )
    			local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
    			if ( #getData == 0 ) then 
    				setPlayerTime( player, 0, 0, 0, 0, 0 )
    			else 
    				for i, data in ipairs( getData ) do 
    					local weeks = data.weeks or 0
    					local days = data.days or 0
    					local hours = data.hours or 0 
    					local minutes = data.minutes or 0 
    					local seconds = data.secs or 0
    					setPlayerTime( player, weeks ,days, hours, minutes, seconds )
    				end
    			end
    			setTimer( updatePlayerTime, 1000, 0, player )
    		end
    	end)
    addEventHandler("onPlayerLogin", root, 
    	function ( ... )
    		local accountN = getAccountName( getPlayerAccount( source ) )
    		local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
    		if ( #getData == 0 ) then 
    			setPlayerTime( source, 0, 0, 0, 0, 0 )
    		else 
    			for i, data in ipairs( getData ) do 
				    local weeks = data.weeks or 0
    				local days = data.days or 0
    				local hours = data.hours or 0 
    				local minutes = data.minutes or 0 
    				local seconds = data.secs or 0
    				setPlayerTime( source, weeks ,days, hours, minutes, seconds )
    			end
    		end
    		setTimer( updatePlayerTime, 1000, 0, source )
    	end)
    function setPlayerTime( player, weeks ,days, hours, minutes, secs )
    	local accountN = getAccountName( getPlayerAccount( player ) ) 
    	if ( accountN ) then  
    		local dbe = dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN)
    		local result = dbPoll( dbe, -1 )
    		local realTime = table.concat{ weeks.."w ",days.."d "..hours.."h ", minutes.."min.", secs.."s "  }
			--", secs.."s "
    		setElementData( player, "time", realTime )
    		if ( #result == 0 ) then 
    			dbExec( DB, "INSERT INTO online( playerACC, weeks, days, hours, minutes, secs ) VALUES( ?, ?, ?, ?, ?, ? ) ", accountN, tonumber( weeks ), tonumber( days ), tonumber( hours ), tonumber( minutes ), tonumber( secs ) ) 
    		else 
    			dbExec( DB, "UPDATE online SET playerACC=?, weeks=?, days=?, hours=?, minutes=?, secs=?", accountN, tonumber( weeks ), tonumber( days ), tonumber( hours ), tonumber( minutes ), tonumber( secs) )
    		end
    	end
    end
    function updatePlayerTime( player )
    	local accountN = getAccountName( getPlayerAccount( player ) )
    	if ( accountN ) then 
    		local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
    		if ( getData ) then 
    			for index, data in ipairs( getData ) do 
    				data.secs = data.secs + 1
    				if ( data.secs == 5 ) then 
    					data.hours = data.hours + 1 
    					data.secs = 0 
    				elseif ( data.minutes == 60 ) then 
    					data.hours = data.hours + 1 
    					data.minutes = 0 
    				elseif ( data.hours == 24 ) then 
    					data.days = data.days + 1 
    					data.hours = 0 
    				elseif ( data.days == 7 ) then 
    					data.weeks = data.weeks + 1 
    					data.days = 0
    				end
    				setPlayerTime( player, data.weeks, data.days, data.hours, data.minutes, data.secs )
    			end
    		end 
    	end
    end

And i have 3problems and one question .

1st problem

WARNING: [gameplay]/killcount-scoreboard/server.lua 62. bad argument @ 'getAccountName' [Expected element at argument 1] [DUP x11] .

 

2nd problem that it converts seconds 60sec to 1min i tried changing it and it worked but it can't convert second third and fourth only seconds.

 

3rd problem that it saves data only to one account for example there is 3players it should load time every player but it load 1st only acc for example on sqlite it shows:

Dziugasc I 0weeks I 0days I 15hours I 35minutes I 45 seconds

Dziugasc I 0weeks I 0days I 15hours I 35minutes I 45 seconds

Dziugasc I 0weeks I 0days I 15hours I 35minutes I 45 seconds

 

And question how to make that it would show only time that player was online for example he was online only 1d and 18hours and on scoreboard it would show 1d 18h

in my scoreboard it shows 0w 0d 0h 15min. [I don't need 0 that way it would be nicer to look.

Edited by Dziugasc
code
Link to comment
  • Moderators
local data = {0, 2, 4, 0, 0}

local definition = {"w", "d", "h", "min", "sec"}

local theString = ""

for i=1, #data do
local item = data[i]
if item ~= 0 then
  theString = theString .. definition[i] .. item .. " "

end

end


print(theString)

Prototype.(mobile)

 

WARNING: [gameplay]/killcount-scoreboard/server.lua 62. bad argument @ 'getAccountName' [Expected element at argument 1] [DUP x11] .

This means that the variable you tried to get account data from isn't an element. Validate your data before using it.

Edited by IIYAMA
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...