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

OS Personal Lock

Started by Darky_Alan, 17 July 2012 - 05:10 PM
Darky_Alan #1
Posted 17 July 2012 - 07:10 PM
Sup, the name's Alan and this is my first post.

Today I bring to you a Computer lock, the original idea was not mine.
To be fair I'll reference the original thread from where I got the idea, or to be more clear what I improved on. Click Here.

Alright so now that that's out of the way, basically I just started learning code syntax and I've had a close friend take me step by step through basic syntax and variable scopes, etc.
I've been messing around with it and cleaning it up, and I do believe I severely improved on the code, or atleast made it look much cleaner.

So give me your critique or any suggestions, here we go:

FORUM MADE THE SPACING LOOK WEIRD CLICK HERE FOR AN EASIER TO READ VERSION.

function clear()
		term.clear()
		term.setCursorPos(1,1)
end
function lock()
		local PASSWORD = "PASSWORD GOES HERE"
		local temp = os.pullEvent
		local function dkeys(swap)
				disable = {
				[0] = function() os.pullEvent = temp end,
				[1] = function() os.pullEvent = os.pullEventRaw end
				}
				disable[swap]()
		end
		clear()
		dkeys(1)
		print("Enter password:")
		write("> ")
		local input = read()
		if input == PASSWORD then
				clear()
				dkeys(0)
				print("Access granted!")
				textutils.slowPrint("Loading...")
				sleep(1)
				clear()
				print("WELCOME MESSAGE")
		else
				clear()
				print("Access denied!")
				textutils.slowPrint("Rebooting...")
				sleep(1)
				os.reboot()
		end
end
lock()
Lyqyd #2
Posted 17 July 2012 - 09:53 PM
Should use a while true do loop rather than rebooting. Should take three lines to disable termination (two at the top, one at the bottom to re-enable). Functions that are only called once should be inlined. Needless complexity is good for experimentation, but bad for final implementation.

Looks like an excellent start, though!
Darky_Alan #3
Posted 17 July 2012 - 11:30 PM
Should use a while true do loop rather than rebooting. Should take three lines to disable termination (two at the top, one at the bottom to re-enable). Functions that are only called once should be inlined. Needless complexity is good for experimentation, but bad for final implementation.

Looks like an excellent start, though!

I made it local for a plain reason, and even if it were not, it would server the same function.

I made it local because I want it to be a single function, this is but a piece of a bigger puzzle I'm working on. Thanks for your input though!
MysticT #4
Posted 17 July 2012 - 11:54 PM
I think he meant that you shouldn't create a function for that, since you can simply put those lines where you call them.
So, instead of:

dkeys(1)
You put:

os.pullEvent = os.pullEventRaw
Darky_Alan #5
Posted 18 July 2012 - 03:38 AM
I think he meant that you shouldn't create a function for that, since you can simply put those lines where you call them. So, instead of:
 dkeys(1) 
You put:
 os.pullEvent = os.pullEventRaw 
I kinda see what you mean, I'm currently building the spawn for our server so I'll go into the code in a few when I'm done and repost it.
Cranium #6
Posted 21 July 2012 - 10:01 PM
I tried your code for fun, but I got an error:
bios:206: [string "test"]:10: '(' expected
Noodle #7
Posted 24 July 2012 - 09:14 AM
BIG SPACING.
os.pullEvent = os.pullEventRaw is anti-terminate (You know that now..)
Could just put shell.run("clear") for the clear commands.
Make a var for WELCOME MESSAGE
IE:
Message = "blah" <———————
print(Message) – Custom Message ^
Darky_Alan #8
Posted 24 July 2012 - 09:14 AM
I tried your code for fun, but I got an error:
bios:206: [string "test"]:10: '(' expected

My bad! should be fixed now!
Sorry the spacing looks weird now, the forum spaced it weirdly.
Click here For a better highlighted/spaced representation.



BIG SPACING.
os.pullEvent = os.pullEventRaw is anti-terminate (You know that now..)
Could just put shell.run("clear") for the clear commands.
Make a var for WELCOME MESSAGE
IE:
Message = "blah" <———————
print(Message) – Custom Message ^

Yeah I learned about shell.run("clear") a few days ago, I might edit out the whole code later.
Noodle #9
Posted 24 July 2012 - 09:15 AM
^^ That's better and more readable :)/>/>
Darky_Alan #10
Posted 24 July 2012 - 09:17 AM
^^ That's better and more readable :)/>/>

Sublime is awesome :]
Noodle #11
Posted 24 July 2012 - 09:30 AM
I'm using it from your suggestion. Thx :)/>/>
Darky_Alan #12
Posted 24 July 2012 - 09:33 AM
I'm using it from your suggestion. Thx :)/>/>

The friend I was talking about in another thread recommended it, you can also change color schemes or download custom themes aswell as set colors for syntax to your liking.

It has a bunch of neat functions like simultanious editing where it selects kinda like when searching with F3, and allows you to change every single thing written the exact ssame at the same time, like for example if you changed your idea on the name of a variable or a function and are worried you might miss one somewhere in an extremely long code.
Noodle #13
Posted 24 July 2012 - 09:38 AM
It looks awesome (I'm using it right now).
It's GUI and it's features 0.0
Lyqyd #14
Posted 24 July 2012 - 04:00 PM
Using shell.run to do something that can be done with two lines (or even just declaring a local function and then using one line) is a bad idea. It slows your program down and consumes more resources just to try to save space. A local function is a better idea.
Noodle #15
Posted 25 July 2012 - 09:38 AM
Slows it down? Not really, I use it all the time and it has no slowdown.