· 10 years ago · Feb 21, 2016, 10:24 PM
1local DB_HOST = "vps.sgve.eu"
2local DB_USERNAME = "root"
3local DB_PASSWORD = "983333"
4local DB_DATABASE = "donate"
5local SERVER_ID = "sgve2"
6
7-------------------------------------------------------------
8-- Leave everything bellow ----------------------------------
9-------------------------------------------------------------
10
11DonationSystem = { }
12
13---- Logs ---------------------------------------------------
14
15DonationSystem.Logs = { }
16
17local function addlog( str )
18 DonationSystem.Logs[ #DonationSystem.Logs + 1 ] = { os.date( ), str }
19 ServerLog( "[DonationSystem] " .. tostring( str ) .. "\n" )
20end
21
22concommand.Add( "ds_printlogs",function( ply, cmd, args, str )
23 if IsValid( ply ) then return end
24 print( "DonationSystem Logs:" )
25 for i=1, #DonationSystem.Logs do
26 print( unpack( DonationSystem.Logs[ i ] ) )
27 end
28end )
29
30---- MySQLOO ------------------------------------------------
31
32require( "mysqloo" )
33if not mysqloo then
34 addlog( "No mysqloo module was found" )
35 return nil
36end
37
38DonationSystem.Database = mysqloo.connect( DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE, 3306 )
39local db = DonationSystem.Database
40
41local queue = {}
42local function query( sql, callback )
43 local q = db:query( sql )
44 if not q then
45 table.insert( queue, { sql, callback } )
46 db:connect( )
47 return
48 end
49 function q:onSuccess( data )
50 if type( callback ) == "function" then
51 callback( data, q )
52 end
53 end
54 function q:onError( err )
55 if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
56 table.insert( queue, { sql, callback } )
57 db:connect( )
58 return
59 else
60 DonationSystem.DatabaseCheck( )
61 addlog( "Query Error: " .. err .. " sql: " .. sql )
62 end
63 end
64 q:start()
65end
66
67function db:onConnected( )
68 addlog( "Connected to database" )
69 DonationSystem.DatabaseCheck( )
70 for k, v in pairs( queue ) do
71 query( v[ 1 ], v[ 2 ] )
72 end
73 queue = {}
74end
75
76function db:onConnectionFailed( err )
77 addlog( "Connection to database failed! Error: " .. err )
78end
79
80db:connect( )
81
82function DonationSystem.DatabaseCheck( )
83 query( [[
84 CREATE TABLE IF NOT EXISTS `commands` (
85 id MEDIUMINT NOT NULL AUTO_INCREMENT,
86 serverid varchar(40) NOT NULL,
87 packageid int(6) unsigned NOT NULL,
88 online TINYINT(1) UNSIGNED NOT NULL,
89 commandid int(6) unsigned NOT NULL,
90 delay INT UNSIGNED NOT NULL,
91 activatetime INT UNSIGNED NOT NULL,
92 command TEXT NOT NULL,
93 activated TINYINT(1) UNSIGNED NOT NULL,
94 transactionid varchar(125) NOT NULL,
95 playerid varchar(40) NOT NULL,
96 playername varchar(40) NOT NULL,
97 PRIMARY KEY (id))
98 ]] )
99end
100
101concommand.Add( "ds_status", function( ply, cmd, args, str )
102 addlog( db:status( ) )
103end )
104
105---- Commands -----------------------------------------------
106
107util.AddNetworkString( "DonationSystemColorChat" )
108util.AddNetworkString( "DonationSystemConCommand" )
109
110hook.Add( "PlayerInitialSpawn", "DonationSystemPlayerJoinSendLua", function(ply)
111 ply:SendLua( [[ net.Receive( "DonationSystemColorChat", function( len ) chat.AddText( unpack( net.ReadTable() ) ) end ) ]] )
112 ply:SendLua( [[ net.Receive( "DonationSystemCmd", function( len ) RunConsoleCommand( unpack( net.ReadTable() ) ) end ) ]] )
113end)
114
115DonationSystem.Commands = {
116 [ "darkrp_money" ] = function( data, args, ply )
117 if not IsValid( ply ) then
118 addlog( "Error executing darkrp_money command, Error: player is not valid" )
119 return nil
120 end
121 local succ, err = pcall( function( )
122 local PLAYER = FindMetaTable( "Player" )
123 if type( PLAYER.AddMoney ) == "function" then
124 ply:AddMoney( tonumber( args[ 1 ] ) )
125 elseif type( PLAYER.addMoney ) == "function" then
126 ply:addMoney( tonumber( args[ 1 ] ) )
127 else
128 addlog( "Error executing darkrp_money command, Error: No functions were found" )
129 end
130 end )
131 if not succ then
132 addlog( "Error executing darkrp_money command, Error: " .. err )
133 end
134 end,
135 [ "pointshop_points" ] = function( data, args, ply )
136 if not IsValid( ply ) then
137 addlog( "Error executing pointshop_points command, Error: player is not valid" )
138 return nil
139 end
140 local succ, err = pcall( function() ply:PS_GivePoints( tonumber( args[ 1 ] ) ) end )
141 if not succ then
142 addlog( "Error executing pointshop_points command, Error: " .. err )
143 end
144 end,
145 ["print"] = function( data, args, ply )
146 if not IsValid( ply ) then
147 addlog( "Error executing print command, Error: player is not valid" )
148 return nil
149 end
150 if type( args ) == "table" then
151 for i=1, #args do
152 if type( args[ i ] ) == "table" then
153 args[ i ] = Color( args[ i ][ 1 ], args[ i ][ 2 ], args[ i ][ 3 ] )
154 elseif type( args[ i ] ) == "string" then
155 args[ i ] = string.Replace( args[ i ], "%name%", tostring( data.playername ) )
156 args[ i ] = string.Replace( args[ i ], "%orderid%", tostring( data.id ) )
157 args[ i ] = string.Replace( args[ i ], "%transactionid%", tostring( data.transactionid ) )
158 args[ i ] = string.Replace( args[ i ], "%packageid%", tostring( data.packageid ) )
159 args[ i ] = string.Replace( args[ i ], "%gamename%", tostring( ply:Name( ) ) )
160 args[ i ] = string.Replace( args[ i ], "%steamid%", tostring( ply:SteamID( ) ) )
161 args[ i ] = string.Replace( args[ i ], "%steamid64%", tostring( ply:SteamID64( ) ) )
162 args[ i ] = string.Replace( args[ i ], "%uniqueid%", tostring( ply:UniqueID( ) ) )
163 args[ i ] = string.Replace( args[ i ], "%userid%", tostring( ply:UserID( ) ) )
164 end
165 end
166 net.Start( "DonationSystemColorChat" )
167 net.WriteTable( args )
168 net.Send( ply )
169 end
170 end,
171 [ "broadcast" ] = function( data, args, ply )
172 if type( args ) == "table" then
173 for i=1, #args do
174 if type( args[ i ] ) == "table" then
175 args[ i ] = Color( args[ i ][ 1 ], args[ i ][ 3 ], args[ i ][ 5 ] )
176 elseif type( args[ i ] ) == "string" then
177 args[ i ] = string.Replace( args[ i ], "%name%", tostring( data.playername ) )
178 args[ i ] = string.Replace( args[ i ], "%orderid%", tostring( data.id ) )
179 args[ i ] = string.Replace( args[ i ], "%transactionid%", tostring( data.transactionid ) )
180 args[ i ] = string.Replace( args[ i ], "%packageid%", tostring( data.packageid ) )
181 args[ i ] = string.Replace( args[ i ], "%steamid%", tostring( data.playerid ) )
182 args[ i ] = string.Replace( args[ i ], "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
183 if IsValid( ply ) then
184 args[ i ] = string.Replace( args[ i ], "%steamid64%", tostring( ply:SteamID64( ) ) )
185 args[ i ] = string.Replace( args[ i ], "%gamename%", tostring( ply:Nick( ) ) )
186 args[ i ] = string.Replace( args[ i ], "%userid%", tostring( ply:UserID( ) ) )
187 end
188 end
189 end
190 net.Start( "DonationSystemColorChat" )
191 net.WriteTable( args )
192 net.Broadcast( )
193 end
194 end,
195 [ "broadcast_omit" ] = function( data, args, ply )
196 if type( args ) == "table" then
197 for i=1, #args do
198 if type( args[ i ] ) == "table" then
199 args[ i ] = Color( args[ i ][ 1 ], args[ i ][ 3 ], args[ i ][ 5 ] )
200 elseif type( args[ i ] ) == "string" then
201 args[ i ] = string.Replace( args[ i ], "%name%", tostring( data.playername ) )
202 args[ i ] = string.Replace( args[ i ], "%orderid%", tostring( data.id ) )
203 args[ i ] = string.Replace( args[ i ], "%transactionid%", tostring( data.transactionid ) )
204 args[ i ] = string.Replace( args[ i ], "%packageid%", tostring( data.packageid ) )
205 args[ i ] = string.Replace( args[ i ], "%steamid%", tostring( data.playerid ) )
206 args[ i ] = string.Replace( args[ i ], "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
207 if IsValid( ply ) then
208 args[ i ] = string.Replace( args[ i ], "%steamid64%", tostring( ply:SteamID64( ) ) )
209 args[ i ] = string.Replace( args[ i ], "%gamename%", tostring( ply:Nick( ) ) )
210 args[ i ] = string.Replace( args[ i ], "%userid%", tostring( ply:UserID( ) ) )
211 end
212 end
213 end
214 net.Start( "DonationSystemColorChat" )
215 net.WriteTable( args )
216
217 if IsValid(ply) then
218 net.SendOmit( ply )
219 else
220 net.Broadcast( )
221 end
222 end
223 end,
224 [ "lua" ] = function( data, args, ply )
225 local oldPLAYER = PLAYER
226 local oldSTEAMID = STEAMID
227 local oldORDERDATA = ORDERDATA
228 local oldQUERY = QUERY
229
230 PLAYER = ply
231 STEAMID = data.playerid
232 ORDERDATA = data
233 QUERY = query
234
235 RunStringEx( args[ 1 ], "[DonationSystem] Lua: " )
236
237 PLAYER = oldPLAYER
238 STEAMID = oldSTEAMID
239 ORDERDATA = oldORDERDATA
240 QUERY = oldQUERY
241 end,
242 [ "sv_cmd" ] = function( data, args, ply )
243 for i=1, #args do
244 args[ i ] = string.Replace( args[ i ], "%name%", tostring( data.playername ) )
245 args[ i ] = string.Replace( args[ i ], "%orderid%", tostring( data.id ) )
246 args[ i ] = string.Replace( args[ i ], "%transactionid%", tostring( data.transactionid ) )
247 args[ i ] = string.Replace( args[ i ], "%packageid%", tostring( data.packageid ) )
248 args[ i ] = string.Replace( args[ i ], "%steamid%", tostring( data.playerid ) )
249 args[ i ] = string.Replace( args[ i ], "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
250 if IsValid( ply ) then
251 args[i] = string.Replace( args[i], "%steamid64%", tostring( ply:SteamID64( ) ) )
252 args[i] = string.Replace( args[i], "%gamename%", tostring( ply:Nick( ) ) )
253 args[i] = string.Replace( args[i], "%userid%", tostring( ply:UserID( ) ) )
254 end
255 end
256 RunConsoleCommand( unpack( args ) )
257 end,
258 [ "disabled" ] = function( data, args, ply ) end,
259 [ "cl_cmd" ] = function( data, args, ply )
260 if not IsValid( ply ) then
261 addlog( "Error executing cl_cmd command, Error: player is not valid" )
262 return nil
263 end
264 for i=1, #args do
265 args[ i ] = string.Replace( args[ i ], "%name%", tostring( data.playername ) )
266 args[ i ] = string.Replace( args[ i ], "%orderid%", tostring( data.id ) )
267 args[ i ] = string.Replace( args[ i ], "%transactionid%", tostring( data.transactionid ) )
268 args[ i ] = string.Replace( args[ i ], "%packageid%", tostring( data.packageid ) )
269 args[ i ] = string.Replace( args[ i ], "%steamid%", tostring( data.playerid ) )
270 args[ i ] = string.Replace( args[ i ], "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
271
272 args[ i ] = string.Replace( args[ i ], "%steamid64%", tostring( ply:SteamID64( ) ) )
273 args[ i ] = string.Replace( args[ i ], "%gamename%", tostring( ply:Nick( ) ) )
274 args[ i ] = string.Replace( args[ i ], "%userid%", tostring( ply:UserID( ) ) )
275 end
276 net.Start( "DonationSystemCmd" )
277 net.WriteTable( args )
278 net.Send( ply )
279 end,
280 [ "sql" ] = function( data, args, ply )
281 local querystring = args.query or args[ 1 ]
282 querystring = string.Replace( querystring, "%name%", tostring( data.playername ) )
283 querystring = string.Replace( querystring, "%orderid%", tostring( data.id ) )
284 querystring = string.Replace( querystring, "%transactionid%", tostring( data.transactionid ) )
285 querystring = string.Replace( querystring, "%packageid%", tostring( data.packageid ) )
286 querystring = string.Replace( querystring, "%steamid%", tostring( data.playerid ) )
287 querystring = string.Replace( querystring, "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
288 querystring = string.Replace( querystring, "%name_esc%", db:escape( tostring( data.playername ) ) )
289 querystring = string.Replace( querystring, "%ostime%", tostring( os.time( ) ) )
290
291 if IsValid( ply ) then
292 querystring = string.Replace( querystring, "%steamid64%", tostring( ply:SteamID64( ) ) )
293 querystring = string.Replace( querystring, "%gamename%", tostring( ply:Nick( ) ) )
294 querystring = string.Replace( querystring, "%gamename_esc%", db:escape( tostring( ply:Name( ) ) ) )
295 querystring = string.Replace( querystring, "%userid%", tostring( ply:UserID( ) ) )
296 end
297
298 query( querystring )
299 end,
300 [ "sql_ext" ] = function( data, args,ply )
301 local querystring = args.query
302 querystring = string.Replace( querystring, "%name%", tostring( data.playername ) )
303 querystring = string.Replace( querystring, "%orderid%", tostring( data.id ) )
304 querystring = string.Replace( querystring, "%transactionid%", tostring( data.transactionid ) )
305 querystring = string.Replace( querystring, "%packageid%", tostring( data.packageid ) )
306 querystring = string.Replace( querystring, "%steamid%", tostring( data.playerid ) )
307 querystring = string.Replace( querystring, "%uniqueid%", tostring( util.CRC( "gm_" .. data.playerid .. "_gm" ) ) )
308 querystring = string.Replace( querystring, "%name_esc%", db:escape( tostring( data.playername ) ) )
309 querystring = string.Replace( querystring, "%ostime%", tostring( os.time( ) ) )
310
311 if IsValid(ply) then
312 querystring = string.Replace( querystring, "%steamid64%", tostring(ply:SteamID64()) )
313 querystring = string.Replace( querystring, "%gamename%", tostring(ply:Nick()) )
314 querystring = string.Replace( querystring, "%gamename_esc%", db:escape(tostring(ply:Name())) )
315 querystring = string.Replace( querystring, "%userid%", tostring(ply:UserID()) )
316 end
317
318 local db = mysqloo.connect( args.host, args.database, args.username, args.password, 3306 )
319
320 function db:onConnected( )
321 local q = self:query( querystring )
322 function q:onSuccess( data ) end
323 function q:onError( err, sql )
324 addlog( "Error executing 'sql_ext' command, error: " .. err .. " sql: " .. sql )
325 end
326 q:start( )
327 end
328 function db:onConnectionFailed( err )
329 addlog( "Error executing 'sql_ext' command, error: ", err )
330 end
331
332 db:connect( )
333 end,
334 [ "cancel" ] = function( data, args, ply )
335 local excludeself = args.excludeself or args[ 1 ]
336 local serverid = args.serverid or args[ 2 ]
337 local packageid = args.packageid or args[ 3 ]
338 local online = args.online or args[ 4 ]
339 local delay = args.delay or args[ 5 ]
340 local commandid = args.commandid or args[ 6 ]
341
342 if #args > 6 then
343 addlog( "Error executing 'cancel' command, Error: Arguments can't exceed 6" )
344 elseif #args >= 2 and type(excludeself) ~= "boolean" then
345 addlog( "Error executing 'cancel' command, Error: Invalid 'excludeself' argument(1). Expected boolean, got " .. type( excludeself ) .. ": " .. excludeself )
346 elseif type( serverid ) ~= "string" then
347 addlog( "Error executing 'cancel' command, Error: Invalid 'serverid' argument(2). Expected string, got " .. type( serverid ) .. ": " .. serverid )
348 elseif #args >= 3 and tonumber( packageid ) == nil then
349 addlog( "Error executing 'cancel' command, Error: Invalid 'packageid' argument(3). Expected number, got " .. type( packageid ) .. ": " .. packageid )
350 elseif #args >= 4 and type( online ) ~= "boolean" then
351 addlog( "Error executing 'cancel' command, Error: Invalid 'online' argument(4). Expected boolean, got " .. type( online ) .. ": " .. online )
352 elseif #args >= 5 and tonumber( delay ) == nil then
353 addlog( "Error executing 'cancel' command, Error: Invalid 'delay' argument(5). Expected number, got " .. type( delay ) .. ": " .. delay )
354 elseif #args >= 6 and tonumber( commandid ) == nil then
355 addlog( "Error executing 'cancel' command, Error: Invalid 'commandid' argument(6). Expected number, got " .. type( commandid ) .. ": " .. commandid )
356 else -- All arguments are validated
357 local querystr = "UPDATE commands SET activated = 1 WHERE playerid = \"" .. db:escape( data.playerid ) .. "\""
358 if #args>=1 and excludeself then querystr = querystr .. " AND transactionid <> \"" .. db:escape( data.transactionid ) .. "\"" end
359 if #args>=2 then querystr = querystr .. " AND serverid = \"" .. db:escape( serverid ) .. "\""end
360 if #args>=3 then querystr = querystr .. " AND packageid = \"" .. db:escape( packageid ) .. "\"" end
361 if #args>=4 then querystr = querystr .. " AND online = \"" .. ( online and 1 or 0 ) .. "\"" end
362 if #args>=5 then querystr = querystr .. " AND delay = \"" .. db:escape( delay ) .. "\"" end
363 if #args>=6 then querystr = querystr .. " AND commandid = \"" .. db:escape( commandid ) .. "\"" end
364 query( querystr )
365 end
366 end
367}
368
369---- Main Timer ---------------------------------------------
370
371local activated = {}
372timer.Create( "DonationSystemCheck", 60, 0, function( ) -- Check database every 60 seconds
373 -- Store players to table, and add players conditions to query.
374 local plyEnts = { }
375 local sqlplayers = ""
376 for _, ply in pairs( player.GetAll( ) ) do
377 if ply:TimeConnected( ) > 60 then -- Making sure player is fully initialised
378 plyEnts[ ply:SteamID( ) ] = ply
379 sqlplayers = sqlplayers .. " OR playerid = \"" .. db:escape( ply:SteamID( ) ) .. "\""
380 end
381 end
382
383 -- Get all commands that should be activated in next 60 seconds
384 query("SELECT *, ( UNIX_TIMESTAMP() ) AS unixtime FROM `commands` WHERE serverid = \"" .. db:escape( SERVER_ID ) .. "\" AND activated = 0 AND activatetime <= (UNIX_TIMESTAMP()+59) AND ( online = 0 " .. sqlplayers .. ")", function( commands )
385 for _, cmddata in pairs( commands ) do
386 -- Delay the command
387 local timeoffset = math.max( 1, cmddata.activatetime - cmddata.unixtime )
388
389 timer.Simple( timeoffset, function( )
390 if cmddata.online == 0 or IsValid( plyEnts[ cmddata.playerid ] ) then -- Check if player still on server
391 query("UPDATE `commands` SET activated='1' WHERE id=" .. cmddata.id, function( data, q ) -- Activate it
392 if q:affectedRows( ) > 0 and not activated[ cmddata.id ] then -- Making sure the command is not activated before to prevent duplicate execution
393 if cmddata.online == 0 or IsValid( plyEnts[ cmddata.playerid ] ) then
394 activated[ cmddata.id ] = true
395 local args = util.JSONToTable( cmddata.command )
396 local command = table.remove( args, 1 )
397
398 local succ, err = pcall( function( ) DonationSystem.Commands[ command ]( cmddata, args, plyEnts[ cmddata.playerid ] ) end )
399 if not succ then
400 addlog( "Error executing command '" .. command .. "'(" .. cmddata.id .. "). Error: " .. err )
401 end
402
403 addlog( "Activated command '" .. command .. "' for " .. cmddata.playername .. "(" .. cmddata.playerid .. ")(cmdid = " .. cmddata.id .. ")")
404 else
405 query("UPDATE `commands` SET activated='0' WHERE id=" .. cmddata.id) -- If player disconnect, we might want to deactivate the command
406 end
407 else
408 addlog( "Error executing command (" .. cmddata.id .. "). Conditions failed. " .. tostring( IsValid( plyEnts[ cmddata.playerid ] ) ) )
409 end
410 end )
411 end
412 end )
413 end
414 end )
415end )