Jump to content




[LUA][Error(?)] Code wont work.


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

#1 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 22 April 2013 - 11:41 PM

So, I am making a "saving/loading" thing for my OS (MackOS)

But I get the text "Login failed" even though the login info is correct.
Why?
What am I doing wrong?

Code:

function login()
term.clear()
term.setCursorPos(math.floor(w-string.len("Username:"))/2, 1)
print("Logging in")
term.setCursorPos(math.floor(w-string.len("Username:"))/2, 4)
write("Username: ")
usrName = read()
term.setCursorPos(math.floor(w-string.len("Password:"))/2, 6)
write("Password: ")
local pass = read("*")

local file2 = fs.open("users/"..usrName.."/bg", "r")
local logoPath = file2.readLine()
logo = paintutils.loadImage(logoPath)
file2.close()
file = fs.open("users/"..usrName.."/"..usrName,"r")
if not fs.exists("users/"..usrName.."/"..usrName) then
term.clear()
term.setCursorPos(math.floor(w-string.len("Login failed"))/2, 2)
print("Login failed")
sleep(1)
term.clear()
drawDesktop()
elseif fs.exists("users/"..usrName.."/"..usrName) 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(math.floor(w-string.len("Login succeded!"))/2, 2)
print("Login succeded!")
sleep(1)
term.clear()
drawDesktop2()
else
term.clear()
term.setCursorPos(math.floor(w-string.len("Login failed!"))/2, 2)
print("Login failed!")
sleep(1)
term.clear()
drawDesktop()
end
end
end



#2 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 April 2013 - 02:44 AM

From which block is it failing? THe first if or the elseif?

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 23 April 2013 - 02:44 AM

View PostMackan90096, on 22 April 2013 - 11:41 PM, said:

the login info is correct
What makes you sure about that exactly?

#4 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 23 April 2013 - 02:57 AM

View PostLBPHacker, on 23 April 2013 - 02:44 AM, said:

View PostMackan90096, on 22 April 2013 - 11:41 PM, said:

the login info is correct
What makes you sure about that exactly?
It would error here if the username didn't exist...
local file2 = fs.open("users/"..usrName.."/bg", "r") local logoPath = file2.readLine() logo = paintutils.loadImage(logoPath) file2.close()
Therefor answers my question... Add some debug lines, try this:
function login()
    term.clear()
    term.setCursorPos(math.floor(w-string.len("Logging in")/2), 1) -- was checking the length of Username: :?
    write("Logging in")
    term.setCursorPos(math.floor(w-string.len("Username:")/2), 4) -- you had the brackets wrong. you were using math.floor on an integer all the time, the /2 needs to be inside
    write("Username: ")
    local usrName = read()
    term.setCursorPos(math.floor(w-string.len("Password:")/2), 6)
    write("Password: ")
    local pass = read("*")

    if not fs.exists("users/"..usrName.."/"..usrName) then
        term.clear()
        term.setCursorPos(math.floor(w-string.len("Login failed"))/2, 2)
        print("Login failed")
    elseif fs.exists("users/"..usrName.."/"..usrName) then
        -- if the username exists, shouldn't the bg file exist too?
        local file2 = fs.open("users/"..usrName.."/bg", "r")
        local logoPath = file2.readLine()
        logo = paintutils.loadImage(logoPath)
        file2.close()

        local file = fs.open("users/"..usrName.."/"..usrName,"r") -- Only open if it exists..
        local fileData = {} -- if the file has only the password, there is no point of this :P/>
        for line in file.readLine do -- cleaner way, i think this is right. If not try adding the () after readLine
            table.insert(fileData,line)
        end
        file.close()

        local passFromFile = fileData[1]
        if pass == passFromFile then
            term.clear()
            term.setCursorPos(math.floor(w-string.len("Login succeded!"))/2, 2)
            print("Login succeded!")
        else
            term.clear()
            term.setCursorPos(math.floor(w-string.len("Login failed!"))/2, 2)
            print("Login failed!")
            error( pass .. " - " .. passFromFile ) -- debug line, if it reaches here, check what the variables are
        end
    end
    -- put this here so you don't have to repeat it all the time
    sleep(1)
    term.clear()
    drawDesktop()
end


#5 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 23 April 2013 - 02:59 AM

View PostremiX, on 23 April 2013 - 02:57 AM, said:

View PostLBPHacker, on 23 April 2013 - 02:44 AM, said:

View PostMackan90096, on 22 April 2013 - 11:41 PM, said:

the login info is correct
What makes you sure about that exactly?
It would error here if the username didn't exist...
local file2 = fs.open("users/"..usrName.."/bg", "r") local logoPath = file2.readLine() logo = paintutils.loadImage(logoPath) file2.close()

It wouldn't if the directory itself did exsist but the file inside with the same name didn't.
if not fs.exists("users/"..usrName.."/"..usrName) then






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users