Advertisement
Kodos

Untitled

Oct 20th, 2015
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local os = require("os")
  4.  
  5. local ol = component.openlight
  6. local cb = component.chat_box
  7. local door = component.os_door
  8. local char_space = string.byte(" ")
  9. local running = true
  10. local char_pause = string.byte("p")
  11.  
  12. cb.setName("Dave")
  13.  
  14. local messages = {
  15.  
  16.   ["lights"] = {
  17.     --Syntax: ["keyword"] = {function, all, other, arguments}
  18.     ["on"] = {},
  19.     ["off"] = {}
  20.   },
  21.  
  22.   ["door"] = {
  23.     ["open"] = {openDoor}
  24.   },
  25.  
  26.   ["lamp"] = {
  27.     ["on"] = {ol.setColor, 0x00FF00},
  28.     ["off"] = {ol.setColor, 0xFF0000}
  29.   },
  30.  
  31.   ["core"] = {
  32.     ["current"] = {cb.say, "Herpderp"},
  33.     ["max"] = {cb.say, "Herpderp"}
  34.   },
  35.  
  36.   ["reactor"] = {
  37.     ["fuel"] = {}
  38.   },
  39.  
  40.   ["awesome"] = {
  41.     ["you're"] = {cb.say, "No, you're awesome."}
  42.   }
  43.  
  44. }
  45.  
  46. local function openDoor()
  47.   door.toggle()
  48.   os.sleep(5)
  49.   door.toggle()
  50.   return
  51. end
  52.  
  53. local function cardMenu()
  54.   term.clear()
  55.   term.setCursor(1,1)
  56.   print("Do you wish to add or remove a UUID? (A/r)")
  57.   choice = io.read()
  58.   if choice and (choice == "" or choice:sub(1,1):lower() == "a") then
  59.     choice = "a" -- I have no idea where I was going with this
  60.   else choice = "r"
  61.   end
  62.   return choice
  63. end
  64.  
  65. local function doChoice()
  66. end
  67.  
  68. local function authorized()
  69.   local env = {}
  70.   local config = loadfile("/etc/auth.dat", nil, env)
  71.   if config then
  72.     pcall(config)
  73.   end
  74.   return env.authorized
  75. end
  76.  
  77. local authorized = authorized() -- This one?
  78.  
  79. local function getFunction(tbl, msg)
  80.   if tbl[1] then
  81.     return tbl
  82.   end
  83.   for i,j in pairs(tbl) do
  84.     if type(i) == "string" then
  85.       -- print(i)
  86.       if msg:find(i) and type(j) == "table" then
  87.         return getFunction(j, msg)
  88.       end
  89.     end
  90.   end
  91. end
  92.  
  93. local function parseMessage(msg)
  94.   local fnc = getFunction(messages, msg)
  95.   if not fnc then return false, "no command found" end
  96.   return pcall(table.unpack(fnc))
  97. end
  98.  
  99. function unknownEvent()
  100. end
  101.  
  102. local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
  103.  
  104. function myEventHandlers.key_up(adress, char, code, playerName)
  105.   if (char == char_space) then
  106.     running = false
  107.     --[[elseif (char == char_pause) then
  108.     cardMenu()
  109.     doChoice()  ]]--
  110.   end
  111. end
  112.  
  113. function myEventHandlers.chat_message(adr,playerName,message)
  114.   if message:find("^%Dave, .+") then
  115.     local result, response = parseMessage(message:lower())
  116.   end
  117. end
  118.  
  119.  
  120.  
  121.  
  122. function myEventHandlers.magData(addr, playerName, data, UUID, locked)
  123.   for i = 1,#authorized do
  124.     -- print("Checking index #" .. i .. " for a match against: " .. UUID)
  125.     if UUID == authorized[i] then
  126.       print("Door opening for " .. playerName .. ".")
  127.       openDoor()
  128.       break
  129.     elseif UUID ~= authorized[i] then
  130.       print("Unauthorized Access attempt from " .. playerName .. "!")
  131.     end
  132.   end
  133. end
  134.  
  135. function handleEvent(eventID, ...)
  136.   if (eventID) then -- can be nil if no event was pulled for some time
  137.     myEventHandlers[eventID](...) -- call the appropriate event handler with all remaining arguments
  138.   end
  139. end
  140.  
  141. while running do
  142.   handleEvent(event.pull())
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement