Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
132
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.87 KB | None
  1. ESX                 = nil
  2. Jobs                = {}
  3. RegisteredSocieties = {}
  4.  
  5. TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
  6.  
  7. function stringsplit(inputstr, sep)
  8.   if sep == nil then
  9.     sep = "%s"
  10.   end
  11.   local t={} ; i=1
  12.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  13.     t[i] = str
  14.     i = i + 1
  15.   end
  16.   return t
  17. end
  18.  
  19. function GetSociety(name)
  20.   for i=1, #RegisteredSocieties, 1 do
  21.     if RegisteredSocieties[i].name == name then
  22.       return RegisteredSocieties[i]
  23.     end
  24.   end
  25. end
  26.  
  27. AddEventHandler('onMySQLReady', function()
  28.  
  29.   local result = MySQL.Sync.fetchAll('SELECT * FROM jobs', {})
  30.  
  31.   for i=1, #result, 1 do
  32.     Jobs[result[i].name]        = result[i]
  33.     Jobs[result[i].name].grades = {}
  34.   end
  35.  
  36.   local result2 = MySQL.Sync.fetchAll('SELECT * FROM job_grades', {})
  37.  
  38.   for i=1, #result2, 1 do
  39.     Jobs[result2[i].job_name].grades[tostring(result2[i].grade)] = result2[i]
  40.   end
  41.  
  42. end)
  43.  
  44. AddEventHandler('esx_society:registerSociety', function(name, label, account, datastore, inventory, data)
  45.  
  46.   local found = false
  47.  
  48.   local society = {
  49.     name      = name,
  50.     label     = label,
  51.     account   = account,
  52.     datastore = datastore,
  53.     inventory = inventory,
  54.     data      = data,
  55.   }
  56.  
  57.   for i=1, #RegisteredSocieties, 1 do
  58.     if RegisteredSocieties[i].name == name then
  59.       found                  = true
  60.       RegisteredSocieties[i] = society
  61.       break
  62.     end
  63.   end
  64.  
  65.   if not found then
  66.     table.insert(RegisteredSocieties, society)
  67.   end
  68.  
  69. end)
  70.  
  71. AddEventHandler('esx_society:getSocieties', function(cb)
  72.   cb(RegisteredSocieties)
  73. end)
  74.  
  75. AddEventHandler('esx_society:getSociety', function(name, cb)
  76.   cb(GetSociety(name))
  77. end)
  78.  
  79. RegisterServerEvent('esx_society:withdrawMoney')
  80. AddEventHandler('esx_society:withdrawMoney', function(society, amount)
  81.  
  82.   local xPlayer = ESX.GetPlayerFromId(source)
  83.   local society = GetSociety(society)
  84.  
  85.   TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  86.  
  87.     if amount > 0 and account.money >= amount then
  88.  
  89.       account.removeMoney(amount)
  90.       xPlayer.addMoney(amount)
  91.  
  92.       TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_withdrawn', amount))
  93.  
  94.     else
  95.       TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  96.     end
  97.  
  98.   end)
  99.  
  100. end)
  101.  
  102. RegisterServerEvent('esx_society:depositMoney')
  103. AddEventHandler('esx_society:depositMoney', function(society, amount)
  104.  
  105.   local xPlayer = ESX.GetPlayerFromId(source)
  106.   local society = GetSociety(society)
  107.  
  108.   if amount > 0 and xPlayer.get('money') >= amount then
  109.  
  110.     TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  111.       xPlayer.removeMoney(amount)
  112.       account.addMoney(amount)
  113.     end)
  114.  
  115.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('have_deposited', amount))
  116.  
  117.   else
  118.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  119.   end
  120.  
  121. end)
  122.  
  123. RegisterServerEvent('esx_society:washMoney')
  124. AddEventHandler('esx_society:washMoney', function(society, amount)
  125.  
  126.   local xPlayer = ESX.GetPlayerFromId(source)
  127.   local account = xPlayer.getAccount('black_money')
  128.  
  129.     if amount and amount > 0 and account.money >= amount then
  130.     local howlong = 10
  131.     local xtime   = 1 -- si 1 = alors le nombre ci-dessus sera en SECONDES. si 60 = en MINUTES
  132.     local minorsec  = ''
  133.     if xtime == 1 then
  134.      minorsec = ' secondes'
  135.     elseif xtime == 60 then
  136.     minorsec = ' minutes'
  137.     end
  138.    
  139.     xPlayer.removeAccountMoney('black_money', amount)
  140.     TriggerClientEvent('esx:showNotification', xPlayer.source, ('Tu as ~g~' .. ESX.Math.GroupDigits(amount) .. '$~s~ en attente de ~y~blanchiment d\'argent~s~ (' .. howlong .. minorsec .. ').'))
  141.     Citizen.Wait(1000 * xtime * howlong)
  142.     xPlayer.addMoney(amount)
  143.     TriggerClientEvent('esx:showNotification', xPlayer.source, ('~p~Vos~s~ ~g~' .. ESX.Math.GroupDigits(amount) .. '$~s~ on été ~y~Blanchi~s~ et mis dans le coffre entreprise'))
  144.  
  145.   else
  146.     TriggerClientEvent('esx:showNotification', xPlayer.source, _U('invalid_amount'))
  147.   end
  148.  
  149. end)
  150.  
  151. RegisterServerEvent('esx_society:putVehicleInGarage')
  152. AddEventHandler('esx_society:putVehicleInGarage', function(societyName, vehicle)
  153.  
  154.   local society = GetSociety(societyName)
  155.  
  156.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  157.     local garage = store.get('garage') or {}
  158.     table.insert(garage, vehicle)
  159.     store.set('garage', garage)
  160.   end)
  161.  
  162. end)
  163.  
  164. RegisterServerEvent('esx_society:removeVehicleFromGarage')
  165. AddEventHandler('esx_society:removeVehicleFromGarage', function(societyName, vehicle)
  166.  
  167.   local society = GetSociety(societyName)
  168.  
  169.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  170.    
  171.     local garage = store.get('garage') or {}
  172.  
  173.     for i=1, #garage, 1 do
  174.       if garage[i].plate == vehicle.plate then
  175.         table.remove(garage, i)
  176.         break
  177.       end
  178.     end
  179.  
  180.     store.set('garage', garage)
  181.  
  182.   end)
  183.  
  184. end)
  185.  
  186. ESX.RegisterServerCallback('esx_society:getSocietyMoney', function(source, cb, societyName)
  187.  
  188.   local society = GetSociety(societyName)
  189.  
  190.   if society ~= nil then
  191.  
  192.     TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  193.       cb(account.money)
  194.     end)
  195.  
  196.   else
  197.     cb(0)
  198.   end
  199.  
  200. end)
  201.  
  202. ESX.RegisterServerCallback('esx_society:getEmployees', function(source, cb, society)
  203.  
  204.   if Config.EnableESXIdentity then
  205.     MySQL.Async.fetchAll(
  206.       'SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC',
  207.       { ['@job'] = society },
  208.       function (results)
  209.         local employees = {}
  210.  
  211.         for i=1, #results, 1 do
  212.           table.insert(employees, {
  213.             name        = results[i].firstname .. ' ' .. results[i].lastname,
  214.             identifier  = results[i].identifier,
  215.             job = {
  216.               name        = results[i].job,
  217.               label       = Jobs[results[i].job].label,
  218.               grade       = results[i].job_grade,
  219.               grade_name  = Jobs[results[i].job].grades[tostring(results[i].job_grade)].name,
  220.               grade_label = Jobs[results[i].job].grades[tostring(results[i].job_grade)].label,
  221.             }
  222.           })
  223.         end
  224.  
  225.         cb(employees)
  226.       end
  227.     )
  228.   else
  229.     MySQL.Async.fetchAll(
  230.       'SELECT * FROM users WHERE job = @job ORDER BY job_grade DESC',
  231.       { ['@job'] = society },
  232.       function (result)
  233.         local employees = {}
  234.  
  235.         for i=1, #result, 1 do
  236.           table.insert(employees, {
  237.             name        = result[i].name,
  238.             identifier  = result[i].identifier,
  239.             job = {
  240.               name        = result[i].job,
  241.               label       = Jobs[result[i].job].label,
  242.               grade       = result[i].job_grade,
  243.               grade_name  = Jobs[result[i].job].grades[tostring(result[i].job_grade)].name,
  244.               grade_label = Jobs[result[i].job].grades[tostring(result[i].job_grade)].label,
  245.             }
  246.           })
  247.         end
  248.  
  249.         cb(employees)
  250.       end
  251.     )
  252.   end
  253. end)
  254.  
  255. ---SECONDJOB INCLUDED
  256. ESX.RegisterServerCallback('esx_society:getEmployees2', function(source, cb, society)
  257.  
  258.   if Config.EnableESXIdentity then
  259.     MySQL.Async.fetchAll(
  260.       'SELECT * FROM users WHERE job2 = @job2 ORDER BY job2_grade DESC',
  261.       { ['@job2'] = society },
  262.       function (results)
  263.         local employees = {}
  264.  
  265.         for i=1, #results, 1 do
  266.           table.insert(employees, {
  267.             name        = results[i].firstname .. ' ' .. results[i].lastname,
  268.             identifier  = results[i].identifier,
  269.             job2 = {
  270.               name        = results[i].job2,
  271.               label       = Jobs[results[i].job2].label,
  272.               grade       = results[i].job2_grade,
  273.               grade_name  = Jobs[results[i].job2].grades[tostring(results[i].job2_grade)].name,
  274.               grade_label = Jobs[results[i].job2].grades[tostring(results[i].job2_grade)].label,
  275.             }
  276.           })
  277.         end
  278.  
  279.         cb(employees)
  280.       end
  281.     )
  282.   else
  283.     MySQL.Async.fetchAll(
  284.       'SELECT * FROM users WHERE job2 = @job2 ORDER BY job2_grade DESC',
  285.       { ['@job2'] = society },
  286.       function (result)
  287.         local employees = {}
  288.  
  289.         for i=1, #result, 1 do
  290.           table.insert(employees, {
  291.             name        = result[i].name,
  292.             identifier  = result[i].identifier,
  293.             job2 = {
  294.               name        = result[i].job2,
  295.               label       = Jobs[result[i].job2].label,
  296.               grade       = result[i].job2_grade,
  297.               grade_name  = Jobs[result[i].job2].grades[tostring(result[i].job2_grade)].name,
  298.               grade_label = Jobs[result[i].job2].grades[tostring(result[i].job2_grade)].label,
  299.             }
  300.           })
  301.         end
  302.  
  303.         cb(employees)
  304.       end
  305.     )
  306.   end
  307. end)
  308.  
  309. ESX.RegisterServerCallback('esx_society:getJob', function(source, cb, society)
  310.  
  311.   local job    = json.decode(json.encode(Jobs[society]))
  312.   local grades = {}
  313.  
  314.   for k,v in pairs(job.grades) do
  315.     table.insert(grades, v)
  316.   end
  317.  
  318.   table.sort(grades, function(a, b)
  319.     return a.grade < b.grade
  320.   end)
  321.  
  322.   job.grades = grades
  323.  
  324.   cb(job)
  325.  
  326. end)
  327.  
  328. ---SECONDJOB INCLUDED
  329. ESX.RegisterServerCallback('esx_society:getJob2', function(source, cb, society)
  330.  
  331.   local job2    = json.decode(json.encode(Jobs[society]))
  332.   local grades = {}
  333.  
  334.   for k,v in pairs(job2.grades) do
  335.     table.insert(grades, v)
  336.   end
  337.  
  338.   table.sort(grades, function(a, b)
  339.     return a.grade < b.grade
  340.   end)
  341.  
  342.   job2.grades = grades
  343.  
  344.   cb(job2)
  345.  
  346. end)
  347.  
  348. ESX.RegisterServerCallback('esx_society:setJob', function(source, cb, identifier, job, grade, type)
  349.  
  350.     local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
  351.  
  352.     if xPlayer ~= nil then
  353.         xPlayer.setJob(job, grade)
  354.        
  355.         if type == 'hire' then
  356.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_hired', job))
  357.         elseif type == 'promote' then
  358.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_promoted'))
  359.         elseif type == 'fire' then
  360.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_fired', xPlayer.getJob().label))
  361.         end
  362.     end
  363.  
  364.     MySQL.Async.execute(
  365.         'UPDATE users SET job = @job, job_grade = @job_grade WHERE identifier = @identifier',
  366.         {
  367.             ['@job']        = job,
  368.             ['@job_grade']  = grade,
  369.             ['@identifier'] = identifier
  370.         },
  371.         function(rowsChanged)
  372.             cb()
  373.         end
  374.     )
  375.  
  376. end)
  377.  
  378. ---SECONDJOB INCLUDED
  379. ESX.RegisterServerCallback('esx_society:setJob2', function(source, cb, identifier, job2, grade2, type)
  380.  
  381.     local xPlayer = ESX.GetPlayerFromIdentifier(identifier)
  382.  
  383.     if xPlayer ~= nil then
  384.         xPlayer.setJob2(job2, grade2)
  385.        
  386.         if type == 'hire' then
  387.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_hired', job2))
  388.         elseif type == 'promote' then
  389.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_promoted'))
  390.         elseif type == 'fire' then
  391.             TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_been_fired', xPlayer.getJob2().label))
  392.         end
  393.     end
  394.  
  395.     MySQL.Async.execute(
  396.         'UPDATE users SET job2 = @job2, job2_grade = @job2_grade WHERE identifier = @identifier',
  397.         {
  398.             ['@job2']        = job2,
  399.             ['@job2_grade']  = grade2,
  400.             ['@identifier'] = identifier
  401.         },
  402.         function(rowsChanged)
  403.             cb()
  404.         end
  405.     )
  406.  
  407. end)
  408.  
  409. ESX.RegisterServerCallback('esx_society:setJobSalary', function(source, cb, job, grade, salary)
  410.  
  411.   MySQL.Async.execute(
  412.     'UPDATE job_grades SET salary = @salary WHERE job_name = @job_name AND grade = @grade',
  413.     {
  414.       ['@salary']   = salary,
  415.       ['@job_name'] = job,
  416.       ['@grade']    = grade
  417.     },
  418.     function(rowsChanged)
  419.  
  420.       Jobs[job].grades[tostring(grade)].salary = salary
  421.  
  422.       local xPlayers = ESX.GetPlayers()
  423.  
  424.       for i=1, #xPlayers, 1 do
  425.  
  426.         local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  427.  
  428.         if xPlayer.job.name == job and xPlayer.job.grade == grade then
  429.           xPlayer.setJob(job, grade)
  430.         end
  431.  
  432.       end
  433.  
  434.       cb()
  435.     end
  436.   )
  437.  
  438. end)
  439.  
  440. ---SECONDJOB INCLUDED
  441. ESX.RegisterServerCallback('esx_society:setJobSalary2', function(source, cb, job2, grade2, salary)
  442.  
  443.   MySQL.Async.execute(
  444.     'UPDATE job2_grades SET salary = @salary WHERE job2_name = @job2_name AND grade2 = @grade',
  445.     {
  446.       ['@salary']   = salary,
  447.       ['@job2_name'] = job2,
  448.       ['@grade']    = grade2
  449.     },
  450.     function(rowsChanged)
  451.  
  452.       Jobs[job2].grades[tostring(grade2)].salary = salary
  453.  
  454.       local xPlayers = ESX.GetPlayers()
  455.  
  456.       for i=1, #xPlayers, 1 do
  457.  
  458.         local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  459.  
  460.         if xPlayer.job2.name == job2 and xPlayer.job2.grade == grade2 then
  461.           xPlayer.setJob2(job2, grade2)
  462.         end
  463.  
  464.       end
  465.  
  466.       cb()
  467.     end
  468.   )
  469.  
  470. end)
  471.  
  472. ESX.RegisterServerCallback('esx_society:getOnlinePlayers', function(source, cb)
  473.  
  474.   local xPlayers = ESX.GetPlayers()
  475.   local players  = {}
  476.  
  477.   for i=1, #xPlayers, 1 do
  478.  
  479.     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  480.  
  481.     table.insert(players, {
  482.       source     = xPlayer.source,
  483.       identifier = xPlayer.identifier,
  484.       name       = xPlayer.name,
  485.       job        = xPlayer.job
  486.     })
  487.  
  488.   end
  489.  
  490.   cb(players)
  491.  
  492. end)
  493.  
  494. ---SECONDJOB INCLUDED
  495. ESX.RegisterServerCallback('esx_society:getOnlinePlayers2', function(source, cb)
  496.  
  497.   local xPlayers = ESX.GetPlayers()
  498.   local players  = {}
  499.  
  500.   for i=1, #xPlayers, 1 do
  501.  
  502.     local xPlayer = ESX.GetPlayerFromId(xPlayers[i])
  503.  
  504.     table.insert(players, {
  505.       source     = xPlayer.source,
  506.       identifier = xPlayer.identifier,
  507.       name       = xPlayer.name,
  508.       job2        = xPlayer.job2
  509.     })
  510.  
  511.   end
  512.  
  513.   cb(players)
  514.  
  515. end)
  516.  
  517. ESX.RegisterServerCallback('esx_society:getVehiclesInGarage', function(source, cb, societyName)
  518.  
  519.   local society = GetSociety(societyName)
  520.  
  521.   TriggerEvent('esx_datastore:getSharedDataStore', society.datastore, function(store)
  522.     local garage = store.get('garage') or {}
  523.     cb(garage)
  524.   end)
  525.  
  526. end)
  527.  
  528. --[[function WashMoneyCRON(d, h, m)
  529.   MySQL.Async.fetchAll('SELECT * FROM society_moneywash', {}, function(result)
  530.     for i=1, #result, 1 do
  531.       local society = GetSociety(result[i].society)
  532.       local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
  533.  
  534.       -- add society money
  535.       TriggerEvent('esx_addonaccount:getSharedAccount', society.account, function(account)
  536.         account.addMoney(result[i].amount)
  537.       end)
  538.  
  539.       -- send notification if player is online
  540.       if xPlayer then
  541.         TriggerClientEvent('esx:showNotification', xPlayer.source, _U('you_have_laundered', ESX.Math.GroupDigits(result[i].amount)))
  542.       end
  543.  
  544.       MySQL.Async.execute('DELETE FROM society_moneywash WHERE id = @id', {
  545.         ['@id'] = result[i].id
  546.       })
  547.     end
  548.   end)
  549. end
  550.  
  551. TriggerEvent('cron:runAt', 3, 0, WashMoneyCRON)]]
Advertisement
RAW Paste Data Copied
Advertisement