21 posts
Posted 22 October 2012 - 11:32 PM
I have a door lock program that requires the user to know the password to gain entry, but I want to know how I can code it so the user can change the password and have the change stay even after a reboot.
function lock()
term.clear()
term.setCursorPos(1,1)
os.pullEvent = os.pullEventRaw
pass = io.read()
if pass == "*ignore asterisks, password goes here**" then
term.clear()
redstone.setOutput("left" , true)
sleep(3)
redstone.setOutput("left" , false)
lock()
else
lock()
end
end
lock()
2005 posts
Posted 23 October 2012 - 01:31 AM
Use the
file API You want to do something like"
local hndl = fs.open("password","r")
local match = hndl.readAll() hndl.close()
You use match in "if pass == match". Then later, when you want to save a different password entered by the user, you use
hndl = fs.open("password","w")
hndl.writeLine(read()) hndl.close()
21 posts
Posted 23 October 2012 - 03:42 AM
Use the
file API You want to do something like"
local hndl = fs.open("password","r")
local match = hndl.readAll() hndl.close()
You use match in "if pass == match". Then later, when you want to save a different password entered by the user, you use
hndl = fs.open("password","w")
hndl.writeLine(read()) hndl.close()
So the variable 'local hndl' will create a file to store the passwords by itself?
I'm kind of confused how to combine them.
function lock()
term.clear()
term.setCursorPos(1,1)
os.pullEvent = os.pullEventRaw
print("Hello user! Type your password or type 'New' to change your password.")
local hndl = fs.open("password" , "r")
local match = hndl.readAll() hndl.close()
pass = io.read()
if pass == "match" then
term.clear()
redstone.setOutput("left" , true)
sleep(3)
redstone.setOutput("left" , false)
lock()
elseif pass= "New"
-- Kind of stuck at this point
lock()
end
end
lock()
2088 posts
Location
South Africa
Posted 23 October 2012 - 05:04 PM
Here's a simple program for you
os.pullEvent = os.pullEventRaw
sFile = "passwords.txt"
sRoot = "DoorLock/"
function clear()
term.clear()
term.setCursorPos(1,1)
end
-- [[ Check if folder/file for password exists | read password and set pass functions ]] --
function checkFolder()
if not fs.exists(sRoot) then
fs.makeDir(sRoot)
file = fs.open(sRoot..sFile, "w")
file.close()
end
end
function readPass()
file = fs.open(sRoot..sFile, "r")
line = file.readLine()
file.close()
end
function setPass()
clear()
print("Please set a password:")
write(" > ")
pass = read()
file = fs.open(sRoot..sFile, "w")
file.write(pass)
file.close()
print("nPassword has been changed to: "..pass)
sleep(3)
end
-- [[ Open door function ]] --
function OpenDoor()
clear()
print("Password correct! Please proceed.")
redstone.setOutput("left" , true)
sleep(3)
redstone.setOutput("left" , false)
end
checkFolder()
-- [[ Main part ]] --
while true do
readPass()
clear()
if line == nil then
setPass()
else
print("Type 'new' to set a new password")
print("Please enter password:")
write(" > ")
pass = read("*")
if pass == line then
OpenDoor()
elseif pass == "new" then
setPass()
else
clear()
print("Invalid password.")
sleep(3)
end
end
end
2005 posts
Posted 23 October 2012 - 05:10 PM
The creation of local hndl = fs.open("password","r") just makes it so that you can access a file named password. You should create that file yourself using the edit command. The creation of local match = hndl.readAll() is what allows you to use the password you stored in the file named password. Then we call hndl.close() so that we don't have the file still open, cause we already read it and the password is stored in match.
21 posts
Posted 25 October 2012 - 01:03 AM
.