Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AddCSLuaFile( "client/fonts.lua" )
- AddCSLuaFile( "client/hud.lua" )
- AddCSLuaFile( "client/scoreboard.lua" )
- AddCSLuaFile( "client/menu_f1.lua" )
- AddCSLuaFile( "client/menu_f2.lua" )
- AddCSLuaFile( "client/menu_f3.lua" )
- AddCSLuaFile( "client/menu_f4.lua" )
- AddCSLuaFile( "client/menu_q.lua" )
- AddCSLuaFile( "cl_init.lua" )
- AddCSLuaFile( "shared.lua" )
- include( "config.lua" )
- include( "server/utils.lua" )
- include( "shared.lua" )
- include( "server/commands.lua" )
- /* LOCAL VARIABLES */
- local db = mysqloo.connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT)
- local antiTimeout = 0
- function db:onConnected()
- print( "[MySQL] Connected to database on "..DB_HOST..":"..DB_PORT.." and version "..db:serverInfo() )
- end
- function db:onConnectionFailed( err )
- print( "[MySQL] Failed to connect to database on "..DB_HOST..":"..DB_PORT )
- print( "Error:", err )
- end
- function LoadPlayerFromDatabase( ply )
- local function Set( data )
- for k,v in pairs(data or {}) do
- ply:SetNWInt("points", v.points)
- ply:SetNWInt("money", v.money)
- ply:SetTeam( v.job )
- end
- end
- local function Add()
- 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() .. "')")
- function query2:onError( err, sql )
- print( "[MySQL]: Query error while trying to add player \""..ply:Nick().."\" to the database!" )
- print( "Query:", sql )
- print( "Error:", err )
- end
- query2:start()
- query2:wait()
- end
- local function Read()
- local query1 = db:query("SELECT * FROM `players` WHERE `steamid` = '"..ply:SteamID().."';")
- function query1:onSuccess( data )
- PrintTable( data )
- return data
- end
- function query1:onError( err, sql )
- print( "[MySQL]: Query error while trying to load player \""..ply:Nick().."\" from the database!" )
- print( "Query:", sql )
- print( "Error:", err )
- return nil
- end
- query1:start()
- query1:wait()
- end
- local data = Read()
- if data[1] then
- Set( data )
- print("[MySQL]: Player \""..ply:Nick().."\" loaded from database.")
- else
- Add()
- Set( data )
- print("[MySQL]: Player \""..ply:Nick().."\" added to the database.")
- end
- end
- function Initialize()
- db:connect()
- db:wait()
- end
- hook.Add( "Initialize", "Initialize", Initialize )
- function GM:PlayerInitialSpawn( ply )
- 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
- LoadPlayerFromDatabase( ply )
- end
- function GM:PlayerSpawn( ply )
- GAMEMODE:PlayerLoadout( ply )
- GAMEMODE:PlayerSetModel ( ply )
- if not ply:IsBot() then
- ply:SetPlayerColor( Vector( 0.4, 0.4, 0.4 ) )
- ply:SetWeaponColor( Vector( 0.4, 0.4, 0.4 ) )
- else
- ply:SetModel( "models/player/police.mdl" )
- ply:SetPlayerColor( Vector( 1,0,0 ) )
- end
- end
- function GM:PlayerLoadout( ply )
- ply:StripWeapons()
- ply:RemoveAllAmmo()
- if ply:Team() == TEAM_CITIZEN then
- ply:Give( "weapon_physcannon" )
- ply:Give( "weapon_physgun" )
- elseif ply:Team() == TEAM_COP then
- ply:Give( "weapon_physcannon" )
- ply:Give( "weapon_physgun" )
- ply:GiveAmmo( 100, "pistol", true )
- ply:Give( "weapon_pistol" )
- ply:GiveAmmo( 100, "buckshot", true )
- ply:Give( "weapon_shotgun" )
- elseif ply:Team() == TEAM_MEDIC then
- ply:Give( "weapon_physcannon" )
- ply:Give( "weapon_physgun" )
- elseif ply:Team() == TEAM_MAYOR then
- ply:Give( "weapon_physcannon" )
- ply:Give( "weapon_physgun" )
- end
- end
- function SpawnNPCsFromDatabase()
- local query1 = db:query( "SELECT * FROM `npcs` WHERE `map` = '"..game.GetMap().."';" )
- function query1:onSuccess( data )
- if data[1] then
- for k,v in pairs(data or {}) do
- local npc = ents.Create(v.class)
- if npc:IsValid() then
- npc:SetPos(Vector(v.pos))
- npc:SetAngles(Angle(v.ang))
- npc:Spawn()
- else print("[ERROR] Trying to spawn invalid NPC "..v.name.."("..v.id..")")
- end
- end
- else print("[MySQL]: No NPC's for this map were found.")
- end
- end
- function query1:onError( err, sql )
- print( "[MySQL]: Query error while trying to load NPC's from the database!" )
- print( "Query:", sql )
- print( "Error:", err )
- end
- query1:start()
- query1:wait()
- end
- function InitPostEntity()
- -- npcs =
- -- {
- -- {
- -- class = "npc_job_cop",
- -- pos = Vector(370, 4420, 132.03125),
- -- ang = Angle(0, -180, 0),
- -- name = "Cop"
- -- },
- -- }
- SpawnNPCsFromDatabase()
- end
- hook.Add( "InitPostEntity", "InitPostEntity", InitPostEntity )
- function GM:Think()
- -- Anti Timeout System
- -- function AntiTimeout() local query = db:query( "SELECT 1+1" ) query:start() antiTimeout = 0 end
- -- if antiTimeout == 0 then timer.Simple(30, AntiTimeout, nil) antiTimeout = 1 end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement