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

[Lua][Question] A registrer system

Started by stabby, 20 January 2013 - 04:32 AM
stabby #1
Posted 20 January 2013 - 05:32 AM
Hello all pros!

I'm working on a bank system where you enter your card and your pin.

So my idea is: Every card got an ID, and an unique pin. When you enter your pin and your card to the ATM machine the ATM machine is supposed to send the ID and the PIN to a server computer. The server computer will compare the pin and the ID with some kind of database. So all i want to do is a way for the server computer to add new users and reply to the atm machine with a boolean and a balance value.

I know how to do all of that except the registrer system.

Thanks for your answer!
ikke009 #2
Posted 20 January 2013 - 05:51 AM
what if you hold all user info as a table inside a main table..

userdata = {}
function register(username,password)
  table.insert(userdata,{
    user = username,
    pass = password
    balance = 0
  }
end

that way you could just have it register in an easy function if the username isnt found in the database
stabby #3
Posted 20 January 2013 - 06:11 AM
what if you hold all user info as a table inside a main table..

userdata = {}
function register(username,password)
  table.insert(userdata,{
	user = username,
	pass = password
	balance = 0
  }
end

that way you could just have it register in an easy function if the username isnt found in the database

Yea i'm pretty sure i'm supposed to use a table but i'm not skilled enough to save it on a file and then make the computer search for the user and all that.. Thats pretty much my problem
ikke009 #4
Posted 20 January 2013 - 07:45 AM
I never tried saving things on files so I dont know how to do that.. but searching for the user is pretty easy..

for i,#userdata do --goes trough all the excisting places in the userdata table
  local u = userdata[i] --makes it more comprehensable
  if u then --if userdata[i] is not nil
    if diskuser == u.user then --if the username on the disk == username in the userdata table
	  do stuff login or whatever
    end
  end
end

go play with that and see what you can achieve
mibac138 #5
Posted 20 January 2013 - 07:52 AM
@up

I think this don't work maybe i write some code :)/>


for i=1,#userdata, 1 do
  local u = userdata[i]
  if u then
    if diskuser == u.user then
		  --[[For example]] print("Access granted!")
    end
  end
end
ChunLing #6
Posted 20 January 2013 - 09:42 AM
Just index the table by user. Then you don't have to search the table using a loop, you just check and see if there is an entry indexed by that user.
remiX #7
Posted 20 January 2013 - 10:10 AM
Try this for example, using fs api to write/read to/from files.

Spoiler

invalidChars = "#@!`&amp;/,\\';=?><:\"|- )*"
userdata = {}
screenX, screenY = term.getSize()
function readUsers()
	if fs.exists("users") then
		file = fs.open("users", "r")
		l = file.readLine()
		while l do
			t = split(l)
			table.insert(userdata, { user = t[1], pass = t[2], balance = tonumber(t[3]) })
			l = file.readLine()
		end
		file.close()
	end
end

function writeUser(username, password, balance)
	file = fs.open("users", "a")
	file.writeLine(username .. " " .. password .. " " .. tostring(balance))
	file.close()
end

function register(username, password)
	-- Check to see if the username contains an invalid character
	local temp = ""
	for i = 1, #invalidChars do
		char = string.sub(invalidChars, i, i)
		if string.find(username, char) then
			temp = temp .. char
		end
	end
	if #temp > 0 then return false, "Invalid Character(s) " .. temp end
	
	if fs.exists("users") then
		file = fs.open("users", "r")
		l = file.readLine()
		while l do
			t = split(l)
			if string.lower(user) == string.lower(t[1]) and string.lower(pass) ~= string.lower(t[2]) then
				return false, "User already exists"
			elseif string.lower(user) == string.lower(t[1]) and string.lower(pass) == string.lower(t[2]) then
				return true, t[3]	
			end
			l = file.readLine()
		end
		file.close()
	end
	table.insert(userdata,{
		user = username,
		pass = password,
		balance = 0
	})
	writeUser(username, password, 0)
	return true, "Registered"
end

function validateUsername(user)
	
end

function split(_str)
	line = {}
	for word in string.gmatch(_str, "%w+") do
		table.insert(line, word)
	end
	return line
end

readUsers()

while true do
	term.clear()
	for i = 1, #userdata do
		term.setCursorPos(1, i)
		write(userdata[i].user .. " " .. userdata[i].pass .. " " .. userdata[i].balance)
	end
	term.setCursorPos(screenX - 11, 1)
	write("Username:")
	term.setCursorPos(screenX - 11, 3)
	write("Password:")
	term.setCursorPos(screenX - 11, 2)
	user = read()
	term.setCursorPos(screenX - 11, 4)
	pass = read("*")
	bRegistered, balance = register(user, pass)
	if bRegistered then
		if balance == "Registered" then
			register(user, pass)
			term.setCursorPos(1, 1)
			print("Thank you for registering, " .. user)
		elseif tonumber(balance) then
			term.clear()
			term.setCursorPos(1, 1)
			print("Welcome back " .. user .. "! Your current balance is " .. tonumber(balance))
		end
	else
		term.clear()
		term.setCursorPos(1, 1)
		if balance == "User already exists"  then
			print("Error: " .. balance)
		elseif string.find(balance, "Invalid Character") then
			print("Error: " .. balance)
		end
	end
	sleep(2)
end
dan14941 #8
Posted 20 January 2013 - 12:13 PM
do you want it to add people automatically?