Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.83 KB | None | 0 0
  1. ESX                 = nil
  2. Jobs                = {}
  3. RegisteredSocieties = {}
  4.  
  5. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  6.  
  7. function sendToDiscord (name,message,color)
  8.   -- Modify here your discordWebHook username = name, content = message,embeds = embeds
  9.  
  10. local embeds = {
  11.     {
  12.         ["title"]=message,
  13.         ["type"]="rich",
  14.         ["color"] =color,
  15.         ["footer"]=  {
  16.         ["text"]= "Przy użyciu pluginu esx_society",
  17.        },
  18.     }
  19. }
  20.  
  21.   if message == nil or message == '' then return FALSE end
  22.   PerformHttpRequest(Config.webhook, function(err, text, headers) end, 'POST', json.encode({ username = name,embeds = embeds}), { ['Content-Type'] = 'application/json' })
  23. end
  24.  
  25. function sendToDiscord2 (name,message,color)
  26.   -- Modify here your discordWebHook username = name, content = message,embeds = embeds
  27.  
  28. local embeds = {
  29.     {
  30.         ["title"]=message,
  31.         ["type"]="rich",
  32.         ["color"] =color,
  33.         ["footer"]=  {
  34.         ["text"]= "Przejebana sprawa! poinformuj kogoś z Administracji",
  35.        },
  36.     }
  37. }
  38.  
  39.   if message == nil or message == '' then return FALSE end
  40.   PerformHttpRequest(Config.webhook2, function(err, text, headers) end, 'POST', json.encode({ username = name,embeds = embeds}), { ['Content-Type'] = 'application/json' })
  41. end
  42.  
  43. function GetSociety(name)
  44.   for i=1, #RegisteredSocieties, 1 do
  45.     if RegisteredSocieties[i].name == name then
  46.       return RegisteredSocieties[i]
  47.     end
  48.   end
  49. end
  50.  
  51. AddEventHandler('onMySQLReady', function()
  52.  
  53.   local result = MySQL.Sync.fetchAll('SELECT * FROM jobs', {})
  54.  
  55.   for i=1, #result, 1 do
  56.     Jobs[result[i].name]        = result[i]
  57.     Jobs[result[i].name].grades = {}
  58.   end
  59.  
  60.   local result2 = MySQL.Sync.fetchAll('SELECT * FROM job_grades', {})
  61.  
  62.   for i=1, #result2, 1 do
  63.     Jobs[result2[i].job_name].grades[tostring(result2[i].grade)] = result2[i]
  64.   end
  65.  
  66. end)
  67.  
  68. AddEventHandler('esx_society:registerSociety', function(name, label, account, datastore, inventory, data)
  69.  
  70.   local found = false
  71.  
  72.   local society = {
  73.     name      = name,
  74.     label     = label,
  75.     account   = account,
  76.     datastore = datastore,
  77.     inventory = inventory,
  78.     data      = data,
  79.   }
  80.  
  81.   for i=1, #RegisteredSocieties, 1 do
  82.     if RegisteredSocieties[i].name == name then
  83.       found                  = true
  84.       RegisteredSocieties[i] = society
  85.       break
  86.     end
  87.   end
  88.  
  89.   if not found then
  90.     table.insert(RegisteredSocieties, society)
  91.   end
  92.  
  93. end)
  94.  
  95. AddEventHandler('esx_society:getSocieties', function(cb)
  96.   cb(RegisteredSocieties)
  97. end)
  98.  
  99. AddEventHandler('esx_society:getSociety', function(name, cb)
  100.   cb(GetSociety(name))
  101. end)
  102.  
  103. RegisterServerEvent('esx_society:withdrawMoney')
  104. AddEventHandler('esx_society:withdrawMoney', function(society, amount)
  105.  
  106.   local xPlayer = ESX.GetPlayerFromId(source)
  107.   local society = GetSociety(society)
  108.  
  109.   TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  110.  
  111.     if amount > 0 and account.money >= amount then
  112.  
  113.       account.removeMoney(amount)
  114.       xPlayer.addMoney(amount)
  115.       sendToDiscord('Konto Frakcji', xPlayer.name ..' wypłacił z konta frakcji: $'.. amount,Config.green)
  116.  
  117.       TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_withdrawn', amount))
  118.  
  119.     else
  120.       TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  121.     end
  122.  
  123.   end)
  124.  
  125. end)
  126.  
  127. RegisterServerEvent('esx_society:depositMoney')
  128. AddEventHandler('esx_society:depositMoney', function(society, amount)
  129.  
  130.   local xPlayer = ESX.GetPlayerFromId(source)
  131.   local society = GetSociety(society)
  132.  
  133.   if amount > 0 and xPlayer.get('money') >= amount then
  134.  
  135.     TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  136.       xPlayer.removeMoney(amount)
  137.       account.addMoney(amount)
  138.       sendToDiscord('Konto Frakcji', xPlayer.name ..' wpłacił na konto frakcji: $'.. amount,Config.green)
  139.     end)
  140.  
  141.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_deposited', amount))
  142.  
  143.   else
  144.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  145.   end
  146.  
  147. end)
  148.  
  149. RegisterServerEvent('esx_society:washMoney')
  150. AddEventHandler('esx_society:washMoney', function(society, amount)
  151.  
  152.   local xPlayer = ESX.GetPlayerFromId(source)
  153.   local account = xPlayer.getAccount('black_money')
  154.  
  155.   if amount > 0 and account.money >= amount then
  156.  
  157.     xPlayer.removeAccountMoney('black_money', amount)
  158.     sendToDiscord('Frakcja - Brudne Pieniądze', xPlayer.name ..' rozpoczął pranie brudnych pieniędzy: $'.. amount,Config.green)
  159.  
  160.       MySQL.Async.execute(
  161.         'INSERT INTO society_moneywash (identifier, society, amount) VALUES (@identifier, @society, @amount)',
  162.         {
  163.           ['@identifier'] = xPlayer.identifier,
  164.           ['@society']    = society,
  165.           ['@amount']     = amount
  166.         },
  167.         function(rowsChanged)
  168.           TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have', amount))
  169.         end
  170.       )
  171.  
  172.   else
  173.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  174.   end
  175.  
  176. end)
  177.  
  178. RegisterServerEvent('esx_society:putVehicleInGarage')
  179. AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
  180.  
  181.   local society = GetSociety(societyName)
  182.  
  183.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  184.     local garage = store.get('garage') or {}
  185.     table.insert(garage, vehicle)
  186.     store.set('garage', garage)
  187.   end)
  188.  
  189. end)
  190.  
  191. RegisterServerEvent('esx_society:removeVehicleFromGarage')
  192. AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
  193.  
  194.   local society = GetSociety(societyName)
  195.  
  196.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  197.    
  198.     local garage = store.get('garage') or {}
  199.  
  200.     for i=1, #garage, 1 do
  201.       if garage[i].plate == vehicle.plate then
  202.         table.remove(garage, i)
  203.         break
  204.       end
  205.     end
  206.  
  207.     store.set('garage', garage)
  208.  
  209.   end)
  210.  
  211. end)
  212.  
  213. ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
  214.  
  215.   local society = GetSociety(societyName)
  216.  
  217.   if society ~= nil then
  218.  
  219.     TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  220.       cb(account.money)
  221.     end)
  222.  
  223.   else
  224.     cb(0)
  225.   end
  226.  
  227. end)
  228.  
  229. ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
  230.  
  231.   if Config.EnableESXIdentity then
  232.     MySQL.Async.fetchAll(
  233.       'SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC',
  234.       { ['@job'] = society },
  235.       function (results)
  236.         local employees = {}
  237.  
  238.         for i=1, #results, 1 do
  239.           table.insert(employees, {
  240.             name        = results[i].firstname .. ' ' .. results[i].lastname,
  241.             identifier  = results[i].identifier,
  242.             job = {
  243.               name        = results[i].job,
  244.               label       = Jobs[results[i].job].label,
  245.               grade       = results[i].job_grade,
  246.               grade_name  = Jobs[results[i].job].grades[tostring(results[i].job_grade)].name,
  247.               grade_label = Jobs[results[i].job].grades[tostring(results[i].job_grade)].label,
  248.             }
  249.           })
  250.         end
  251.  
  252.         cb(employees)
  253.       end
  254.     )
  255.   else
  256.     MySQL.Async.fetchAll(
  257.       'SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC',
  258.       { ['@job'] = society },
  259.       function (result)
  260.         local employees = {}
  261.  
  262.         for i=1, #result, 1 do
  263.           table.insert(employees, {
  264.             name        = result[i].name,
  265.             identifier  = result[i].identifier,
  266.             job = {
  267.               name        = result[i].job,
  268.               label       = Jobs[result[i].job].label,
  269.               grade       = result[i].job_grade,
  270.               grade_name  = Jobs[result[i].job].grades[tostring(result[i].job_grade)].name,
  271.               grade_label = Jobs[result[i].job].grades[tostring(result[i].job_grade)].label,
  272.             }
  273.           })
  274.         end
  275.  
  276.         cb(employees)
  277.       end
  278.     )
  279.   end
  280. end)
  281.  
  282. ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
  283.  
  284.   local job    = json.decode(json.encode(Jobs[society]))
  285.   local grades = {}
  286.  
  287.   for k,v in pairs(job.grades) do
  288.     table.insert(grades, v)
  289.   end
  290.  
  291.   table.sort(grades, function(a, b)
  292.     return a.grade < b.grade
  293.   end)
  294.  
  295.   job.grades = grades
  296.  
  297.   cb(job)
  298.  
  299. end)
  300.  
  301.  
  302. ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
  303.  
  304.     local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
  305.  
  306.     if xPlayer ~= nil then
  307.         xPlayer.setJob(job, grade)
  308.        
  309.         if type == 'hire' then
  310.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_hired', job))
  311.         elseif type == 'promote' then
  312.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_promoted'))
  313.         elseif type == 'fire' then
  314.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_fired', xPlayer.getJob().label))
  315.         end
  316.     end
  317.    
  318.     sendToDiscord('Frakcja - Pracownicy', 'Zaktualizowano gracza o id '.. identifier ..' Nowa Praca: '.. job ..' Nowe Stanowisko: '.. grade,Config.green)
  319.     MySQL.Async.execute(
  320.         'UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier',
  321.         {
  322.             ['@job']        = job,
  323.             ['@job_grade']  = grade,
  324.             ['@identifier'] = identifier
  325.         },
  326.         function(rowsChanged)
  327.             cb()
  328.         end
  329.     )
  330.  
  331. end)
  332.  
  333. ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
  334.  
  335.     sendToDiscord2('Zmiana Wypłaty we Frakcji!', 'Ktoś zmienił wypłatę we frakcji: '.. job ..' na stanowisku: '.. grade..' na: $'.. salary,Config.green)
  336.    
  337.   MySQL.Async.execute(
  338.     'UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade',
  339.     {
  340.       ['@salary']   = salary,
  341.       ['@job_name'] = job,
  342.       ['@grade']    = grade
  343.     },
  344.     function(rowsChanged)
  345.  
  346.       Jobs[job].grades[tostring(grade)].salary = salary
  347.  
  348.       local xPlayers = ESX.GetPlayers()
  349.  
  350.       for i=1, #xPlayers, 1 do
  351.  
  352.         local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  353.  
  354.         if xPlayer.job.name == job and xPlayer.job.grade == grade then
  355.           xPlayer.setJob(job, grade)
  356.         end
  357.  
  358.       end
  359.  
  360.       cb()
  361.     end
  362.   )
  363.  
  364. end)
  365.  
  366. ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
  367.  
  368.   local xPlayers = ESX.GetPlayers()
  369.   local players  = {}
  370.  
  371.   for i=1, #xPlayers, 1 do
  372.  
  373.     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  374.  
  375.     table.insert(players, {
  376.       source     = xPlayer.source,
  377.       identifier = xPlayer.identifier,
  378.       name       = xPlayer.name,
  379.       job        = xPlayer.job
  380.     })
  381.  
  382.   end
  383.  
  384.   cb(players)
  385.  
  386. end)
  387.  
  388. ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
  389.  
  390.   local society = GetSociety(societyName)
  391.  
  392.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  393.     local garage = store.get('garage') or {}
  394.     cb(garage)
  395.   end)
  396.  
  397. end)
  398.  
  399. function WashMoneyCRON()
  400.     MySQL.Async.fetchAll(
  401.     'SELECT * FROM society_moneywash', {},
  402.     function(result)
  403.         for i=1, #result, 1 do
  404.  
  405.             -- add society money
  406.             local society = GetSociety(result[i].society)
  407.             TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  408.                 account.addMoney(result[i].amount)
  409.             end)
  410.  
  411.             -- send notification if player is online
  412.             local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
  413.             if xPlayer ~= nil then
  414.                 TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_laundered', result[i].amount))
  415.             end
  416.  
  417.             MySQL.Async.execute('DELETE FROM society_moneywash WHERE id = @id',
  418.             {
  419.                 ['@id'] = result[i].id
  420.             })
  421.         end
  422.     end)
  423. end
  424.  
  425. TriggerEvent('cron:runAt', 3, 0, WashMoneyCRON)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement