8 posts
Posted 04 July 2012 - 02:09 PM
Hello, Im new to this Fourms, but i'm a big minecraft finatic, on the server i play on i am making a login in system, i have a computer setup like this
[CD]
[CD][CP]
i want the help to code for a database type of this, in the back disk drive will have a floppy called "DataBase". on this floppys will be usernames and passwords.
like this:
User1 = Takedown12
Pass1 = password
in my code i was woundering if it possible to read the variable "User1" from the floppy and compare it to the input on the console, same this with the password, but another big trick is to scan multiple names from the floppy
is this possible???
113 posts
Posted 04 July 2012 - 02:45 PM
Yes, but I don't know how. I believe you use tables to do this. I don't know how though, so look at other peoples coding.
286 posts
Location
Bonn Germany
Posted 04 July 2012 - 03:07 PM
Thebinformation could be saved in a CSV style (comma splitten value) you can load it in tables on startup and cyclr through them qhen looking for the login data. This woud be way easier. Cant give code NOW since less time but will be back this evening. Keep waiting ir find someone else to answer
1604 posts
Posted 04 July 2012 - 03:15 PM
You can make the file like this:
User1: Password1
User2: Password2
…
UserN: PasswordN
And then read it in a table like this:
local function loadUsers()
local file = fs.open("/disk/database", "r")
if file then
local t = {}
local line = file.readLine()
while line do
local usr, pass = string.match(line, "(.+): (.+)")
if usr and pass then
t[usr] = pass
end
line = file.readLine()
end
file.close()
return t
end
return nil
end
It returns a table containing the usernames as keys and the passwords as it's values, something like:
t = {
["User1"] = "Password1",
["User2"] = "Password2",
…
["UserN"] = "PasswordN"
}
This is just an example, you might need to change it to work with your system.
I use something like this for my os, but the passwords are saved as a sha256 hash.
8 posts
Posted 04 July 2012 - 04:52 PM
Thanks mystic, but although i have coded before in java, im new to lua, could u dumm it down a bit?
1604 posts
Posted 04 July 2012 - 05:16 PM
Here's some comments explaining the code:
local function loadUsers()
local file = fs.open("/disk/database", "r") -- open the file for read
if file then -- check if the file is open
local t = {} -- create a new table to store the usernames and passwords
local line = file.readLine() -- read a line from the file
while line do -- while there's a line
local usr, pass = string.match(line, "(.+): (.+)") -- get the username and password
if usr and pass then -- if there's a username and password (ie, the line was in the right format)
t[usr] = pass -- store the username and password in the table
end
line = file.readLine() -- read the next line from the file
end
file.close() -- close the file
return t -- return the table
end
return nil -- an error occurred while opening the file, return nil to indicate error
end
You can then use the function like:
local t = loadUsers()
local usr = read() -- get username
if t[usr] ~= nil then -- check if the user exists
local pass = read("*") -- get password
if t[usr] == pass then -- check if the password is correct
-- username and password correct
else
-- wrong password
end
else
-- wrong username
end
8 posts
Posted 04 July 2012 - 08:22 PM
the first bach of code should that be on the floppy?
1604 posts
Posted 04 July 2012 - 08:30 PM
No, you need to include a function like that in the login program.
It's just an example, you need to write your own function to do this. But to do it, you need to understand what it does.
8 posts
Posted 04 July 2012 - 09:50 PM
can u explain what u gave me is it similar becuase i dont know where to add or what
1604 posts
Posted 04 July 2012 - 09:54 PM
It's a function, a block of code that can be executed. To call a function use:
<FunctionName>(<arguments>)
The rest of the code is pretty well commented, if you don't understand it, you should read more about lua and programming.
8 posts
Posted 04 July 2012 - 10:37 PM
It works, now all i have to do is tweek it
8 posts
Posted 05 July 2012 - 12:43 AM
Mystic, just a question how would i distinguish the back disk
8543 posts
Posted 05 July 2012 - 12:49 AM
I'm not MysticT, but you would use the
peripheral API.
8 posts
Posted 05 July 2012 - 12:51 AM
i figured it out
8 posts
Posted 05 July 2012 - 03:43 AM
My Final Result, Thanks everyone
shell.run("clear")
local function loadUsers()
local file = fs.open("/disk/database", "r") -- open the file for read
if file then -- check if the file is open
local t = {} -- create a new table to store the usernames and passwords
local line = file.readLine() -- read a line from the file
while line do -- while there's a line
local usr, pass = string.match(line, "(.+): (.+)") -- get the username and password
if usr and pass then -- if there's a username and password (ie, the line was in the right format)
t[usr] = pass -- store the username and password in the table
end
line = file.readLine() -- read the next line from the file
end
file.close() -- close the file
return t -- return the table
end
return nil -- an error occurred while opening the file, return nil to indicate error
end
print " Welcome to SRFC"
print " "
print "Booting Login System"
print " "
print " Loading "
textutils.slowPrint ("=====================",8)
print " "
shell.run("Clear")
print "Login Verification Please"
print " "
write("Login: ")
local t = loadUsers()
local usr = read() -- get username
if t[usr] ~= nil then -- check if the user exists
write("Password: ")
local pass = read("*") -- get password
if t[usr] == pass then -- check if the password is correct
print("Please Insert Your ID Card")
print(" ")
print("you have 10 seconds")
sleep(10)
else
-- wrong password
shell.run("clear")
rs.setOutput("bottom",true)
sleep(4)
rs.setOutput("bottom",false)
os.shutdown()
end
elseif usr == "123" then
exit()
else
shell.run("clear")
rs.setOutput("bottom",true)
sleep(4)
rs.setOutput("bottom",false)
os.shutdown()
end
pass = "KeyCard"
--KeyCard
print ("Loading ID Card")
textutils.slowPrint ("===============",5)
shell.run("clear")
if peripheral.isPresent("left") == true then
if disk.getLabel("left") == pass then
disk.eject("left")
print ("+++++++++++++++++++++++++++++++")
print ("+Identification Card Confirmed+")
print ("+++++++++++++++++++++++++++++++")
rs.setOutput("top",true)
sleep(12)
rs.setOutput("top",false)
os.shutdown()
end
shell.run("clear")
rs.setOutput("bottom",true)
sleep(4)
rs.setOutput("bottom",false)
os.shutdown()
end