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

[Programming][Question] Trying to make a password door with a twist

Started by Tehsov, 10 April 2012 - 08:01 PM
Tehsov #1
Posted 10 April 2012 - 10:01 PM
The twist is that the door logs all attempts by sending a rednet signal to another computer, computer #8, and will allow computer #8 to send a single door pass to computer #9. How do I make the door pass send, and how do I save the logs to a disk at computer #8?

@ computer # 8 I get an error: bios:206: [string "startup"] :37: 'end' expected (to close 'function' at line 3)


rednet.open("right")

local function entry()
function os. pullEvent() return os.pullEventRaw() end
term.clear()
print("Enter Username")
user=io.read()

print("Enter Password. If you do not have a password, type in REQ-PASS. If you have been denied a password, and request single-use access, type in SINGLE-OPEN")
pass=io.read()

if pass=="Password" then
print ("Access Granted")
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", false)
rednet.send (8, "User" ..user.. "has entered using the correct password.")
sleep(2)

if pass=="REQ-PASS" then
print ("Your request has been sent.")
rednet.send (8, "User" ..user.. " has requested a password to enter.")
sleep(2)

if pass=="SINGLE-OPEN" then
rednet.send (8, "user" ..user.." requested a single usage. ACCEPT/DENY?")

else
print ("Access denied. Attempt logged.")
sleep(2)
end
end

while true do
entry()

end
Luanub #2
Posted 10 April 2012 - 10:12 PM
I see a couple of things that need to be fixed


print("Enter Username")
user=io.read()  -- if user is inputting values use read() not io.read() that is for file handling
print("Enter Password. If you do not have a password, type in REQ-PASS". If you have been denied a password, and request single-use access, type in "SINGLE-OPEN")
pass=io.read()  -- same here but you can use read("*") to display *'s on the screen

rednet.send (8, "user" ..user.." requested a single usage. ACCEPT/DENY?" -- add a )


This code should then work and send the information to comp 8.

to save the file on the comp you will want to do something like..

if not fs.exists("disk/savefile") then
	local file = io.open("disk/savefile", "w")
	file:write()
	file:close()
end

while true do
local evt, id, msg = os.pullEvent("rednet_message")
local file = io.open("disk/savefile", "a") -- a to append the file which will leave the previous data
file:write(msg.."n") -- add the new data and create a new line
file:close()
end

this needs fixed too, no space in the setCursorPos

term. set CursorPos (1,1)

to
term.setCursorPos(1, 1)
Edited on 10 April 2012 - 08:15 PM
Tehsov #3
Posted 10 April 2012 - 10:48 PM
Ok so I did this, now how do I set it up so that Computer 8 sends the door pass, and resolve the


bios:206: [string "startup"] :37: 'end' expected (to close 'function' at line 3)

error on that same computer?
Luanub #4
Posted 10 April 2012 - 10:54 PM
This should fix the error. Put the os.pullEvent function outside of the entry function. Fixed a couple of typo's and added 2 ends
Spoiler

rednet.open("right")

function os.pullEvent()
return os.pullEventRaw() end

local function entry()
term.clear()
print("Enter Username")
user=io.read()

print("Enter Password. If you do not have a password, type in REQ-PASS. If you have been denied a password, and request single-use access, type in SINGLE-OPEN")
pass=io.read()

if pass=="Password" then
print ("Access Granted")
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", false)
rednet.send (8, "User" ..user.. "has entered using the correct password.")
sleep(2)

if pass=="REQ-PASS" then
print ("Your request has been sent.")
rednet.send (8, "User" ..user.. " has requested a password to enter.")
sleep(2)

if pass=="SINGLE-OPEN" then
rednet.send (8, "user" ..user.." requested a single usage. ACCEPT/DENY?")

else
print ("Access denied. Attempt logged.")
sleep(2)
end
end
end
end

while true do
entry()
end

As for sending the pass, is it located in a file?
If so open the file with an r for read only. Capture its contents in a var and rednet.send the var to whichever computer.

So

local file = io.open("password", "r")
   	 pass = file:read()
        file:close()
	    rednet.send( id, pass )
Edited on 10 April 2012 - 08:56 PM
Tehsov #5
Posted 10 April 2012 - 11:28 PM
I was going to send the password via private message, but saving it in a file and doing that sounds like a better idea. I have the password in a file that simply prints it to the user's screen, and then deletes itself.


print (message)
sleep(#)
print(message)
sleep(#)
fs.delete(Program Name)

I also noticed that the sleep does not work after an incorrect password, a PASS-REQ or a SINGLE-PASS.

How do I open the door remotely from comp #9 as a manual response to a SINGLE-PASS request from comp 8?
Luanub #6
Posted 11 April 2012 - 09:48 AM
The sleep issue has to do with the if statements. I think it may be due to the way they are nested into each other.

for example the logic if if if end end end

vs if end if end if end

I just changed it to if elseif end and the sleeps work as they should


local function entry()

term.clear()
print("Enter Username")
user=io.read()

print("Enter Password. If you do not have a password, type in REQ-PASS. If you have been denied a password, and request single-use access, type in SINGLE-OPEN")
pass=io.read()

if pass=="Password" then
print ("Access Granted")
redstone.setOutput("left", true)
sleep(2)
redstone.setOutput("left", false)
rednet.send (8, "User" ..user.. "has entered using the correct password.")
sleep(2)
elseif pass=="REQ-PASS" then
print ("Your request has been sent.")
rednet.send (8, "User" ..user.. " has requested a password to enter.")
sleep(2)
elseif pass=="SINGLE-OPEN" then
rednet.send (8, "user" ..user.." requested a single usage. ACCEPT/DENY?")
else
print ("Access denied. Attempt logged.")
sleep(2)
end
end

As far as opening the door remotely you will want to send a key word to comp 9 from 8 and have #9 perform the action of opening the door when it receives that message.

Small example.

Comp 8:

rednet.send(9, "open")

Comp 9:

local id, msg = rednet.receive()
if msg == "open" then
rs.setOutput("side", true )
sleep (3)
rs.setOutput("side", false)