· 6 years ago · Jul 19, 2019, 01:32 PM
1 exports.scoreboard:addScoreboardColumn( 'online time', root, 250, 'Online', 4 ) -- Online time is the element data
2 local DB = dbConnect( "sqlite", "time.db" )
3 addEventHandler("onResourceStart", resourceRoot,
4 function ( ... )
5 dbExec( DB, "CREATE TABLE IF NOT EXISTS online( playerACC, weeks,days, hours, minutes, seconds )")
6 for i, player in ipairs( getElementsByType("player") ) do
7 local accountN = getAccountName( getPlayerAccount( player ) )
8 local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
9 if ( #getData == 0 ) then
10 setPlayerTime( player, 0, 0, 0, 0, 0 )
11 else
12 for i, data in ipairs( getData ) do
13 local weeks = data.weeks or 0
14 local days = data.days or 0
15 local hours = data.hours or 0
16 local minutes = data.minutes or 0
17 local seconds = data.seconds or 0
18 setPlayerTime( player, weeks ,days, hours, minutes, seconds )
19 end
20 end
21 setTimer( updatePlayerTime, 1000, 0, player )
22 end
23 end)
24 addEventHandler("onPlayerLogin", root,
25 function ( ... )
26 local accountN = getAccountName( getPlayerAccount( source ) )
27 local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
28 if ( #getData == 0 ) then
29 setPlayerTime( source, 0, 0, 0, 0, 0 )
30 else
31 for i, data in ipairs( getData ) do
32 local weeks = data.weeks or 0
33 local days = data.days or 0
34 local hours = data.hours or 0
35 local minutes = data.minutes or 0
36 local seconds = data.seconds or 0
37 setPlayerTime( source, weeks ,days, hours, minutes, seconds )
38 end
39 end
40 setTimer( updatePlayerTime, 1000, 0, source )
41 end)
42 function setPlayerTime( player, weeks ,days, hours, minutes, seconds )
43 local accountN = getAccountName( getPlayerAccount( player ) )
44 if ( accountN ) then
45 local dbe = dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN)
46 local result = dbPoll( dbe, -1 )
47 local realTime = table.concat{ weeks.."w ",days.."d "..hours.."h ", minutes.."min.", seconds.."s " }
48 --", seconds.."s "
49 setElementData( player, "time", realTime )
50 if ( #result == 0 ) then
51 dbExec( DB, "INSERT INTO online( playerACC, weeks, days, hours, minutes, seconds ) VALUES( ?, ?, ?, ?, ?, ? ) ", accountN, tonumber( weeks ), tonumber( days ), tonumber( hours ), tonumber( minutes ), tonumber( seconds ) )
52 else
53 dbExec( DB, "UPDATE online SET playerACC=?, weeks=?, days=?, hours=?, minutes=?, seconds=?", accountN, tonumber( weeks ), tonumber( days ), tonumber( hours ), tonumber( minutes ), tonumber( seconds) )
54 end
55 end
56 end
57 function updatePlayerTime( player )
58 local accountN = getAccountName( getPlayerAccount( player ) )
59 if ( accountN ) then
60 local getData = dbPoll( dbQuery( DB, "SELECT * FROM online WHERE playerACC=?", accountN), -1 )
61 if ( getData ) then
62 for index, data in ipairs( getData ) do
63 data.seconds = data.seconds + 1
64 if ( data.seconds == 60 ) then
65 data.minutes = data.minutes + 1
66 data.seconds = 0
67 elseif ( data.minutes == 60 ) then
68 data.hours = data.hours + 1
69 data.minutes = 0
70 elseif ( data.hours == 24 ) then
71 data.days = data.days + 1
72 data.hours = 0
73 elseif ( data.days == 7 ) then
74 data.weeks = data.weeks + 1
75 data.days = 0
76 end
77 setPlayerTime( player, data.weeks, data.days, data.hours, data.minutes, data.seconds )
78 end
79 end
80 end
81 end