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

Can you code this?

Started by TurtleGilmore, 01 October 2012 - 04:27 PM
TurtleGilmore #1
Posted 01 October 2012 - 06:27 PM
Hello, I require an advanced door lock that can protect all the doors inside my factory.

CHECK POSTS FOR MY ATTEMPTS!

Passwords:
Bypass ID: X
Normal ID: Y
Administration ID: Z

Here is how I want it:

When turned on:
''Welcome to Vurruca National Production Company"
"Please enter your Security ID for: Alpha"
"ID:"

Password correct:
"Reading ID entered…"
Pause for 2 seconds
"Checking database…"
Pause for 2 seconds
"Security ID verified - Correct!"
"Un-locking Alpha security…"
Door opens (door is on the right)
Pause 3 seconds
"Re-activating Alpha security"
"LOCKED!"
Reboot

Password incorrect:
"Reading ID entered…"
Pause for 2 seconds
"Checking database…"
Pause for 2 seconds
"Security ID locked down - Incorrect!"
"Locking Alpha security…"
"Activating Alarm systems"
Alarm activates (attached to the back of the computer)
Password required to turn the alarm off
"ENTER ADMINISTRATION ID:"
"ID:"
Correct Admin ID:
"De-activating alarms"
Reboot

Bypassing system to edit program:
I need to be able to enter a password into the computer to terminate the program, giving me the ability to edit the programs coding. Here's what it says if the code is correct:
"Reading ID entered…"
Pause for 2 seconds
"Checking database…"
Pause for 2 seconds
"Security ID verified - Correct!"
"Bypassing Alpha security…"
If password is incorrect, do the ''password incorrect'' section seen above.

​If anyone is able to create this for me I would be most grateful!
cheekycharlie101 #2
Posted 01 October 2012 - 06:48 PM
First, read the forum rules: do not expect people to code for you. second i could probally make something like this with bundles cables. like you have diferent color red power cable going to different doors. Note im not the best coder here, so there will always be someone who can do it better than me. But first. make an attempt our self to code something. then yes i will help you out. also use the wiki for help with the apies.
TurtleGilmore #3
Posted 01 October 2012 - 07:12 PM
Hence the fact it says COULD you. Also, I do not require all the doors to be powered from one computer, I would have a computer next to each door.
I just require the code for this.
GopherAtl #4
Posted 01 October 2012 - 07:13 PM
Oh, you're just asking if I could?

Yes, yes I could.
TurtleGilmore #5
Posted 01 October 2012 - 07:14 PM
Would you be able to code this for me?
GopherAtl #6
Posted 01 October 2012 - 07:21 PM
Would I be able? Yes, that would be within my abilities.

Am I going to volunteer to write it for you? Unlikely.

I was replying to be a smartass because your response missed the point. You are asking people to write code for you. When criticized, you said you were "just asking." Which is nice, better I suppose than demanding people write code for you, but ultimately besides the point.

Browse the forum. There is a lot of code very similar to this around. A lot of it. Door locks are the Hello World of Computercraft, so we get several new door lock programs posted every week. Find some, read the code, try to understand it, lookup the functions they call on the wiki, and when you find something you don't understand or can't figure out, then post and ask for help with whatever you're stuck on and you'll almost certainly get it.
Cranium #7
Posted 01 October 2012 - 07:23 PM
Able: yes.
Willing: no.
Again, read the forum rules. If you make absolutely no attempt to code this yourself, then you are very unlikely to get any help. Cheeky did make a good point. He's a new coder as well, and he has learned a lot from the wiki, seeing what he's doing here so far. Lua is one of the easiest languages to learn, so I think you would greatly benefit from learning it on your own.
TurtleGilmore #8
Posted 01 October 2012 - 07:54 PM
Going to have an attempt, sorry to look desperate.
Cranium #9
Posted 01 October 2012 - 07:58 PM
Hey, we were all new at one point, and asking for someone to do it is so much easier than doing it. But I guarantee you will feel a lot better after you create something.
Give a man a code, lock his house for a day.
Teach a man to code, and that man will RULE THE DAY…
TurtleGilmore #10
Posted 01 October 2012 - 08:08 PM
Well, I gave it a stab and this is what I got (see code), you may notice that I haven't done the bypass, this is because I have no idea at all what to put and after re-reading this code I've probably done alot wrong here too!


term.clear()
term.setCursorPos(1,1)
x = "Y"
z = "Z"
print("Welcome to Vurruca National Production Company")
print("Please enter your Security ID for: Alpha")
print("ID:")
input = read()
if input == x then
  term.clear()
  term.setCursorPos(1,1)
  print("Reading ID entered...")
  sleep(2)
  print("Checking database...")
  sleep(2)
  print("Security ID verified - Correct!")
  print("Un-locking Alpha security...")
  rs.setOutput("right",true)
  sleep(3)
  print("Re-activating Alpha security")
  print("LOCKED!")
  rs.setOutput("right",false)
end
os.reboot()
else
  term.clear()
  term.setCursorPos(1,1)
  print("Reading ID entered...")
  sleep(2)
  print("Checking database...")
  sleep(2)
  print("Security ID locked down - Incorrect!")
  print("Locking Alpha security...")
  sleep(1)
  print("Activating alarm systems!")
  rs.setOutput("back",true)
  print("Enter Administration ID:")
  print("ID:")
  input = read()
  if input == z then
  term.clear()
  term.setCursorPos(1,1)
  print("Reading ID entered...")
  sleep(2)
  print("Checking database...")
  sleep(2)
  print("Administration ID verified - Correct!")
  print("De-activating Alpha alarms!")
  rs.setOutput("back",false)
end
os.reboot

Any advice?
jag #11
Posted 01 October 2012 - 08:14 PM
So because I don't currently got anything to do I made the script for you:
Spoiler
-- Made by jag_e_nummer_ett
-- Created for TurtleGilmore and his factory
-- 1 / 10 - 2012

-- ⇊ change this for the id you want ⇊
local factoryID = "Alpha"
local bypassID = "bypass"
local normalID = "normal"
local adminID = "admin"

-- ⇊ Program start, dont change anything here ⇊

function clearPrint(string, x, y)
  if not x or x < 0 then x = 1 end
  if not y or y < 0 then y = 1 end
  term.clear()
  term.setCursorPos(x,y)
  print(string)
end

function checkID(ID)
  clearPrint("Reading ID entered...")
  sleep(2)
  clearPrint("Checking database...")
  sleep(2)
  if ID == normalID or ID == adminID then
	clearPrint("Security ID verified - Correct!")
	sleep(.4)
	print("Un-locking "..factoryID.." security...")
	rs.setOutput("right", true)
	sleep(3)
	clearPrint("Re-activating "..factoryID.." security...")
	sleep(1.5)
	rs.setOutput("right", false)
	clearPrint("LOCKED!")
	sleep(2)
  elseif ID == bypassID then
	clearPrint("Security ID verified - Correct!")
	sleep(.4)
	print("Bypassing "..factoryID.." security...")
	sleep(1.5)
	clearPrint("Done!")
	return "bypass"
  else
	clearPrint("Security ID locked down - Incorrect!")
	sleep(.4)
	print("Activating alarm systems!")
	sleep(2)
	rs.setOutput("back", true)
	local alarmInput = ""
	while alarmInput ~= adminID do
	  clearPrint("ENTER ADMINISTRATION ID")
	  write("ID: ")
	  alarmInput = read()
	end
	clearPrint("Correct Admin ID!")
	sleep(.4)
	print("De-activating alarms")
	rs.setOutput("back", false)
	sleep(2)
  end
end

-- ⇊ Main program below ⇊

while true do
  clearPrint("Welcome to Vurruca National Production Company")
  print("Please enter your Security ID for: "..factoryID)
  write("ID: ")
  if checkID(read()) == "bypass" then break end
end
Also on pastebin: hSbVbr9V

Images:
Spoiler


TurtleGilmore #12
Posted 01 October 2012 - 08:16 PM
Well, I see my code is completely different to what you supplied, thanks so much for your time anyhow.

EDIT: If someone could still tell me what I did wrong, it would be very helpful!
jag #13
Posted 01 October 2012 - 08:28 PM
Well, I see my code is completely different to what you supplied, thanks so much for your time anyhow.

EDIT: If someone could still tell me what I did wrong, it would be very helpful!
First of all, you got an end and an else command (near the first os.reboot()).
Secondly you don't got a else statement for if you type in the wrong admin password/ID, so if you enter the wrong password/ID nothing will happend, the program will just reboot…
Finally, your os.reboot at the bottom is missing some of those: → ( ← and → ) ←.
GopherAtl #14
Posted 01 October 2012 - 08:28 PM
Much better.

Biggest thing, your if statements are followed by end, and you have else's after those. An if will only have one matching end, and the else needs to come first, example:


if somecondition then
  some code if true
else
  some code if false
end

Second, you're repeating the4 os.reboot after each case, when you only need one os.reboot at the end. Just remove the first one between "end" and "else" while you're removing that first unnecessary (and error-throwing) "end." Also note, the first one is written correctly, while the second is missing the (), so it would throw an error. Be sure to add those.

As for how to implement the bypass, that's easy, this line in a program would give the user a command prompt:

return

There's quite a few more things I could point out but that ought to be enough to let you get it working. We can worry about style and efficiency after that. :)/>/>

One last tip for now, if you pass "*" as a parameter to read - replace "input=read()" with "input=read("*")" - it will display *s like a password entry field, so someone looking over your shoulder can't see your password!
Cranium #15
Posted 01 October 2012 - 08:58 PM
Not to overload yu with tips, but If you haven't found out already, you can use elseif to define other specific statements. For example:

if condition ==1 then
--do this code
elseif condition == 2 then
--do this other code
else
--if condition is not either one of those, do this code
end
YOu can have as many elseifs as you want, but only one if, and one end. Hope that helps!
jag #16
Posted 01 October 2012 - 09:26 PM
but only one if, and one end.
…………Aaaaaand don't forget only one else too
Cranium #17
Posted 01 October 2012 - 09:28 PM
Oh, yep. Forgot that part.
jag #18
Posted 01 October 2012 - 10:25 PM
Oh, yep. Forgot that part.
Did you see my code btw?
Cranium #19
Posted 01 October 2012 - 10:34 PM
Nah. Wasn't paying attention, since I was just giving a brief tutorial.
jag #20
Posted 01 October 2012 - 10:45 PM
Well go back up and take a look at it!
cheekycharlie101 #21
Posted 04 October 2012 - 05:21 PM
Not to overload yu with tips, but If you haven't found out already, you can use elseif to define other specific statements. For example:

if condition ==1 then
--do this code
elseif condition == 2 then
--do this other code
else
--if condition is not either one of those, do this code
end
YOu can have as many elseifs as you want, but only one if, and one end. Hope that helps!
not sure if im right but dont you need a space after == on line 1?
Cranium #22
Posted 04 October 2012 - 05:44 PM
not sure if im right but dont you need a space after == on line 1?
Not really. You can keep it with or without spaces. It just reads easier if spaced. Proper spacing and indentation is important if you intend for others to decode your scripts and help you.