Guest User

Unworking door lock/unlock

a guest
Feb 10th, 2012
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. -- Passlock v0
  2. -- (C) 2012 User 'Advert' at http://www.computercraft.info/forums2/
  3. -- X11/mit licence (use/modify at your will, but please, leave credit where due)
  4.  
  5. -- Settings.
  6. local dConfig = {} -- Marking defaults, so we can revert to them easily.
  7.  
  8. do -- Because I like do statements.
  9.     dConfig["sUserPassword"]    = "open"
  10.     dConfig["sRootPassword"]    = "2good4u"
  11.     dConfig["nOpenTime"]        = 3 -- Number of seconds to keep the door open.
  12.     dConfig["nUserPassFailLock"]= 3 -- Number of user password fails before lock. (0 = disable, 1 = 1 fail, 2 = 2 fails, etc.) Reset at success (root can still login)
  13.     dConfig["nUserPassFailLockDuration"] = 15 -- Number of seconds to lock after nUserPassFailLock password fails
  14.     dConfig["bRootForceOpen"] = false -- Door is forced open by Root user.
  15.     dConfig["bLockAtStartup"] = true -- This will lock the computer for nUserPassFailLockDuration seconds at startup, to prevent bypassing of nUserLockPassFailLock.
  16.     dConfig["sRedstoneSide"] = "back" -- The side the redstone is at.
  17.     -- Stuff not stored here: door state, etc.
  18. end
  19. -- small helper function ;_;
  20. local function copyTable(o)
  21.     -- Will simply copy a table, non-recursively.
  22.     local t = {}
  23.     for k, v in pairs(o) do t[k] = v end
  24. end
  25.  
  26. local config = copyTable(dConfig)
  27.  
  28. -- Misc functions
  29.  
  30. local function mergeTable(t1, t2)
  31.     -- This function puts everyting in t2 into t1.
  32.     for k, v in pairs(t2) do
  33.         t1[k] = v
  34.     end
  35.     return t1 -- We return t1 because that's the table you'll want afterwards (even though it doesn't make a new table)
  36. end
  37.  
  38. -- Major functions
  39. -- Making functions local (I don't like messing up _G)
  40. local renderDisplay, checkEvents, loadFromFile, saveToFile
  41.  
  42. -- Core loop components
  43. function renderDisplay()
  44.     term.clear()
  45.     term.setCursorPos(1,1)
  46.     --TODO
  47. end
  48.  
  49. --
  50. function checkEvents()
  51.     -- Used in our loop to check for new OS events.
  52.     local event, p1, p2 = os.pullEvent()
  53.     --TODO
  54.     print("Event: ", event, "\n p1 & p2: ", p1, ",", p2)
  55.     if event == "char" then
  56.         if p1 == "x" then
  57.             error("User pressed 'x'. 'x' is a bad key to press.")
  58.         elseif p1 == "s" then
  59.             saveToFile()
  60.             loadFromFile()
  61.         end
  62.     end
  63.  
  64. end
  65.  
  66. function checkDoorTimer()
  67.  
  68. end
  69.  
  70. function checkUserLockTimer()
  71.  
  72. end
  73.  
  74. -- Door toggler.
  75. function toggleDoor(bState)
  76.     -- This will toggle the door, or set it's state to bState
  77.     if bState ~= nil then
  78.         bDoorState = bState
  79.     else
  80.         bDoorState = not bDoorState -- not negates the state in boolean: true, any data not nil -> false; false, nil -> true
  81.     end
  82.     return bDoorState -- Good practice to return result, in this case the door's state.
  83. end
  84.  
  85.  
  86.  
  87. -- Saving/loading
  88. do -- This just to make the code neater, really. (And so I can easily collapse the code in ScITE)
  89.  
  90.     function loadFromFile()
  91.         if not fs.exists("passlock-data") then return end -- We can't load nothing.
  92.         local err, res = pcall(dofile, "passlock-data") -- Yes, I'm doing this the easy and hard way.
  93.         if err then
  94.             print("Error loading configuration: \n", res)
  95.             print("Enter to continue...")
  96.             read()
  97.         end
  98.         mergeTable(config, res) -- This will ensure the defaults are there if a field is missing.
  99.     end
  100.  
  101.     -- Saving helper format strings
  102.     local saveAs = {}
  103.     saveAs.boolean = [[config[%q] = %s]]
  104.     saveAs.string = [[config[%q] = %q]]
  105.     saveAs.number = [[config[%q] = %d]]
  106.  
  107.     -- Saving function
  108.     function saveToFile()
  109.         -- This is the 'harder' part of the two :(
  110.  
  111.         local sData = [[local config = {} -- This file, generated by Passlock. Edit at your own risk.\n]]
  112.  
  113.         for k, v in pairs(config) do
  114.             local sDataType = type(v)
  115.             local sSaveHelper = saveHelper[sDataType]
  116.             if not sSaveHelper then
  117.                 -- We have problem, let's just print it, and continue without saving it.
  118.                 print("Warning: (saveToFile) unknown data type, in key: " .. k)
  119.             else
  120.                 -- No errors, save it.
  121.                 sData = sData .. string.format(sSaveHelper, k, v) .. "\n"
  122.             end
  123.         end
  124.  
  125.         sData = sData .. [[return config]] -- We need to return the config in order to retrieve it.
  126.  
  127.         -- Delete and save.
  128.         if fs.exists("passlock-data") then
  129.             fs.delete("passlock-data")
  130.         end
  131.         local hHandle = fs.open("passlock-data", "w")
  132.         if not hHandle then
  133.             -- We're pissed, let's error.
  134.             error("Unable to open file handle; call to saveToFile", 2)
  135.         end
  136.         hHandle:write(sData)
  137.         hHandle:close()
  138.     end
  139. end
  140.  
  141. -- Looping function
  142.  
  143. function doImportantStuff()
  144.     while true do
  145.         if bRootExit then -- Quit if user logged in as Root wishes to.
  146.             break
  147.         end
  148.         checkDoorTimer() -- Lock the door if it has been open long enough
  149.         checkUserLockTimer() -- Allow user to login again, if enough time has passed.
  150.         --renderDisplay() -- We need to see something.
  151.         checkEvents() -- Get our user input, or yet another timer event for updating the screen.
  152.         -- Doesn't seem like much, does it? D:
  153.     end
  154. end
  155.  
  156.  
  157.  
  158.  
  159. -- Loop
  160. while true do
  161.     local err, res = pcall(doImportantStuff)
  162.     if bRootExit then -- Quit if user logged in as Root wishes to.
  163.         break
  164.     end
  165.     if err ~= true then
  166.         print("error: ", string.format("%q", res))
  167.         print("enter to continue... (ctrl-t is SUPER EFFECTIVE right now!)")
  168.         read()
  169.     end
  170. end
Advertisement
Add Comment
Please, Sign In to add comment