· 8 years ago · Mar 19, 2018, 08:44 PM
1-- Written by Team Ulysses, http://ulyssesmod.net/
2module( "Utime", package.seeall )
3if not SERVER then return end
4
5utime_welcome = CreateConVar( "utime_welcome", "1", FCVAR_ARCHIVE )
6
7if not sql.TableExists( "utime" ) then
8 sql.Query( "CREATE TABLE IF NOT EXISTS utime ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, player INTEGER NOT NULL, totaltime INTEGER NOT NULL, lastvisit INTEGER NOT NULL );" )
9 sql.Query( "CREATE INDEX IDX_UTIME_PLAYER ON utime ( player DESC );" )
10end
11
12function onJoin( ply )
13 local uid = ply:UniqueID()
14 local row = sql.QueryRow( "SELECT totaltime, lastvisit FROM utime WHERE player = " .. uid .. ";" )
15 local time = 0
16
17 if row then
18 if utime_welcome:GetBool() then
19 ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
20 end
21 sql.Query( "UPDATE utime SET lastvisit = " .. os.time() .. " WHERE player = " .. uid .. ";" )
22 time = row.totaltime
23 else
24 if utime_welcome:GetBool() then
25 ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
26 end
27 sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. uid .. ", 0, " .. os.time() .. " );" )
28 end
29 ply:SetUTime( time )
30 ply:SetUTimeStart( CurTime() )
31end
32hook.Add( "PlayerInitialSpawn", "UTimeInitialSpawn", onJoin )
33
34function updatePlayer( ply )
35 sql.Query( "UPDATE utime SET totaltime = " .. math.floor( ply:GetUTimeTotalTime() ) .. " WHERE player = " .. ply:UniqueID() .. ";" )
36end
37hook.Add( "PlayerDisconnected", "UTimeDisconnect", updatePlayer )
38
39function updateAll()
40 local players = player.GetAll()
41
42 for _, ply in ipairs( players ) do
43 if ply and ply:IsConnected() then
44 updatePlayer( ply )
45 end
46 end
47end
48timer.Create( "UTimeTimer", 67, 0, updateAll )