Save accounts as encoded json text, migrate script found in development discord

This commit is contained in:
ElPumpo
2020-03-06 20:41:37 +01:00
parent e31077efbb
commit 3ba2ed150f
5 changed files with 68 additions and 139 deletions
+2 -10
View File
@@ -2,10 +2,11 @@ USE `essentialmode`;
CREATE TABLE `users` (
`identifier` VARCHAR(40) NOT NULL,
`accounts` LONGTEXT NULL DEFAULT NULL,
`group` VARCHAR(50) NULL DEFAULT 'user',
`inventory` LONGTEXT NULL DEFAULT NULL,
`job` VARCHAR(20) NULL DEFAULT 'unemployed',
`job_grade` INT(11) NULL DEFAULT 0,
`inventory` LONGTEXT NULL DEFAULT NULL,
`loadout` LONGTEXT NULL DEFAULT NULL,
`position` VARCHAR(53) NULL DEFAULT '{"x":-269.4,"y":-955.3,"z":31.2,"heading":205.8}',
@@ -45,12 +46,3 @@ CREATE TABLE `jobs` (
);
INSERT INTO `jobs` VALUES ('unemployed','Unemployed');
CREATE TABLE `user_accounts` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`identifier` VARCHAR(40) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`money` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
+12 -36
View File
@@ -82,8 +82,18 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
return self.variables[k]
end
self.getAccounts = function()
return self.accounts
self.getAccounts = function(minimal)
if minimal then
local minimalAccounts = {}
for k,v in ipairs(self.accounts) do
minimalAccounts[v.name] = v.money
end
return minimalAccounts
else
return self.accounts
end
end
self.getAccount = function(account)
@@ -151,40 +161,6 @@ function CreateExtendedPlayer(playerId, identifier, group, accounts, inventory,
self.name = newName
end
self.getMissingAccounts = function(cb)
local missingAccounts = {}
for account,label in pairs(Config.Accounts) do
local found = false
for k2,v2 in ipairs(self.accounts) do
if account == v2.name then
found = true
end
end
if not found then
missingAccounts[account] = Config.StartingAccountMoney[account] or 0
end
end
cb(missingAccounts)
end
self.createMissingAccounts = function(missingAccounts, cb)
for name,money in pairs(missingAccounts) do
MySQL.Async.execute('INSERT INTO user_accounts (identifier, name, money) VALUES (@identifier, @name, @money)', {
['@identifier'] = self.identifier,
['@name'] = name,
['@money'] = money
}, function(rowsChanged)
if cb then
cb()
end
end)
end
end
self.setAccountMoney = function(accountName, money)
if money >= 0 then
local account = self.getAccount(accountName)
+1 -13
View File
@@ -5,7 +5,6 @@ ESX.Items = {}
ESX.ServerCallbacks = {}
ESX.TimeoutCount = -1
ESX.CancelledTimeouts = {}
ESX.LastPlayerData = {}
ESX.Pickups = {}
ESX.PickupId = 0
ESX.Jobs = {}
@@ -58,17 +57,6 @@ MySQL.ready(function()
print('[es_extended] [^2INFO^7] ESX developed by ESX-Org has been initialized')
end)
AddEventHandler('esx:playerLoaded', function(playerId)
local xPlayer, accounts = ESX.GetPlayerFromId(playerId), {}
local xPlayerAccounts = xPlayer.getAccounts()
for i=1, #xPlayerAccounts, 1 do
accounts[xPlayerAccounts[i].name] = xPlayerAccounts[i].money
end
ESX.LastPlayerData[playerId] = {accounts = accounts}
end)
RegisterServerEvent('esx:clientLog')
AddEventHandler('esx:clientLog', function(msg)
if Config.EnableDebug then
@@ -80,7 +68,7 @@ RegisterServerEvent('esx:triggerServerCallback')
AddEventHandler('esx:triggerServerCallback', function(name, requestId, ...)
local playerId = source
ESX.TriggerServerCallback(name, requestID, playerId, function(...)
ESX.TriggerServerCallback(name, requestId, playerId, function(...)
TriggerClientEvent('esx:serverCallback', playerId, requestId, ...)
end, ...)
end)
+7 -25
View File
@@ -150,7 +150,7 @@ ESX.RegisterServerCallback = function(name, cb)
end
ESX.TriggerServerCallback = function(name, requestId, source, cb, ...)
if ESX.ServerCallbacks[name] ~= nil then
if ESX.ServerCallbacks[name] then
ESX.ServerCallbacks[name](source, cb, ...)
else
print(('[es_extended] [^3WARNING^7] Server callback "%s" does not exist. Make sure that the server sided file really is loading, an error in that file might cause it to not load.'):format(name))
@@ -160,26 +160,9 @@ end
ESX.SavePlayer = function(xPlayer, cb)
local asyncTasks = {}
-- User accounts
for k,v in ipairs(xPlayer.accounts) do
if ESX.LastPlayerData[xPlayer.source].accounts[v.name] ~= v.money then
table.insert(asyncTasks, function(cb)
MySQL.Async.execute('UPDATE user_accounts SET money = @money WHERE identifier = @identifier AND name = @name', {
['@money'] = v.money,
['@identifier'] = xPlayer.identifier,
['@name'] = v.name
}, function(rowsChanged)
cb()
end)
end)
ESX.LastPlayerData[xPlayer.source].accounts[v.name] = v.money
end
end
-- Job, loadout, inventory and position
table.insert(asyncTasks, function(cb)
MySQL.Async.execute('UPDATE users SET job = @job, job_grade = @job_grade, loadout = @loadout, position = @position, inventory = @inventory WHERE identifier = @identifier', {
MySQL.Async.execute('UPDATE users SET accounts = @accounts, job = @job, job_grade = @job_grade, loadout = @loadout, position = @position, inventory = @inventory WHERE identifier = @identifier', {
['@accounts'] = json.encode(xPlayer.getAccounts(true)),
['@job'] = xPlayer.job.name,
['@job_grade'] = xPlayer.job.grade,
['@loadout'] = json.encode(xPlayer.getLoadout(true)),
@@ -194,15 +177,14 @@ ESX.SavePlayer = function(xPlayer, cb)
Async.parallel(asyncTasks, function(results)
print(('[es_extended] [^2INFO^7] Saved player "%s^7"'):format(xPlayer.getName()))
if cb ~= nil then
if cb then
cb()
end
end)
end
ESX.SavePlayers = function(cb)
local asyncTasks = {}
local xPlayers = ESX.GetPlayers()
local xPlayers, asyncTasks = ESX.GetPlayers(), {}
for i=1, #xPlayers, 1 do
table.insert(asyncTasks, function(cb)
@@ -213,7 +195,7 @@ ESX.SavePlayers = function(cb)
Async.parallelLimit(asyncTasks, 8, function(results)
print(('[es_extended] [^2INFO^7] Saved %s player(s)'):format(#xPlayers))
if cb ~= nil then
if cb then
cb()
end
end)
@@ -259,7 +241,7 @@ ESX.UseItem = function(source, item)
end
ESX.GetItemLabel = function(item)
if ESX.Items[item] ~= nil then
if ESX.Items[item] then
return ESX.Items[item].label
end
end
+46 -55
View File
@@ -20,7 +20,14 @@ function onPlayerJoined(playerId)
if result then
loadESXPlayer(identifier, playerId)
else
MySQL.Async.execute('INSERT INTO users (identifier) VALUES (@identifier)', {
local accounts = {}
for account,money in pairs(Config.StartingAccountMoney) do
accounts[account] = money
end
MySQL.Async.execute('INSERT INTO users (accounts, identifier) VALUES (@accounts, @identifier)', {
['@accounts'] = json.encode(accounts),
['@identifier'] = identifier
}, function(rowsChanged)
loadESXPlayer(identifier, playerId)
@@ -43,31 +50,31 @@ function loadESXPlayer(identifier, playerId)
playerName = GetPlayerName(playerId)
}
-- get accounts
table.insert(tasks, function(cb)
MySQL.Async.fetchAll('SELECT name, money FROM user_accounts WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(accounts)
for k,v in ipairs(accounts) do
if Config.Accounts[v.name] then
table.insert(userData.accounts, {
name = v.name,
money = v.money,
label = Config.Accounts[v.name]
})
end
end
cb()
end)
end)
table.insert(tasks, function(cb)
MySQL.Async.fetchAll('SELECT job, job_grade, `group`, loadout, position, inventory FROM users WHERE identifier = @identifier', {
MySQL.Async.fetchAll('SELECT accounts, job, job_grade, `group`, loadout, position, inventory FROM users WHERE identifier = @identifier', {
['@identifier'] = identifier
}, function(result)
local job, grade, jobObject, gradeObject = result[1].job, tostring(result[1].job_grade)
local fonudAccounts, foundItems = {}, {}
-- Accounts
if result[1].accounts and result[1].accounts ~= '' then
local accounts = json.decode(result[1].accounts)
for account,money in pairs(accounts) do
fonudAccounts[account] = money
end
end
for account,label in pairs(Config.Accounts) do
table.insert(userData.accounts, {
name = account,
money = fonudAccounts[account] or Config.StartingAccountMoney[account] or 0,
label = label
})
end
-- Job
if ESX.DoesJobExist(job, grade) then
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
else
@@ -76,8 +83,6 @@ function loadESXPlayer(identifier, playerId)
jobObject, gradeObject = ESX.Jobs[job], ESX.Jobs[job].grades[grade]
end
userData.job = {}
userData.job.id = jobObject.id
userData.job.name = jobObject.name
userData.job.label = jobObject.label
@@ -93,8 +98,7 @@ function loadESXPlayer(identifier, playerId)
if gradeObject.skin_male then userData.job.skin_male = json.decode(gradeObject.skin_male) end
if gradeObject.skin_female then userData.job.skin_female = json.decode(gradeObject.skin_female) end
local foundItems = {}
-- Inventory
if result[1].inventory and result[1].inventory ~= '' then
local inventory = json.decode(result[1].inventory)
@@ -123,6 +127,7 @@ function loadESXPlayer(identifier, playerId)
})
end
-- Group
if result[1].group then
userData.group = result[1].group
else
@@ -133,6 +138,7 @@ function loadESXPlayer(identifier, playerId)
return a.label < b.label
end)
-- Loadout
if result[1].loadout and result[1].loadout ~= '' then
local loadout = json.decode(result[1].loadout)
@@ -154,6 +160,7 @@ function loadESXPlayer(identifier, playerId)
end
end
-- Position
if result[1].position and result[1].position ~= '' then
userData.coords = json.decode(result[1].position)
else
@@ -167,37 +174,22 @@ function loadESXPlayer(identifier, playerId)
Async.parallel(tasks, function(results)
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.job, userData.loadout, userData.playerName, userData.coords)
ESX.Players[playerId] = xPlayer
TriggerEvent('esx:playerLoaded', playerId, xPlayer)
xPlayer.getMissingAccounts(function(missingAccounts)
if ESX.Table.SizeOf(missingAccounts) > 0 then
for name,money in pairs(missingAccounts) do
table.insert(xPlayer.accounts, {
name = name,
money = money,
label = Config.Accounts[name]
})
end
xPlayer.triggerEvent('esx:playerLoaded', {
accounts = xPlayer.getAccounts(),
coords = xPlayer.getCoords(),
identifier = xPlayer.identifier,
inventory = xPlayer.getInventory(),
job = xPlayer.getJob(),
loadout = xPlayer.getLoadout(),
maxWeight = xPlayer.maxWeight,
money = xPlayer.getMoney(),
})
xPlayer.createMissingAccounts(missingAccounts)
end
ESX.Players[playerId] = xPlayer
TriggerEvent('esx:playerLoaded', playerId, xPlayer)
xPlayer.triggerEvent('esx:playerLoaded', {
identifier = xPlayer.identifier,
money = xPlayer.getMoney(),
accounts = xPlayer.getAccounts(),
coords = xPlayer.getCoords(),
inventory = xPlayer.getInventory(),
job = xPlayer.getJob(),
loadout = xPlayer.getLoadout(),
maxWeight = xPlayer.maxWeight
})
xPlayer.triggerEvent('esx:createMissingPickups', ESX.Pickups)
xPlayer.triggerEvent('esx:registerSuggestions', ESX.RegisteredCommands)
end)
xPlayer.triggerEvent('esx:createMissingPickups', ESX.Pickups)
xPlayer.triggerEvent('esx:registerSuggestions', ESX.RegisteredCommands)
end)
end
@@ -210,7 +202,6 @@ AddEventHandler('playerDropped', function(reason)
ESX.SavePlayer(xPlayer, function()
ESX.Players[playerId] = nil
ESX.LastPlayerData[playerId] = nil
end)
end
end)