From f4829871adafd76d7980d711f7d7b05765ca3b83 Mon Sep 17 00:00:00 2001 From: ElPumpo Date: Tue, 14 Apr 2020 16:49:28 +0200 Subject: [PATCH] Initial improvements --- esx_identity.sql | 2 +- locales/cs.lua | 7 ++- locales/en.lua | 7 ++- server/main.lua | 144 ++++++++++++++++++++++------------------------- 4 files changed, 75 insertions(+), 85 deletions(-) diff --git a/esx_identity.sql b/esx_identity.sql index b8eeb2ee..56b88ae4 100644 --- a/esx_identity.sql +++ b/esx_identity.sql @@ -1,4 +1,4 @@ -USE `essentialmode`; +USE `es_extended`; ALTER TABLE `users` ADD COLUMN `firstname` VARCHAR(15) NULL DEFAULT NULL, diff --git a/locales/cs.lua b/locales/cs.lua index e7d2ed38..98172461 100644 --- a/locales/cs.lua +++ b/locales/cs.lua @@ -1,5 +1,6 @@ Locales['cs'] = { - ['data_incorrect'] = 'Data byla zadána nesprávně. Prosím zkuste to znovu.', - ['invalid_format'] = 'Data byla nesprávně naformátována. Prosím zkuste to znovu.', - ['no_identifiers'] = '[Chyba] Nelze najít identifikátory.' + ['data_incorrect'] = 'data byla zadána nesprávně. Prosím zkuste to znovu.', + ['invalid_format'] = 'data byla nesprávně naformátována. Prosím zkuste to znovu.', + ['no_identifier'] = '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.', + ['missing_identity'] = 'there was an error loading your character!\nError code: identity-missing\n\nIt seems like your identity is missing, try connecting again.', } diff --git a/locales/en.lua b/locales/en.lua index 64237849..a8e381e4 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -1,5 +1,6 @@ Locales['en'] = { - ['data_incorrect'] = 'Data entered incorrectly. Please try again.', - ['invalid_format'] = 'Data formatted incorrectly. Please try again.', - ['no_identifiers'] = '[Error] Could not find identifiers.' + ['data_incorrect'] = 'data entered incorrectly. Please try again.', + ['invalid_format'] = 'data formatted incorrectly. Please try again.', + ['no_identifier'] = '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.', + ['missing_identity'] = 'there was an error loading your character!\nError code: identity-missing\n\nIt seems like your identity is missing, try connecting again.', } diff --git a/server/main.lua b/server/main.lua index b8b9dbc0..3fbc4962 100644 --- a/server/main.lua +++ b/server/main.lua @@ -1,106 +1,96 @@ -ESX = nil -local tempIdentity = {} +ESX = nil +local playerIdentity = {} TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end) -AddEventHandler("playerConnecting", function(name, setKickReason, deferrals) - local player = source - local identifiers = GetPlayerIdentifiers(player) - local identifier +AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals) deferrals.defer() + local playerId, identifier = source + Citizen.Wait(100) - Citizen.Wait(500) - - for k, v in pairs(identifiers) do + for k,v in ipairs(GetPlayerIdentifiers(playerId)) do if string.match(v, 'license:') then identifier = string.sub(v, 9) break end end - Citizen.Wait(500) - if identifier then MySQL.Async.fetchAll('SELECT firstname, lastname, dateofbirth, sex, height FROM users WHERE identifier = @identifier', { ['@identifier'] = identifier }, function(result) if result[1] then - if result[1].firstname then - tempIdentity[identifier] = { - firstName = result[1].firstname, - lastName = result[1].lastname, - dateOfBirth = result[1].dateofbirth, - sex = result[1].sex, - height = result[1].height, - registered = false - } - end - end - end) + playerIdentity[identifier] = { + firstName = result[1].firstname, + lastName = result[1].lastname, + dateOfBirth = result[1].dateofbirth, + sex = result[1].sex, + height = result[1].height + } - Citizen.Wait(500) - - if tempIdentity[identifier] and tempIdentity[identifier].firstName then - tempIdentity[identifier].registered = true - deferrals.done() - else - deferrals.presentCard([==[{"type": "AdaptiveCard", "body": [{ "type": "ColumnSet", "columns": [{ "type": "Column", "items": [ - { "type": "Input.Text", "placeholder": "First Name (Max 15 Characters)", "id": "firstname" }], "width": "stretch" }]}, - { "type": "Input.Text", "placeholder": "Last Name (Max 15 Characters)", "id": "lastname" }, - { "type": "Input.Text", "placeholder": "Height (24-96 Inches)", "id": "height" }, - { "type": "Input.Date", "id": "dateofbirth" }, - { "type": "Input.ChoiceSet", "placeholder": "Sex", "choices": [{ "title": "Male", "value": "m" }, { "title": "Female", "value": "f" }], "style": "expanded", "value": "m", "id": "sex" }], - "actions": [{ "type": "Action.Submit", "title": "Submit" }], - "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", - "version": "1.0" - }]==], function(submittedData, rawData) - if submittedData.firstname == "" or submittedData.lastname == "" or submittedData.dateofbirth == "" or submittedData.sex == "" or submittedData.height == "" then - deferrals.done(_U('data_incorrect')) - else - if checkNameFormat(submittedData.firstname) and checkNameFormat(submittedData.lastname) and checkDOBFormat(submittedData.dateofbirth) and checkSexFormat(submittedData.sex) and checkHeightFormat(submittedData.height) then - local formattedFirstName = formatName(submittedData.firstname) - local formattedLastName = formatName(submittedData.lastname) - local formattedHeight = tonumber(submittedData.height) - tempIdentity[identifier] = { - firstName = formattedFirstName, - lastName = formattedLastName, - dateOfBirth = submittedData.dateofbirth, - sex = submittedData.sex, - height = formattedHeight, - registered = false - } - deferrals.done() + deferrals.done() + else + deferrals.presentCard([==[{"type":"AdaptiveCard","body":[{"type":"Container","items":[{"type":"TextBlock","text":"Identity Registration","horizontalAlignment":"Center","size":"Large","weight":"Bolder"},{"type":"ColumnSet","columns":[{"type":"Column","items":[{"type":"Input.Text","placeholder":"First Name","id":"firstname","maxLength":15}],"width":"stretch"},{"type":"Column","width":"stretch","items":[{"type":"Input.Text","placeholder":"Last Name","id":"lastname","maxLength":15}]}]},{"type":"ColumnSet","columns":[{"type":"Column","width":"stretch","items":[{"type":"Input.Text","placeholder":"Date of Birth (YYYY-MM-DD)","id":"dateofbirth","maxLength":10}],"style":"default"},{"type":"Column","width":"stretch","items":[{"type":"Input.Text","placeholder":"Height (24-96 inches)","id":"height","maxLength":2}]}]},{"type":"Input.ChoiceSet","placeholder":"Sex","choices":[{"title":"Male","value":"m"},{"title":"Female","value":"f"}],"style":"expanded","id":"sex"}],"style":"default","backgroundImage":"https://nischat.se/wp-content/uploads/Light-grey-background-1024x683.jpg"}],"actions":[{"type":"Action.Submit","title":"Submit"}],"$schema":"http://adaptivecards.io/schemas/adaptive-card.json","version":"1.0"}]==], function(data, rawData) + if + data.firstname == '' or + data.lastname == '' or + data.dateofbirth == '' or + data.sex == '' or + data.height == '' + then + deferrals.done(_U('data_incorrect')) else - deferrals.done(_U('invalid_format')) + if + checkNameFormat(data.firstname) and + checkNameFormat(data.lastname) and + checkDOBFormat(data.dateofbirth) and + checkSexFormat(data.sex) and + checkHeightFormat(data.height) + then + playerIdentity[identifier] = { + firstName = formatName(data.firstname), + lastName = formatName(data.lastname), + dateOfBirth = data.dateofbirth, + sex = data.sex, + height = tonumber(data.height), + saveToDatabase = true + } + + deferrals.done() + else + deferrals.done(_U('invalid_format')) + end end - end - end) - end + end) + end + end) else - deferrals.done(_U('no_identifiers')) + deferrals.done(_U('no_identifier')) end end) RegisterNetEvent('esx:playerLoaded') AddEventHandler('esx:playerLoaded', function(playerId, xPlayer) - if tempIdentity[xPlayer.identifier] then - local currentIdentity = tempIdentity[xPlayer.identifier] + if playerIdentity[xPlayer.identifier] then + local currentIdentity = playerIdentity[xPlayer.identifier] + xPlayer.setName(('%s %s'):format(currentIdentity.firstName, currentIdentity.lastName)) xPlayer.set('firstName', currentIdentity.firstName) xPlayer.set('lastName', currentIdentity.lastName) xPlayer.set('dateofbirth', currentIdentity.dateOfBirth) xPlayer.set('sex', currentIdentity.sex) xPlayer.set('height', currentIdentity.height) - if not currentIdentity.registered then - currentIdentity.registered = true - SetIdentity(xPlayer.identifier, currentIdentity) - else - tempIdentity[xPlayer.identifier] = nil + + if currentIdentity.saveToDatabase then + SaveIdentityToDatabase(xPlayer.identifier, currentIdentity) end + + playerIdentity[xPlayer.identifier] = nil + else + xPlayer.kick(_('missing_identity')) end end) - function checkNameFormat(name) if not checkAlphanumeric(name) then if not checkForNumbers(name) then @@ -174,7 +164,7 @@ function checkDate(str) d = tonumber(d) if ((d <= 0) or (d > 31)) or ((m <= 0) or (m > 12)) or ((y <= Config.LowestYear) or (y > Config.HighestYear)) then return false - elseif m == 4 or m == 6 or m == 9 or m == 11 then + elseif m == 4 or m == 6 or m == 9 or m == 11 then if d > 30 then return false else @@ -194,7 +184,7 @@ function checkDate(str) return true end end - else + else if d > 31 then return false else @@ -206,15 +196,13 @@ function checkDate(str) end end -function SetIdentity(identifier, identity) - MySQL.Async.execute('UPDATE users SET firstname = @firstname, lastname = @lastname, dateofbirth = @dateofbirth, sex = @sex, height = @height WHERE identifier = @identifier', { +function SaveIdentityToDatabase(identifier, identity) + MySQL.Sync.execute('UPDATE users SET firstname = @firstname, lastname = @lastname, dateofbirth = @dateofbirth, sex = @sex, height = @height WHERE identifier = @identifier', { ['@identifier'] = identifier, - ['@firstname'] = identity.firstName, - ['@lastname'] = identity.lastName, + ['@firstname'] = identity.firstName, + ['@lastname'] = identity.lastName, ['@dateofbirth'] = identity.dateOfBirth, - ['@sex'] = identity.sex, - ['@height'] = identity.height + ['@sex'] = identity.sex, + ['@height'] = identity.height }) - - tempIdentity[identifier] = nil end