32 posts
Posted 09 November 2014 - 12:59 AM
As I'm sure some of you have seen, I have been trying very hard recently to create some kind of system that allows users to chose their own username and password to open a door. Now, this sounds very simple, but in actual fact, it's the single-most hardest thing I have ever attempted to code in ComputerCraft.
Detail:
I need to be able to have a password, for me as an Administrator, that I can input to then allow a user to input their chosen username and password.
The catch is, I then want it so that the user can input the username and password and get into their vault.
I have been trying for the past week to get this right, I've posed here a couple of times, and it did help, it helped a lot, but I just can't piece it all together…
Thank you for taking your time to help me :)/>
-Shadow
8543 posts
Posted 09 November 2014 - 01:14 AM
Please post your current code and give more detail on exactly which part of this project you're stuck on.
32 posts
Posted 09 November 2014 - 01:19 AM
The code is gone, we had a severe crash earlier on, and I lost all of it. Even the labeled disks.
I would give you something to start with, if I had anything.
Sorry
1023 posts
Posted 09 November 2014 - 01:39 AM
-snip
Is it just going to set a redstone signal on or off, or is it rednet or some other means of opening the vault. You could also just use the code that you posted in the last ask a pro you posted yesterday.
7083 posts
Location
Tasmania (AU)
Posted 09 November 2014 - 02:06 AM
The code is gone, we had a severe crash earlier on, and I lost all of it. Even the labeled disks.
I would give you something to start with, if I had anything.
Sorry
Well, there's
this and
this.
Edited on 09 November 2014 - 01:06 AM
1023 posts
Posted 09 November 2014 - 02:08 AM
Well I got bored and wrote up a quick little thing based on what I understood you wanted. I know that there are better ways to do this, but this might help you make one. Quickly wrote this, so I might have missed something.
http://pastebin.com/TipPLByT
Edited on 09 November 2014 - 01:10 AM
32 posts
Posted 09 November 2014 - 02:49 AM
You absolute star! Thank you so much!
32 posts
Posted 09 November 2014 - 03:03 AM
startup:6: attempt to index ? (a nil value)
The code it is referring to is
h.writeLine(pass)
As far as I'm aware that is fine…
1023 posts
Posted 09 November 2014 - 03:24 AM
startup:6: attempt to index ? (a nil value)
The code it is referring to is
h.writeLine(pass)
As far as I'm aware that is fine…
Line 6 in the pastebin link i posted is h.close(). I tested it, and it worked. The only way I can see it not working for you, but it is for me is if you changed something.
What is the username you tried to make?
Edited on 09 November 2014 - 02:25 AM
32 posts
Posted 09 November 2014 - 03:28 AM
I changed the "changeme" to "Shadow" and that's it
Even when I re-copy and paste the code, it does the same thing.
7083 posts
Location
Tasmania (AU)
Posted 09 November 2014 - 04:02 AM
I could be wrong, but wouldn't you need to ensure your target directory exists before you attempt to open files within it?
1023 posts
Posted 09 November 2014 - 04:06 AM
I could be wrong, but wouldn't you need to ensure your target directory exists before you attempt to open files within it?
From my experience from using the fs api when you use fs.write it creates the directory for you. I have tested this on both a CC 1.65 server, and a CC emulator and it worked on both… I am unable to recreate the error, which leads me to believe that the server he is using it on has something which causes it to error, such as a edited api.
edit:
Btw I should note that I got him to run this code on the server
h = fs.open("users/joe/password","w")
For k,v in pairs(h)
print(k)
end
and it errored with
bad argument: table expected, got nil
Edited on 09 November 2014 - 03:12 AM
7083 posts
Location
Tasmania (AU)
Posted 09 November 2014 - 04:17 AM
That second error indicates the exact same problem: h is nil. This is because fs.open() failed to create the file handle.
You can quickly determine whether the path is the problem by testing something like this:
h = fs.open("test1","w")
if h then
print("Got a handle!")
h.close()
else print("Failed to open file!") end
1023 posts
Posted 09 November 2014 - 04:21 AM
That second error indicates the exact same problem: h is nil. This is because fs.open() failed to create the file handle.
You can quickly determine whether the path is the problem by testing something like this:
h = fs.open("test1","w")
if h then
print("Got a handle!")
h.close()
else print("Failed to open file!") end
The fact still is that there is something about the server that he is on that is causing either the path not to work or it not to open. It has worked both of the places I have tested it. He has gotten off for night, and since he is the only one that can test it we can not really figure it out.
7083 posts
Location
Tasmania (AU)
Posted 09 November 2014 - 10:08 AM
I'm sure there's no great rush. :)/> If there's a difference in behaviour, it'll likely come down to the version of ComputerCraft in use, or the filesystem the server's running on.
32 posts
Posted 09 November 2014 - 02:44 PM
I'm back now :3
So I did a quick search before and, nothing. Apparently, no one else has ever had this problem before…
I will run this:
h = fs.open("test1","w")if h then print("Got a handle!") h.close()else print("Failed to open file!") endNow, that code came up with "Got a handle!" when I ran it, but I'm still getting the
startup:5: attempt to index ? (a nil value)
8543 posts
Posted 09 November 2014 - 05:18 PM
Okay, let's try this.
sleep(0)
local handle = fs.open("/test", "w")
if handle then
print(type(handle))
if type(handle) == "table" then
print("Got a handle")
handle.close()
end
else
print("No handle received")
end
32 posts
Posted 09 November 2014 - 05:24 PM
table
Got a handle
Seems to be working…
8543 posts
Posted 09 November 2014 - 05:26 PM
You were running the other one as startup. I added a sleep(0) to get around problems that used to occur in older versions of CC (though I don't remember problems with file handles). If you remove the sleep(0) at the top and reboot the computer (if that's startup), the problem may come back. You may want to try that to confirm.
32 posts
Posted 09 November 2014 - 06:40 PM
Nope, it's fine and the same.
32 posts
Posted 09 November 2014 - 07:02 PM
I added it to the code:
sleep(0)
side = "left"
function save(usr,pass)
h = fs.open("users/"..usr.."/password","w")
h.writeLine(pass)
h.close()
end
function check(usr,pass)
if usr == "changeme" then
return "admin"
end
if not fs.exists("users/"..usr.."/password") then
return "nousr"
end
c = fs.open("users/"..usr.."/password","r")
if pass == c.readLine() then
return true
else
return false
end
end
while true do
term.clear()
term.setCursorPos(1,1)
term.write("Input Username: ")
username = read()
term.setCursorPos(1,2)
term.write("Input Password: ")
password = read("*")
result = check(username,password)
if result == true then
redstone.setOutput(side,true)
sleep(2)
redstone.setOutput(side,false)
elseif result == "admin" then
term.clear()
term.setCursorPos(1,1)
term.write("Input new Account username: ")
newUser = read()
term.setCursorPos(1,2)
term.write("Input passowrd for account "..newUser..": ")
newPass = read("*")
save(newUser,newPass)
elseif result == false then
print("Invalid password")
sleep(2)
elseif result == "nousr" then
print("No such user")
sleep(2)
end
end
and I get the same error but with a different number.
8543 posts
Posted 09 November 2014 - 07:12 PM
Swap out your save function and see if it fixes it:
function save(usr, pass)
if not fs.exists(fs.combine("users", usr)) then
if not fs.exists("users") then
fs.makeDir("users")
end
fs.makeDir(fs.combine("users", usr))
end
local h = fs.open(fs.combine(fs.combine("users", usr), "password"), "w")
if h then
h.writeLine(pass)
h.close()
else
error("Couldn't open file handle!")
end
end
32 posts
Posted 09 November 2014 - 07:29 PM
Like this?
sleep(0)
side = "left"
function save(usr, pass)
if not fs.exists(fs.combine("users", usr)) then
if not fs.exists("users") then
fs.makeDir("users")
end
fs.makeDir(fs.combine("users", usr))
end
local h = fs.open(fs.combine(fs.combine("users", usr), "password"), "w")
if h then
h.writeLine(pass)
h.close()
else
error("Couldn't open file handle!")
end
end
function check(usr,pass)
if usr == "changeme" then
return "admin"
end
if not fs.exists("users/"..usr.."/password") then
return "nousr"
end
c = fs.open("users/"..usr.."/password","r")
if pass == c.readLine() then
return true
else
return false
end
end
while true do
term.clear()
term.setCursorPos(1,1)
term.write("Input Username: ")
username = read()
term.setCursorPos(1,2)
term.write("Input Password: ")
password = read("*")
result = check(username,password)
if result == true then
redstone.setOutput(side,true)
sleep(2)
redstone.setOutput(side,false)
elseif result == "admin" then
term.clear()
term.setCursorPos(1,1)
term.write("Input new Account username: ")
newUser = read()
term.setCursorPos(1,2)
term.write("Input passowrd for account "..newUser..": ")
newPass = read("*")
save(newUser,newPass)
elseif result == false then
print("Invalid password")
sleep(2)
elseif result == "nousr" then
print("No such user")
sleep(2)
end
end
That^ gives me the error:
startup:29: attempt to call nil
Line 29 is
return true
8543 posts
Posted 09 November 2014 - 07:32 PM
What inputs did you give it when you ran it? I'd suspect that the line count is slightly off between what's here and what's actually running on the computer, since that line would never throw that error. Can you verify that the code matches exactly, including line breaks? Use the pastebin program to upload it if necessary. I'd bet that the error is actually coming from line 28 in the code here. It would be good to know what inputs you're using to generate that error.
32 posts
Posted 09 November 2014 - 07:40 PM
I copy and pasted the code from the disk file in the server file manager, the line breaks should all be accurate. I'm using
changeme
admin - to log in, then it throws that error
pastebin
Edited on 09 November 2014 - 06:41 PM
1023 posts
Posted 09 November 2014 - 07:41 PM
I copy and pasted the code from the disk file in the server file manager, the line breaks should all be accurate. I'm using
changeme
admin - to log in, then it throws that error
pastebin
You deleted the check function from the program, which is what is causing it to throw that error.
Edited on 09 November 2014 - 06:42 PM
32 posts
Posted 09 November 2014 - 07:44 PM
function check(usr,pass)
if usr == "changeme" then
return "admin"
end
if not fs.exists("users/"..usr.."/password") then
return "nousr"
end
c = fs.open("users/"..usr.."/password","r")
if pass == c.readLine() then
return true
else
return false
end
end
The check function is still in there, I removed the save function and swapped it out for that Lyqyd put.
Edited on 09 November 2014 - 06:45 PM
1023 posts
Posted 09 November 2014 - 07:45 PM
I removed the save function…
-snip
Formatting messed up. Check the pastebin there is no check function in that.
edit:
Unless if that is not all of the code in the pastebin; the function was removed.
Edited on 09 November 2014 - 06:47 PM
8543 posts
Posted 09 November 2014 - 07:48 PM
I'd imagine the pastebin is accurate, since the error makes sense there (correct error, correct line).
32 posts
Posted 09 November 2014 - 07:50 PM
Yeah, I added it in(I have no idea why it went, I didn't(intentionally) remove it).
Thank you both so much, you're both life savers! :D/>