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