feat: Database connection validation

Suggested by tabarra. Instead of throwing them into awaiting scripts with 0 information. stop them from joining and tell them the user
This commit is contained in:
Mycroft
2023-03-01 11:43:06 -08:00
parent 3a77657679
commit e4b36d2d4c
3 changed files with 32 additions and 6 deletions
+2 -1
View File
@@ -8,7 +8,7 @@ Core.RegisteredCommands = {}
Core.Pickups = {}
Core.PickupId = 0
Core.PlayerFunctionOverrides = {}
Core.DatabaseConnected = false
Core.playersByIdentifier = {}
Core.vehicleTypesByModel = {}
@@ -39,6 +39,7 @@ local function StartDBSync()
end
MySQL.ready(function()
Core.DatabaseConnected = true
if not Config.OxInventory then
local items = MySQL.query.await('SELECT * FROM items')
for k, v in ipairs(items) do
+10 -4
View File
@@ -56,6 +56,7 @@ function onPlayerJoined(playerId)
if result then
loadESXPlayer(identifier, playerId, false)
else
createESXPlayer(identifier, playerId)
end
end
@@ -97,18 +98,23 @@ if not Config.Multichar then
local identifier = ESX.GetIdentifier(playerId)
if OnesyncState == "off" or OnesyncState == "legacy" then
deferrals.done(('[ESX] ESX Requires Onesync Infinity to work. This server currently has Onesync set to: %s'):format(OnesyncState))
return deferrals.done(('[ESX] ESX Requires Onesync Infinity to work. This server currently has Onesync set to: %s'):format(OnesyncState))
end
if not Core.DatabaseConnected then
return deferrals.done(('[ESX] ESX Cannot Connect to your database. Please make sure it is correctly configured in your server.cfg'):format(OnesyncState))
end
if identifier then
if ESX.GetPlayerFromIdentifier(identifier) then
deferrals.done(
return deferrals.done(
('[ESX] There was an error loading your character!\nError code: identifier-active\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same account.\n\nYour identifier: %s'):format(
identifier))
else
deferrals.done()
return deferrals.done()
end
else
deferrals.done(
return deferrals.done(
'[ESX] There was an error loading your character!\nError code: identifier-missing\n\nThe cause of this error is not known, your identifier could not be found. Please come back later or report this problem to the server administration team.')
end
end)
+20 -1
View File
@@ -1,6 +1,10 @@
if not ESX then
error('\n^1Unable to start Multicharacter - you must be using ESX Legacy^0')
elseif ESX.GetConfig().Multichar then
local DatabaseConnected = false
local DatabaseFound = false
local OnesyncState = GetConvar('onesync', 'off')
local DATABASE do
local connectionString = GetConvar('mysql_connection_string', '');
if connectionString == '' then
@@ -8,6 +12,7 @@ elseif ESX.GetConfig().Multichar then
elseif connectionString:find('mysql://') then
connectionString = connectionString:sub(9, -1)
DATABASE = connectionString:sub(connectionString:find('/')+1, -1):gsub('[%?]+[%w%p]*$', '')
DatabaseFound = true
else
connectionString = {string.strsplit(';', connectionString)}
for i = 1, #connectionString do
@@ -16,6 +21,7 @@ elseif ESX.GetConfig().Multichar then
DATABASE = v:sub(10, #v)
end
end
DatabaseFound = true
end
end
@@ -92,11 +98,23 @@ elseif ESX.GetConfig().Multichar then
AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals)
deferrals.defer()
local identifier = GetIdentifier(source)
if OnesyncState == "off" or OnesyncState == "legacy" then
return deferrals.done(('[ESX] ESX Requires Onesync Infinity to work. This server currently has Onesync set to: %s'):format(OnesyncState))
end
if not DatabaseFound then
deferrals.done(('[ESX Multicharacter] Cannot Find the servers mysql_connection_string. Please make sure it is correctly configured in your server.cfg'):format(OnesyncState))
end
if not DatabaseConnected then
deferrals.done(('[ESX Multicharacter] ESX Cannot Connect to your database. Please make sure it is correctly configured in your server.cfg'):format(OnesyncState))
end
if identifier then
if not ESX.GetConfig().EnableDebug then
if ESX.Players[identifier] then
deferrals.done(('A player is already connected to the server with this identifier.\nYour identifier: %s:%s'):format(PRIMARY_IDENTIFIER, identifier))
deferrals.done(('[ESX Multicharacter] A player is already connected to the server with this identifier.\nYour identifier: %s:%s'):format(PRIMARY_IDENTIFIER, identifier))
else
deferrals.done()
end
@@ -179,6 +197,7 @@ elseif ESX.GetConfig().Multichar then
until next(ESX.Jobs)
FETCH = 'SELECT identifier, accounts, job, job_grade, firstname, lastname, dateofbirth, sex, skin, disabled FROM users WHERE identifier LIKE ? LIMIT ?'
DatabaseConnected = true
end
end)