Jump to content

[HELP] Set player nickname from mysql login panel


Recommended Posts

hi all

resently i edited the login panel that stores data in mysql and i edited it to receive nick name from player and save it

but after register and login it can't get player saved nickname

here is my server side script

i think line 110 or 54 have issue

 

local db_host = "" --change this if your using MySQL off the server machine your using for the MTA server.
local db_username = "" --database username
local db_password = "" --datbase password
local db_table = ""
local db_port = ""
local connection = false

function connect()
	connection = dbConnect("mysql","dbname="..db_table..";host="..db_host..";port="..db_port..";unix_socket=/var/run/mysqld/mysqld.sock",db_username,db_password)
	if (connection) then
		outputChatBox("connected")
		return true
	else
		outputChatBox("notconnected")
		setTimer(connect,5000,1)
	end
end
addEventHandler("onResourceStart",resourceRoot,connect)

function singleQuery(str,...)
	if (connection) then
		local query = dbQuery(connection,str,...)
		local result = dbPoll(query,-1)
		if (type(result == "table")) then
			return result[1]
		else
			return result
		end
	else
		return false
	end
end

function execute(str,...)
	if (connection) then
		local query = dbExec(connection,str,...)
		return query
	else
		return false
	end
end

----------------------------------------------------------------------------------------------------------------------

addEvent("onPlayerAttemptLogin",true)
addEventHandler("onPlayerAttemptLogin",root,
function(username,nickname,password)
	if (singleQuery("SELECT * FROM accounts WHERE username=? LIMIT 1",string.lower(username))) then
		local accData = singleQuery("SELECT * FROM accounts WHERE username=? AND nickname=? AND password=? LIMIT 1",string.lower(username),string.lower(nickname),sha256(password))
		if (accData) then
			outputChatBox("Welcome back "..getPlayerName(source),source,0,255,0)
			setElementData(source,"accountID",accData.id)
			setElementData(source,"accountUsername",accData.username)
			setElementData(source,"accountNickname",accData.nickname)
			fadeCamera(source,false,1.0,0,0,0)
			setTimer(fadeCamera,2000,1,source,true,1.0,0,0,0)
			setCameraTarget(source,source)
			showChat(source,true)
			showPlayerHudComponent(source,"radar",true)
			showPlayerHudComponent(source,"area_name",true)
			spawnPlayer(source,accData.x,accData.y,accData.z+1,accData.rotation,accData.skin,accData.interior,accData.dimension,accData.team)
			
			local weapons = fromJSON(accData.weapons)
			if (weapons) then
				for k,v in pairs(weapons) do
					giveWeapon(source,tonumber(k),tonumber(v))
				end
			end
			
			if (accData.health == 0) then
				killPed(source)
			else
				setElementHealth(source,tonumber(accData.health))
			end
			
			setPedArmor(source,tonumber(accData.armor))
			setPlayerMoney(source,tonumber(accData.money))
			setElementData(source,"isGuestAccount",false)
			triggerClientEvent(source,"closeLoginWindow",source)
			triggerEvent("onAccountPlayerLogin",source,accData.id,accData.username,accData.nickname)
			
			log(source) --store the login data.
		else
			outputChatBox("Your username and/or password is incorrect!",source,255,0,0)
		end
	else
		outputChatBox("This username doesn't exist!",source,255,0,0)
	end
end)

addEvent("onPlayerAttemptRegister",true)
addEventHandler("onPlayerAttemptRegister",root,
function(username,nickname,password1,password2,email)
	if (singleQuery("SELECT username FROM accounts WHERE username=? LIMIT 1",string.lower(username))) then
		outputChatBox("This username is taken! please choose another!",source,255,0,0)
	else
		local x,y,z = 1450.32421875, -2287.287109375, 13.546875
		if (execute("INSERT INTO accounts (username,nickname,password,email,serial,x,y,z,interior,dimension,skin,health) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)",string.lower(username),string.lower(nickname),sha256(password1),email,getPlayerSerial(source),x,y,z+1,0,0,0,100)) then
			outputChatBox("Your account was registered successfully!",source,0,255,0)
			triggerClientEvent(source,"switchToLogin",source)
		end
	end
end)
------------------------------

