With tables you pretty much have anything as a key, for example:
user = "fred"
database = {} -- our database table
database[user] = {} -- this is a table in a table so it could be also written like data.fred = {}
database[user].password = "pass"
database[user].menu = "menu key"
still not getting you. heres the whole program.
it doesnt serialise the table before writing, it splits it into "username password" and writes that, an when reading, it takes that an sticks it into a table.
i know serialising it would be easier, i just dont know how to get there. the top function is the actual program, the bottom function is just to decide what happens. function returns the username and true if login is successful
function clear()
term.clear()
term.setCursorPos(1,1)
end
function doLogin() --returns true if login succeeds, false if it fails
local sPasswdPath = "ACCOUNTS.F"
--check we have a login path, if not, what we enter will create the account instead of log in
if fs.exists( sPasswdPath ) then--Password exists so this is not the first run
local loginCount=0
while loginCount < 3 do
local tPasswd={}
local file = fs.open(sPasswdPath,"r")
local sLine = file:readLine()
while sLine do
for k, v in string.gmatch(sLine, "(%w+)%s(%w+)") do
tPasswd[k]=v
end
sLine = file:readLine()
end
file:close()
local username,password="",""
clear()
print("terminal is locked. login.")
write("username: ")
username = read()
print(tPasswd[username])
write("password: ")
password = read()
if (tPasswd[username]==password) then --Login ok
return username,true
else --invalid login
loginCount=loginCount+1
print("Invalid login")
sleep(1)
end
end
return "",false
else--This is the first run, ask if they want to secure this box.
clear()
print("secure?")
local sText = read()
if (sText=="yes") then
local passwordMatch=false
local username,password,password2,menu="",""
while not passwordMatch do
write("username: ")
username = read()
write("password: ")
password = read()
write("password again: ")
password2 = read()
write("Menu: ")
menu = read()
if (password == password2) then
passwordMatch = true
else
print("Passwords do not match")
sleep(1)
end
end
--Our passwords match, store them for later.
tPasswd={}
tPasswd[username]=password
local file = io.open(sPasswdPath,"w")
for user,pass in pairs(tPasswd) do
file:write(user .. " " .. pass .."\n")
end
file:close()
return username,true
else --Do not secure
local file = io.open(sPasswdPath,"w")
file:close()
return "",true
end
end
end
-- Do login code
function login()
local user, succ = doLogin() --calls doLogin in redowrks API (above because of how lua works)
if not succ then
--Login failed
term.clear()
term.setCursorPos(1,5)
print("login failed.\n\nLocking terminal for 15 seconds")
local bExit=false
os.startTimer(15)
while not bExit do
falt, sEvent, param = pcall(os.pullEvent)
if sEvent == "timer" then
bExit = true
end
end
os.shutdown()
else
print("logged in as: " .. user)
end
end
login()