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

Password Protected Door (Members only)

Started by Xab, 20 March 2014 - 03:46 AM
Xab #1
Posted 20 March 2014 - 04:46 AM
First post :)/>
Here the first program I've worked on for your reading pleasure. User enters their name and password, and the door opens if they're in the member/leader list. Users in the blacklist get something else..
pastebin: http://pastebin.com/qF2H6AUM
Spoiler

--[[
  A simple members-only password system for ComputerCraft doors.
  Leaders get a special message, while blacklisted users get ridiculed.
]]
-- vars a server would be interested in..
local members = {"Cow"}
local leaders = {"Pig", "Dog"}
local blacklist = {"Zombie", "Creeper", "Skeleton"}
local password = "chicken"
-- Concatenates two tables.
-- http://rosettacode.org/wiki/Array_Concatenation#Lua
function concatTables(aTable, target)
  table.foreach(aTable, function(i,v) table.insert(target,v) end)
  for i,v in next,target do io.write (v..' ') end
end
concatTables(leaders, members)
-- Returns location of value in an array if found.
-- Note: function is non-case sensitive.
function match(value, array)
  value = string.lower(value)
  for i in ipairs(array) do
	if value == string.lower(array[i]) then
	  return i
	end
  end
  return nil
end
function power(aSide, aTime)
  rs.setOutput(aSide, true)
  sleep(aTime)
  rs.setOutput(aSide, false)
end
function getInput(message, isPassword)
  write(message)
  if isPassword then return read('*') end
  return read()
end

function main()
  local openTime = 7
  local waitTime = 2
  local side = "left"
  local trapSide = "back"

  repeat repeat -- allows 'break' to act like a 'continue'
	term.clear()
	term.setCursorPos(1,1)
	local name = getInput("Username: ")
  
	if match(name, blacklist) then
	  print("Go away.")
	  power(trapSide, waitTime)
	  break
	end
  
	name = members[match(name,members)]
	if getInput("Password: ",true) ~= password or not name then
	  print("Unrecognized username or password")
	  rs.setOutput(side,false)
	  sleep(waitTime)
	  break
	end
	if match(name, leaders) then
	  print("Hail "..name.."!")
	else
	  print("Welcome "..name.."!")
	end
	power(side, openTime)
  until false until false
end
main()
Note: You can still ctrl+t to terminate the program, so don't trust your valuables with it just yet.
awsmazinggenius #2
Posted 20 March 2014 - 07:03 AM
Anti Ctrl+T code:

os.pullEvent = os.pullEventRaw
ATMunn #3
Posted 31 March 2014 - 06:30 PM
Is this really a turtle program? Either way, it seems cool :)/>