Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.93 KB | None | 0 0
  1. -- Written by Team Ulysses, http://ulyssesmod.net/
  2. module( "Utime", package.seeall )
  3. if not SERVER then return end
  4.  
  5. require("mysqloo")
  6.  
  7. include("utime_mysql.lua")
  8.  
  9. local dbconn, err = mysqloo.connect(UTimeDB.Hostname, UTimeDB.Username, UTimeDB.Password, UTimeDB.DBName, UTimeDB.Port, nil, CLIENT_MULTI_STATEMENTS)
  10.  
  11. if err then error(err) end
  12.  
  13. utime_welcome = CreateConVar( "utime_welcome", "1", FCVAR_ARCHIVE )
  14.  
  15. dbconn:query("CREATE TABLE IF NOT EXISTS utime (steamid BIGINT(20) NOT NULL PRIMARY KEY, totaltime INTEGER NOT NULL, lastvisit INTEGER NOT NULL)")
  16.  
  17. function onJoin( ply )
  18.     local uid = ply:SteamID64()
  19.     local row = dbconn:query( "SELECT totaltime, lastvisit FROM utime WHERE steamid = " .. uid .. " LIMIT 1;", function(result)
  20.         local time = 0
  21.         if table.Count(result[1].data) > 0 then
  22.             if utime_welcome:GetBool() then
  23.                 ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", result[1].data[1].lastvisit ) )
  24.             end
  25.             dbconn:query("UPDATE utime SET lastvisit = "..os.time().." WHERE steamid="..uid..";")
  26.             time = result[1].data[1].totaltime
  27.         else
  28.             if utime_welcome:GetBool() then
  29.                 ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
  30.             end
  31.             dbconn:query("INSERT INTO utime (steamid, totaltime, lastvisit) VALUES ("..uid..", 0, "..os.time()..");")
  32.         end
  33.         ply:SetUTime(time)
  34.         ply:SetUTimeStart(CurTime())
  35.     end)
  36. end
  37. hook.Add( "PlayerInitialSpawn", "UTimeInitialSpawn", onJoin )
  38.  
  39. function updatePlayer( ply )
  40.     dbconn:query( "UPDATE utime SET totaltime = " .. math.floor( ply:GetUTimeTotalTime() ) .. " WHERE steamid = " .. ply:SteamID64() .. ";" )
  41. end
  42. hook.Add( "PlayerDisconnected", "UTimeDisconnect", updatePlayer )
  43.  
  44. function updateAll()
  45.     local players = player.GetAll()
  46.  
  47.     for _, ply in ipairs( players ) do
  48.         if ply and ply:IsConnected() then
  49.             updatePlayer( ply )
  50.         end
  51.     end
  52. end
  53. timer.Create( "UTimeTimer", 67, 0, updateAll )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement