23 posts
Posted 10 June 2012 - 11:30 PM
I am trying to make a computer pass code lock that can accept multiple passwords, but cant figure it out. If anybody can help it would be appreciated. If you need any form of code I can also supply that as I have a strange approach to coding and some suggestions may not make sense…I have tried for a long time but cannot get it to work. please help.
1604 posts
Posted 10 June 2012 - 11:37 PM
You should use a table to store the usernames and passwords:
local tUsers = {
["User1"] = "Password1",
["User2"] = "Password2",
...
["UserN"] = "PasswordN"
}
and then you can check if a user-password combination is correct like:
-- get user and password
local usr = read()
local pass = read()
-- verify user and password
if tUsers[usr] == pass then
-- correct password
else
-- incorrect username/password
end
You could store them in a file, so you can add and remove users, and change passwords. Use the fs api for that.
23 posts
Posted 10 June 2012 - 11:40 PM
Thanks!
23 posts
Posted 11 June 2012 - 12:00 AM
Ok, it worked for a passcode lock but when I tried to modify it for a keycard lock that reacts based on the label on the disk (I am not understanding fs api at all) nothing happens when I insert a card.
Here is my code:
local tKey = {
["Key1"] = "Access Level 1",
["Key2"] = "Access Level 2",
["Key3"] = "Access Level 3",
["Key4"] = "Access Level 4",
["Key5"] = "Access Level 5",
["Master"] = "Skeleton Card"
}
os.pullEvent("disk", "right")
local pass = disk.getLabel("right")
if tKey == pass then
rs.setOutput("back",true)
sleep(5)
rs.setPutput("back",false)
os.reboot()
else
os.reboot()
end
1604 posts
Posted 11 June 2012 - 12:27 AM
Ok, it worked for a passcode lock but when I tried to modify it for a keycard lock that reacts based on the label on the disk (I am not understanding fs api at all) nothing happens when I insert a card.
Here is my code:
local tKey = {
["Key1"] = "Access Level 1",
["Key2"] = "Access Level 2",
["Key3"] = "Access Level 3",
["Key4"] = "Access Level 4",
["Key5"] = "Access Level 5",
["Master"] = "Skeleton Card"
}
os.pullEvent("disk", "right")
local pass = disk.getLabel("right")
if tKey == pass then
rs.setOutput("back",true)
sleep(5)
rs.setPutput("back",false)
os.reboot()
else
os.reboot()
end
tKey is the table, you have to compare to one of its values. Like:
if tKey[label] == "Access Level 1" then
For this case, I think it would be better to make the access levels numbers, and give access to certain stuff depending on the level:
local tLevels = {
["Access Level 1"] = 1,
["Access Level 2"] = 2,
["Access Level 3"] = 3,
["Skeleton Card"] = 999
}
while true do
term.clear()
term.setCursorPos(1, 1)
print("Enter Access Card")
local evt, side = os.pullEvent("disk")
if side == "right" then
local level = tLevels[disk.getLabel("right")]
if not level then
print("Invalid Key Card")
sleep(1)
elseif level >= 1 then
-- level 1 stuff
if level >= 2 then
-- level 2 stuff
if level >= 3 then
-- level 3 stuff
end
end
end
end
end
23 posts
Posted 11 June 2012 - 01:05 AM
That was a good solution, thanks! I am not very good at the more advanced parts of computercraft so I over complicate and do things wrong alot…
134 posts
Location
Salt Lake, UT
Posted 11 June 2012 - 02:25 AM
Annother Option would be to name the floppies and to have passwords accosiated with the floppies… for example
local a = os.pullEvent("disk")
-- Now i assume that you will only have 1 disk drive on it so we just want it to wait for an insert of the disk... variable a really isn't needed.
name = disk.getLabel("side") -- the name of the disk determines the password that the user must enter next. (security feature)
if name == "master" then
term.clear()
term.setCursorPos(1,1)
print("Please Enter Password")
pass = io.read()
if pass == "(password)" then
...
else
...
end
-- continue "if" statements here for each password
end
If you have any more questions feel free to ask