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

Computer Crafts Ultimate door lock

Started by ben657, 18 September 2012 - 08:52 PM
ben657 #1
Posted 18 September 2012 - 10:52 PM
Hello there!

Well we all know a door lock is basically the Hello World of CC, and there are hundreds of variations on it kicking around these forums, so I thought, why not create the lock to end all locks as a group? I'll write up the simplest one I can, and you guys throw your code at it to make it awesome!

Want it to turn a light on above? Go for it.
Want it to say hello? Go for it. Want it to shoot a laser because not even you can go in your house? Go for it!

Hopefully we can just have a play about with some crazy ideas! :D/>/>

As suggestions come in, I'll try and update this code regularly.


term.clear()
term.setCursorPos(1,1)
password = "1234"
print("Enter the password please.")
input = read()
if input == password then
  rs.setOutput("back",true)
  sleep(3)
  rs.setOutput("back",false)
end
os.reboot()

That's my part, now what can you guys do? :)/>/>

(Wrote that on my phone, pretty sure it works, but not certain… :)/>/>)
Cranium #2
Posted 18 September 2012 - 11:20 PM

local admin = "cranium"
local adminpass = "mypass"
local user = "guest"
local userpass = "password"
while true do
rs.setOutput("left",false)
rs.setOutput("right",false)
term.clear()
term.setCursorPos(1,1)
write("Please enter your username: ")
local name = read()
if name == admin then
term.clear()
term.setCursorPos(1,1)
write("Please enter password: ")
local pass = read("*")
if pass == adminpass then
rs.setOutput("right",true)
term.clear()
term.setCursorPos(1,1)
print("Welcome home, Cranium.")
sleep(2)
elseif name == user then
term.clear()
term.setCursorPos(1,1)
write("Please enter password: ")
local pass = read("*")
if pass == userpass then
rs.setOutput("left",true)
term.clear()
term.setCursorPos(1,1)
print("Welcome home, guest.")
sleep(2)
end
end
Here's another. it's got an admin and user password. Admin on the right, guest on the left. Nothing special.

Edit: Wow, where did my spacing go?
ben657 #3
Posted 18 September 2012 - 11:51 PM
Dw about the spacing, I'll do it when I add it into the main post :)/>/>
KaoS #4
Posted 19 September 2012 - 07:00 AM
The way I do it:


local function login(user)
  rednet.broadcast(textutils.serialize({'login',user,tUsers[user][2],os.time()}))
  rs.setOutput(doorside, true) --I have removed this in another version, I use CCSensors to sense each players location, if you are logged in it opens doors as you pass, if you are admin then it also opens secret passageways and rooms. if you are not logged in it kills you
  sleep(4)
  rs.setOutput(doorside, false)
end

local tUsers={KaoS={mypassword, 'admins'}; Boris={hispassword, 'admins'}; Aurelius={password, 'users'}}
os.pullEvent=os.pullEventRaw
rednet.open(modemside)

while true do
  term.clear()
  term.setCursorPos(1,1)
  print('Enter Login Detailsn')
  write('Username: ')
  local sName=read()
  write('nPassword: ')
  local sPass=read('*')
  if tUsers[sName] and tUsers[sName][1]==sPass then
	login(sName)
	print('nn Login Successfull!')
	displaylogo()
  end
end
ben657 #5
Posted 19 September 2012 - 07:14 AM
The way I do it:


local function login(user)
  rednet.broadcast(textutils.serialize({'login',user,tUsers[user][2],os.time()}))
  rs.setOutput(doorside, true) --I have removed this in another version, I use CCSensors to sense each players location, if you are logged in it opens doors as you pass, if you are admin then it also opens secret passageways and rooms. if you are not logged in it kills you
  sleep(4)
  rs.setOutput(doorside, false)
end

local tUsers={KaoS={mypassword, 'admins'}; Boris={hispassword, 'admins'}; Aurelius={password, 'users'}}
os.pullEvent=os.pullEventRaw
rednet.open(modemside)

while true do
  term.clear()
  term.setCursorPos(1,1)
  print('Enter Login Detailsn')
  write('Username: ')
  local sName=read()
  write('nPassword: ')
  local sPass=read('*')
  if tUsers[sName] and tUsers[sName][1]==sPass then
	login(sName)
	print('nn Login Successfull!')
	displaylogo()
  end
end

ooOOoo, looks good! Just out of interest, is the broadcast so that you can log entrances on another computer?
KaoS #6
Posted 19 September 2012 - 07:23 AM
Exactly, I have a central PC which knows the ID of every other computer, no other computer has any code other than to listen to the input from the server and execute it, when the server boots up it sends the relevant programs to each PC, this minimizes the risk of people hacking in and modifying your stuff. there are a few exceptions (little trap executing PCs etc)

normally I would have that not broadcasted but sent for security reasons and would only accept that info from the correct PC, once I know who is logged in then I track their movements (I am developing a record builder of where people go as well that can be rendered on a map of my base), the point is that my tracker needs to know who is logged in and what is their status so it knows who to allow in, who to open secret passages for and who to kill, if you leave the tracked area then you are logged out, I have a PC that auto logs in certain frequent visitors as well and I want to implement a set time login where you are only welcome for a certain amount of time etc etc.

You are right
Mr. Fang #7
Posted 20 September 2012 - 04:02 AM
Thank you guys so much! I used both of your codes on my home security system. I used the first code to help me make a lighting system and I used the second one for making a userbased lock. You guys really helped me alot. Love ya!
ben657 #8
Posted 20 September 2012 - 08:20 AM
Hmm… I think when I've got some time after college, I'll write something up so I can put all your locks in functions and pick a random one to run, just to showcase them, then keep actual copies in spoilers for your names :)/>/>