Advertisement
proff001

Convert.lua for linden_inventory

Jul 22nd, 2021 (edited)
1,829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.83 KB | None | 0 0
  1. --[[
  2.     Delete this file if you do not require it! If you have converted your database you can delete this file
  3.     Remember to backup your database in the event of errors
  4.  
  5.     ! This will only convert items where inventories are stored as a table in `users`
  6.     ! If you are using a `user_inventory` table this won't do anything
  7.     ! AKA this is for ESX 1.2+
  8.  
  9.     ExtendedMode has a migratedb function of its own that you could try before using this
  10.     This allows upgrading from ESX 1.1 to 1.2+ format
  11.     https://github.com/extendedmode/extendedmode/blob/master/server/dbmigrate.lua
  12.    
  13.     This conversion method is based on the above file
  14. ]]
  15.  
  16. local start = false
  17. local running = false
  18. local totalCount = 0
  19. local currentCount = 0
  20. local totalTrunkCount = 0
  21. local currentTrunkCount = 0
  22.  
  23. local player = {
  24.     identifier = {},
  25.     inventory = {},
  26.     loadout = {},
  27.     accounts = {},
  28. }
  29.  
  30. local trunk = {
  31.     plate = {},
  32.     owner = {},
  33.     inventory = {},
  34.     weapons = {},
  35.     accounts = {
  36.         money = {},
  37.         black = {},
  38.     },
  39. }
  40.  
  41. local Print = function(l1, l2)
  42.     print('^8   =================================================================^0')
  43.     print('^3   '..l1..'^0')
  44.     if l2 then print('^3    '..l2..'^0') end
  45.     print('^8   =================================================================^0')
  46. end
  47.  
  48. RegisterCommand('convertinventory', function(source, args)
  49.     if source == 0 then
  50.         if not running then
  51.             if start then
  52.                 RetrieveData()
  53.             else
  54.                 Print('ENSURE NO PLAYERS ARE ONLINE AND YOUR DATABASE HAS BEEN BACKED UP', 'ENTER THE COMMAND AGAIN TO BEGIN CONVERSION')
  55.                 start = true
  56.             end
  57.         end
  58.     end
  59. end, true)
  60.  
  61. RetrieveData = function()
  62.     running = true
  63.     local result = exports.ghmattimysql:executeSync('SELECT identifier, inventory, loadout, accounts FROM users', {})
  64.     if result then
  65.         for k, v in pairs(result) do
  66.             if (v.inventory ~= nil and v.inventory ~= '' and v.inventory ~= '[]') or (v.loadout ~= nil and v.loadout ~= '' and v.loadout ~= '[]') or (v.accounts ~= nil and v.accounts ~= '' and v.accounts ~= '[]') then
  67.                 if v.inventory:find('"slot":') == nil then
  68.                     local count = #player.identifier+1
  69.                     player.identifier[count] = v.identifier
  70.                     if v.inventory then player.inventory[count] = json.decode(v.inventory) end
  71.                     if v.loadout then player.loadout[count] = json.decode(v.loadout) end
  72.                     if v.accounts then player.accounts[count] = json.decode(v.accounts) end
  73.                 end
  74.             end
  75.         end
  76.         totalCount = #player.identifier
  77.         if totalCount == 0 then
  78.             Print('THERE ARE NO PLAYERS WITH INVENTORIES TO CONVERT')
  79.         else
  80.             Print('FOUND '..totalCount..' PLAYERS WITH OLD INVENTORY DATA', 'STARTING CONVERSION - WAIT UNTIL THE PROCESS IS COMPLETE')
  81.             BeginConversion()
  82.         end
  83.     else
  84.         Print('NO RESULTS FOUND, DATABASE MUST INCLUDE IDENTIFIER AND INVENTORY^0')
  85.     end
  86.  
  87.     local result = exports.ghmattimysql:executeSync('SELECT plate, data FROM trunk_inventory', {})
  88.     if result then
  89.         Print('GOT TRUNK RESULT, STARTED PARSING TRUNK INVENTORIES')
  90.         for k, v in pairs(result) do
  91.             if (v.data ~= nil and v.data ~= '' and v.data ~= '{}') then
  92.                 v.data = json.decode(v.data)
  93.                 if v.data.coffre == nil then v.data.coffre = {} end
  94.                 if v.data.weapons == nil then v.data.weapons = {} end
  95.                 if v.data.money == nil then v.data.money = {} elseif v.data.money[1].amount < 1 then v.data.money = {} end
  96.                 if v.data.black_money == nil then v.data.black_money = {} elseif v.data.black_money[1].amount < 1 then v.data.black_money = {} end
  97.                 if (#v.data.coffre ~= 0 or #v.data.weapons ~= 0 or #v.data.money ~= 0 or #v.data.black_money ~= 0) then
  98.                     local owner = exports.ghmattimysql:scalarSync('SELECT `owner` FROM `owned_vehicles` WHERE plate = @plate', { ['@plate'] = v.plate })
  99.                     if owner == nil then
  100.                         DeleteTrunk(v.plate, 'because of no owner')
  101.                     else
  102.                         local count = #trunk.plate+1
  103.                         trunk.plate[count] = v.plate
  104.                         trunk.owner[count] = owner
  105.                         if #v.data.coffre ~= 0 then trunk.inventory[count] = v.data.coffre end
  106.                         if #v.data.weapons ~= 0 then trunk.weapons[count] = v.data.weapons end
  107.                         if (#v.data.money ~= 0 or #v.data.black_money ~= 0) then
  108.                             trunk.accounts[count] = {}
  109.                             if #v.data.money ~= 0 then trunk.accounts[count].money = v.data.money[1] end
  110.                             if #v.data.black_money ~= 0 then trunk.accounts[count].black_money = v.data.black_money[1] end
  111.                         end
  112.                     end
  113.                 else
  114.                     DeleteTrunk(v.plate, 'because of no data')
  115.                 end
  116.             else
  117.                 DeleteTrunk(v.plate, 'because of no data')
  118.             end
  119.         end
  120.         totalTrunkCount = #trunk.plate
  121.         if totalTrunkCount == 0 then
  122.             Print('THERE ARE NO TRUNK INVENTORIES TO CONVERT')
  123.         else
  124.             Print('FOUND '..totalTrunkCount..' TRUNKS WITH OLD INVENTORY DATA', 'STARTING CONVERSION - WAIT UNTIL THE PROCESS IS COMPLETE')
  125.             BeginTrunkConversion()
  126.         end
  127.     else
  128.         Print('NO RESULTS FOUND, DATABASE MUST INCLUDE PLATE AND DATA^0')
  129.     end
  130. end
  131.  
  132. DeleteTrunk = function(plate, reason)
  133.     local deleted = exports.ghmattimysql:executeSync('DELETE FROM `trunk_inventory` WHERE plate = @plate', { ['@plate'] = plate })
  134.     if deleted.affectedRows > 0 then
  135.         print('^1 Deleted trunk for '..plate..' '..reason)
  136.     else
  137.         print('^1 FAILED TO DELETE TRUNK FOR '..plate)
  138.     end
  139. end
  140.  
  141. local newInventory = {}
  142. local newTrunkInventory = {}
  143.  
  144. BeginConversion = function()
  145.     for i=1, #player.identifier do
  146.         local newInv = {}
  147.         local loop = 0
  148.         if player.accounts[i] then
  149.             for k, v in pairs(player.accounts[i]) do
  150.                 local xItem = Items[k]
  151.                 if xItem and v > 0 then
  152.                     loop = loop + 1
  153.                     v = {slot=loop, name=k, count=v}
  154.                     newInv[loop] = v
  155.                 end
  156.             end
  157.         end
  158.         if player.loadout[i] then
  159.             for k, v in pairs(player.loadout[i]) do
  160.                 if k == 'GADGET_PARACHUTE' then k = 'parachute' end
  161.                 local xItem = Items[k]
  162.                 if xItem then
  163.                     loop = loop + 1
  164.                     v = {slot=loop, name=k, count=1, metadata = {}}
  165.                     if not Items[k].ammoname then
  166.                         if not v.metadata.durability then v.metadata.durability = 100 end
  167.                     else
  168.                         if not v.metadata.durability then v.metadata.durability = 100 end
  169.                         if Items[k].ammoname then v.metadata.ammo = 0 end
  170.                         if not v.metadata.components then v.metadata.components = {} end
  171.                         v.metadata.serial = GenerateSerial()
  172.                     end
  173.                     newInv[loop] = v
  174.                 end
  175.             end
  176.         end
  177.         if player.inventory[i] then
  178.             for k, v in pairs(player.inventory[i]) do
  179.                 local xItem = Items[k]
  180.                 if xItem and v > 0 then
  181.                     loop = loop + 1
  182.                     v = {slot=loop, name=k, count=v}
  183.                     newInv[loop] = v
  184.                 end
  185.             end
  186.         end
  187.         newInventory[i] = json.encode(newInv)
  188.         local result = exports.ghmattimysql:executeSync('UPDATE users SET inventory = @inventory WHERE identifier = @identifier', {
  189.             ['@inventory'] = newInventory[i],
  190.             ['@identifier'] = player.identifier[i]
  191.         })
  192.         if result then
  193.             currentCount = currentCount + 1
  194.             print('^2 Updated '..currentCount..' of '..totalCount..' players^0')
  195.         end
  196.     end
  197. end
  198.  
  199. BeginTrunkConversion = function()
  200.     for i = 1, #trunk.plate do
  201.         local newTrunk = {}
  202.         local loop = 0
  203.         if trunk.inventory[i] then
  204.             for k, v in pairs(trunk.inventory[i]) do
  205.                 local xItem = Items[v.name]
  206.                 if xItem and v.count > 0 then
  207.                     loop = loop + 1
  208.                     v = {slot = loop, name = v.name, count = v.count, metadata = {}}
  209.                     newTrunk[loop] = v
  210.                 end
  211.             end
  212.         end
  213.         if trunk.weapons[i] then
  214.             for k,v in pairs(trunk.weapons[i]) do
  215.                 if v.name == 'GADGET_PARACHUTE' then v.name = 'parachute' end
  216.                 local xItem = Items[v.name]
  217.                 if xItem then
  218.                     loop = loop + 1
  219.                     v = {slot = loop, name = v.name, count = 1, metadata = {}}
  220.                     if not Items[v.name].ammoname then
  221.                         if not v.metadata.durability then v.metadata.durability = 100 end
  222.                     else
  223.                         if not v.metadata.durability then v.metadata.durability = 100 end
  224.                         if Items[v.name].ammoname then v.metadata.ammo = 0 end
  225.                         if not v.metadata.components then v.metadata.components = {} end
  226.                         v.metadata.serial = GenerateSerial()
  227.                     end
  228.                     newTrunk[loop] = v
  229.                 end
  230.             end
  231.         end
  232.         if trunk.accounts[i] then
  233.             for k,v in pairs(trunk.accounts[i]) do
  234.                 local xItem = Items[k]
  235.                 if xItem and v.amount > 0 then
  236.                     loop = loop + 1
  237.                     v = {slot = loop, name = k, count = v.amount, metadata = {}}
  238.                     newTrunk[loop] = v
  239.                 end
  240.             end
  241.         end
  242.        
  243.         if #newTrunk > 0 then
  244.             newTrunkInventory[i] = json.encode(newTrunk)
  245.             local result = exports.ghmattimysql:executeSync('INSERT INTO `linden_inventory` (owner, name, data) VALUES (@owner, @name, @data)', {
  246.                 ['@owner'] = trunk.owner[i],
  247.                 ['@name'] = 'trunk-'..trunk.plate[i],
  248.                 ['@data'] = newTrunkInventory[i]
  249.             })
  250.             if result then
  251.                 currentTrunkCount = currentTrunkCount + 1
  252.                 DeleteTrunk(trunk.plate[i], ' converted!')
  253.                 print('^2 Updated '..currentTrunkCount..' of '..totalTrunkCount..' trunks^0')
  254.             end
  255.         else
  256.             currentTrunkCount = currentTrunkCount + 1
  257.             print('^1 Failed due to no data '..currentTrunkCount..' of '..totalTrunkCount..' trunks^0')
  258.         end
  259.     end
  260. end
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement