Skip to content

Commit

Permalink
Check player name (see README.md)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPumpo committed Mar 25, 2018
1 parent 84d404f commit 01f2fc5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Disclaimer: we have not developed all of the scripts in this project.
- Server restart alert
- Drift mode! `Numpad 9` to toggle the script and press `Left Shift` to drift
- Firendly NPCs!
- Many checks for the player name
- checks if the first and surname starts with a capital letter
- checks for special characters
- checks for short / long names

### Requirements
- ES
Expand Down
3 changes: 2 additions & 1 deletion __resource.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ server_scripts {
"server/discordbot_server.lua",
"server/gpstools-server.lua",
"server/commands-server.lua",
"server/restart_alert-server.lua"
"server/restart_alert-server.lua",
"server/check_name-server.lua"
}

client_scripts {
Expand Down
72 changes: 72 additions & 0 deletions server/check_name-server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
-- Mark this connection as deferred, this is to prevent problems while checking player identifiers.
deferrals.defer()

-- Save source in case we somehow lose it (which happens a lot)
local _source = source

-- Letting the user know what's going on.
deferrals.update('Validating player name . . .')

-- Needed, not sure why.
Citizen.Wait(100)

local playerName = GetPlayerName(_source)
local allowed = true
local reason = nil

-- Don't allow short user names
local nameLength = string.len(playerName)
if nameLength > 25 or nameLength < 7 then
reason = 'your player name is too short or too long. The name limit min: 7 | max: 25.'
allowed = false
end

-- Don't allow special characters (doesn't always work)
local charMatch = playerName:match("%W")
if charMatch == '' or charMatch == ' ' or charMatch == '' or charMatch == nil then

else
if reason == nil then
reason = 'your player name contains special characters that are not allowed on this server.'
end
allowed = false
end

-- Does the player carry a first and last name?
--
-- Example:
-- Allowed: 'Bob Joe'
-- Not allowed: 'Bob'
-- Not allowed: 'Bob joe'
local spacesInName = 0
local spacesWithUpper = 0
for word in string.gmatch(playerName, '%S+') do

if string.match(word, '%u') then
spacesWithUpper = spacesWithUpper + 1
end

spacesInName = spacesInName + 1
end

if spacesInName < 2 then
if reason == nil then
reason = 'your player name does not contain a first and last name.'
end
allowed = false
end

if spacesWithUpper ~= spacesInName then
if reason == nil then
reason = 'your first and surname must start with a capital letter.'
end
allowed = false
end

if allowed then
deferrals.done()
else
deferrals.done('You cannot connect with your current player name. Reason: ' .. reason .. ' If you belive that something is wrong, please contact the SCRP staff')
end
end)

0 comments on commit 01f2fc5

Please sign in to comment.