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

Door locking program with security login

Started by Master_crab01, 18 August 2015 - 11:18 PM
Master_crab01 #1
Posted 19 August 2015 - 01:18 AM
Hey im new to the forums, a mate gave me some code to do with a door locking program and i have been trying to integrate it into my computer security program.

However because the PC reboots it resets the counter i have been trying to implement.
I am completely new to Computer craft and have like 3 days experience, im looking to improve my knowledge and skills, im not overly bad as i have a backgroud in java.

But if i could get some help that would be great here is the code, what i want is everytime you use the door with a passworded disk it opens resets and then you have to login to the pc again to repeat, after the first login i wish it to keep looping through the door opening code.

P,s im also having issues with a disk without the passcode breaks the program, any help would be great.


os.pullEvent = os.pullEventRaw
term.clear()
term.setCursorPos(1,1)
print("Crab Computer Security 1.1")
print("Username: Master Crab")
write("Password: ")
function pass()
t= io.read()
if t == "Monopoly" then
print ("Access Granted.")
sleep(2)
term.clear()
term.setCursorPos(1,1)
else
print("Incorrect Login Details.")
sleep(1)
term.clear()
term.setCursorPos(1,1)
print("Crab Computer Security 1.1")
print("Username: Master Crab")
write("Password: ")
pass()
end
end
pass()

end –end if

local oside=""
local pass=""
local config="config"
local sides={"front","back","left","right","top","bottom"}
local info={}
local default_state=nil
local delay=0
local event={}
local event2={}
local state=nil
term.clear()
term.setCursorPos(1,1)

local function lockout()
os.pullEvent=os.pullEventRaw
print("hacking attempt detected!")
print("deadlocked for 30 seconds")
for i=30,0,-1 do
sleep(1)
print(tostring(i).."…")
end
print("unlocking..")
sleep(3)
for i,side in ipairs(sides) do
disk.eject(side)
end
end

local function setstr(file,input) –writes to file from a var
local f=fs.open(file,"a")
f.writeLine(input)
f.close()
end

local function mkfile(file) –creates a new file, deletes original first
shell.run("delete "..file)
local f=fs.open(file,"w")
f.close()
end

local function sidevalid(str) –checks a string to ensure that a correct side name was used
for i2 = 1, 6 do
if str:lower() == sides[i2] then return true end
end

return false
end

local function sideerror() –self explanatory
print([[side entered incorrect, sides can only be:
top,bottom,left,right,front or back
please enter the correct side]])
end

local function conf()
mkfile(config)
print("please enter a name for this lock")
local name=read()
shell.run("label set "..name)
print("enter redstone output side for this lock")
local rside=read()
if not sidevalid(rside) then
repeat
sideerror()
rside = read()
until sidevalid(rside)
setstr(config,rside)
elseif sidevalid(rside) then
setstr(config,rside)
end
print("by default. On:true or Off:false, type true or false")
state=read()
if state== "true" then
state=true
setstr(config,state)
elseif state =="false" then
state=false
setstr(config,state)
else repeat
print("invalid response, please only type true or false!")
state=read()
until (state=="true" or "false")
setstr(config,state)
end
print("press space to enter password")
print("or simply insert preconfigured passkey into disc slot")
event2= {os.pullEvent()}
if event2[1]=="key" and event2[2]==57 then
print("password entry: please enter your password")
local pass=read("*")
elseif event2[1]=="disk" then
if disk.hasData(event2[2]) then
sFile = "disk/pass"
hRead = assert(fs.open(sFile,"r"))
sPass = hRead.readLine()
hRead.close()
setstr(config,sPass)
disk.eject(event2[2])
os.reboot()
counter = counter + 1
end
end
end

local function getvars(file) –// Localize the function too, define it before we use it in 'startup' function
local f = fs.open(file,"r")
for i = 1,10 do
info = f.readLine()
end
f.close()
end

local function startup()
getvars(config)
pass=info[3]
oside=info[1]
default_state=info[2]
end

local function state_invert()
if default_state then return false
else return true
end
end

local function state_check()
if default_state=="true" then
default_state=true
return true
else
default_state=false
return false
end
end

if not fs.exists(config) then
conf()
else
startup()
end

rs.setOutput(oside,state_check())

while true do
event = {os.pullEvent()}
if event[1]=="disk" then
if disk.hasData(event[2]) then
sFile = "disk/pass"
hRead = assert(fs.open(sFile,"r"))
sPass = hRead.readLine()
hRead.close()
if sPass == pass then
rs.setOutput(oside,state_invert())
disk.eject(event[2])
sleep(3)
os.reboot()
else
disk.eject(event[2])
os.reboot()
end
else
disk.eject(event[2])
os.reboot()
end
end
if event[1]=="key" then
if event[2]==29 or event[2]==157 then
lockout()
os.reboot()
elseif event[2]==22 then
print("lock Killer initiated, press 1 to confirm or 2 to reboot")
event={os.pullEvent("key")}
if event[2]==2 then
print("Killing Lock..")
sleep(1)
shell.run("label set Broken Lock")
shell.run("delete startup")
shell.run("delete config")
os.reboot()
elseif event[2]==3 then
os.reboot()
elseif event[2]==29 or event[2]==157 then
lockout()
os.reboot()
end
end
end
end
KingofGamesYami #2
Posted 19 August 2015 - 04:37 AM
1. Don't reboot the computer. Then your counter won't reset.

2. Basically, the only way to get around this issue is to have a disk/startup file on a higher-priority side. I'm not sure which side of the computer has the highest priority, but some simple tests with a couple of disk drives and different startup files should work to figure it out.

3. [code] tags will make your code more readable. If it's long, you can put it on pastebin. (I'd consider your code to be long, myself)
Edited on 19 August 2015 - 02:38 AM
Lyqyd #3
Posted 19 August 2015 - 05:56 AM
The priority is whatever order rs.getSides() returns. In older versions, top is first, but in newer versions, bottom is first. I'm not sure when exactly it changed, but I suspect it was when Dan added the bundled cable stuff to the ComputerCraft API.

Of course, in newer versions, if a wired modem is present earlier in the order returned by rs.getSides() than the first disk drive, drives attached to the wired network as peripherals will have precedence over locally connected drives, since the modem had a higher priority. Priority of devices within the wired network depends on the order the getNamesRemote method on the modem returns them in.
HPWebcamAble #4
Posted 19 August 2015 - 06:05 AM
A major difference between Lua and Java is that in Lua, functions must be defined before they are called.
Because of this, you might want to define all your functions together, then have the code that executes the program.

Also, indenting really helps make your code more readable.

Also also, just glancing at the code, there are a few things you can improve, like this function:

local function state_invert()
if default_state then return false
else return true
end
end
That can be shortened to:

local function state_invert()
  return not default_state
end
Edited on 19 August 2015 - 04:06 AM
Master_crab01 #5
Posted 24 August 2015 - 08:44 AM
Ok thanks for all for posting, i appreciate it, what im doing is posting a pastebin link that way you can see the code formatted properly.

http://pastebin.com/bfcdifBn

Its formatted for lua it should be much more readable now.

I also forgot to mention the error i get when i put the disk into the drive that doesn't belong i recieve the error

startup 179: assertion failed

Which is this section


while true do
event = {os.pullEvent()}
if event[1]=="disk" then
  if disk.hasData(event[2]) then
   sFile = "disk/pass"
   hRead = assert(fs.open(sFile,"r"))
   sPass = hRead.readLine()
   hRead.close()
   if sPass == pass then
    rs.setOutput(oside,state_invert())
    disk.eject(event[2])
    sleep(3)
    os.reboot()
   else
    disk.eject(event[2])
    os.reboot()
   end
  else
   disk.eject(event[2])
   os.reboot()
  end
end

And also how would i get the program to loop if i remove the reboot command from the second program?? wouldn't it just keep cycling through?

As for the rs.side() i will try putting the disk drive on the top of the computer and see if that fixes something, just like you all suggested.

also


A major difference between Lua and Java is that in Lua, functions must be defined before they are called.
Because of this, you might want to define all your functions together, then have the code that executes the program.

Also, indenting really helps make your code more readable.

Also also, just glancing at the code, there are a few things you can improve, like this function:

local function state_invert()
if default_state then return false
else return true
end
end
That can be shortened to:

local function state_invert()
  return not default_state
end
Thanks for the suggestion i have implemented that peice of code.
If you cannot access it from pastebin let me know i will make it public.
Regards
Master Crab
Thanks guys
HPWebcamAble #6
Posted 24 August 2015 - 05:51 PM
posting a pastebin link that way you can see the code formatted properly.

Your formating is much better, so thats good.

Though I noticed a few things:
Spoiler

local counter = 0
if counter == 0 then
Counter will always be 0, this doesn't really do anything useful
Rebooting the computer resets all variables!


function pass()
  --# Password related things
  pass()
end
This is a recursive function. Which is fine in Java. Not in lua; it will cause a stack overflow if it runs more than 256 times.
Change it to a tail call

function pass()
  --# Password stuff
  return pass() --# WON'T cause a stack overflow
end

As for the error:
Spoiler
I also forgot to mention the error i get when i put the disk into the drive that doesn't belong i recieve the error

startup 179: assertion failed
Obviously, thats this line:

hRead = assert(fs.open(sFile,"r"))
Which means 'fs.open' didn't return anything.
I'm not sure why you use 'assert', you can just check the handle, like this:

local hRead = fs.open(sFile,"r")

local sPass --# Create 'sPass' variable
if hRead then
  sPass = hRead.readLine()
  hRead.close()
end

if pass == sPass then
  ...

And also how would i get the program to loop if i remove the reboot command from the second program?? wouldn't it just keep cycling through?
That's a good question.

There are 6 or 7 places where you use 'os.reboot', not all of them have the same solution.

I really tried my best to figure them all out, but I'm having trouble following the program's logic. Honestly, it would be quicker for me to just write the program myself.
Someone else might be willing to tackle it.