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

Password Locker W/ Config Files Help

Started by DannySMc, 13 November 2013 - 01:46 PM
DannySMc #1
Posted 13 November 2013 - 02:46 PM
Okay so in essence I am making a program that will open a door by emitting a redstone pulse. Now I am using a config file called: ".uLockConf". Before you so anything the config is working and then API is correct. I keep getting an error saying:

BIOS:132: Expected Number

Now i don't understand this cause the actual program works fine, until I enter the password and hit enter, it gets to emitting the signal but then errors, now i thought it was probably something to do with the sleep() function as I am using:


sleep(config.redPulse)

but another coder has done the same and it works :S so yeah I am a bit stuck! Anyway here is the program code:

-- Simple Door Lock
-- Created by DannySMc
-- Version 1.1
-- Platform: Lua Virtual Machine
-- Security Method
oldEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
-- Check for API
if fs.exists("uapi") == false then
  print("Missing uAPI!")
  print("Attempting to download..")
  getGit = http.get("https://raw.github.com/dannysmc95/uprograms/master/uapi")
  getGit = getGit.readAll()
  file = fs.open("uapi", "w")
  file.write(getGit)
  file.close()
  print("Done!")
  sleep(0.5)
end
os.loadAPI("uapi")
-- Installer for Configuration File
if fs.exists(".uLockConf") == false then
  uapi.cs()
  print("Running Installer...")
  sleep(0.5)
  term.setCursorPos(1,3)
  term.write("Redstone output side: ")
  strSide = read()
  term.setCursorPos(1,5)
  term.write("Redstone pulse time: ")
  strPulse = read()
  term.setCursorPos(1,7)
  term.write("Terminal Label: ")
  strName = read()
  term.setCursorPos(1,9)
  term.write("Password for unlock: ")
  strPassword = read("*")
  term.setCursorPos(1,11)
  print("What is the name this program is saved as?")
  term.setCursorPos(1,12)
  term.write("Program Name: ")
  strProgName = read()

  -- Create Configuration File
  uapi.saveConfig({['progName']=strProgName, ['terminalName']=strName, ['redSide']=strSide, ['redPulse']=strPulse, ['lockPassword']=strPassword}, ".uLockConf")

  -- Startup File
  term.setCursorPos(1,14)
  print("Do you wish to create a startup file?")
  term.setCursorPos(1,15)
  repeat
  term.write("YES/NO: ")
  startupAnswer = read()
  until ((startupAnswer == "YES") or (startupAnswer == "NO"))

  if startupAnswer == "YES" then
	startFile = fs.open("startup", "w")
	startFile.writeLine('os.loadAPI("uapi")')
	startFile.writeLine('config = uapi.loadConfig(".uLockConf")')
	startFile.writeLine("shell.run(config.progName)")
	startFile.close()
	print("Install Complete!")
	sleep(0.5)
  else
	print("Install Complete!")
	sleep(0.5)
  end

  print("Rebooting the terminal...")
  sleep(1)
  os.reboot()
end
-- Load Configuration File
config = uapi.loadConfig(".uLockConf")
-- Password Lock Program
uapi.cs()
uapi.setCol("white", "blue")
print("												   ")
print("												   ")
uapi.printC(config.terminalName, 2, false, "white", "blue")
print("												   ")
print("												   ")
uapi.resetCol()
term.setCursorPos(1,19)
uapi.setCol("white", "blue")
print(" Password Lock -> Created By DannySMc -> Ver: 1.21 ")
uapi.resetCol()
term.setCursorPos(1,8)
repeat
term.write("Password: ")
password = read("*")
until((password == config.lockPassword))
if password == config.lockPassword then
  term.setCursorPos(1,10)
  print("Password Correct!")
  rs.setOutput(config.redSide, true)
  sleep(config.redPulse)
  rs.setOutput(config.redSide, false)
  sleep(0.5)
  os.reboot()
else
  term.setCursorPos(1,10)
  print("Password Incorrect!")
  sleep(1.5)
  os.reboot()
end

and here is the finished config file (AN EXAMPLE):

{["terminalName"]="Danny tester",["lockPassword"]="testing",["redSide"]="back",["progName"]="lock",["redPulse"]="5",}

Please help! Thanks
Edited on 13 November 2013 - 02:19 PM
jag #2
Posted 13 November 2013 - 03:30 PM
Ah yes I see the problem.
As you might or might not notice the sleep function gets a non-number which it dislikes.
Now look at your example save:

{["terminalName"]="Danny tester",["lockPassword"]="testing",["redSide"]="back",["progName"]="lock",["redPulse"]="5",}
You see the variable "redPulse"? It's value is set to the string value of 5 instead of the number equivalent version of 5.
Now the reason of this is that the read() function always return a string.
To fix this you can simply use tonumber() to transform "5" to 5.

Now if you enter "ballon" when it's expecting a number it will still save that and error later. So you should add a check for that like so:

strPulse = 0
repeat
	strPulse = tonumber(read())
until type(strPulse) == "number"
Good to know, tonumber() only accepts "perfect" numbers, i.e. "12", "35", "4899" etc. It does not accept strings with mixed numbers and other symbols for example "5$", "€2.5", "65 apples" etc.

If you don't know what the type() function does, its actually really simple; it returns the type of the variable sent to it.
Ex: type("crowbar") = "string", type(42) = "number", type( {1,2,{3.1,3.2}} ) = "table" etc.
Edited on 13 November 2013 - 02:34 PM
DannySMc #3
Posted 13 November 2013 - 03:38 PM
Ah yes I see the problem.
As you might or might not notice the sleep function gets a non-number which it dislikes.
Now look at your example save:

{["terminalName"]="Danny tester",["lockPassword"]="testing",["redSide"]="back",["progName"]="lock",["redPulse"]="5",}
You see the variable "redPulse"? It's value is set to the string value of 5 instead of the number equivalent version of 5.
Now the reason of this is that the read() function always return a string.
To fix this you can simply use tonumber() to transform "5" to 5.

Now if you enter "ballon" when it's expecting a number it will still save that and error later. So you should add a check for that like so:

strPulse = 0
repeat
	strPulse = tonumber(read())
until type(strPulse) == "number"
Good to know, tonumber() only accepts "perfect" numbers, i.e. "12", "35", "4899" etc. It does not accept strings with mixed numbers and other symbols for example "5$", "€2.5", "65 apples" etc.

If you don't know what the type() function does, its actually really simple; it returns the type of the variable sent to it.
Ex: type("crowbar") = "string", type(42) = "number", type( {1,2,{3.1,3.2}} ) = "table" etc.

Thanks so much you are literally my saviour, I actually thought the same, but i wasn't sure how to make it a number. Anyway thanks
jag #4
Posted 13 November 2013 - 03:40 PM
Thanks so much you are literally my saviour, I actually thought the same, but i wasn't sure how to make it a number. Anyway thanks
You're much welcome, glad I could help out ^_^/>