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

[QUESTION][HELP]Coding.

Started by Mackan90096, 13 March 2013 - 03:56 AM
Mackan90096 #1
Posted 13 March 2013 - 04:56 AM
Hi! I'm developing an os for computercraft. But im having a problem with the login system.

Here is The code snippet:


function login()
term.clear()
term.setCursorPos(1,1)
print("Logging in")
term.setCursorPos(1,3)
write("Username: ")
usrName = read()
write("Password: ")
local pass = read("*")
file = fs.open("users/"..usrName,"r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData, line)
line = file.readLine()
until line == nil -- readLine()
file.close()
local passFromFile = fileData[1]
if pass == passFromFile then
term.clear()
term.setCursorPos(1,1)
print("Login succeded!")
sleep(1)
term.clear()
start2()

I need help with detecting if the login details are wrong and then run a function from that.

Thankful for help. //Mackan90096
Doyle3694 #2
Posted 13 March 2013 - 06:19 AM
Your function has more problems than that - for example it will write out "Username: Password: "And then take a input which will turn out as the username.

For the basics of your question however, you would want your if loop to have a else in the end, which will be ran if pass is not equal to passFromFile, simmilar to this:

if pass == passFromFile then
 term.clear()
 term.setCursorPos(1,1)
 print("Login succeded!")
 sleep(1)
 term.clear()
 start2()
else
 -- Here goes everything you want to run if pass ~= passFromFile
end
Pharap #3
Posted 13 March 2013 - 04:38 PM
What you want is something like this:

local bool = false
for cnt = 1,#fileData do
bool = (filedata[cnt]==pass) -- bool will be true if the line matches the password
if(pass)then break end
end
if(not pass)then --if no passwords matched
os.reboot() -- reboot the computer
--alternatively some other fancy way of getting back to the password enter thingy
end
--log user in here, carry on as normal--
I haven't tested this, I'm just going with what should work.
Mackan90096 #4
Posted 13 March 2013 - 11:02 PM
Ah thanks. I'll try it
Doyle3694 #5
Posted 13 March 2013 - 11:14 PM
wtf is that overcomplicated code? @Pharap

He asked for a way to detect wrong password, not how to use innefficient os.reboot()'s and overcomplicate stuff.
Mackan90096 #6
Posted 13 March 2013 - 11:40 PM
Not really wrong password but The wrong username
Doyle3694 #7
Posted 13 March 2013 - 11:48 PM

function login()
while true do -- starts a while true loop so it will run until it's broken'
  term.clear()
  term.setCursorPos(1,1)
  print("Logging in")
  term.setCursorPos(1,3)
  write("Username: ")
  usrName = read()
  write("Password: ")
  local pass = read("*")
  file = fs.open("users/"..usrName,"r")
  if file then -- If the file "users/"..usrName does exist then
   local fileData = {}
   local line = file.readLine()
  
   repeat
	table.insert(fileData, line)
	line = file.readLine()
   until line == nil -- readLine()

   file.close()
   local passFromFile = fileData[1]
   if pass == passFromFile then
	term.clear()
	term.setCursorPos(1,1)
	print("Login succeded!")
	sleep(1)
	term.clear()
	start2()
	break -- breaks the loop
   else -- if password is wrong
	term.clear()
	term.setCursorPos(1,1)
	print("Wrong password!")
	sleep(1.5)
  else -- If username is not found
   term.clear()
   term.setCursorPos(1,1)
   print("Username not found!")
   sleep(1.5)
  end
end
end
Bubba #8
Posted 14 March 2013 - 12:59 AM
My method is a bit more complicated, but I feel that the end result is worth it. This requires a table of usernames and passwords to be stored in a file called "passwords.txt". They must be in table form, which is the following:
{["index"]="value", ["username"]="password", ["etc"]="etc"}

The advantage to this is that you can enter as many users as you want and read it easily.

Here is an example password file:

{["admin"]="password",["guest"]="default"}

And here is the main program.

local users = {}

local function onStart()
  if not fs.exists("passwords.txt") then
    print("passwords.txt required to run!")
    return false
  end
  local f = fs.open("passwords.txt", "r")
  users = textutils.unserialize(f.readAll()) --Get the entire file contents and unserialize the table
  --[[
  To go further in depth about unserialize, all that the function does is take
  the string form of a table and convert it into an actual table]]
  f.close() --Remember to close the file
  if not users then
    print("Something is weird in the passwords.txt file: It must be a string form of a table!")
    return false
  end
  return true
end


local function login()
  while true do
    term.clear() term.setCursorPos(1,1)
    term.write("Username: ")
    local username = read()
    term.setCursorPos(1,2)
    term.write("Password: ")
    local password = read("*")
    term.clear() term.setCursorPos(1,1)
    if not users[username] or users[username] ~= password then --Check the username and password
       print("Invalid username or password.")
       os.pullEvent() --Wait for the user to do something
    else
       print("Success!")
       return true
    end
  end
end

local function doStuffAfterLogin()
  print("Hi this does stuff")
end

if not onStart() then return false end--If the startup function does not manage to load everything required of it then stop the program


login()
doStuffAfterLogin() --They've managed to log in with valid credentials. Now continue with the rest of the program.
Mackan90096 #9
Posted 14 March 2013 - 01:41 AM
I Dont really want to overcomplicate stuff
subzero22 #10
Posted 14 March 2013 - 03:31 AM
SNIP

Is that script protected by ctrl+T? if so I'd love to use it.
remiX #11
Posted 14 March 2013 - 03:40 AM
SNIP

Is that script protected by ctrl+T? if so I'd love to use it.

Add this at the top
oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw

and this at the bottom:

os.pullEvent = oldPull
Mackan90096 #12
Posted 14 March 2013 - 06:19 AM
My method is a bit more complicated, but I feel that the end result is worth it. This requires a table of usernames and passwords to be stored in a file called "passwords.txt". They must be in table form, which is the following:
{["index"]="value", ["username"]="password", ["etc"]="etc"}

The advantage to this is that you can enter as many users as you want and read it easily.

Here is an example password file:
-SNIP.-

Well… i'd love to use that but can you make fix the script so that i can use it with my current stuff? Cause else i'd have to recode a crapload of stuff and that would take even more time than i have planned. I want to get this out to the beta testers in 1-3 days maximum.

Edit:
Wait a bit.. think i can make something.