diff --git a/locale/en.lua b/locale/en.lua index a41748e..a8b4a5a 100644 --- a/locale/en.lua +++ b/locale/en.lua @@ -17,6 +17,8 @@ local Translations = { no_permission = 'You don\'t have permissions for this..', no_waypoint = 'No Waypoint Set.', tp_error = 'Error While Teleporting.', + connecting_database_error = 'A database error occurred while connecting to the server. (Is the SQL server on?)', + connecting_database_timeout = 'Connection to database timed out. (Is the SQL server on?)', }, success = { server_opened = 'The server has been opened', diff --git a/server/events.lua b/server/events.lua index 06876ce..406fe98 100644 --- a/server/events.lua +++ b/server/events.lua @@ -34,8 +34,6 @@ local function onPlayerConnecting(name, _, deferrals) end end - deferrals.update(string.format(Lang:t('info.checking_ban'), name)) - for _, v in pairs(identifiers) do if string.find(v, 'license') then license = v @@ -43,30 +41,54 @@ local function onPlayerConnecting(name, _, deferrals) end end - -- Mandatory wait - Wait(2500) - - deferrals.update(string.format(Lang:t('info.checking_whitelisted'), name)) - - local isBanned, Reason = QBCore.Functions.IsPlayerBanned(src) - local isLicenseAlreadyInUse = QBCore.Functions.IsLicenseInUse(license) - local isWhitelist, whitelisted = QBCore.Config.Server.Whitelist, QBCore.Functions.IsWhitelisted(src) - - Wait(2500) - - deferrals.update(string.format(Lang:t('info.join_server'), name)) - if not license then - deferrals.done(Lang:t('error.no_valid_license')) - elseif isBanned then - deferrals.done(Reason) - elseif isLicenseAlreadyInUse and QBCore.Config.Server.CheckDuplicateLicense then + deferrals.done(Lang:t('error.no_valid_license')) + elseif QBCore.Config.Server.CheckDuplicateLicense and QBCore.Functions.IsLicenseInUse(license) then deferrals.done(Lang:t('error.duplicate_license')) - elseif isWhitelist and not whitelisted then - deferrals.done(Lang:t('error.not_whitelisted')) end - deferrals.done() + local databaseTime = os.clock() + local databasePromise = promise.new() + + CreateThread(function() + deferrals.update(string.format(Lang:t('info.checking_ban'), name)) + local databaseSuccess, databaseError = pcall(function() + local isBanned, Reason = QBCore.Functions.IsPlayerBanned(src) + if isBanned then + deferrals.done(Reason) + end + end) + + if QBCore.Config.Server.Whitelist then + deferrals.update(string.format(Lang:t('info.checking_whitelisted'), name)) + databaseSuccess, databaseError = pcall(function() + if not QBCore.Functions.IsWhitelisted(src) then + deferrals.done(Lang:t('error.not_whitelisted')) + end + end) + end + + if not databaseSuccess then + databasePromise:reject(databaseError) + end + databasePromise:resolve() + end) + + databasePromise:next(function() + deferrals.update(string.format(Lang:t('info.join_server'), name)) + deferrals.done() + end, function (databaseError) + deferrals.done(Lang:t('error.connecting_database_error')) + print('^1' .. databaseError) + end) + + while databasePromise.state == 0 do -- while database promise is pending + if os.clock() - databaseTime > 30 then -- if 30 seconds were spent waiting for the database + deferrals.done(Lang:t('error.connecting_database_timeout')) + break + end + Wait(1000) + end -- Add any additional defferals you may need! end