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

How would a pro have done this?

Started by roy291, 06 June 2013 - 04:58 AM
roy291 #1
Posted 06 June 2013 - 06:58 AM
Hello just started trying out how this mod works ( no lua experience)
and after a few hours i wrote this login.

my question is how woud you guys have done this ( what would you change/ optimize)
So dat i know what i can focus on when make my next script.

( i might have a few typo's but it did work :)/> )
it is a startup file

os.pullEvent = os.pullEventRaw

function clear()
term.clear()
term.setCursorPos(1,1)
end

function red()
redstone.setOutput("right", true)
sleep(3)
redstone.setOutput("right", false)
end


print "welcome"
write "login: "
login = read()

if login == ("login1") then
write "pasword: "
if pasword == ("pas1") then
clear()
print "hello login1"
red()
clear()
else
clear()
print "wrong pasword"
sleep(2)
os.reboot()
end

elseif login == ("login2") then
write "pasword: "
if pasword == ("pas2") then
clear()
print "hello login2"
red()
clear()
else
clear()
print "wrong pasword"
sleep(2)
os.reboot()
end

else
clear()
print "unkown login"
sleep(2)
os.reboot()
end

Thanks for helping me

Roy
Lyqyd #2
Posted 06 June 2013 - 10:31 AM
Split into new topic.
LBPHacker #3
Posted 06 June 2013 - 02:17 PM
Suppose I'm a pro.
Pretty much the same. Indentation and some spaces added.

Spoiler
os.pullEvent = os.pullEventRaw

function clear()
    term.clear()
    term.setCursorPos(1, 1)
end

function red()
    redstone.setOutput("right", true)
    sleep(3)
    redstone.setOutput("right", false)
end

print("welcome")
write("login: ")
local login = read()

if login == "login1" then
    write("pasword: ")
    if pasword == "pas1" then
        clear()
        print("hello login1")
        red()
        clear()
    else
        clear()
        print("wrong pasword")
        sleep(2)
        os.reboot()
    end
elseif login == "login2" then
    write("pasword: ")
    if pasword == "pas2" then
        clear()
        print("hello login2")
        red()
        clear()
    else
        clear()
        print("wrong pasword")
        sleep(2)
        os.reboot()
    end
else
    clear()
    print("unkown login")
    sleep(2)
    os.reboot()
end

Yes, I know that
print "hello world"
is correct, but only if you have only one string parameter.
Sorroko #4
Posted 06 June 2013 - 03:05 PM
Something a little more complex, (note: I haven't actually tested it):

Spoiler[php]
os.pullEvent = os.pullEventRaw
local tW, tH = term.getSize()
local users = {
["user1"] = "foo",
["user2"] = "bar"
}
function clear()
term.clear()
term.setCursorPos(1, 1)
end
function pulse()
redstone.setOutput("right", true)
sleep(3)
redstone.setOutput("right", false)
end
function drawLogin()
clear()
print("Welcome.")
term.setCursorPos(tW / 2 - 4, tH / 2)
term.write("Login: ")
term.setCursorPos(tW / 2 - 4, tH / 2 + 1)
term.write("Password: ")
end
while true do
drawLogin()
term.setCursorPos(tW / 2 + 10, tH / 2) – Set to username position
local user = read()
term.setCursorPos(tW / 2 + 10, tH / 2 + 1) – Set to password position
local pass = read()
if users[user] ~= nil then – User exists, (the below if statement can be merged)
if users[user] == pass then – If the stored password equals the input
clear()
pulse()
print("Welcome " .. user .. "!")
sleep(2)
end
end
end[/php]

Uses tables to store users which makes it a lot more flexible also adding a while loop instead of rebooting and some small tweaks.
Of course, you can continue to expand on this to make it count attempts, log users, do different actions depending on the user etc.
I suggest you have a look around for some table tutorials, they are very useful in lua. ;)/>

EDIT: Fixed code formatting
roy291 #5
Posted 06 June 2013 - 03:26 PM
thanks for the help

will look at some tutorials about tables