Jump to content

[HELP] Register check during authorization


Recommended Posts

I do authorization, I made a check so that two players with the same login could not log in at the same time. How to check if the case of the login entered in the game matches the login in the database?
"test" (game) - "test" (base) = the player will not be skipped, since the player with this login is already in the game.
"Test" (game) - "test" (base) = will skip the player and there will be 2 players in the game under one account.

 

addEvent('playerLogged', true)
addEventHandler('playerLogged', root, function(login, password)
	local check = exports.mysql:mysql_query_single("SELECT * FROM `players` WHERE `login` = ?", login)
	for _, p in pairs(getElementsByType("Player")) do
		if(getElementData(p, "login") == login) then
			outputChatBox("The player under this login is on the server.", source) 
			return 
		end
	end
	.....

 

Link to comment

You don't seem to be using the result of

local check = exports.mysql:mysql_query_single("SELECT * FROM `players` WHERE `login` = ?", login)

anywhere in the snippet, you might want to move it closer to where you use it for more readability, and either way, its not relevant to this snippet so it could have been omitted.

Now if you want to do a case insensitive comparison between people's logins, your best bet is to lowercase or uppercase both sides of the comparison, or ensure one of the sides is always lowercase/uppercase and case fold the other side:

-- turn both sides to lowercase
if(string.lower(getElementData(p, "login")) == string.lower(login)) then
  -- ...
end

-- or 

-- lowercase the user input
local lowercaseLogin = string.lower(login)
for _, p in --[[...]] do
  if(getElementData(p, "login") == lowercaseLogin) then -- compare stored
    -- ...
  end
end
--- ...

-- ensure the login value in element data is always stored in lowercase
setElementData(p, "login", lowercaseLogin)

The second option is slightly more efficient since you only need to lowercase it once per player when logging in, rather than for every player when one player logs in, essentially a O(1) vs O(n) difference.

Link to comment

Thanks. This is exactly what I did as a temporary solution. But I need to compare the player's login case sensitive. Unfortunately, I did not find a function for this.

if(getPlayerCount() > 1) then
	for _, p in pairs(getElementsByType("Player")) do
		if(hasElementData(p, "isLogged")) then 
			if(string.lower(login) == string.lower(getElementData(p, "login"))) then
				outputChatBox("The player under this login is on the server.", source) 
				return 
			end
		end
	end
end
Edited by egorkoltyshev
Link to comment
  • Moderators

I think you removed a lot of the actual code but your loop should work if you fix the typo here (everything you did is case sensitive already):

for _, p in pairs(getElementsByType("player")) do -- "player" not "Player"

If that's not the issue in your code, then there has to be an error in the logs ?

(playerLogged is the server event called from the login GUI form right ?)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...