Jump to content




Help with Login System - OS


  • You cannot reply to this topic
11 replies to this topic

#1 Zee

  • Members
  • 42 posts
  • LocationIndiana

Posted 17 July 2013 - 11:40 PM

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.

#2 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 17 July 2013 - 11:43 PM

If your press and hold Ctrl + R at the same time for a couple of seconds, the computer should reboot.

#3 Zee

  • Members
  • 42 posts
  • LocationIndiana

Posted 17 July 2013 - 11:45 PM

View PostGrim Reaper, on 17 July 2013 - 11:43 PM, said:

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.

#4 Zee

  • Members
  • 42 posts
  • LocationIndiana

Posted 17 July 2013 - 11:49 PM

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


#5 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 18 July 2013 - 02:23 AM

View PostDanJZ0404, on 17 July 2013 - 11:49 PM, said:

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!

#6 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 18 July 2013 - 04: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()


#7 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 18 July 2013 - 08:08 PM

Don't use os.reboot() as a loop.

#8 albrat

  • Members
  • 162 posts
  • LocationA Chair

Posted 19 July 2013 - 04:56 AM

View PostDanJZ0404, on 17 July 2013 - 11:45 PM, said:

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

#9 NOTUSEDPLEASEDELETE

  • Members
  • 70 posts

Posted 21 July 2013 - 02:52 AM

View Postimmibis, on 18 July 2013 - 08:08 PM, said:

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.

#10 YoYoYonnY

  • Members
  • 49 posts

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[/size]
[size=4]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()[/size]
[size=4]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)[/size]
   error()
  end
end
end
requestLogin()
end


#11 TechMasterGeneral

  • Members
  • 149 posts
  • LocationUnited States

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 by LuaCrawler, 25 January 2014 - 06:22 PM.


#12 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

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?





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users