Posted 04 April 2012 - 12:08 AM
This is a very simple client/server program which allows for the user to make a terminal and a server (which holds and distributes the password.) The code is fairly simple but there are some interesting features that most door locks wouldn't include.
Code:
Client:
Installation:
Client:
Features:
Code:
Spoiler
Server:
modem = "back"
rednet.open(modem)
term.clear()
term.setCursorPos(1,1)
print("Serving Password Requests...")
print("['q' to quit 'c' to change password]")
file = io.open("disk/password.txt","r")
password = file:read()
file:close()
answer = ""
while(true) do
event,param1,param2 = os.pullEvent()
if(event == "rednet_message" and param2 == "password") then rednet.send(param1,password)
elseif(event == "char") then
if(param1 == "q") then break
elseif(param1 == "c") then
write("New Password: ")
while(true) do
event,param1,param2 = os.pullEvent()
if(event == "char") then
answer = answer..param1
write(param1)
elseif(event == "key") then
if(param1 == 28) then -- ENTER
break
elseif(param1 == 14) then -- Backspace
x,y = term.getCursorPos()
term.clearLine()
term.setCursorPos(1,y)
write("New Password: ")
answer = ""
end
end
end
file = io.open("disk/password.txt","w")
file:write(answer)
print("")
print("Password Changed to: "..answer)
password = answer
file:close()
answer = ""
end
end
end
Client:
username = "Cookies19"
door = "left"
modem = "back"
os.startTimer(30) --go to sleep after 30 seconds (see event)
rednet.open(modem)
rednet.broadcast("password")
id,message = rednet.receive(5)
password = message
term.clear()
term.setCursorPos(1,1)
print("Terminal of Awesomeness")
print("***********************")
print("User Name: ")
write("Password: ")
term.setCursorPos(12,3)
textutils.slowPrint(username)
term.setCursorPos(11,4)
answer = ""
while(true) do
event,param1,param2 = os.pullEventRaw()
if(event == "terminate") then print("")print("")print("Now why on earth would I let you do that?") sleep(3) os.shutdown()
elseif(event == "char") then
answer = answer..param1
write(param1)
elseif(event == "key")
then
if(param1 == 28) then --ENTER
break
elseif(param1 == 14) then --BKSPACE
term.setCursorPos(1,4)
term.clearLine()
write("Password: ")
answer = ""
end
elseif(event == "timer") then term.clear() term.setCursorPos(1,1) print("Going to sleep...") sleep(1) os.shutdown()
end
end
print("")
print("")
if(answer == password) then print("Access Granted!") redstone.setOutput(door,true) sleep(3) os.shutdown()
else print("Access Denied!") sleep(1) rednet.broadcast("bye") os.shutdown() end
Installation:
Spoiler
Server:- Copy the above code into a file called "startup" and place it in a disk near the server
- Set the variable "modem" to the side with your wireless modem
- Create a file called "passwords.txt" on the same disk
Client:
- Copy the above code directly to the system and call the file "startup"
- Change the variables "username", "door", and "modem" to the apropriate states (door is a variable for the side the door is on)
- Create some kind of redstone powered mechanism to be activated on an incorrect password entry
- Place a system near the contraption, to turn it on
- Create a code like this and call it "startup" (you may also choose to put it on a disk near the system):
modem = "back"
trap = "front"
rednet.open(modem)
while(true) do
event,param1,param2 = os.pullEvent()
if(event == "rednet_message" and param2 == "bye") then redstone.setOutput(trap,true) sleep(3) redstone.setOutput(trap,false) end
end
print("hehe")
Note that "bye" is the failure message broadcasted by terminals.Features:
- Username entry (this is mostly just a novilty as the system automatically enters the username)
- Termination protection (Ctrl+T won't work)
- Failure events
- Server side password storage
- Password change without rebooting the server
- Simple installation (at least I believe it's simple)
- The word "cookies" in the code (come on, you know you love it)
- Reading my beautiful post