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

Multiple Passwords

Started by tysciman7, 09 June 2013 - 12:50 AM
tysciman7 #1
Posted 09 June 2013 - 02:50 AM
Im using floppy disks for keycards to open doors and there are five different keycards with different certifications where clearence5 can open all doors (1-5 terminals) where as a keycard will have a different word that only works on terminals 1-4 etc. how do i change my code so level 5 accepts one password, level 4 accepts 2 pass, level 3 accepts 3 pass, etc.


The code:

password = "password"
debug_char = "0"
side = "left"

opentime = 3

term.clear()

function mainLoop()
while(true) do
	    event, driveSide = os.pullEventRaw()
			    if(event=="disk" and driveSide) then
					    path = disk.getMountPath(driveSide)
			    if(path) then
					    path = path.."/passcard"
					    file = fs.exists(path) and io.open(path, "r") or nil
end
disk.eject(driveSide)

if(file and file:read()==password) then
	    rs.setOutput("left", true)
	    sleep(3)
	    rs.setOutput("left", false)
end
if(file) then file:close() file = nil end
elseif(debug_char and event=="char" and driveSide==debug_char) then return("break") end
end
end

rs.setOutput("left", false)
sfile = io.open("/startup", "w")
sfile:write('shell.run("passcard")')
sfile:close()

repeat
ok, err, val = pcall(mainLoop)
if(not ok and err) then
if(err=="Terminated") then print ("Please obtain sufficient clearance.")
else
print(err)
end
end
until(ok and err=="break")
Bomb Bloke #2
Posted 09 June 2013 - 04:26 AM
Is there a reason people don't just dig these computers up and put a redstone torch in their place? And why do I have a sneaking suspicion that if this were "your" code, you'd know what changes to make (you know what that debug character does, right)? ;)/>

Anyway, change the top from this:

password = "password"

… to something like this:

password = {"lvl1password","lvl2password","lvl3password","lvl4password","lvl5password"}
terminalLevel = 1  -- The higher this is, the higher the password level you 
                   -- need to get in to this particular computer.

Then change the password checker bit from this:

if(file and file:read()==password) then
            rs.setOutput("left", true)
            sleep(3)
            rs.setOutput("left", false)
end

… to something like this:

if file then
            diskCode = file:read()
            for i=terminalLevel,#password do if diskCode == password[i] then
                        rs.setOutput("left", true)
                        sleep(3)
                        rs.setOutput("left", false)
end end end