function log(player)
	if (player) then
		local serial = getPlayerSerial(player)
		local ip = getPlayerIP(player)
		local name = getElementData(palyer, "accountNickname")
		local username = getElementData(player,"accountUsername")
		local timestamp = getRealTime().timestamp
		
		execute("INSERT INTO logs (username,playerName,serial,ip,timestamp) VALUES (?,?,?,?,?)",username,name,serial,ip,timestamp)
	end
end

function prepair()
	setElementData(source,"isGuestAccount",true)
	outputChatBox("Welcome to the server.",source,0,255,0)
end
addEventHandler("onPlayerJoin",root,prepair)

function saveData()
	if not (getElementData(source,"isGuestAccount") == true) then
		x,y,z = getElementPosition(source)
		int,dim = getElementInterior(source),getElementDimension(source)
		health,armor = getElementHealth(source),getPedArmor(source)
		money = getPlayerMoney(source)
		rotation = getElementRotation(source)
		team = getTeamName(getPlayerTeam(soure))
		skin = getElementModel(source)
		
		weapons = {}
		for i=0,12 do
			local wep = getPedWeapon(source,i)
			if (wep > 0) then
				local ammo = getPedTotalAmmo(source,i)
				if (ammo > 0) then
					table.insert(weapons,{wep,ammo})
				end
			end
		end
		weaponsStr = toJSON(weapons)
		
		id = getElementData(source,"accountID")
		execute("UPDATE accounts SET x=?,y=?,z=?,interior=?,dimension=?,health=?,armor=?,money=?,rotation=?,team=?,skin=?,weapons=? WHERE id=?",x,y,z,int,dim,health,armor,money,rotation,team,skin,weaponsStr,id)
	else
		return false
	end
end
addEventHandler("onPlayerQuit",root,saveData)
addEventHandler("onPlayerWasted",root,saveData)

 

Link to comment

i solved this problem by change line 53 & 110 to below code

-- line 53
setPlayerName(source, accData.nickname)

-- line 110

local name = getPlayerName(palyer)

 

but after join  i can't get admin rights by my loggin panel

i need need script to check player is in the acl or not

Link to comment

Hey,

You can check the examples in this AclGroupListObjects function documentation. It loops through all the objects (usernames) in the group Admin.
On resource start you can store all the usernames which are in the Admin group in a table and when a player logs in you can check if he is in this table and then set the admin rights.

Link to comment
Posted (edited)

@SpecT @nikitafloy hi is this method works?

 

function(username)
	if (singleQuery("SELECT * FROM accounts WHERE username=? LIMIT 1",(username))) then
	local myacc = singleQuery("SELECT * FROM accounts WHERE username=? LIMIT 1",(username))
    local aaccount = aclGroupListObjects(Admin)
    local admins = {}
    local agroup = aclGetGroup( "Admin" )
    
    if (group) then
        aclGroupListObjects(group)
            local objType = gettok( object, 1, string.byte('.') )
            
            if (objType == "user") then
                local _name = gettok( object, 2, string.byte('.') )
                table.insert( admins, _name )
            end
    end


    if (myacc == admins) then
        aclSetRight()
    
        aclSave ()
    end



    end
end

 

Edited by sinakh
Link to comment

More like something like this:

function onLogin(username)
    if isAccountAdmin(username) then
        givePlayerAdminRights() -- put here the stuff you use to give players admin access
    end
end


function isAccountAdmin(username)
	local admins = {} -- creates the table in which will be added the accounts of "Admin" group
	local group = aclGetGroup( "Admin" )
	if (group) then
		for _, object in ipairs(aclGroupListObjects(group) or {}) do
			local objType = gettok( object, 1, string.byte('.') )
			-- objType: gets the object type only, which can be either "user" or "resource"
			if (objType == "user") then -- checks if it's a player account
				local _name = gettok( object, 2, string.byte('.') ) -- ignores "user." by separating that from the account name
				table.insert( admins, _name ) -- adds the account name to the "admins" table
			end
		end
	end
	for i, name in ipairs(admins) do -- loop through the table "admins"
		if name == username then
			return true -- we found a match
		end
	end
	return false -- if nothing was found
end

*used the 2nd example in the wiki

BUT if the server is big and there are many players logging in it might cause performance issues.

To prevent that you can use the code from the "isAccountAdmin" to store the admin usernames in a table but on resource start so it won't do all the operations every time someone logs in.
And when a player logs in use the loop to go through all the admin usernames and check whether the player's account name is one of them or not.

Edited by SpecT
  • Thanks 1
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...