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

[lua][question]HELP ASAP!

Started by predatorxil, 24 August 2012 - 01:38 AM
predatorxil #1
Posted 24 August 2012 - 03:38 AM
ok, so im trying to make a login program, i got it to work for one person, but i want multiple users, i heard one person say use a table, and im not sure of other ways. please help, this is for my server and i need it SOON. :D/>/>
THANKS
Grim Reaper #2
Posted 24 August 2012 - 04:04 AM
That was my suggestion, thanks…

You should write multiple users into a file in such a fromat:

username1
password1
username2
password2
username3
password3
etc.

If you had your file setup like so:

PaymentOption
test
predatorxil
suerpcomp
You have two users with two separate passwords. Here's how you might write some code to get that information into a table.

Here is a short script I wrote that should accomplish what you need. Please give me credit if you use it.
Spoiler

tUsers = {} -- The user table.
-- ^^ Format as such: tUsers[n] = { Username, Password }
local UserName = "" -- Holds the username entered by the user who is attempting to login.
local Password = "" -- Holds the password entered by the user who is attempting to login.
function CheckAgainstUsers( sName, sPass, UserTable ) -- Checks to see if the passed information matches an account in the user table.
-- Written by Grim Reaper (PaymentOption)
for i=1, #UserTable do -- Iterate through the user tbale.
  if sName == UserTable[i].Username then -- If the passed username matches one in the user table.
   if sPass == UserTable[i].Password then -- If the passed password matches the corrosponding username's password.
    return true -- Report a login match.
   else -- If the passed password does not match the corrosponding username's password.
    return false -- Report a login failure.
   end
  end
end

-- If the passed username does not match any username in the user table.
return false -- Report a login failure.
end
function LoadUsers( UserFilePath ) -- Loads the usernames and passwords from the passed file path.
-- Written by Grim Reaper (PaymentOption)
local UserTable = {} -- Setup a temporary table.
local File = io.open( UserFilePath, "r" ) -- Open the file in read mode.

-- Now that we have the file opened, lets get the length of the file in lines.
local FileLength = 0 -- This variable will hold the amount of lines in the file.
for line in File:lines() do -- Get an iterator function fro io:lines.
  FileLength = FileLength + 1 -- After every iteration add one to the length of the file in lines.
end
File:close() -- Close the file handle.

-- Now that we have the length of the file, lets get the information we need into a table.
if FileLength >= 2 then -- If the length of the file is greater than or equal to 2; there is at least one username and password.
  File = fs.open( UserFilePath, "r" ) -- Reopen the file with fs.open so we can use fs.readLine().
 
  for i=1, FileLength, 2 do -- Iterate through the file with an increment of two to the iterator every iteration.
   UserTable[#UserTable+1] = { Username = File.readLine(), Password = File.readLine() } -- Load the information by reading the file.
  end
else -- If the length of the file is not greater than or equal to 2; there is not at least one username and password.
  error( "User file format error." ) -- Throw an error.
end

return UserTable -- Return the now loaded user table.
end
tUsers = LoadUsers( ".UserFile" ) -- Load the tUsers table with the information stored in .UserFile.
print( "Username: " )
UserName = read()
print( "Password: " )
Password = read()
if CheckAgainstUsers( UserName, Password, tUsers ) then -- If the information entered is valid.
print( "Welcome " .. UserName .. "!" ) -- Welcome the user.
else -- If the information passed is not valid.
print( "Information was not found!" ) -- Reject the user.
sleep( 1.3 )
os.shutdown() -- Shutdown the machine.
end

Hope I helped! :D/>/>