Jump to content

Why not save username and password the loginpanel?


Turbe$Z

Recommended Posts

client:

local localPlayer = getLocalPlayer()
local playerName = getPlayerName(localPlayer)

function noBinds()
guiSetInputMode("no_binds_when_editing")
end
addEventHandler("onClientResourceStart", root, noBinds)

function createLoginWindow()
	windowLogin = guiCreateWindow(0.3945,0.3646,0.2109,0.2018,"Magyar Play Szerver - Loginpanel by turbesz",true)
	guiSetSize(windowLogin, 270, 175, false)
	guiSetAlpha(windowLogin,1)
	labelUsername = guiCreateLabel(10,52,59,24,"Felh.név:",false,windowLogin)
	guiSetAlpha(labelUsername,1)
	guiLabelSetColor(labelUsername,255,255,255)
	guiLabelSetVerticalAlign(labelUsername,"center")
	guiLabelSetHorizontalAlign(labelUsername,"left",false)
	labelPassword = guiCreateLabel(10,86,59,24,"Jelszó:",false,windowLogin)
	guiSetAlpha(labelPassword,1)
	guiLabelSetColor(labelPassword,255,255,255)
	guiLabelSetVerticalAlign(labelPassword,"center")
	guiLabelSetHorizontalAlign(labelPassword,"left",false)
	labelInfo = guiCreateLabel(10,26,250,17,"Regizz, és jelentkezz be a játékhoz.",false,windowLogin)
	guiSetAlpha(labelInfo,1)
	guiLabelSetColor(labelInfo,255,255,255)
	guiLabelSetVerticalAlign(labelInfo,"top")
	guiLabelSetHorizontalAlign(labelInfo,"center",false)
	guiSetFont(labelInfo,"default-bold-small")
	editUsername = guiCreateEdit(79,52,181,25,"",false,windowLogin)
	guiSetAlpha(editUsername,1)
	guiEditSetMaxLength(editUsername, 50)
	editPassword = guiCreateEdit(79,86,181,25,"",false,windowLogin)
	guiSetAlpha(editPassword,1)
	guiEditSetMasked(editPassword, true)
	guiEditSetMaxLength(editPassword, 50)
	buttonLogin = guiCreateButton(10,121,120,21,"Bejelentkezés",false,windowLogin)
	guiSetAlpha(buttonLogin,1)
	buttonRegister = guiCreateButton(143,121,117,21,"Regisztrálás",false,windowLogin)
	guiSetAlpha(buttonRegister,1)
	buttonGuest = guiCreateButton(10,145,121,21,"Vendég",false,windowLogin)
	guiSetAlpha(buttonGuest,1)
	checkbox_save = guiCreateCheckBox(157,145,117,21,"Adatok mentése",false,false,windowLogin)
	guiSetFont(checkbox_save,"default-small")
	guiWindowSetSizable ( windowLogin, false )
	
	guiSetVisible(windowLogin, false)
	
	addEventHandler("onClientGUIClick", buttonLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", buttonRegister, clientSubmitRegister, false)

	local username, password = loadLoginFromXML()
	
	if not( username == "" or password == "") then
		guiCheckBoxSetSelected ( checkbox_save, true )
		guiSetText ( editUsername, tostring(username))
		guiSetText ( editPassword, tostring(password))
	else
		guiCheckBoxSetSelected ( checkbox_save, false )
		guiSetText ( editUsername, tostring(username))
		guiSetText ( editPassword, tostring(password))
	end
end

function loadLoginFromXML()
	local xml_save_log_File = xmlLoadFile ("files/xml/adatok.xml")
    if not xml_save_log_File then
        xml_save_log_File = xmlCreateFile("files/xml/adatok.xml", "login")
    end
    local usernameNode = xmlFindChild (xml_save_log_File, "username", 0)
    local passwordNode = xmlFindChild (xml_save_log_File, "password", 0)
    if usernameNode and passwordNode then
        return xmlNodeGetValue(usernameNode), xmlNodeGetValue(passwordNode)
    else
		return "", ""
    end
    xmlUnloadFile ( xml_save_log_File )
end

function saveLoginToXML(username, password)
    local xml_save_log_File = xmlLoadFile ("files/xml/adatok.xml")
    if not xml_save_log_File then
        xml_save_log_File = xmlCreateFile("files/xml/adatok.xml", "login")
    end
	if (username ~= "") then
		local usernameNode = xmlFindChild (xml_save_log_File, "username", 0)
		if not usernameNode then
			usernameNode = xmlCreateChild(xml_save_log_File, "username")
		end
		xmlNodeSetValue (usernameNode, tostring(username))
	end
	if (password ~= "") then
		local passwordNode = xmlFindChild (xml_save_log_File, "password", 0)
		if not passwordNode then
			passwordNode = xmlCreateChild(xml_save_log_File, "password")
		end		
		xmlNodeSetValue (passwordNode, tostring(password))
	end
    xmlSaveFile(xml_save_log_File)
    xmlUnloadFile (xml_save_log_File)
end
addEvent("saveLoginToXML", true)
addEventHandler("saveLoginToXML", getRootElement(), saveLoginToXML)

function resetSaveXML()
		local xml_save_log_File = xmlLoadFile ("files/xml/adatok.xml")
		if not xml_save_log_File then
			xml_save_log_File = xmlCreateFile("files/xml/adatok.xml", "login")
		end
		if (username ~= "") then
			local usernameNode = xmlFindChild (xml_save_log_File, "username", 0)
			if not usernameNode then
				usernameNode = xmlCreateChild(xml_save_log_File, "username")
			end
		end
		if (password ~= "") then
			local passwordNode = xmlFindChild (xml_save_log_File, "password", 0)
			if not passwordNode then
				passwordNode = xmlCreateChild(xml_save_log_File, "password")
			end		
			xmlNodeSetValue (passwordNode, "")
		end
		xmlSaveFile(xml_save_log_File)
		xmlUnloadFile (xml_save_log_File)
end
addEvent("resetSaveXML", true)
addEventHandler("resetSaveXML", getRootElement(), resetSaveXML)

addEventHandler("onClientGUIClick",root,
  function ()
    if source == buttonGuest then
     guiSetVisible ( windowLogin , false )
     showCursor(false)
   end
end
)

function resourceStart()
	createLoginWindow()
	if (windowLogin ~= nil) then
		guiSetVisible(windowLogin, true)
	else
		outputChatBox("Whoops, valami error történt.")
	end
	showCursor(true)
	guiSetInputEnabled(true)
end

function clientSubmitLogin(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitLogin", getRootElement(), localPlayer, username, password)
		else
			guiSetText(labelInfo, "Írj be felh.nevet és jelszót.")
		end
	end
end

function clientSubmitRegister(button, state)
	if button == "left" and state == "up" then
		local username = guiGetText(editUsername)
		local password = guiGetText(editPassword)
		if username and password then
			triggerServerEvent("submitRegister", getRootElement(), localPlayer, username, password)
		else
			guiSetText(labelInfo, "Írj be felh.nevet és jelszót.")
		end
	end
end

function hideLoginWindow()
	guiSetInputEnabled(false)
	guiSetVisible(windowLogin, false)
	showCursor(false)
end

function unknownError()
	guiSetText(labelInfo, "Ismeretlen hiba.")
end

function loginWrong()
	guiSetText(labelInfo, "Hibás adatok.")
end

function registerTaken()
	guiSetText(labelInfo, "Felhasználó név regisztrálva van.")
end

addEvent("hideLoginWindow", true)
addEvent("unknownError", true)
addEvent("loginWrong", true)
addEvent("registerTaken", true)
addEventHandler("hideLoginWindow", getRootElement(), hideLoginWindow)
addEventHandler("unknownError", getRootElement(), unknownError)
addEventHandler("loginWrong", getRootElement(), loginWrong)
addEventHandler("registerTaken", getRootElement(), registerTaken)
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), resourceStart)

fileDelete("client.lua")

server:

function loginHandler(player, username, password, checksave)
	local account = getAccount(username, password)
	if (account ~= false) then
		if (logIn(player, account, password) == true) then
			triggerClientEvent (player, "hideLoginWindow", getRootElement())
					if checksave == true then
						triggerClientEvent(source,"saveLoginToXML",getRootElement(),username,password)
					else
						triggerClientEvent(source,"resetSaveXML",getRootElement(),username,password)
					end
		else
			triggerClientEvent (player, "unknownError", getRootElement())
		end
	else
		triggerClientEvent (player, "loginWrong", getRootElement())
	end
end

function registerHandler(player, username, password)
	local account = getAccount(username, password)
	if (account ~= false) then
		triggerClientEvent(player, "registerTaken", getRootElement())
	else
		account = addAccount(username, password)
		if (logIn(player, account, password) == true) then
			triggerClientEvent(player, "hideLoginWindow", getRootElement())
		else
			triggerClientEvent(player, "unknownError", getRootElement())
		end
	end
end

addEvent("submitLogin", true)
addEvent("submitRegister", true)
addEventHandler("submitLogin", root, loginHandler)
addEventHandler("submitRegister", root, registerHandler)

why not save? :S

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...