This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
DefiledCobra's profile picture

Trouble with login system

Started by DefiledCobra, 20 September 2012 - 12:53 AM
DefiledCobra #1
Posted 20 September 2012 - 02:53 AM
So basically I'm throwing together a login system for CC. However, I'm having a bit of a problem. Yes, I am very new to LUA and ComputerCraft, but this is the code:
Spoiler
- snip -
print("Login")
term.setCursorPos(1,2)
loopUserName = true
while (loopUserName == true) do
write("Username: ")
userNameInput = read()
if userNameInput == "admin" then <-- This line!
loopUserName = false
loopPassword = true
while (loopPassword == true) do
write("Password: ")
passwordInput = read("*")
if passwordInput == "banana" then
print("Verifying login...")
sleep(2)
print("Login successful!")
loopPassword = false
else
print("Verifying login...")
sleep(2)
print("Password incorrect!")
loopPassword = true
end
end
end
end

The line I pointed out is the one I can't quite figure out. Basically, it's an if then else statement, but I want to add else at the end. For example, if they type in a username that isn't admin I want it to say "Username not found", but I can't use "else" since it's already used to the password portion. Yes yes, it all seems confusing but maybe one of you will be able to help me. Like, remove some unnecessary code?

Edit: I think I figured it out. I placed "else" inbetween the four ends and it seems to work now…but some fixing up would be nice.
KaoS #2
Posted 20 September 2012 - 06:27 AM
cleaned up: try using break rather than changing a var, it ends the current loop immediately so don't forget to put it at the end


term.clear()
term.setCursorPos(1,1)
print("Login")
while true do
  write("Username: ")
  userNameInput = read()
  if userNameInput == "admin" then
   break
  else
   print("Username incorrect")
  end
end
while true do
  write("Password: ")
  passwordInput = read("*")
  print("Verifying login...")
  sleep(2)
  if passwordInput == "banana" then
   print("Login successful!")
   break
  else
   print("Password incorrect!")
  end
end
DefiledCobra #3
Posted 20 September 2012 - 10:44 PM
Thanks for that! I didn't know break existed, so I'll have to experiment with it now.
It also seems that some youtube videos make things more complicated than need-be.

Also, is there a line of code that can be added that says "Press x key to continue" or "Press any key to continue"?
And… print a sentence or something but make it look like it's just being typed versus making the whole sentence just appear?

And… if you type the wrong username I want it to look like this:

Login
Username: derp
Username not found.

And then keep everything on screen but make you type in another username. I tried setting the cursor position but the previous username typed stays there until you press a key. How do I clear the username?
Mr. Fang #4
Posted 21 September 2012 - 01:00 AM
I suggest for that you type towards the end this:

print("Type 'X' To Continue")
read()
if local input == "X" then
term.setCursorBlink(true)
end

Basically something roughly like that…
DefiledCobra #5
Posted 21 September 2012 - 02:20 AM
I'll have to try that out!
Edit: No, that didn't seem to work. It gives an error: unexpected symbol.

Also, more questions! Is there a way to make a timeout if too many incorrect passwords are entered?
And when an incorrect username/password is typed, I want the Username: currentusername to clear itself and put the cursor there. When I do put the cursor there, the old username still shows.
Also, when I do this with the correct username, when
Password:
pops up, the "Incorrect username" still shows when I type in an incorrect username prior to one that does exist.
Can I clear only one line at a time? If so, that would help a lot.
Fatal_Exception #6
Posted 21 September 2012 - 06:08 AM
term.setCursorPos(column, line)
term.clearLine()
KaoS #7
Posted 21 September 2012 - 08:13 AM
Give this a try


local function pause(key)
  print('press '..key..' to continue')
  while true do
    local sEvent, sParam=os.pullEvent('char')
    if string.lower(sParam)==key then
   break
    end
  end
end
local intcount=0
term.clear()
term.setCursorPos(1,1)
print("Login")
while true do
  write("Username: ")
  userNameInput = read()
  if userNameInput == "admin" then
   break
  else
   print("Username incorrect")
   intcount=intcount+1
   sleep(1)
   for i=3,2,-1 do
    term.setCursorPos(1,i)
    term.clearLine()
   end
  end
  if intcount==3 then
   print('your 3 tries have been used')
   sleep(2)
   os.shutdown()
  end
end
local intcount=0
while true do
  write("Password: ")
  passwordInput = read("*")
  print("nVerifying login...")
  sleep(2)
  if passwordInput == "banana" then
   print("Login successful!")
   break
  else
   print("Password incorrect!")
   intcount=intcount+1
   sleep(1)
   for i=6,3,-1 do
    term.setCursorPos(1,i)
    term.clearLine()
   end
  end
  if intcount==3 then
   print('your 3 tries have been used')
   sleep(2)
   os.shutdown()
  end
end
pause('x')
DefiledCobra #8
Posted 21 September 2012 - 11:56 PM
Give this a try

Spoiler

local function pause(key)
  print('press '..key..' to continue')
  while true do
	local sEvent, sParam=os.pullEvent('char')
	if string.lower(sParam)==key then
   break
	end
  end
end
local intcount=0
term.clear()
term.setCursorPos(1,1)
print("Login")
while true do
  write("Username: ")
  userNameInput = read()
  if userNameInput == "admin" then
   break
  else
   print("Username incorrect")
   intcount=intcount+1
   sleep(1)
   for i=3,2,-1 do
	term.setCursorPos(1,i)
	term.clearLine()
   end
  end
  if intcount==3 then
   print('your 3 tries have been used')
   sleep(2)
   os.shutdown()
  end
end
local intcount=0
while true do
  write("Password: ")
  passwordInput = read("*")
  print("nVerifying login...")
  sleep(2)
  if passwordInput == "banana" then
   print("Login successful!")
   break
  else
   print("Password incorrect!")
   intcount=intcount+1
   sleep(1)
   for i=6,3,-1 do
	term.setCursorPos(1,i)
	term.clearLine()
   end
  end
  if intcount==3 then
   print('your 3 tries have been used')
   sleep(2)
   os.shutdown()
  end
end
pause('x')

Well, that's a rather long code. I'll try it out later, and thanks for the help! I'll see if I can experiment with any of those… new codes I see.
Edit: It never displayed "Press key to continue" screen.

One question for future reference: Where did you get the "for i=3,2,-1" part from? Are those coordinates? I thought it was only an x and y axis.
Fatal_Exception #9
Posted 22 September 2012 - 02:08 AM
It's a for loop that:
sets the variable i to 3 at the start
ends the loop when i == 2
subtracts 1 every time through the loop
KaoS #10
Posted 22 September 2012 - 07:48 AM
when would you like the press any key screen? it shows at the end when the pause('x') function is called, you can put it anywhere you like
DefiledCobra #11
Posted 22 September 2012 - 08:32 PM
when would you like the press any key screen? it shows at the end when the pause('x') function is called, you can put it anywhere you like

It didn't even show it then. It gave an error saying something about nil. I didn't necessarily want it for this script, I just wanted to know it for future reference :P/>/>
KaoS #12
Posted 23 September 2012 - 07:02 AM
I have tested this code and it works perfectly… are you sure you got the whole thing?
DefiledCobra #13
Posted 24 September 2012 - 01:44 AM
Yeah, I guess I accidentally removed a line of code. I tried it again and it works great. Thanks for the help!
Mr. Fang #14
Posted 24 September 2012 - 01:58 PM
Your Welcome :P/>/>
(Did nothing)
KaoS #15
Posted 25 September 2012 - 07:38 AM
awesome stuff, if you want it to accept any key to resume then replace the pause function with:


local function pause(key)
print('press '..(key or 'any key')..' to continue')
while true do
  local sEvent, sParam=os.pullEvent('char')
  if not key or string.lower(sParam)==key then
   break
  end
end
end

then you call the pause function with a parameter as normal and it will ask for that key, if you call it without any params then it will ask for any key