Dziugasc

[HELP] Problem in Playtime Scoreboard

Recommended Posts

Hello, so I got this Playtime script and problem is that it counts time only for 1player and I cant find any solution to it :( 

Server.lua

Spoiler

exports.scoreboard:addScoreboardColumn( 'Online time', root, 200 ) -- 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, hour, minutes, seconds )")
		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.hour or 0 
					local minutes = data.minutes or 0 
					local seconds = data.seconds 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 )
		else 
			for i, data in ipairs( getData ) do 
			    local weeks = data.weeks or 0
				local days = data.days or 0
				local hours = data.hour or 0 
				local minutes = data.minutes or 0 
				local seconds = data.seconds or 0
				setPlayerTime( source, weeks, days, hours, minutes, seconds )
			end
		end
		setTimer( updatePlayerTime, 1000, 0, source )

	end)

function setPlayerTime( player, weeks, days, hour, minutes, seconds )
	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 "..hour.."h ", minutes.."m ", seconds.."s " }
		setElementData( player, "Online time", realTime )
		if ( #result == 0 ) then 
			dbExec( DB, "INSERT INTO online( playerACC, weeks, days, hour, minutes, seconds ) VALUES( ?, ?, ?, ?, ? ) ", accountN, tonumber( weeks ), tonumber( days ), tonumber( hour ), tonumber( minutes ), tonumber( seconds ) ) 
		else 
			dbExec( DB, "UPDATE online SET playerACC=?, weeks=?, days=?, hour=?, minutes=?, seconds=?", accountN, tonumber( weeks ), tonumber( days ), tonumber( hour ), tonumber( minutes ), tonumber( seconds) )
		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.seconds = data.seconds + 1 
				if ( data.seconds == 60 ) then 
					data.minutes = data.minutes + 1 
					data.seconds = 0 
				elseif ( data.minutes == 60 ) then 
					data.hour = data.hour + 1 
					data.minutes = 0 
				elseif ( data.hour == 24 ) then 
					data.days = data.days + 1 
					data.hour = 0 
				elseif ( data.days == 7 ) then 
					data.weeks = data.weeks + 1 
					data.days = 0 
				end
				setPlayerTime( player, data.weeks, data.days, data.hour, data.minutes, data.seconds )
			end
		end 
	end
end

 

 

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.