· 8 years ago · Jan 16, 2018, 05:52 AM
1-- Written by Team Ulysses, http://ulyssesmod.net/
2-- Modified by ACProdigy for MySQL operation
3-- Modified by MRDRMUFN for gm_mysqloo support
4-- Modified by TweaK for more reliable operation
5
6if not SERVER then return end
7
8module( "Utime", package.seeall )
9
10require( "mysqloo" )
11
12----- Database connection details -----
13
14local DATABASE_HOST = ""
15local DATABASE_USERNAME = ""
16local DATABASE_PASSWORD = ""
17local DATABASE_PORT = 3306
18local DATABASE_NAME = ""
19
20--=== DO NOT EDIT BELOW THIS POINT ===--
21
22local utime_welcome = CreateConVar( "utime_welcome", "1", FCVAR_ARCHIVE )
23local queue = {}
24
25local db = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT )
26
27local function query( str, callback )
28 local q = db:query( str )
29
30 function q:onSuccess( data )
31 if callback ~= nil then callback( data ) end
32 end
33
34 function q:onError( err )
35 if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
36 table.insert( queue, { str, callback } )
37 db:connect()
38 return end
39
40 print( "[UTime] Error! Query failed: " .. err )
41 end
42
43 q:start()
44end
45
46function db:onConnected()
47 print( "[UTime] Connected to MySQL." )
48
49 for k, v in pairs( queue ) do
50 query( v[ 1 ], v[ 2 ] )
51 end
52
53 queue = {}
54end
55
56function db:onConnectionFailed( err )
57 print( "[UTime] Database connection failed: " .. err )
58end
59
60db:connect()
61
62-- Check that the table exists, create it if not
63table.insert( queue, { "SHOW TABLES LIKE 'utime'", function( data )
64 if table.Count( data ) < 1 then -- the table doesn't exist
65 query( "CREATE TABLE utime (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, player INTEGER UNSIGNED NOT NULL, totaltime INTEGER UNSIGNED NOT NULL, lastvisit INTEGER UNSIGNED NOT NULL, steamid VARCHAR(45) NOT NULL, playername VARCHAR(100) NOT NULL, PRIMARY KEY (id))", function( data )
66 print( "[UTime] Successfully created table!" )
67 end )
68 end
69end } )
70
71function PlayerJoined( ply )
72 local uid = ply:UniqueID()
73 local sid = ply:SteamID()
74
75 query( "SELECT totaltime, lastvisit FROM utime WHERE player = " .. uid, function( data )
76 local time = 0
77
78 if table.Count( data ) > 0 then -- player exists
79 row = data[ 1 ]
80 if utime_welcome:GetBool() then
81 ULib.tsay( ply, "Welcome back! You last played here on " .. os.date( "%a, %b %d, %Y", row.lastvisit ) )
82 end
83
84 -- update lastvisit time
85 query( "UPDATE utime SET lastvisit = " .. os.time() .. " WHERE player = " .. uid, function() end )
86
87 time = row.totaltime
88 else -- player does not exist
89 if utime_welcome:GetBool() then
90 -- ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
91 ULib.tsay( ply, "Welcome to our server " .. ply:Nick() .. "!" )
92 end
93
94 -- create the player
95 query( "INSERT into utime ( player, totaltime, lastvisit, steamid, playername ) VALUES ( " ..
96 uid .. ", 0, " .. os.time() .. ", '" .. sid .. "', '" .. db:escape( ply:Nick() ) .. "' )",
97 function() print( "[UTime] Successfully created new player " .. ply:Nick() .. "." ) end )
98 end
99
100 ply:SetUTime( time )
101 ply:SetUTimeStart( CurTime() )
102 ply.UTimeLoaded = true
103 end )
104end
105hook.Add( "PlayerInitialSpawn", "UTimeInitialSpawn", PlayerJoined )
106
107function UpdatePlayer( ply )
108 if (ply.UTimeLoaded and ply:GetUTimeTotalTime() > 120) then
109 query( "UPDATE utime SET totaltime = " .. math.floor( ply:GetUTimeTotalTime() ) .. " WHERE player = " .. ply:UniqueID() )
110 end
111end
112hook.Add( "PlayerDisconnected", "UTimeDisconnect", UpdatePlayer )
113
114function UpdateAll()
115 for _, ply in pairs( player.GetAll() ) do
116 if IsValid( ply ) and ply:IsConnected() and ply.UTimeLoaded then
117 UpdatePlayer( ply )
118 end
119 end
120end
121hook.Add( "TTTEndRound", "UpdatePlayerShit", UpdateAll )