Advertisement
Benjiko99

Untitled

Jan 23rd, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.15 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" )
  11. AddCSLuaFile( "shared.lua" )
  12.  
  13. include( "config.lua" )
  14. include( "server/utils.lua" )
  15. include( "shared.lua" )
  16. include( "server/commands.lua" )
  17.  
  18. /* LOCAL VARIABLES */
  19. local db = mysqloo.connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT)
  20. local antiTimeout = 0
  21.  
  22.  
  23. function db:onConnected()
  24.     print( "[MySQL] Connected to database on "..DB_HOST..":"..DB_PORT.." and version "..db:serverInfo() )
  25. end
  26.  
  27. function db:onConnectionFailed( err )
  28.     print( "[MySQL] Failed to connect to database on "..DB_HOST..":"..DB_PORT )
  29.     print( "Error:", err )
  30. end
  31.  
  32.  
  33. function LoadPlayerFromDatabase( ply )
  34.    
  35.     local function Set( data )
  36.    
  37.         for k,v in pairs(data or {}) do
  38.            
  39.             ply:SetNWInt("points", v.points)
  40.             ply:SetNWInt("money", v.money)
  41.             ply:SetTeam( v.job )
  42.            
  43.         end
  44.    
  45.     end
  46.    
  47.    
  48.     local function Add()
  49.    
  50.         local query2 = db:query("INSERT INTO `players` (`nick`, `steamid`, `points`, `money`, `job`, `joined`, `lastjoined`) VALUES ('" .. db:escape( ply:Nick() ) .. "', '" .. ply:SteamID() .. "', " .. 100 .. ", " .. 200 .. ", " .. 1 .. ", '" .. os.date() .. "', '" .. os.date() .. "')")
  51.        
  52.         function query2:onError( err, sql )
  53.  
  54.             print( "[MySQL]: Query error while trying to add player \""..ply:Nick().."\" to the database!" )
  55.             print( "Query:", sql )
  56.             print( "Error:", err )
  57.  
  58.         end
  59.        
  60.         query2:start()
  61.         query2:wait()
  62.    
  63.     end
  64.    
  65.    
  66.     local function Read()
  67.    
  68.         local query1 = db:query("SELECT * FROM `players` WHERE `steamid` = '"..ply:SteamID().."';")
  69.         function query1:onSuccess( data )
  70.        
  71.             PrintTable( data )
  72.             return data
  73.            
  74.         end
  75.      
  76.         function query1:onError( err, sql )
  77.  
  78.             print( "[MySQL]: Query error while trying to load player \""..ply:Nick().."\" from the database!" )
  79.             print( "Query:", sql )
  80.             print( "Error:", err )
  81.             return nil
  82.  
  83.         end
  84.    
  85.         query1:start()
  86.         query1:wait()
  87.    
  88.     end
  89.    
  90.    
  91.     local data = Read()
  92.  
  93.     if data[1] then
  94.        
  95.         Set( data )
  96.         print("[MySQL]: Player \""..ply:Nick().."\" loaded from database.")
  97.        
  98.     else
  99.    
  100.         Add()
  101.         Set( data )
  102.         print("[MySQL]: Player \""..ply:Nick().."\" added to the database.")
  103.    
  104.     end
  105.    
  106. end
  107.  
  108. function Initialize()
  109.    
  110.     db:connect()
  111.     db:wait()
  112.    
  113. end
  114.  
  115. hook.Add( "Initialize", "Initialize", Initialize )
  116.  
  117.  
  118. function GM:PlayerInitialSpawn( ply )
  119.    
  120.     if db:status() == mysqloo.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
  121.     LoadPlayerFromDatabase( ply )
  122.    
  123. end
  124.  
  125.  
  126. function GM:PlayerSpawn( ply )
  127.    
  128.     GAMEMODE:PlayerLoadout( ply )
  129.     GAMEMODE:PlayerSetModel ( ply )
  130.    
  131.     if not ply:IsBot() then
  132.    
  133.         ply:SetPlayerColor( Vector( 0.4, 0.4, 0.4 ) )
  134.         ply:SetWeaponColor( Vector( 0.4, 0.4, 0.4 ) )
  135.        
  136.     else
  137.    
  138.         ply:SetModel( "models/player/police.mdl" )
  139.         ply:SetPlayerColor( Vector( 1,0,0 ) )
  140.        
  141.     end
  142.    
  143. end
  144.  
  145.  
  146. function GM:PlayerLoadout( ply )
  147.  
  148.     ply:StripWeapons()
  149.     ply:RemoveAllAmmo()
  150.  
  151.     if ply:Team() == TEAM_CITIZEN then
  152.         ply:Give( "weapon_physcannon" )
  153.         ply:Give( "weapon_physgun" )
  154.    
  155.     elseif ply:Team() == TEAM_COP then
  156.         ply:Give( "weapon_physcannon" )
  157.         ply:Give( "weapon_physgun" )
  158.         ply:GiveAmmo( 100, "pistol", true )
  159.         ply:Give( "weapon_pistol" )
  160.         ply:GiveAmmo( 100, "buckshot", true )
  161.         ply:Give( "weapon_shotgun" )
  162.        
  163.     elseif ply:Team() == TEAM_MEDIC then
  164.         ply:Give( "weapon_physcannon" )
  165.         ply:Give( "weapon_physgun" )
  166.        
  167.     elseif ply:Team() == TEAM_MAYOR then
  168.         ply:Give( "weapon_physcannon" )
  169.         ply:Give( "weapon_physgun" )
  170.     end
  171.    
  172. end
  173.  
  174.  
  175. function SpawnNPCsFromDatabase()
  176.  
  177.     local query1 = db:query( "SELECT * FROM `npcs` WHERE `map` = '"..game.GetMap().."';" )
  178.     function query1:onSuccess( data )
  179.    
  180.          if data[1] then
  181.  
  182.             for k,v in pairs(data or {}) do
  183.            
  184.                 local npc = ents.Create(v.class)
  185.                
  186.                 if npc:IsValid() then
  187.                
  188.                     npc:SetPos(Vector(v.pos))
  189.                     npc:SetAngles(Angle(v.ang))
  190.                     npc:Spawn()
  191.                
  192.                 else print("[ERROR] Trying to spawn invalid NPC "..v.name.."("..v.id..")")
  193.                
  194.                 end
  195.                
  196.             end
  197.            
  198.         else print("[MySQL]: No NPC's for this map were found.")
  199.        
  200.         end
  201.        
  202.     end
  203.  
  204.     function query1:onError( err, sql )
  205.  
  206.         print( "[MySQL]: Query error while trying to load NPC's from the database!" )
  207.         print( "Query:", sql )
  208.         print( "Error:", err )
  209.  
  210.     end
  211.  
  212.     query1:start()
  213.     query1:wait()
  214.    
  215. end
  216.  
  217. function InitPostEntity()
  218.    
  219.     -- npcs =
  220.     -- {
  221.         -- {
  222.             -- class = "npc_job_cop",
  223.             -- pos = Vector(370, 4420, 132.03125),
  224.             -- ang = Angle(0, -180, 0),
  225.             -- name = "Cop"
  226.         -- },
  227.     -- }
  228.     SpawnNPCsFromDatabase()
  229.    
  230. end
  231.  
  232. hook.Add( "InitPostEntity", "InitPostEntity", InitPostEntity )
  233.  
  234. function GM:Think()
  235.    
  236.     -- Anti Timeout System
  237.     -- function AntiTimeout() local query = db:query( "SELECT 1+1" ) query:start() antiTimeout = 0 end
  238.     -- if antiTimeout == 0 then timer.Simple(30, AntiTimeout, nil) antiTimeout = 1 end
  239.    
  240. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement