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

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

Started by Mackan90096, 22 April 2013 - 10:41 PM
Mackan90096 #1
Posted 23 April 2013 - 12:41 AM
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

remiX #2
Posted 23 April 2013 - 03:44 AM
From which block is it failing? THe first if or the elseif?
LBPHacker #3
Posted 23 April 2013 - 03:44 AM
the login info is correct
What makes you sure about that exactly?
remiX #4
Posted 23 April 2013 - 03:57 AM
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
LBPHacker #5
Posted 23 April 2013 - 03:59 AM
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