Advertisement
Guest User

Untitled

a guest
May 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 27.01 KB | None | 0 0
  1. include("static_data.lua")
  2. DB.privcache = {}
  3. /*---------------------------------------------------------
  4. Escape Function / DB Connect
  5. ---------------------------------------------------------*/
  6. function StripForHTTP ( Name, Stop )
  7.     if !Name then return "NA"; end
  8.  
  9.     Name = mysql.escape(db, Name);
  10.        
  11.     return tostring(Name);
  12. end
  13.  
  14. require( "mysql" )
  15.  
  16. local host = "localhost"
  17. local username = "user"
  18. local password = "pw"
  19. local db = "db"
  20.  
  21. db, error = mysql.connect(host, username, password, db)  
  22. if (db == 0) then print(tostring(error) .. "\n") return end  
  23. /*---------------------------------------------------------
  24.  Database initialize
  25.  ---------------------------------------------------------*/
  26. function DB.Init()
  27.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_settings('key' TEXT NOT NULL, 'value' INTEGER NOT NULL, PRIMARY KEY('key'))");
  28.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_globals('key' TEXT NOT NULL, 'value' INTEGER NOT NULL, PRIMARY KEY('key'))");
  29.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_tspawns('map' TEXT NOT NULL, 'team' INTEGER NOT NULL, 'x' NUMERIC NOT NULL, 'y' NUMERIC NOT NULL, 'z' NUMERIC NOT NULL, PRIMARY KEY('map', 'team'))");
  30.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_privs('steam' TEXT NOT NULL, 'admin' INTEGER NOT NULL, 'mayor' INTEGER NOT NULL, 'cp' INTEGER NOT NULL, 'tool' INTEGER NOT NULL, 'phys' INTEGER NOT NULL, 'prop' INTEGER NOT NULL, PRIMARY KEY('steam'))");
  31.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_salaries('steam' TEXT NOT NULL, 'salary' INTEGER NOT NULL, PRIMARY KEY('steam'))");
  32.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_wallets('steam' TEXT NOT NULL, 'amount' INTEGER NOT NULL, PRIMARY KEY('steam'))");
  33.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_jailpositions('map' TEXT NOT NULL, 'x' NUMERIC NOT NULL, 'y' NUMERIC NOT NULL, 'z' NUMERIC NOT NULL, 'lastused' NUMERIC NOT NULL, PRIMARY KEY('map', 'x', 'y', 'z'))");
  34.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_rpnames('steam' TEXT NOT NULL, 'name' TEXT NOT NULL, PRIMARY KEY('steam'))");
  35.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_zspawns('map' TEXT NOT NULL, 'x' NUMERIC NOT NULL, 'y' NUMERIC NOT NULL, 'z' NUMERIC NOT NULL)");
  36.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_wiseguys('steam' TEXT NOT NULL, 'time' NUMERIC NOT NULL, PRIMARY KEY('steam'))");
  37.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_disableddoors('map' TEXT NOT NULL, 'idx' INTEGER NOT NULL, 'title' TEXT NOT NULL, PRIMARY KEY('map', 'idx'))");
  38.         mysql.query(db, "CREATE TABLE IF NOT EXISTS darkrp_cpdoors('map' TEXT NOT NULL, 'idx' INTEGER NOT NULL, 'title' TEXT NOT NULL, PRIMARY KEY('map', 'idx'))");
  39.  
  40.     DB.CreatePrivs()
  41.     DB.CreateJailPos()
  42.     DB.CreateSpawnPos()
  43.     DB.CreateZombiePos()
  44.     DB.SetUpNonOwnableDoors()
  45. end
  46.  
  47. /*---------------------------------------------------------
  48.  The privileges
  49.  ---------------------------------------------------------*/
  50. function DB.CreatePrivs()
  51.     if reset_all_privileges_to_these_on_startup then
  52.         mysql.query(db, "DELETE FROM darkrp_privs");
  53.     end
  54.     local already_inserted = {}
  55.     for k, v in pairs(RPAdmins) do
  56.         local admin = 0
  57.         local mayor = 0
  58.         local cp = 0
  59.         local tool = 0
  60.         local phys = 0
  61.         local prop = 0
  62.         for a, b in pairs(RPAdmins[k]) do
  63.             if b == ADMIN then admin = 1 end
  64.             if b == MAYOR then mayor = 1 end
  65.             if b == CP then cp = 1 end
  66.             if b == PTOOL then tool = 1 end
  67.             if b == PHYS then phys = 1 end
  68.             if b == PROP then prop = 1 end
  69.         end
  70.         if already_inserted[RPAdmins[k]] then
  71.             mysql.query(db, "UPDATE darkrp_privs SET admin = " .. admin .. ", mayor = " .. mayor .. ", cp = " .. cp .. ", tool = " .. tool .. ", phys = " .. phys .. ", prop = " .. prop .. " WHERE steam = " .. StripForHTTP(RPAdmins[k]) .. "");
  72.         else
  73.             mysql.query(db, "INSERT INTO darkrp_privs VALUES(" .. StripForHTTP(k) .. ", " .. admin .. ", " .. mayor .. ", " .. cp .. ", " .. tool .. ", " .. phys .. ", " .. prop .. ")");
  74.             already_inserted[RPAdmins[k]] = true
  75.         end
  76.     end
  77. end
  78.  
  79. function DB.Priv2Text(priv)
  80.     if priv == ADMIN then
  81.         return "admin"
  82.     elseif priv == MAYOR then
  83.         return "mayor"
  84.     elseif priv == CP then
  85.         return "cp"
  86.     elseif priv == PTOOL then
  87.         return "tool"
  88.     elseif priv == PHYS then
  89.         return "phys"
  90.     elseif priv == PROP then
  91.         return "prop"
  92.     else
  93.         return nil
  94.     end
  95. end
  96.  
  97. function DB.HasPriv(ply, priv)
  98.     local SteamID = ply:SteamID()
  99.     if priv == ADMIN and (ply:EntIndex() == 0 or ply:IsAdmin()) then return true end
  100.  
  101.     local p = DB.Priv2Text(priv)
  102.     if not p then return false end
  103.  
  104.     -- If there is a current cache of priveleges
  105.     if DB.privcache[SteamID] and DB.privcache[SteamID][p] ~= nil then
  106.         if DB.privcache[SteamID][p] == 1 then
  107.             return true
  108.         else
  109.             return false
  110.         end
  111.     -- If there is no cache for this user
  112.     else
  113.         local result2 = mysql.query(db, "SELECT " .. StripForHTTP(p) .. " FROM darkrp_privs WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  114.         local result = tonumber(result2)
  115.         if not DB.privcache[SteamID] then
  116.             DB.privcache[SteamID] = {}
  117.         end
  118.  
  119.         if result == 1 then
  120.             DB.privcache[SteamID][p] = 1
  121.             return true
  122.         else
  123.             DB.privcache[SteamID][p] = 0
  124.             return false
  125.         end
  126.     end
  127. end
  128.  
  129. function DB.GrantPriv(ply, priv)
  130.     local steamID = ply:SteamID()
  131.     local p = DB.Priv2Text(priv)
  132.     if not p then return false end
  133.     local privsql = mysql.query(db, "SELECT COUNT(*) FROM darkrp_privs WHERE steam = " .. StripForHTTP(steamID) .. "");
  134.     if tonumber(privsql) > 0 then
  135.         mysql.query(db, "UPDATE darkrp_privs SET " .. p .. " = 1 WHERE steam = " .. StripForHTTP(steamID) .. "");
  136.     else
  137.         mysql.query(db, "INSERT INTO darkrp_privs VALUES(" .. StripForHTTP(steamID) .. ", 0, 0, 0, 0, 0, 0)");
  138.         mysql.query(db, "UPDATE darkrp_privs SET " .. StripForHTTP(p) .. " = 1 WHERE steam = " .. StripForHTTP(steamID) .. "");
  139.     end
  140.     -- privelege structure altered, fix the goddamn cache
  141.  
  142.     if DB.privcache[steamID] == nil then
  143.         DB.privcache[steamID] = {}
  144.     end
  145.     ply:SetNWBool("Priv"..p, true)
  146.     DB.privcache[steamID][p] = 1
  147.     return true
  148. end
  149.  
  150. function DB.RevokePriv(ply, priv)
  151.     local steamID = ply:SteamID()  
  152.     local p = DB.Priv2Text(priv)
  153.     local val2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_privs WHERE steam = " .. StripForHTTP(steamID) .. "");
  154.     local val = tonumber(val2)
  155.     if not p or val < 1 then return false end
  156.     mysql.query(db, "UPDATE darkrp_privs SET " .. p .. " = 0 WHERE steam = " .. StripForHTTP(steamID) .. "");
  157.    
  158.     -- privelege structure altered, alter the cache
  159.     if DB.privcache[steamID] == nil then
  160.         DB.privcache[steamID] = {}
  161.     end
  162.     DB.privcache[steamID][p] = 0
  163.     if ply:GetNWBool("Priv"..p) then
  164.         ply:SetNWBool("Priv"..p, false)
  165.     end
  166.     return true
  167. end
  168.  
  169. /*---------------------------------------------------------
  170.  positions
  171.  ---------------------------------------------------------*/
  172. function DB.CreateSpawnPos()
  173.     local map = string.lower(game.GetMap())
  174.     if not team_spawn_positions then return end
  175.  
  176.     for k, v in pairs(team_spawn_positions) do
  177.         if v[1] == map then
  178.             DB.StoreTeamSpawnPos(v[2], Vector(v[3], v[4], v[5]))
  179.         end
  180.     end
  181. end
  182.  
  183. function DB.CreateZombiePos()
  184.     if not zombie_spawn_positions then return end
  185.     local map = string.lower(game.GetMap())
  186.  
  187.     local once = false
  188.  
  189.         for k, v in pairs(zombie_spawn_positions) do
  190.             if map == string.lower(v[1]) then
  191.                 if not once then
  192.                     mysql.query(db, "DELETE FROM darkrp_zspawns");
  193.                     once = true
  194.                 end
  195.                 mysql.query(db, "INSERT INTO darkrp_zspawns VALUES(" .. StripForHTTP(map) .. ", " .. v[2] .. ", " .. v[3] .. ", " .. v[4] .. ")");
  196.             end
  197.         end
  198. end
  199.  
  200. function DB.StoreZombies()
  201.     local map = string.lower(game.GetMap())
  202.     mysql.query(db, "DELETE FROM darkrp_zspawns WHERE map = " .. StripForHTTP(map) .. "");
  203.  
  204.     for k, v in pairs(zombieSpawns) do
  205.         local s = string.Explode(" ", v)
  206.         mysql.query(db, "INSERT INTO darkrp_zspawns VALUES(" .. StripForHTTP(map) .. ", " .. s[1] .. ", " .. s[2] .. ", " .. s[3] .. ")");
  207.     end
  208. end
  209.  
  210. function DB.RetrieveZombies()
  211.     zombieSpawns = {}
  212.     local r = mysql.query(db, "SELECT * FROM darkrp_zspawns WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. "");
  213.     if not r then return end
  214.     for map, row in pairs(r) do
  215.         zombieSpawns[map] = tostring(row.x) .. " " .. tostring(row.y) .. " " .. tostring(row.z)
  216.     end
  217. end
  218.  
  219. local function IsEmpty(vector)
  220.     local point = util.PointContents(vector)
  221.     local a = point ~= CONTENTS_SOLID
  222.     and point ~= CONTENTS_MOVEABLE
  223.     and point ~= CONTENTS_LADDER
  224.     and point ~= CONTENTS_PLAYERCLIP
  225.     and point ~= CONTENTS_MONSTERCLIP
  226.     local b = true
  227.    
  228.     for k,v in pairs(ents.FindInSphere(vector, 35)) do
  229.         if v:IsNPC() or v:IsPlayer() or v:GetClass() == "prop_physics" then
  230.             b = false
  231.         end
  232.     end
  233.     return a and b
  234. end
  235.  
  236. function DB.RetrieveRandomZombieSpawnPos()
  237.     local map = string.lower(game.GetMap())
  238.     local r = false
  239.     local c2 = mysql.query(db, "SELECT * FROM darkrp_zspawns WHERE map = " .. StripForHTTP(map) .. "");
  240.     local c = tonumber(c2)
  241.    
  242.     if c and c >= 1 then
  243.         r = math.random(1, c)
  244.         if not IsEmpty(Vector(r.x, r.y, r.z)) then
  245.             local found = false
  246.             for i = 40, 200, 10 do
  247.                 if IsEmpty(Vector(r.x, r.y, r.z) + Vector(i, 0, 0)) then
  248.                     found = true
  249.                     return Vector(r.x, r.y, r.z) + Vector(i, 0, 0)
  250.                 end
  251.             end
  252.            
  253.             if not found then
  254.                 for i = 40, 200, 10 do
  255.                     if IsEmpty(Vector(r.x, r.y, r.z) + Vector(0, i, 0)) then
  256.                         found = true
  257.                         return Vector(r.x, r.y, r.z) + Vector(0, i, 0)
  258.                     end
  259.                 end
  260.             end
  261.            
  262.             if not found then
  263.                 for i = 40, 200, 10 do
  264.                     if IsEmpty(Vector(r.x, r.y, r.z) + Vector(-i, 0, 0)) then
  265.                         found = true
  266.                         return Vector(r.x, r.y, r.z) + Vector(-i, 0, 0)
  267.                     end
  268.                 end
  269.             end
  270.            
  271.             if not found then
  272.                 for i = 40, 200, 10 do
  273.                     if IsEmpty(Vector(r.x, r.y, r.z) + Vector(0, -i, 0)) then
  274.                         found = true
  275.                         return Vector(r.x, r.y, r.z) + Vector(0, -i, 0)
  276.                     end
  277.                 end
  278.             end
  279.         else
  280.             return Vector(r.x, r.y, r.z)
  281.         end
  282.     end  
  283.     return Vector(r.x, r.y, r.z) + Vector(0,0,70)        
  284. end
  285.  
  286. function DB.CreateJailPos()
  287.     if not jail_positions then return end
  288.     local map = string.lower(game.GetMap())
  289.  
  290.     local once = false
  291.         for k, v in pairs(jail_positions) do
  292.             if map == string.lower(v[1]) then
  293.                 if not once then
  294.                     mysql.query(db, "DELETE FROM darkrp_jailpositions");
  295.                     once = true
  296.                 end
  297.                 mysql.query(db, "INSERT INTO darkrp_jailpositions VALUES(" .. StripForHTTP(map) .. ", " .. v[2] .. ", " .. v[3] .. ", " .. v[4] .. ", " .. 0 .. ")");
  298.             end
  299.         end
  300. end
  301.  
  302. function DB.StoreJailPos(ply, addingPos)
  303.     local map = string.lower(game.GetMap())
  304.     local pos = string.Explode(" ", tostring(ply:GetPos()))
  305.     local already2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_jailpositions WHERE map = " .. StripForHTTP(map) .. "");
  306.     local already = tonumber(already2)
  307.     if not already or already == 0 then
  308.         mysql.query(db, "INSERT INTO darkrp_jailpositions VALUES(" .. StripForHTTP(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ")");
  309.         Notify(ply, 1, 4,  "You have created the first jail position!")
  310.     else
  311.         if addingPos then
  312.             mysql.query(db, "INSERT INTO darkrp_jailpositions VALUES(" .. StripForHTTP(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ")");
  313.             Notify(ply, 1, 4,  "You have added one extra jail position!")
  314.         else
  315.             mysql.query(db, "DELETE FROM darkrp_jailpositions WHERE map = " .. StripForHTTP(map) .. "");
  316.             mysql.query(db, "INSERT INTO darkrp_jailpositions VALUES(" .. StripForHTTP(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ")");
  317.             Notify(ply, 1, 5,  "You have removed all jail positions and you have added a new one here.")
  318.         end
  319.     end
  320. end
  321.  
  322. function DB.RetrieveJailPos()
  323.     local map = string.lower(game.GetMap())
  324.     local r = mysql.query(db, "SELECT x, y, z, lastused FROM darkrp_jailpositions WHERE map = " .. StripForHTTP(map) .. "");
  325.     if not r then return Vector(0,0,0) end
  326.  
  327.     -- Retrieve the least recently used jail position
  328.     local now = CurTime()
  329.     local oldest = 0
  330.     local ret = nil
  331.  
  332.     for _, row in pairs(r) do
  333.         if (now - tonumber(row.lastused)) > oldest then
  334.             oldest = (now - tonumber(row.lastused))
  335.             ret = row
  336.         end
  337.     end
  338.  
  339.     -- Mark that position as having been used just now
  340.     mysql.query(db, "UPDATE darkrp_jailpositions SET lastused = " .. CurTime() .. " WHERE map = " .. StripForHTTP(map) .. " AND x = " .. ret.x .. " AND y = " .. ret.y .. " AND z = " .. ret.z .. "");
  341.  
  342.     return Vector(ret.x, ret.y, ret.z)
  343. end
  344.  
  345. function DB.CountJailPos()
  346.     local cjp = mysql.query(db, "SELECT COUNT(*) FROM darkrp_jailpositions WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. "");
  347.     return tonumber(cjp)
  348. end
  349.  
  350. function DB.StoreJailStatus(ply, time)
  351.     local steamID = ply:SteamID()
  352.     -- Is there an existing outstanding jail sentence for this player?
  353.     local r2 = mysql.query(db, "SELECT time FROM darkrp_wiseguys WHERE steam = " .. StripForHTTP(steamID) .. "");
  354.     local r = tonumber(r2)
  355.  
  356.    
  357.     if not r and time ~= 0 then
  358.         -- If there is no jail record for this player and we're not trying to clear an existing one
  359.         mysql.query(db, "INSERT INTO darkrp_wiseguys VALUES(" .. StripForHTTP(steamID) .. ", " .. time .. ")");
  360.     else
  361.         -- There is a jail record for this player
  362.         if time == 0 then
  363.             -- If we are reducing their jail time to zero, delete their record
  364.             mysql.query(db, "DELETE FROM darkrp_wiseguys WHERE steam = " .. StripForHTTP(steamID) .. "");
  365.         else
  366.             -- Increase this player's sentence by the amount specified
  367.             mysql.query(db, "UPDATE darkrp_wiseguys SET time = " .. r + time .. " WHERE steam = " .. StripForHTTP(steamID) .. ")");
  368.         end
  369.     end
  370. end
  371.  
  372. function DB.StoreTeamSpawnPos(t, pos)
  373.     local map = string.lower(game.GetMap())
  374.     local already2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_tspawns WHERE team = " .. t .. " AND map = " .. StripForHTTP(map) .. "");
  375.     local already = tonumber(already2)
  376.     if not already or already == 0 then
  377.         mysql.query(db, "INSERT INTO darkrp_tspawns VALUES(" .. StripForHTTP(map) .. ", " .. t .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ")");
  378.         print(team.GetName(t).."'s spawn position created.")
  379.     else
  380.         mysql.query(db, "UPDATE darkrp_tspawns SET x = " .. pos[1] .. ", y = " .. pos[2] .. ", z = " .. pos[3] .. " WHERE team = " .. t .. " AND map = " .. StripForHTTP(map) .. "");
  381.         print(team.GetName(t).."'s spawn position updated.")
  382.     end
  383. end
  384.  
  385. function DB.RetrieveTeamSpawnPos(ply)
  386.     local map = string.lower(game.GetMap())
  387.     local t = ply:Team()
  388.    
  389.     -- this should return a map name.
  390.     local r = mysql.query(db, "SELECT x FROM darkrp_tspawns WHERE team = " .. t .. " AND map = ".. StripForHTTP(map).."");
  391.     if not r then return nil end
  392.    
  393.     local x = mysql.query(db, "SELECT x FROM darkrp_tspawns WHERE team = " .. t .. " AND map = ".. StripForHTTP(map).."");
  394.     local y = mysql.query(db, "SELECT y FROM darkrp_tspawns WHERE team = " .. t .. " AND map = ".. StripForHTTP(map).."");
  395.     local z = mysql.query(db, "SELECT z FROM darkrp_tspawns WHERE team = " .. t .. " AND map = ".. StripForHTTP(map).."");
  396.     return Vector(x,y,z)
  397. end
  398.  
  399. /*---------------------------------------------------------
  400. Players
  401.  ---------------------------------------------------------*/
  402. function DB.RetrieveJailStatus(ply)
  403.     -- How much time does this player owe in jail?
  404.     local r2 = mysql.query(db, "SELECT time FROM darkrp_wiseguys WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  405.     local r = tonumber()
  406.     if r then
  407.         return r
  408.     else
  409.         return 0
  410.     end
  411. end
  412.  
  413. function DB.StoreRPName(ply, name)
  414.     if not name or string.len(name) < 2 then return end
  415.     local r = mysql.query(db, "SELECT name FROM darkrp_rpnames WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  416.     if r then
  417.         mysql.query(db, "UPDATE darkrp_rpnames SET name = " .. StripForHTTP(name) .. " WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  418.     else
  419.         mysql.query(db, "INSERT INTO darkrp_rpnames VALUES(" .. StripForHTTP(ply:SteamID()) .. ", " .. StripForHTTP(name) .. ")");
  420.     end
  421.  
  422.     ply:SetNWString("rpname", name)
  423. end
  424.  
  425. local rpnameslist --Make sure the DB doesn't get checked for ALL RPnames when someone InitialSpawns
  426. function DB.RetrieveRPNames()
  427.     if rpnameslist then
  428.         return rpnameslist
  429.     end
  430.    
  431.     local r = mysql.query(db, "SELECT * FROM darkrp_rpnames");
  432.     if r then rpnameslist = r return rpnameslist
  433.     else rpnameslist = {} return {} end
  434. end
  435.  
  436. function DB.RetrieveRPName(ply)
  437.     return mysql.query(db, "SELECT name FROM darkrp_rpnames WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  438. end
  439.  
  440. function DB.StoreMoney(ply, amount)
  441.     if not ValidEntity(ply) then return end
  442.     if amount < 0  then return end
  443.     local steamID = ply:SteamID()
  444.     local r = mysql.query(db, "SELECT amount FROM darkrp_wallets WHERE steam = " .. StripForHTTP(steamID) .. "");
  445.     if r then
  446.         mysql.query(db, "UPDATE darkrp_wallets SET amount = " .. math.floor(amount) .. " WHERE steam = " .. StripForHTTP(steamID) .. "");
  447.     else
  448.         mysql.query(db, "INSERT INTO darkrp_wallets VALUES(" .. StripForHTTP(steamID) .. ", " .. math.floor(amount) .. ")");
  449.     end
  450.     ply:SetNWInt("money", math.floor(amount))
  451. end
  452.  
  453. function DB.RetrieveMoney(ply)
  454.     if not ValidEntity(ply) then return 0 end
  455.     local steamID = ply:SteamID()
  456.     local startingAmount = 500
  457.        
  458.     local r = mysql.query(db, "SELECT amount FROM darkrp_wallets WHERE steam = " .. StripForHTTP(ply:SteamID()) .. "");
  459.     if r then
  460.         ply:SetNWInt("money", math.floor(r))
  461.         return r
  462.     else
  463.         -- No record yet, setting starting cash to 500
  464.         DB.StoreMoney(ply, startingAmount)
  465.         return startingAmount
  466.     end
  467. end
  468.  
  469. function DB.ResetAllMoney(ply,cmd,args)
  470.     if not ply:IsSuperAdmin() then return end
  471.     mysql.query(db, "DELETE FROM darkrp_wallets");
  472.     for k,v in pairs(player.GetAll()) do
  473.         DB.StoreMoney(v, 500)
  474.     end
  475.     if ply:IsPlayer() then
  476.         NotifyAll(1,4, ply:Nick() .. " has reset all player's money!")
  477.     else
  478.         NotifyAll(1,4, "Console has reset all player's money!")
  479.     end
  480. end
  481. concommand.Add("rp_resetallmoney", DB.ResetAllMoney)
  482.  
  483. function DB.PayPlayer(ply1, ply2, amount)
  484.     if not ValidEntity(ply1) or not ValidEntity(ply2) then return end
  485.     local sid1 = ply1:SteamID()
  486.     local sid2 = ply2:SteamID()
  487.     sql.Begin() -- Transaction
  488.         mysql.query(db, "UPDATE darkrp_wallets SET amount = amount - " ..  amount .. " WHERE steam = " .. StripForHTTP(sid1) .. "");
  489.         mysql.query(db, "UPDATE darkrp_wallets SET amount = amount + " ..  amount .. " WHERE steam = " .. StripForHTTP(sid2) .. "");
  490.     sql.Commit()
  491.     DB.RetrieveMoney(ply1)
  492.     DB.RetrieveMoney(ply2)
  493. end
  494.  
  495. function DB.StoreSalary(ply, amount)
  496.     local steamID = ply:SteamID()
  497.     local already2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_salaries WHERE steam = " .. StripForHTTP(steamID) .. "");
  498.     local already = tonumber(already2)
  499.     if not already or already == 0 then
  500.         mysql.query(db, "INSERT INTO darkrp_salaries VALUES(" .. StripForHTTP(steamID) .. ", " .. math.floor(amount) .. ")");
  501.     else
  502.         mysql.query(db, "UPDATE darkrp_salaries SET salary = " .. math.floor(amount) .. " WHERE steam = " .. StripForHTTP(steamID) .. "");
  503.     end
  504.     ply:SetNWInt("salary", math.floor(amount))
  505.     return amount
  506. end
  507.  
  508. function DB.RetrieveSalary(ply)
  509.     if not ValidEntity(ply) then return 0 end
  510.     local steamID = ply:SteamID()
  511.     local normal = GetGlobalInt("normalsalary")
  512.  
  513.     local r = mysql.query(db, "SELECT salary FROM darkrp_salaries WHERE steam = " .. StripForHTTP(steamID) .. "");
  514.     if not r then
  515.         DB.StoreSalary(ply, normal)
  516.         return normal
  517.     else
  518.         return r
  519.     end
  520. end
  521.  
  522. /*---------------------------------------------------------
  523.  Doors
  524.  ---------------------------------------------------------*/
  525. function DB.StoreDoorOwnability(ent)
  526.     local map = string.lower(game.GetMap())
  527.     local nonOwnable = ent:GetNWBool("nonOwnable")
  528.     local r2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_disableddoors WHERE map = " .. StripForHTTP(map) .. " AND idx = " .. ent:EntIndex() .. "");
  529.     local r = tonumber(r2)
  530.     if not r then return end
  531.  
  532.     if r > 0 and not nonOwnable then
  533.         mysql.query(db, "DELETE FROM darkrp_disableddoors WHERE map = " .. StripForHTTP(map) .. " AND idx = " .. ent:EntIndex() .. "");
  534.     elseif r == 0 and nonOwnable then
  535.         mysql.query(db, "INSERT INTO darkrp_disableddoors VALUES(" .. StripForHTTP(map) .. ", " .. ent:EntIndex() .. ", " .. StripForHTTP("Non-Ownable Door") .. ")");
  536.     end
  537. end
  538.  
  539. function DB.StoreNonOwnableDoorTitle(ent, text)
  540.     mysql.query(db, "UPDATE darkrp_disableddoors SET title = " .. StripForHTTP(text) .. " WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. " AND idx = " .. ent:EntIndex() .. "");
  541.     ent:SetNWString("title", text)
  542. end
  543.  
  544. function DB.SetUpNonOwnableDoors()
  545.     local r = mysql.query(db, "SELECT idx, title FROM darkrp_disableddoors WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. "");
  546.     if not r then return end
  547.  
  548.     for _, row in pairs(r) do
  549.         local e = ents.GetByIndex(tonumber(row.idx))
  550.         e:SetNWBool("nonOwnable", true)
  551.         e:SetNWString("title", row.title)
  552.     end
  553. end
  554.  
  555.  
  556. function DB.StoreCPDoorOwnability(ent)
  557.     local map = string.lower(game.GetMap())
  558.     local CPOwnable = ent:GetNWBool("CPOwnable")
  559.     local r2 = mysql.query(db, "SELECT COUNT(*) FROM darkrp_cpdoors WHERE map = " .. StripForHTTP(map) .. " AND idx = " .. ent:EntIndex() .. "");
  560.     local r = tonumber(r2)
  561.     if not r then return end
  562.  
  563.     if r > 0 and not CPOwnable then
  564.         mysql.query(db, "DELETE FROM darkrp_cpdoors WHERE map = " .. StripForHTTP(map) .. " AND idx = " .. ent:EntIndex() .. "");
  565.     elseif r == 0 and CPOwnable then
  566.         mysql.query(db, "INSERT INTO darkrp_cpdoors VALUES(" .. StripForHTTP(map) .. ", " .. ent:EntIndex() .. ", " .. StripForHTTP("CP-Ownable Door") .. ")");
  567.     end
  568. end
  569.  
  570. function DB.StoreCPOwnableDoorTitle(ent, text)
  571.     mysql.query(db, "UPDATE darkrp_cpdoors SET title = " .. StripForHTTP(text) .. " WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. " AND idx = " .. ent:EntIndex() .. "");
  572.     ent:SetNWString("title", text)
  573. end
  574.  
  575. function DB.SetUpCPOwnableDoors()
  576.     local r = mysql.query(db, "SELECT idx, title FROM darkrp_cpdoors WHERE map = " .. StripForHTTP(string.lower(game.GetMap())) .. "");
  577.     if not r then return end
  578.  
  579.     for _, row in pairs(r) do
  580.         local e = ents.GetByIndex(tonumber(row.idx))
  581.         e:SetNWBool("CPOwnable", true)
  582.         e:SetNWString("title", row.title)
  583.     end
  584. end
  585.  
  586. /*---------------------------------------------------------
  587.  Settings and globals
  588.  ---------------------------------------------------------*/
  589. function DB.RetrieveSettings()
  590.     local r = mysql.query(db, "SELECT key, value FROM darkrp_settings");
  591.     if not r then return false end
  592.    
  593.     for k, v in pairs(r) do
  594.         CfgVars[v.key] = tonumber(v.value)
  595.         SetGlobalInt(v.key, v.value)-- Set the global INT so clients can access this information in the admin menu! This does not save though!
  596.     end
  597.     return true
  598. end
  599.  
  600. function DB.SaveSetting(key, value)
  601.     local r = mysql.query(db, "SELECT value FROM darkrp_settings WHERE key = "..StripForHTTP(key).."");
  602.     if not r then
  603.         mysql.query(db, "INSERT INTO darkrp_settings VALUES(" .. StripForHTTP(key) .. ", " .. value .. ")");
  604.         print("Created", key, "=", value)
  605.     elseif tonumber(r) ~= value then
  606.         mysql.query(db, "UPDATE darkrp_settings SET value = " .. value .. " WHERE key = " .. StripForHTTP(key) .. "");
  607.         print("updated", key, "to", value)
  608.     end
  609.     CfgVars[key] = value
  610.     SetGlobalInt(key, value)-- Set the global INT so clients can access this information in the admin menu! This does not save though!
  611. end
  612.  
  613. function DB.RemoveSettings()
  614.     mysql.query(db, "DELETE FROM darkrp_settings");
  615. end
  616.  
  617. function DB.RetrieveGlobals()
  618.     local r = mysql.query(db, "SELECT key, value FROM darkrp_globals");
  619.     if not r then return false end
  620.    
  621.     for k, v in pairs(r) do
  622.         SetGlobalInt(v.key, tonumber(v.value))
  623.     end
  624.     if #r < 30 then
  625.         RefreshGlobals()
  626.     end
  627.     return r
  628. end
  629.  
  630. function DB.SaveGlobal(key, value)
  631.     local r = mysql.query(db, "SELECT value FROM darkrp_globals WHERE key = "..StripForHTTP(key).."");
  632.     if not r then
  633.         mysql.query(db, "INSERT INTO darkrp_globals VALUES(" .. StripForHTTP(key) .. ", " .. value .. ")");
  634.         print("Created", key, "=", value)
  635.     elseif tonumber(r) ~= value then
  636.         mysql.query(db, "UPDATE darkrp_globals SET value = " .. value .. " WHERE key = " .. StripForHTTP(key) .. "");
  637.         print("updated", key, "to", value)
  638.     end
  639.     SetGlobalInt(key, value)
  640. end
  641.  
  642. function DB.RemoveGlobals()
  643.     mysql.query(db, "DELETE FROM darkrp_globals");
  644. end
  645.  
  646. function ResetAllRPSettings(ply,cmd,args)
  647.     if ply:EntIndex() ~= 0 and not ply:IsSuperAdmin() then
  648.         Notify(ply, 1, 5, "You need to be superadmin in order to be able to reset all RP settings.")
  649.         return
  650.     end
  651.     Notify(ply, 1, 4, "You have reset all settings!")
  652.     RefreshRPSettings(true)
  653.     RefreshGlobals()
  654. end
  655. concommand.Add("rp_ResetAllSettings", ResetAllRPSettings)
  656.  
  657. function RefreshGlobals()
  658.     DB.SaveGlobal("druglabcost", 400)
  659.     DB.SaveGlobal("gunlabcost", 500)
  660.     DB.SaveGlobal("mprintercost", 1000)
  661.     DB.SaveGlobal("mprintamount", 250)
  662.     DB.SaveGlobal("microwavecost", 400)
  663.     DB.SaveGlobal("drugpayamount", 15)
  664.     DB.SaveGlobal("ammopistolcost", 30)
  665.     DB.SaveGlobal("ammoriflecost", 60)
  666.     DB.SaveGlobal("ammoshotguncost", 70)
  667.     DB.SaveGlobal("healthcost", 60)
  668.     DB.SaveGlobal("jailtimer", 120)
  669.     DB.SaveGlobal("microwavefoodcost", 30)
  670.     DB.SaveGlobal("maxcopsalary", 100)
  671.     DB.SaveGlobal("maxdrugfood", 2)
  672.     DB.SaveGlobal("npckillpay", 10)
  673.     DB.SaveGlobal("jobtag", 1)
  674.     DB.SaveGlobal("globalshow", 0)
  675.     DB.SaveGlobal("deathnotice", 1)
  676.     DB.SaveGlobal("normalsalary", 45)
  677.     DB.SaveGlobal("globaltags", 1)
  678.     DB.SaveGlobal("nametag", 1)
  679.     DB.SaveGlobal("deathblack", 0)
  680.     DB.SaveGlobal("maxnormalsalary", 90)
  681.     DB.SaveGlobal("maxmayorsetsalary", 90)
  682.    
  683.     DB.SaveGlobal("licenseweapon_weapon_physcannon", 1)
  684.     DB.SaveGlobal("licenseweapon_weapon_physgun", 1)
  685.     DB.SaveGlobal("licenseweapon_weapon_crowbar", 1)
  686.     DB.SaveGlobal("licenseweapon_weapon_stunstick", 1)
  687.     DB.SaveGlobal("licenseweapon_weapon_pistol", 0)
  688.     DB.SaveGlobal("licenseweapon_weapon_357",   0)
  689.     DB.SaveGlobal("licenseweapon_weapon_smg1", 0)
  690.     DB.SaveGlobal("licenseweapon_weapon_shotgun", 0)
  691.     DB.SaveGlobal("licenseweapon_weapon_crossbow", 0)
  692.     DB.SaveGlobal("licenseweapon_weapon_ar2", 0)
  693.     DB.SaveGlobal("licenseweapon_weapon_bugbait", 1)
  694.     DB.SaveGlobal("licenseweapon_weapon_rpg", 0)
  695.     DB.SaveGlobal("licenseweapon_gmod_camera", 1)
  696.    
  697.     local whitelist = {"keys", "gmod_camera", "weaponchecker", "med_kit", "arrest_stick", "unarrest_stick", "stunstick", "door_ram", "lockpick", "bite", "pcmod_", "gmod_tool", "pocket"}
  698.     for k,v in pairs(weapons.GetList()) do
  699.         local allowed = false
  700.         for a,b in pairs(whitelist) do
  701.             if string.find(string.lower(v.Classname), b) then
  702.                 DB.SaveGlobal("licenseweapon_"..string.lower(v.Classname), 1)
  703.                 allowed = true
  704.             end
  705.         end
  706.         if not allowed then
  707.             DB.SaveGlobal("licenseweapon_"..string.lower(v.Classname), 0)
  708.         end
  709.     end
  710. end
  711.  
  712. /*---------------------------------------------------------
  713.  Logging
  714.  ---------------------------------------------------------*/
  715. function DB.Log(text)
  716.     if not util.tobool(CfgVars["logging"]) or not text then return end
  717.     if not DB.File then -- The log file of this session, if it's not there then make it!
  718.         if not file.IsDir("DarkRP_logs") then
  719.             file.CreateDir("DarkRP_logs")
  720.         end
  721.         DB.File = "DarkRP_logs/"..os.date("%m_%d_%Y %I_%M %p")..".txt"
  722.         file.Write(DB.File, os.date().. "\t".. text)
  723.         return
  724.     end
  725.     file.Write(DB.File, file.Read(DB.File).."\n"..os.date().. "\t"..text)
  726. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement