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

Help with Login System - OS

Started by Zee, 17 July 2013 - 10:40 PM
Zee #1
Posted 18 July 2013 - 12:40 AM
Right. I'm working on an OS, and it includes a login system for startup. The problem is, I CANNOT figure out what I can do to reboot the machine when password is incorrect.. Also, props to whoever thinks up the awesomest name!
-- So Far UnnamedOS Startup
-- By DanJZ0404
os.pullEvent = os.pullEventRaw
vNum = "v04" --For getVersion func.
function getVersion() -- returns version number
print("UnnamedOS "..vNum)
end
function userManage() -- This contains user data.
usernames = {"dan", "admin", "foo"}
passwords = {"pass", "12345", "bar"} -- Creates pass table.
end
function newScr() --
term.clear()
term.setCursorPos(1,1)
end
function requestLogin()
userManage()
newScr()
print("Username:")
term.setCursorPos(11,1)
uIn = (read) -- input
print("Password:")
term.setCursorPos(11,2)
pIn = (read("*"))
for l = 1, 3 do
  if uIn==usernames[l] and pIn==passwords[l] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()
   error("Access Granted. - UnnamedOS "..vNum)
  end
end
end
requestLogin()

EDIT: Code didn't copy/paste right. Reload in about 10 minutes.
Grim Reaper #2
Posted 18 July 2013 - 12:43 AM
If your press and hold Ctrl + R at the same time for a couple of seconds, the computer should reboot.
Zee #3
Posted 18 July 2013 - 12:45 AM
If your press and hold Ctrl + R at the same time for a couple of seconds, the computer should reboot.
No. That's not what I meant. I want the computer to reboot if the password is incorrect.
Zee #4
Posted 18 July 2013 - 12:49 AM
Would this change work?
if uIn==usernames[l] and pIn==passwords[l] then
term.setCursorPos(1,18)
print("Access Granted.")
sleep(3)
newScr()
error("Access Granted. - DanOS v03")
elseif not uIn==usernames[l] and pIn==passwords[1] or not uIn==usernames[2] and pIn==passwords[2] or not uIn==usernames[3] and pIn==passwords[3] then
os.reboot()
end
Zudo #5
Posted 18 July 2013 - 03:23 AM
Would this change work?
if uIn==usernames[l] and pIn==passwords[l] then
term.setCursorPos(1,18)
print("Access Granted.")
sleep(3)
newScr()
error("Access Granted. - DanOS v03")
elseif not uIn==usernames[l] and pIn==passwords[1] or not uIn==usernames[2] and pIn==passwords[2] or not uIn==usernames[3] and pIn==passwords[3] then
os.reboot()
end

It would, but dat indentation c_c

Call it KrytenOS!
albrat #6
Posted 18 July 2013 - 05:18 AM

for l = 1, 3 do
  if uIn==usernames[l] and pIn==passwords[l] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()
   error("Access Granted. - UnnamedOS "..vNum)
  end
end
by this bit of code you have limited the OS login system to only 3 users and passwords. – bad idea.

try this instead


for i = 1, #usernames do
  if uIn==usernames[i] and pIn==passwords[i] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()

   printError("Access Granted. - UnnamedOS "..vNum) -- Gives a neater exit
   error()
  end
end
-- we exit the loop and no results so...
  print("Failed")
  sleep(3)
  os.reboot()

Also this function is a little wastefull…


function requestLogin()
userManage()
newScr()
print("Username:")
term.setCursorPos(11,1)
uIn = (read) -- input
print("Password:")
term.setCursorPos(11,2)
pIn = (read("*"))
for l = 1, 3 do
  if uIn==usernames[l] and pIn==passwords[l] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()
   error("Access Granted. - UnnamedOS "..vNum)
  end
end
end
requestLogin()

it basically programs itself then will never finish because you want the os to reboot if it fails to login.

this is a better way of doing it…



-- instead of a function lets call this as the main program its linear...

userManage()
newScr()
print("Username:")
term.setCursorPos(11,1)
uIn = (read) -- input
print("Password:")
term.setCursorPos(11,2)
pIn = (read("*"))
for i = 1, #usernames do
  if uIn==usernames[i] and pIn==passwords[i] then
	term.setCursorPos(1,18)
	print("Access Granted.")
	sleep(3)
	newScr()
  
	printError("Access Granted. - UnnamedOS "..vNum) -- Gives a neater exit
	error()
  end
end

-- we exit the loop and no results so...

  print("Failed")
  sleep(3)
  os.reboot()
immibis #7
Posted 18 July 2013 - 09:08 PM
Don't use os.reboot() as a loop.
albrat #8
Posted 19 July 2013 - 05:56 AM
No. That's not what I meant. I want the computer to reboot if the password is incorrect.

hence I did os.reboot()

Otherwise I would suggest doing a set varible to true, use varible as while true do loop and loop untill password is correct then set varible to false. (which is a better way of doing it)

I would myself write the code like this…

-- instead of a function lets call this as the main program its linear...

local valid = false  -- Make our loop work

while not valid do -- While the value valid is not true.. loop

  userManage()
  newScr()
  print("Username:")
  term.setCursorPos(11,1)
  uIn = read()		   -- get username
  print("Password:")
  term.setCursorPos(11,2)
  pIn = read("*")    -- get password
  for i = 1, #usernames do
	if uIn==usernames[i] and pIn==passwords[i] then
		term.setCursorPos(1,18)
		print("Access Granted.")
		sleep(3)
		newScr()
	    valid = true  -- Finish loop and allow script to continue
		print("Access Granted. - UnnamedOS "..vNum) -- Allow access
	    break
  
	end
  end
  if not valid then
	print("Failed")
	sleep(3)
  end
end

-- More OS program after success


As for a Name… DanOs
NOTUSEDPLEASEDELETE #9
Posted 21 July 2013 - 03:52 AM
Don't use os.reboot() as a loop.
You're right! You should use shell.run(program) instead.
os.reboot() introduces a delay for mistakes.
YoYoYonnY #10
Posted 25 January 2014 - 03:54 PM
Code #1
while true do
  if uIn==usernames[l] and pIn==passwords[l] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()
   print("Access Granted. - UnnamedOS "..vNum)
   error()
  else
   os.reboot()
  end
end
Code #2

-- So Far UnnamedOS Startup
-- By DanJZ0404

os.pullEvent = os.pullEventRaw
vNum = "v04" –For getVersion func.
function getVersion() – returns version number
print("UnnamedOS "..vNum)
end
function userManage() – This contains user data.
usernames = {"dan", "admin", "foo"}
passwords = {"pass", "12345", "bar"} – Creates pass table.
end
function newScr() –
term.clear()
term.setCursorPos(1,1)
end
function requestLogin()
userManage()
newScr()

while true do
print("Username:")
term.setCursorPos(11,1)
uIn = (read) – input
print("Password:")
term.setCursorPos(11,2)
pIn = (read("*"))
while true do
if uIn==usernames[l] and pIn==passwords[l] then
term.setCursorPos(1,18)
print("Access Granted.")
sleep(3)
newScr()
print("Access Granted. - UnnamedOS "..vNum)

error()
end
end
end
requestLogin()
end
TechMasterGeneral #11
Posted 25 January 2014 - 06:21 PM
Here is a simple answer to your question

if uIn==usernames[l] and pIn==passwords[l] then
   term.setCursorPos(1,18)
   print("Access Granted.")
   sleep(3)
   newScr()
   error("Access Granted. - UnnamedOS "..vNum)
else -- quit the program
os.reboot()
end
Edited on 25 January 2014 - 05:22 PM
awsmazinggenius #12
Posted 26 January 2014 - 10:41 PM
Can you let us know if you got it to work?

immibis is correct, why should you use os.reboot as a loop when you can run your own loop and add things like an action on too many failed passwords?