Advertisement
Guest User

Untitled

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