Files
esx_core/esx_kashacters/server/main.lua
T
Dione Batista bbe14d30d3 More Corrections
correction in the camera spawn getting stuck for not finding the player's coordinates
2020-03-26 18:57:53 -03:00

134 lines
4.8 KiB
Lua

---------------------------------------------------------------------------------------
-- Edit this table to all the database tables and columns
-- where identifiers are used (such as users, owned_vehicles, owned_properties etc.)
---------------------------------------------------------------------------------------
local IdentifierTables = {
{table = "addon_account_data", column = "owner"},
{table = "addon_inventory_items", column = "owner"},
{table = "billing", column = "identifier"},
{table = "datastore_data", column = "owner"},
{table = "owned_vehicles", column = "owner"},
{table = "owned_properties", column = "owner"},
{table = "rented_vehicles", column = "owner"},
{table = "users", column = "identifier"},
{table = "user_licenses", column = "owner"},
}
RegisterServerEvent("kashactersS:SetupCharacters")
AddEventHandler('kashactersS:SetupCharacters', function()
local src = source
local LastCharId = GetLastCharacter(src)
SetIdentifierToChar(GetRockstarID(src), LastCharId)
local Characters = GetPlayerCharacters(src)
TriggerClientEvent('kashactersC:SetupUI', src, Characters)
end)
RegisterServerEvent("kashactersS:CharacterChosen")
AddEventHandler('kashactersS:CharacterChosen', function(charid, ischar)
local src = source
local new = true
local spawn = {}
SetLastCharacter(src, tonumber(charid))
SetCharToIdentifier(GetRockstarID(src), tonumber(charid))
if ischar == "true" then
new = false
spawn = GetSpawnPos(src)
else
TriggerClientEvent('skinchanger:loadDefaultModel', src, true, cb)
spawn = { x = 195.55, y = -933.36, z = 29.90 } -- DEFAULT SPAWN POSITION
end
TriggerClientEvent("kashactersC:SpawnCharacter", src, spawn, new)
end)
RegisterServerEvent("kashactersS:DeleteCharacter")
AddEventHandler('kashactersS:DeleteCharacter', function(charid)
local src = source
DeleteCharacter(GetRockstarID(src), charid)
TriggerClientEvent("kashactersC:ReloadCharacters", src)
end)
function GetPlayerCharacters(source)
local Chars = MySQLAsyncExecute("SELECT * FROM `users` WHERE identifier LIKE '%"..GetIdentifierWithoutLicense(GetRockstarID(source)).."%'")
for i = 1, #Chars, 1 do
charJob = MySQLAsyncExecute("SELECT * FROM `jobs` WHERE `name` = '"..Chars[i].job.."'")
charJobgrade = MySQLAsyncExecute("SELECT * FROM `job_grades` WHERE `grade` = '"..Chars[i].job_grade.."'")
Chars[i].job = charJob[1].label
Chars[i].job_grade = charJobgrade[1].label
end
return Chars
end
function GetLastCharacter(source)
local LastChar = MySQLAsyncExecute("SELECT `charid` FROM `user_lastcharacter` WHERE `steamid` = '"..GetRockstarID(source).."'")
if LastChar[1] ~= nil and LastChar[1].charid ~= nil then
return tonumber(LastChar[1].charid)
else
MySQLAsyncExecute("INSERT INTO `user_lastcharacter` (`steamid`, `charid`) VALUES('"..GetRockstarID(source).."', 1)")
return 1
end
end
function SetLastCharacter(source, charid)
MySQLAsyncExecute("UPDATE `user_lastcharacter` SET `charid` = '"..charid.."' WHERE `steamid` = '"..GetRockstarID(source).."'")
end
function SetIdentifierToChar(identifier, charid)
for _, itable in pairs(IdentifierTables) do
MySQLAsyncExecute("UPDATE `"..itable.table.."` SET `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutLicense(identifier).."' WHERE `"..itable.column.."` = '"..identifier.."'")
end
end
function SetCharToIdentifier(identifier, charid)
for _, itable in pairs(IdentifierTables) do
MySQLAsyncExecute("UPDATE `"..itable.table.."` SET `"..itable.column.."` = '"..identifier.."' WHERE `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutLicense(identifier).."'")
end
end
function DeleteCharacter(identifier, charid)
for _, itable in pairs(IdentifierTables) do
MySQLAsyncExecute("DELETE FROM `"..itable.table.."` WHERE `"..itable.column.."` = 'Char"..charid..GetIdentifierWithoutLicense(identifier).."'")
end
end
function GetSpawnPos(source)
local SpawnPos = MySQLAsyncExecute("SELECT `position` FROM `users` WHERE `identifier` = '"..GetIdentifierWithoutLicense(GetRockstarID(source)).."'")
return json.decode(SpawnPos[1].position)
end
function GetIdentifierWithoutLicense(Identifier)
return string.gsub(Identifier, "license:", "")
end
function GetRockstarID(playerId)
local identifier
for k,v in ipairs(GetPlayerIdentifiers(playerId)) do
if string.match(v, 'license:') then
identifier = v
break
end
end
return identifier
end
function MySQLAsyncExecute(query)
local IsBusy = true
local result = nil
MySQL.Async.fetchAll(query, {}, function(data)
result = data
IsBusy = false
end)
while IsBusy do
Citizen.Wait(0)
end
return result
end