Amazing what a quick google search can do.
Place in lua/autorun
cl_time.lua
module( "Utime", package.seeall )
if not CLIENT then return end
local gpanel
--Now convars!
local utime_enable = CreateClientConVar( "utime_enable", "1.0", true, false )
local utime_outsidecolor_r = CreateClientConVar( "utime_outsidecolor_r", "256.0", true, false )
local utime_outsidecolor_g = CreateClientConVar( "utime_outsidecolor_g", "256.0", true, false )
local utime_outsidecolor_b = CreateClientConVar( "utime_outsidecolor_b", "256.0", true, false )
local utime_outsidetext_r = CreateClientConVar( "utime_outsidetext_r", "0.0", true, false )
local utime_outsidetext_g = CreateClientConVar( "utime_outsidetext_g", "0.0", true, false )
local utime_outsidetext_b = CreateClientConVar( "utime_outsidetext_b", "0.0", true, false )
local utime_insidecolor_r = CreateClientConVar( "utime_insidecolor_r", "256.0", true, false )
local utime_insidecolor_g = CreateClientConVar( "utime_insidecolor_g", "256.0", true, false )
local utime_insidecolor_b = CreateClientConVar( "utime_insidecolor_b", "256.0", true, false )
local utime_insidetext_r = CreateClientConVar( "utime_insidetext_r", "0", true, false )
local utime_insidetext_g = CreateClientConVar( "utime_insidetext_g", "0", true, false )
local utime_insidetext_b = CreateClientConVar( "utime_insidetext_b", "0", true, false )
local utime_pos_x = CreateClientConVar( "utime_pos_x", "0.0", true, false )
local utime_pos_y = CreateClientConVar( "utime_pos_y", "0.0", true, false )
local PANEL = {}
PANEL.Small = 40
PANEL.TargetSize = PANEL.Small
PANEL.Large = 100
PANEL.Wide = 160
function initialize()
gpanel = vgui.Create( "UTimeMain" )
gpanel:SetSize( gpanel.Wide, gpanel.Small )
hook.Remove( "OnEntityCreated", "UtimeInitialize" )
end
hook.Add( "InitPostEntity", "UtimeInitialize", initialize )
function think()
if not LocalPlayer():IsValid() then return end
if not utime_enable:GetBool() or not IsValid( LocalPlayer() ) or
(IsValid( LocalPlayer():GetActiveWeapon() ) and LocalPlayer():GetActiveWeapon():GetClass() == "gmod_camera") then
gpanel:SetVisible( false )
else
gpanel:SetVisible( true )
end
--gpanel:SetPos( ScrW() - gpanel:GetWide() - 20, 20 )
gpanel:SetPos( (ScrW() - gpanel:GetWide()) * utime_pos_x:GetFloat() / 100, (ScrH() - gpanel.Large) * utime_pos_y:GetFloat() / 100 )
local textColor = Color( utime_outsidetext_r:GetInt(), utime_outsidetext_g:GetInt(), utime_outsidetext_b:GetInt(), 255 )
gpanel.lblTotalTime:SetTextColor( textColor )
gpanel.lblSessionTime:SetTextColor( textColor )
gpanel.total:SetTextColor( textColor )
gpanel.session:SetTextColor( textColor )
local insideTextColor = Color( utime_insidetext_r:GetInt(), utime_insidetext_g:GetInt(), utime_insidetext_b:GetInt(), 255 )
gpanel.playerInfo.lblTotalTime:SetTextColor( insideTextColor )
gpanel.playerInfo.lblSessionTime:SetTextColor( insideTextColor )
gpanel.playerInfo.lblNick:SetTextColor( insideTextColor )
gpanel.playerInfo.total:SetTextColor( insideTextColor )
gpanel.playerInfo.session:SetTextColor( insideTextColor )
gpanel.playerInfo.nick:SetTextColor( insideTextColor )
end
timer.Create( "UTimeThink", 0.6, 0, think )
local texGradient = surface.GetTextureID( "gui/center_gradient" )
--PANEL.InnerColor = Color( 250, 250, 245, 255 )
--PANEL.OuterColor = Color( 0, 150, 245, 200 )
-----------------------------------------------------------
-- Name: Paint
-----------------------------------------------------------
function PANEL:Paint(w,h)
local wide = self:GetWide()
local tall = self:GetTall()
local outerColor = Color( utime_outsidecolor_r:GetInt(), utime_outsidecolor_g:GetInt(), utime_outsidecolor_b:GetInt(), 200 )
draw****undedBox( 4, 0, 0, wide, tall, outerColor ) -- Draw our base
surface.SetTexture( texGradient )
surface.SetDrawColor( 255, 255, 255, 50 )
surface.SetDrawColor( outerColor )
surface.DrawTexturedRect( 0, 0, wide, tall ) -- Draw gradient overlay
if self:GetTall() > self.Small + 4 then -- Draw the white background for another player's info
local innerColor = Color( utime_insidecolor_r:GetInt(), utime_insidecolor_g:GetInt(), utime_insidecolor_b:GetInt(), 255 )
draw****undedBox( 4, 2, self.Small, wide - 4, tall - self.Small - 2, innerColor )
surface.SetTexture( texGradient )
surface.SetDrawColor( color_white )
surface.SetDrawColor( innerColor )
surface.DrawTexturedRect( 2, self.Small, wide - 4, tall - self.Small - 2 ) -- Gradient overlay
end
return true
end
-----------------------------------------------------------
-- Name: Init
-----------------------------------------------------------
function PANEL:Init()
self.Size = self.Small
self.playerInfo = vgui.Create( "UTimePlayerInfo", self )
self.lblTotalTime = vgui.Create( "DLabel", self )
self.lblSessionTime = vgui.Create( "DLabel", self )
self.total = vgui.Create( "DLabel", self )
self.session = vgui.Create( "DLabel", self )
end
-----------------------------------------------------------
-- Name: ApplySchemeSettings
-----------------------------------------------------------
function PANEL:ApplySchemeSettings()
self.lblTotalTime:SetFont( "DermaDefault" )
self.lblSessionTime:SetFont( "DermaDefault" )
self.total:SetFont( "DermaDefault" )
self.session:SetFont( "DermaDefault" )
self.lblTotalTime:SetTextColor( color_black )
self.lblSessionTime:SetTextColor( color_black )
self.total:SetTextColor( color_black )
self.session:SetTextColor( color_black )
end
-----------------------------------------------------------
-- Name: Think
-----------------------------------------------------------
local locktime = 0
function PANEL:Think()
if self.Size == self.Small then
self.playerInfo:SetVisible( false )
else
self.playerInfo:SetVisible( true )
end
local tr = util.GetPlayerTrace( LocalPlayer(), LocalPlayer():GetAimVector() )
local trace = util.TraceLine( tr )
if trace.Entity and trace.Entity:IsValid() and trace.Entity:IsPlayer() then
self.TargetSize = self.Large
self.playerInfo:SetPlayer( trace.Entity )
locktime = CurTime()
end
if locktime + 2 < CurTime() then
self.TargetSize = self.Small
end
if self.Size ~= self.TargetSize then
self.Size = math.Approach( self.Size, self.TargetSize, (math.abs( self.Size - self.TargetSize ) + 1) * 8 * FrameTime() )
self:PerformLayout()
end
self.total:SetText( timeToStr( LocalPlayer():GetUTimeTotalTime() ) )
self.session:SetText( timeToStr( LocalPlayer():GetUTimeSessionTime() ) )
end
-----------------------------------------------------------
-- Name: PerformLayout
-----------------------------------------------------------
function PANEL:PerformLayout()
self:SetSize( self:GetWide(), self.Size )
self.lblTotalTime:SetSize( 52, 18 )
self.lblTotalTime:SetPos( 8, 2 )
self.lblTotalTime:SetText( "Total: " )
self.lblSessionTime:SetSize( 52, 18 )
self.lblSessionTime:SetPos( 8, 20 )
self.lblSessionTime:SetText( "Session: " )
self.total:SetSize( self:GetWide() - 52, 18 )
self.total:SetPos( 52, 2 )
self.session:SetSize( self:GetWide() - 52, 18 )
self.session:SetPos( 52, 20 )
self.playerInfo:SetPos( 0, 42 )
self.playerInfo:SetSize( self:GetWide() - 8, self:GetTall() - 42 )
end
vgui.Register( "UTimeMain", PANEL, "Panel" )
local INFOPANEL = {}
-----------------------------------------------------------
-- Name: Init
-----------------------------------------------------------
function INFOPANEL:Init()
self.lblTotalTime = vgui.Create( "DLabel", self )
self.lblSessionTime = vgui.Create( "DLabel", self )
self.lblNick = vgui.Create( "DLabel", self )
self.total = vgui.Create( "DLabel", self )
self.session = vgui.Create( "DLabel", self )
self.nick = vgui.Create( "DLabel", self )
end
-----------------------------------------------------------
-- Name: SetPlayer
-----------------------------------------------------------
function INFOPANEL:SetPlayer( ply )
self.Player = ply
end
-----------------------------------------------------------
-- Name: ApplySchemeSettings
-----------------------------------------------------------
function INFOPANEL:ApplySchemeSettings()
self.lblTotalTime:SetFont( "DermaDefault" )
self.lblSessionTime:SetFont( "DermaDefault" )
self.lblNick:SetFont( "DermaDefault" )
self.total:SetFont( "DermaDefault" )
self.session:SetFont( "DermaDefault" )
self.nick:SetFont( "DermaDefault" )
self.lblTotalTime:SetTextColor( color_black )
self.lblSessionTime:SetTextColor( color_black )
self.lblNick:SetTextColor( color_black )
self.total:SetTextColor( color_black )
self.session:SetTextColor( color_black )
self.nick:SetTextColor( color_black )
end
-----------------------------------------------------------
-- Name: Think
-----------------------------------------------------------
function INFOPANEL:Think()
local ply = self.Player
if not ply or not ply:IsValid() or not ply:IsPlayer() then -- Disconnected
self:GetParent().TargetSize = self:GetParent().Small
return
end
self.total:SetText( timeToStr( ply:GetUTime() + CurTime() - ply:GetUTimeStart() ) )
self.session:SetText( timeToStr( CurTime() - ply:GetUTimeStart() ) )
self.nick:SetText( ply:Nick() )
end
-----------------------------------------------------------
-- Name: PerformLayout
-----------------------------------------------------------
function INFOPANEL:PerformLayout()
self.lblNick:SetSize( 52, 18 )
self.lblNick:SetPos( 8, 0 )
self.lblNick:SetText( "Nick: " )
self.lblTotalTime:SetSize( 52, 18 )
self.lblTotalTime:SetPos( 8, 18 )
self.lblTotalTime:SetText( "Total: " )
self.lblSessionTime:SetSize( 52, 18 )
self.lblSessionTime:SetPos( 8, 36 )
self.lblSessionTime:SetText( "Session: " )
self.nick:SetSize( self:GetWide() - 52, 18 )
self.nick:SetPos( 52, 0 )
self.total:SetSize( self:GetWide() - 52, 18 )
self.total:SetPos( 52, 18 )
self.session:SetSize( self:GetWide() - 52, 18 )
self.session:SetPos( 52, 36 )
end
-----------------------------------------------------------
-- Name: Paint
-----------------------------------------------------------
function INFOPANEL:Paint()
return true
end
vgui.Register( "UTimePlayerInfo", INFOPANEL, "Panel" )
----------------------------------------------------------------------------------------------------------
-- Now for the control panels!
----------------------------------------------------------------------------------------------------------
function resetCvars()
RunConsoleCommand( "utime_outsidecolor_r", "0" )
RunConsoleCommand( "utime_outsidecolor_g", "150" )
RunConsoleCommand( "utime_outsidecolor_b", "245" )
RunConsoleCommand( "utime_outsidetext_r", "255" )
RunConsoleCommand( "utime_outsidetext_g", "255" )
RunConsoleCommand( "utime_outsidetext_b", "255" )
RunConsoleCommand( "utime_insidecolor_r", "250" )
RunConsoleCommand( "utime_insidecolor_g", "250" )
RunConsoleCommand( "utime_insidecolor_b", "245" )
RunConsoleCommand( "utime_insidetext_r", "0" )
RunConsoleCommand( "utime_insidetext_g", "0" )
RunConsoleCommand( "utime_insidetext_b", "0" )
RunConsoleCommand( "utime_pos_x", "98" )
RunConsoleCommand( "utime_pos_y", "8" )
buildCP( controlpanel.Get( "Utime" ) )
end
concommand.Add( "utime_reset", resetCvars )
function buildCP( cpanel )
cpanel:ClearControls()
cpanel:AddControl( "Header", { Text = "UTime by Megiddo (Team Ulysses)" } )
cpanel:AddControl( "Checkbox", { Label = "Enable", Command = "utime_enable" } )
cpanel:AddControl( "Slider", { Label = "Position X", Command = "utime_pos_x", Type = "Float", Min = "0", Max = "100" } )
cpanel:AddControl( "Slider", { Label = "Position Y", Command = "utime_pos_y", Type = "Float", Min = "0", Max = "100" } )
cpanel:AddControl( "Color", { Label = "Outside Color", Red = "utime_outsidecolor_r", Green = "utime_outsidecolor_g", Blue = "utime_outsidecolor_b", ShowAlpha = "0", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" } )
cpanel:AddControl( "Color", { Label = "Outside Text Color", Red = "utime_outsidetext_r", Green = "utime_outsidetext_g", Blue = "utime_outsidetext_b", ShowAlpha = "0", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" } )
cpanel:AddControl( "Color", { Label = "Inside Color", Red = "utime_insidecolor_r", Green = "utime_insidecolor_g", Blue = "utime_insidecolor_b", ShowAlpha = "0", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" } )
cpanel:AddControl( "Color", { Label = "Inside Text Color", Red = "utime_insidetext_r", Green = "utime_insidetext_g", Blue = "utime_insidetext_b", ShowAlpha = "0", ShowHSV = "1", ShowRGB = "1", Multiplier = "255" } )
cpanel:AddControl( "Button", { Text = "Reset", Label = "Reset colors and position", Command = "utime_reset" } )
end
function spawnMenuOpen()
buildCP( controlpanel.Get( "Utime" ) )
end
hook.Add( "SpawnMenuOpen", "UtimeSpawnMenuOpen", spawnMenuOpen )
function popToolMenu()
spawnmenu.AddToolMenuOption( "Utilities", "Utime Controls", "Utime", "Utime", "", "", buildCP )
end
hook.Add( "PopulateToolMenu", "UtimePopulateTools", popToolMenu )
function onEntCreated( ent )
if LocalPlayer():IsValid() then -- LocalPlayer was created and is valid now
if utime_outsidecolor_r:GetInt() == 256 then resetCvars() end
end
end
hook.Add( "OnEntityCreated", "UTimeLocalPlayerCheck", onEntCreated ) -- Flag server when we created LocalPlayer()
sh_time.lua
AddCSLuaFile( "autorun/sh_utime.lua" )
AddCSLuaFile( "autorun/cl_utime.lua" )
module( "Utime", package.seeall )
local meta = FindMetaTable( "Player" )
if not meta then return end
function meta:GetUTime()
return self:GetNWInt( "TotalUTime" )
end
function meta:SetUTime( num )
self:SetNWInt( "TotalUTime", num )
end
function meta:GetUTimeStart()
return self:GetNWInt( "UTimeStart" )
end
function meta:SetUTimeStart( num )
self:SetNWInt( "UTimeStart", num )
end
function meta:GetUTimeSessionTime()
return CurTime() - self:GetUTimeStart()
end
function meta:GetUTimeTotalTime()
return self:GetUTime() + CurTime() - self:GetUTimeStart()
end
function timeToStr( time )
local tmp = time
local s = tmp % 60
tmp = math.floor( tmp / 60 )
local m = tmp % 60
tmp = math.floor( tmp / 60 )
local h = tmp % 24
tmp = math.floor( tmp / 24 )
local d = tmp % 7
local w = math.floor( tmp / 7 )
return string.format( "%02iw %id %02ih %02im %02is", w, d, h, m, s )
end
sv_time.lua
module( "Utime", package.seeall )
if not SERVER then return end
utime_welcome = CreateConVar( "utime_welcome", "1", FCVAR_ARCHIVE )
if not sql.TableExists( "utime" ) then
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 );" )
sql.Query( "CREATE INDEX IDX_UTIME_PLAYER ON utime ( player DESC );" )
end
function onJoin( ply )
local uid = ply:UniqueID()
local row = sql.QueryRow( "SELECT totaltime, lastvisit FROM utime WHERE player = " .. uid .. ";" )
local time = 0
if row then
if utime_welcome:GetBool() then
ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
end
sql.Query( "UPDATE utime SET lastvisit = " .. os.time() .. " WHERE player = " .. uid .. ";" )
time = row.totaltime
else
if utime_welcome:GetBool() then
ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
end
sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. uid .. ", 0, " .. os.time() .. " );" )
end
ply:SetUTime( time )
ply:SetUTimeStart( CurTime() )
end
hook.Add( "PlayerInitialSpawn", "UTimeInitialSpawn", onJoin )
function updatePlayer( ply )
sql.Query( "UPDATE utime SET totaltime = " .. math.floor( ply:GetUTimeTotalTime() ) .. " WHERE player = " .. ply:UniqueID() .. ";" )
end
hook.Add( "PlayerDisconnected", "UTimeDisconnect", updatePlayer )
function updateAll()
local players = player.GetAll()
for _, ply in ipairs( players ) do
if ply and ply:IsConnected() then
updatePlayer( ply )
end
end
end
timer.Create( "UTimeTimer", 67, 0, updateAll )