Advertisement
Benjiko99

Untitled

Dec 31st, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.54 KB | None | 0 0
  1. AddCSLuaFile( "client/fonts.lua" )
  2. AddCSLuaFile( "client/hud.lua" )
  3. AddCSLuaFile( "client/scoreboard.lua" )
  4. AddCSLuaFile( "client/menu_f1.lua" )
  5. AddCSLuaFile( "client/menu_f2.lua" )
  6. AddCSLuaFile( "client/menu_f3.lua" )
  7. AddCSLuaFile( "client/menu_f4.lua" )
  8. AddCSLuaFile( "client/menu_q.lua" )
  9.  
  10. AddCSLuaFile( "cl_init.lua" ) --Tell the server that the client needs to download these files
  11. AddCSLuaFile( "shared.lua" )
  12.  
  13. include( "config.lua" )
  14. include( "shared.lua" )
  15. include( "server/commands.lua" )
  16.  
  17. local db = mysqloo.connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT)
  18.  
  19. function checkQuery(query)
  20.    
  21.     local playerInfo = query:getData()
  22.     if playerInfo[1] ~= nil then
  23.         return true
  24.     else
  25.         return false
  26.     end
  27.    
  28. end
  29.  
  30. function db:onConnected()
  31.  
  32.     print("[MySQL] Connected to database  on "..DB_HOST..":"..DB_PORT.." and version "..db:serverInfo())
  33.  
  34.     -- local q = self:query( "SELECT * FROM `test` WHERE `SteamID` = 'Ben';" )
  35.     -- function q:onSuccess( data )
  36.  
  37.         -- print( "Query successful!" )
  38.         -- PrintTable( data )
  39.  
  40.     -- end
  41.  
  42.     -- function q:onError( err, sql )
  43.  
  44.         -- print( "Query errored!" )
  45.         -- print( "Query:", sql )
  46.         -- print( "Error:", err )
  47.  
  48.     -- end
  49.  
  50.     -- q:start()
  51.  
  52. end
  53.  
  54. function db:onConnectionFailed( err )
  55.  
  56.     print("[MySQL] Failed to connect to database on "..DB_HOST..":"..DB_PORT)
  57.     print( "Error:", err )
  58.  
  59. end
  60.  
  61. function LoadPly( ply )
  62.    
  63.     -- if db:status() == DATABASE_NOT_CONNECTED then print("[MySQL] Not connected to database while trying to load player \"" .. ply:Nick() .. "\", trying to re-connect.") db:connect() db:wait() end
  64.     local query1 = db:query( "SELECT * FROM `players` WHERE `SteamID` = '"..ply:SteamID().."';" )
  65.     function query1:onSuccess( data )
  66.    
  67.         if not checkQuery(q)  then
  68.        
  69.             print("[MySQL]: Player \""..ply:Nick().."\" loaded from database.")
  70.             PrintTable( data )
  71.        
  72.         else
  73.        
  74.             local query2 = db:query("INSERT INTO `players` (`Nick`, `SteamID`, `Money`, `Job`) VALUES ('" .. ply:Nick() .. "', '" .. ply:SteamID() .. "', " .. 200 .. ", " .. 1 .. ")")
  75.             query2.onSuccess = function(q)  print("[MySQL]: Player \""..ply:Nick().."\" added to the database.") end
  76.             query2.onError = function(q,e) print("[MySQL]: Error: "..e) end
  77.             query2:start() 
  78.        
  79.         end
  80.        
  81.     end
  82.  
  83.     function query1:onError( err, sql )
  84.  
  85.         print( "[MySQL]: Query error while trying to load player \""..ply:Nick().."\" from the database!" )
  86.         print( "Query:", sql )
  87.         print( "Error:", err )
  88.  
  89.     end
  90.  
  91.     query1:start()
  92.        
  93. end
  94.  
  95.  
  96. function Initialize()
  97.    
  98.     db:connect()
  99.     db:wait()
  100.    
  101. end
  102.  
  103. hook.Add("Initialize", "Initialize", Initialize)
  104.  
  105.  
  106. function GM:PlayerInitialSpawn( ply )
  107.    
  108.     if db:status() == DATABASE_NOT_CONNECTED then print("[MySQL] Not connected to database while trying to load player \"" .. ply:Nick() .. "\", trying to re-connect.") db:connect() db:wait() end
  109.     LoadPly( ply )
  110.    
  111. end
  112.  
  113.  
  114. function GM:PlayerSpawn( ply )
  115.    
  116.     ply:SetTeam( TEAM_CITIZEN )
  117.    
  118.     GAMEMODE:PlayerLoadout( ply )
  119.     GAMEMODE:PlayerSetModel ( ply )
  120.    
  121.     if !ply:IsBot() then
  122.    
  123.         ply:SetPlayerColor( Vector( 0.4, 0.4, 0.4 ) )
  124.         ply:SetWeaponColor( Vector( 0.4, 0.4, 0.4 ) )
  125.        
  126.     else
  127.    
  128.         ply:SetModel( "models/player/police.mdl" )
  129.         ply:SetPlayerColor( Vector( 1,0,0 ) )
  130.        
  131.     end
  132.    
  133. end
  134.  
  135.  
  136. function GM:PlayerLoadout( ply )
  137.  
  138.     ply:StripWeapons()
  139.     ply:RemoveAllAmmo()
  140.  
  141.     if ply:Team() == TEAM_CITIZEN then
  142.         ply:Give( "weapon_physcannon" )
  143.         ply:Give( "weapon_physgun" )
  144.    
  145.     elseif ply:Team() == TEAM_COP then
  146.         ply:Give( "weapon_physcannon" )
  147.         ply:Give( "weapon_physgun" )
  148.         ply:GiveAmmo( 100, "pistol", true )
  149.         ply:Give( "weapon_pistol" )
  150.         ply:GiveAmmo( 100, "buckshot", true )
  151.         ply:Give( "weapon_shotgun" )
  152.        
  153.     elseif ply:Team() == TEAM_MEDIC then
  154.         ply:Give( "weapon_physcannon" )
  155.         ply:Give( "weapon_physgun" )
  156.        
  157.     elseif ply:Team() == TEAM_MAYOR then
  158.         ply:Give( "weapon_physcannon" )
  159.         ply:Give( "weapon_physgun" )
  160.     end
  161.    
  162. end
  163.  
  164.  
  165. function InitPostEntity()
  166.    
  167.     local npcs =
  168.     {
  169.         {
  170.             class = "npc_job_cop",
  171.             pos = Vector(370, 4420, 132.031250),
  172.             ang = Angle(0, -180, 0),
  173.             name = "Cop"
  174.         },
  175.        
  176.         {
  177.             class = "npc_job_cop",
  178.             pos = Vector(370, 3845, 132.031250),
  179.             ang = Angle(0, -180, 0),
  180.             name = "Cop"
  181.         },
  182.     }
  183.    
  184.     for k,v in pairs(npcs or {}) do
  185.        
  186.         local npc = ents.Create(v.class)
  187.        
  188.         if npc:IsValid() then
  189.        
  190.             npc:SetPos(v.pos)
  191.             npc:SetAngles(v.ang)
  192.             npc:SetName(v.name)
  193.             npc:Spawn()
  194.            
  195.         else print("[ERROR] Trying to spawn invalid NPC "..v)
  196.        
  197.         end
  198.        
  199.     end
  200.    
  201. end
  202.  
  203. hook.Add("InitPostEntity", "InitPostEntity", InitPostEntity)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement