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

[lua] Writing a better door lock?

Started by Madster, 19 February 2013 - 07:36 PM
Madster #1
Posted 19 February 2013 - 08:36 PM
Hello Computer Crafters! So, as you can probably tell, I am defiantly new here. Before I get into my questions, I thought I would let you guys know, who I am.

First off, I just want to give a bit of a background for myself. I am 17, male, and love gaming. I play on many servers, one being a very fast, and popular growing FTB server, and I even own a server! I know a bit of java, and just a tad bit of lua, but not to much. I started lua about 3 days ago, and I am learning fairly quick.

So, first off, I want to show you my first lua code.

pass = "Put password here"
print ("door lock")
write "Password: "
input = read()
if input == pass then
print ("Access Granted for 7 seconds, better hurry!")
redstone.setOutput ("left", true)
sleep(7)
os.shutdown()
else
print )"HA! Wrong password! Access Denied!")
sleep(2)
os.shutdown()
end
[size=4]


Now, I know most of you scripting gods out there probably want to rip your hair out, looking at that code. Trust me, I want to rip out my hair looking at it. It's so basic, and generic, but none the less, I was proud of it. It worked. I soon learned that, it was terrible. With that code, I have built a new door locking system, with many tests, and failures. Here are some of the features to my current door lock:
  • Password(door opens for X amount of time, then closes)
  • Admin pass(Door will remain open, until password is entered again)
  • Root pass(allows user to edit the computer, files, ect.)
Now, its not much, but its more then a simple password door that does not even block 'ctrl + t'
So, now that you know a bit about myself, where I stand on lua better, I come with a question, or two…or more.


So, I got the root, admin pass, and password to work perfectly. I was proud of myself, but I wanted to challenge myself to do better, i wanted to release the door lock later in the future, and to do that, I would need a system that lets the user make their own passwords, what side the door is on, how long it's open, ect, and I did just that…Kinda.

So, first off, the selections. When a user first starts the program, they are able to select their main password, admin password, root, what side the door is on, and the amount of time the door will stay open. That works to an extent. If the program is restarted, the user will have to re-enter all of that again. Which kinda defeats the propose of the first part of the program. I want it so after that information is entered, it won't have to be entered again.(NOTE: The link to my pastebin with the code is down below)

Secondly, the time. First part of the program lets the player pick how long they want the door to remain open when the normal pass is used like I said above. But, when I try the pass, it opens the door, but will not close it, and it gives me this:


bios:133: Expected Number

If, someone cloud help me understand what I am doing wrong, that would be awesome!


Thanks in advance!
~Madster

CODE:

local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
function clear()
term.setCursorPos(1,1)
term.clear()
end
clear()
local setpass --for setting the password
local adminpass -- for setting the admin password
local doortime -- for setting how long the door will stay open
local rootpass -- for setting the rootpass
local availableSides = {"top, ", "bottom, ", "left, ", "right, ", "front, ", "back", "\n"}
print("Please define your desired password:")
setpass = read()
clear()
print("Please define the password to gain root access to the computer(to edit files):")
rootpass = read()
clear()
print("Please define the admin password. This password will keep the door open when entered")
adminpass = read()
clear()
print("Please define the amount of time the redstone pulse will be on:")
doortime = tonumber(read())
clear()
print("Which side is the door on?")
print("Sides that can be used:")
for i = 1, 7 do
write(availableSides[i])
end
print("Please define a side:")
redstoneOutputSide = read()
clear()
while true do
clear()
textutils.slowPrint ("Loading...")
textutils.slowPrint ("Done Loading!")
sleep(1)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint ("Welcome to SecurityDoorWatch")
textutils.slowPrint ("v0.5")
sleep(0.5)
term.clear()
print ("Door Lock v0.5")
print ("Coded by Madster")
write "Passcode: "
input = read("*")
if input == setpass then
	print ("Access Granted for a limited time, better hurry!")
	rs.setOutput(redstoneOutputSide, true)
	sleep(doortime)
	os.shutdown()
elseif input == adminpass then
	print ("Door will remain open, remember to close!")
	rs.setOutput(redstoneOutputSide, true)
	write "Passcode: "
	input2 = read("*")
	if input2 == adminpass then
		rs.setOutput(redstoneOutputSide, false)
		os.shutdown()
	else
		textutils.slowPrint ("Incorrect Admin passcode, force-closing door!")
		sleep(3)
		os.shutdown()
	end
elseif input == rootpass then
	textutils.slowPrint ("Welcome, you may now edit the programs/files on this computer!")
	term.clear()
	os.pullEvent = pullEvent
	print ("Root permissions enabled")
	return
else
	print ("Uhhm... Wrong password..")
	sleep(2)
	os.shutdown()
end
end
ChunLing #2
Posted 19 February 2013 - 11:18 PM
Put all the variables that you want to be configurable into a table, and load that table from a file (use textutils.unserialize()). If the file handle is nil, or textutils.unserialize() doesn't return a table, then have the user enter all those values, put them in the table, and save that table in the file (using textutils.serialize()).

I'm not sure why you use os.shutdown() combined with a while true do loop. Is that just to shut off the redstone outputs so that the door closes? It isn't terrible for something used occasionally (like a password locked door), but os.shutdown() and the necessity of then restarting the computer ends up as a pretty expensive operation, so at least consider revising it.

If doortime is not a number, then it must be nil. You can ensure that it is a number by revising the assignment like so:
doortime = tonumber(read())  or 7 -- your previous default time
Or you could put the assignment in a repeat loop until type(doortime) == "number".