Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Delete this file if you do not require it! If you have converted your database you can delete this file
- Remember to backup your database in the event of errors
- ! This will only convert items where inventories are stored as a table in `users`
- ! If you are using a `user_inventory` table this won't do anything
- ! AKA this is for ESX 1.2+
- ExtendedMode has a migratedb function of its own that you could try before using this
- This allows upgrading from ESX 1.1 to 1.2+ format
- https://github.com/extendedmode/extendedmode/blob/master/server/dbmigrate.lua
- This conversion method is based on the above file
- ]]
- local start = false
- local running = false
- local totalCount = 0
- local currentCount = 0
- local totalTrunkCount = 0
- local currentTrunkCount = 0
- local player = {
- identifier = {},
- inventory = {},
- loadout = {},
- accounts = {},
- }
- local trunk = {
- plate = {},
- owner = {},
- inventory = {},
- weapons = {},
- accounts = {
- money = {},
- black = {},
- },
- }
- local Print = function(l1, l2)
- print('^8 =================================================================^0')
- print('^3 '..l1..'^0')
- if l2 then print('^3 '..l2..'^0') end
- print('^8 =================================================================^0')
- end
- RegisterCommand('convertinventory', function(source, args)
- if source == 0 then
- if not running then
- if start then
- RetrieveData()
- else
- Print('ENSURE NO PLAYERS ARE ONLINE AND YOUR DATABASE HAS BEEN BACKED UP', 'ENTER THE COMMAND AGAIN TO BEGIN CONVERSION')
- start = true
- end
- end
- end
- end, true)
- RetrieveData = function()
- running = true
- local result = exports.ghmattimysql:executeSync('SELECT identifier, inventory, loadout, accounts FROM users', {})
- if result then
- for k, v in pairs(result) do
- 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
- if v.inventory:find('"slot":') == nil then
- local count = #player.identifier+1
- player.identifier[count] = v.identifier
- if v.inventory then player.inventory[count] = json.decode(v.inventory) end
- if v.loadout then player.loadout[count] = json.decode(v.loadout) end
- if v.accounts then player.accounts[count] = json.decode(v.accounts) end
- end
- end
- end
- totalCount = #player.identifier
- if totalCount == 0 then
- Print('THERE ARE NO PLAYERS WITH INVENTORIES TO CONVERT')
- else
- Print('FOUND '..totalCount..' PLAYERS WITH OLD INVENTORY DATA', 'STARTING CONVERSION - WAIT UNTIL THE PROCESS IS COMPLETE')
- BeginConversion()
- end
- else
- Print('NO RESULTS FOUND, DATABASE MUST INCLUDE IDENTIFIER AND INVENTORY^0')
- end
- local result = exports.ghmattimysql:executeSync('SELECT plate, data FROM trunk_inventory', {})
- if result then
- Print('GOT TRUNK RESULT, STARTED PARSING TRUNK INVENTORIES')
- for k, v in pairs(result) do
- if (v.data ~= nil and v.data ~= '' and v.data ~= '{}') then
- v.data = json.decode(v.data)
- if v.data.coffre == nil then v.data.coffre = {} end
- if v.data.weapons == nil then v.data.weapons = {} end
- if v.data.money == nil then v.data.money = {} elseif v.data.money[1].amount < 1 then v.data.money = {} end
- 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
- if (#v.data.coffre ~= 0 or #v.data.weapons ~= 0 or #v.data.money ~= 0 or #v.data.black_money ~= 0) then
- local owner = exports.ghmattimysql:scalarSync('SELECT `owner` FROM `owned_vehicles` WHERE plate = @plate', { ['@plate'] = v.plate })
- if owner == nil then
- DeleteTrunk(v.plate, 'because of no owner')
- else
- local count = #trunk.plate+1
- trunk.plate[count] = v.plate
- trunk.owner[count] = owner
- if #v.data.coffre ~= 0 then trunk.inventory[count] = v.data.coffre end
- if #v.data.weapons ~= 0 then trunk.weapons[count] = v.data.weapons end
- if (#v.data.money ~= 0 or #v.data.black_money ~= 0) then
- trunk.accounts[count] = {}
- if #v.data.money ~= 0 then trunk.accounts[count].money = v.data.money[1] end
- if #v.data.black_money ~= 0 then trunk.accounts[count].black_money = v.data.black_money[1] end
- end
- end
- else
- DeleteTrunk(v.plate, 'because of no data')
- end
- else
- DeleteTrunk(v.plate, 'because of no data')
- end
- end
- totalTrunkCount = #trunk.plate
- if totalTrunkCount == 0 then
- Print('THERE ARE NO TRUNK INVENTORIES TO CONVERT')
- else
- Print('FOUND '..totalTrunkCount..' TRUNKS WITH OLD INVENTORY DATA', 'STARTING CONVERSION - WAIT UNTIL THE PROCESS IS COMPLETE')
- BeginTrunkConversion()
- end
- else
- Print('NO RESULTS FOUND, DATABASE MUST INCLUDE PLATE AND DATA^0')
- end
- end
- DeleteTrunk = function(plate, reason)
- local deleted = exports.ghmattimysql:executeSync('DELETE FROM `trunk_inventory` WHERE plate = @plate', { ['@plate'] = plate })
- if deleted.affectedRows > 0 then
- print('^1 Deleted trunk for '..plate..' '..reason)
- else
- print('^1 FAILED TO DELETE TRUNK FOR '..plate)
- end
- end
- local newInventory = {}
- local newTrunkInventory = {}
- BeginConversion = function()
- for i=1, #player.identifier do
- local newInv = {}
- local loop = 0
- if player.accounts[i] then
- for k, v in pairs(player.accounts[i]) do
- local xItem = Items[k]
- if xItem and v > 0 then
- loop = loop + 1
- v = {slot=loop, name=k, count=v}
- newInv[loop] = v
- end
- end
- end
- if player.loadout[i] then
- for k, v in pairs(player.loadout[i]) do
- if k == 'GADGET_PARACHUTE' then k = 'parachute' end
- local xItem = Items[k]
- if xItem then
- loop = loop + 1
- v = {slot=loop, name=k, count=1, metadata = {}}
- if not Items[k].ammoname then
- if not v.metadata.durability then v.metadata.durability = 100 end
- else
- if not v.metadata.durability then v.metadata.durability = 100 end
- if Items[k].ammoname then v.metadata.ammo = 0 end
- if not v.metadata.components then v.metadata.components = {} end
- v.metadata.serial = GenerateSerial()
- end
- newInv[loop] = v
- end
- end
- end
- if player.inventory[i] then
- for k, v in pairs(player.inventory[i]) do
- local xItem = Items[k]
- if xItem and v > 0 then
- loop = loop + 1
- v = {slot=loop, name=k, count=v}
- newInv[loop] = v
- end
- end
- end
- newInventory[i] = json.encode(newInv)
- local result = exports.ghmattimysql:executeSync('UPDATE users SET inventory = @inventory WHERE identifier = @identifier', {
- ['@inventory'] = newInventory[i],
- ['@identifier'] = player.identifier[i]
- })
- if result then
- currentCount = currentCount + 1
- print('^2 Updated '..currentCount..' of '..totalCount..' players^0')
- end
- end
- end
- BeginTrunkConversion = function()
- for i = 1, #trunk.plate do
- local newTrunk = {}
- local loop = 0
- if trunk.inventory[i] then
- for k, v in pairs(trunk.inventory[i]) do
- local xItem = Items[v.name]
- if xItem and v.count > 0 then
- loop = loop + 1
- v = {slot = loop, name = v.name, count = v.count, metadata = {}}
- newTrunk[loop] = v
- end
- end
- end
- if trunk.weapons[i] then
- for k,v in pairs(trunk.weapons[i]) do
- if v.name == 'GADGET_PARACHUTE' then v.name = 'parachute' end
- local xItem = Items[v.name]
- if xItem then
- loop = loop + 1
- v = {slot = loop, name = v.name, count = 1, metadata = {}}
- if not Items[v.name].ammoname then
- if not v.metadata.durability then v.metadata.durability = 100 end
- else
- if not v.metadata.durability then v.metadata.durability = 100 end
- if Items[v.name].ammoname then v.metadata.ammo = 0 end
- if not v.metadata.components then v.metadata.components = {} end
- v.metadata.serial = GenerateSerial()
- end
- newTrunk[loop] = v
- end
- end
- end
- if trunk.accounts[i] then
- for k,v in pairs(trunk.accounts[i]) do
- local xItem = Items[k]
- if xItem and v.amount > 0 then
- loop = loop + 1
- v = {slot = loop, name = k, count = v.amount, metadata = {}}
- newTrunk[loop] = v
- end
- end
- end
- if #newTrunk > 0 then
- newTrunkInventory[i] = json.encode(newTrunk)
- local result = exports.ghmattimysql:executeSync('INSERT INTO `linden_inventory` (owner, name, data) VALUES (@owner, @name, @data)', {
- ['@owner'] = trunk.owner[i],
- ['@name'] = 'trunk-'..trunk.plate[i],
- ['@data'] = newTrunkInventory[i]
- })
- if result then
- currentTrunkCount = currentTrunkCount + 1
- DeleteTrunk(trunk.plate[i], ' converted!')
- print('^2 Updated '..currentTrunkCount..' of '..totalTrunkCount..' trunks^0')
- end
- else
- currentTrunkCount = currentTrunkCount + 1
- print('^1 Failed due to no data '..currentTrunkCount..' of '..totalTrunkCount..' trunks^0')
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement