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

Password Storage

Started by applesauce10189, 03 March 2014 - 02:17 AM
applesauce10189 #1
Posted 03 March 2014 - 03:17 AM
I'm using the fs API to store/change a password, for some reason later on in the code when I use an if statement and read to see if they put in the password correctly, it says it's wrong, I edited the code to print the password as soon as the password is stored into a variable but it doesn't print anything. Quick note, I'm putting the code into a spoiler because it's about 140 lines long.


Spoiler

--files list. best, admin.
local file = fs.open("admin", "w")
file.close()
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()


function menu()

if not fs.exists("rom/apple") then
  shell.run("pastebin get G67vP0VE apple")
end

os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Welcome to Apple's arcade! This is a heavy")
print("work in progress so many things may break,")
print("Here are your options.")
print("1. Guess that number! Type 'number'")
print("There's more to come.")
local select = read()

while true do
  if select == "number" then
    number()
  elseif select == "apples secret" then
    print("Hello, please insert config password.")
    local file = fs.open("admin", "w")
    select = read()
    file.writeLine(select)
    file.close()
    menu()
  elseif select == secret then
    print("Which config would you like to change or reset?")
    print("There's best for number,")
    print("In the future there will be more.")
    select = read()
    if select == "change best" then
      print("What should it be changed to?")
      select = read()
      select = tonumber(select)
      local file = fs.open("best", "w")
      file.writeLine(select)
      file.close()
    end
  elseif select == "reset best" then
    local reset = 1000
    local file = fs.open("best", "w")
    file.writeLine(reset)
    file.close()
    print("done.")
  elseif select == "change pass" then
    print("what should the password be?")
    select = read()
    local file = fs.open("admin", "w")
    file.writeLine(select)
    file.close()
    print("done.")
  else
    print("Sorry, but "..select.." isn't an option.")
    select = read()
  end
end
end
function number()

if not fs.exists("rom/apple") then
  shell.run("pastebin get G67vP0VE apple")
end

os.unloadAPI("apple")
os.loadAPI("apple")
apple.reset()
print("Thanks for playing guess that number!")
local file = fs.open("best", "r")
best = file.readLine()
file.close()
print("Would you like to use a custom range?")
print("(If you say no it will be 1-100)")
print("The current best is: "..best)
print("?  ")
term.setCursorPos(3,5)
local tries = 0

if not tonumber(best) then
  local best = 100
  local file = fs.open("best", "w")
  file.write(best)
  print(best)
  file.close()
end

while true do
  local answer = read()

  if answer == "yes" then
    print("Sorry this feature is a work in progress,")
    print("Please enter minimum then maximum")
    local min = read()
    local max = read()
    break
  elseif answer == "no" then
    print("Okay.")
    local min = 1
    local max = 100
    break
  else
    print("Pease say 'yes' or 'no'")
  end
end
local correct = math.random(1, 100)

while true do
  local guess = read()
  guess = tonumber(guess)
  tries = tries+1

  if guess > correct then
    print("Too high!")
  elseif guess < correct then
    print("Too low!")
  elseif guess == correct then
    print("juuussst riiight")
    print(tries)
    print(best)

    if tonumber(tries)<tonumber(best) then
      best = tries
      print(best)
      local file = fs.open("best", "w")
      file.write(best)
      file.close()
    end
    break
  else
    print("Sorry, there was a problem.")
    return
  end
end
end
menu()
Lyqyd #2
Posted 03 March 2014 - 03:44 AM

local file = fs.open("admin", "w")
file.close()
local file = fs.open("admin", "r")
local secret = file.readLine()
file.close()

The first two lines will always cause the admin file to be empty. You then attempt to read the contents of the file that you just caused to be empty.
Alice #3
Posted 03 March 2014 - 03:54 AM
Protip: use '.' in front of a filename and it won't be shown when using 'dir'