Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local surface = dofile("/surface")
- globalPort = 5296
- localId = "cf3357aaeb3ccdd8b4ab54b9f8ccdf3a"
- modem = peripheral.find("modem")
- modem.open(globalPort)
- database = {
- {name="test", id=1,}
- }
- console = {}
- buttons = {}
- selected = nil
- local surf = surface.create(51,19,colors.black)
- local bg = surface.create(102,57,colors.black)
- commands = {
- alert = function(input)
- assert(type(input) == "string", "helloWorld expects a string for its input")
- print("ALERT: " .. input)
- return true
- end,
- updateDb = function(db)
- assert(type(db) == "table", "updateDB expects a table for its input")
- database = db
- end,
- updateStatus = function(id, status)
- assert(type(id) == "number", "updateStatus expects an int for its input \"id\"")
- assert(type(status) == "table", "updateStatus expects a table for its input \"id\"")
- database[id]=status
- end,
- report = function(id)
- assert(type(id) == "number", "report expects an int for its input")
- commandRunner("pong", id, {localId})
- end
- }
- function commandParser(event, side, channel, replyChannel, message, distance)
- if message.cmd ~= nil and message.args ~= nil then
- newLine = (message.cmd .. " " .. message.tgt)
- for i,arg in pairs(message.args) do
- newLine = (newLine .. " " .. tostring(arg))
- end
- addLnCon(newLine)
- end
- if commands[message.cmd] ~= nil and message.args ~= nil then
- ran, err = pcall(commands[message.cmd], table.unpack(message.args))
- if not ran then
- print(("Error in \"%s\": %s"):format(message.cmd, err))
- end
- else
- print(("No command named \"%s\" found"):format(message.cmd))
- end
- end
- function commandRunner(command, target, arguments)
- assert(type(command) == "string", "commandRunner expects a string for its input \"command\"")
- assert(type(target) == "number", "commandRunner expects a number for its input \"target\"")
- assert(type(arguments) == "table", "commandRunner expects a table for its input \"arguments\"")
- modem.transmit(5296,5296, {cmd=command, tgt=target, args=arguments})
- end
- function checkButtons(x,y)
- for i,button in pairs(buttons) do
- if x>=button.tl[1] and y>=button.tl[2] and x<=button.br[1] and y<=button.br[2] then
- button.fun(i)
- end
- end
- end
- function checkEvents()
- while true do
- event = {os.pullEvent()}
- if event[1] == "modem_message" then
- commandParser(table.unpack(event))
- return
- elseif event[1] == "mouse_click" then
- checkButtons(event[3], event[4])
- return
- end
- end
- end
- function drawBg()
- bg:drawLine(26,0,26,55, colors.gray)
- bg:drawLine(26,0,100,0, colors.gray)
- bg:drawLine(100,0,100,55, colors.gray)
- bg:drawLine(100,55,26,55, colors.gray)
- bg:drawLine(0,0,0,55, colors.gray)
- bg:drawLine(0,0,26,0, colors.gray)
- bg:drawLine(0,55,26,55, colors.gray)
- end
- function drawCon()
- for line, value in pairs(console) do
- surf:drawString(value, 14, 16-line, colors.black, colors.gray)
- end
- surf:drawString("CMD > ", 14,17, colors.black, colors.purple)
- surf:drawString("Network Feed", 25,0, colors.black, colors.purple)
- end
- function addLnCon(input)
- table.move(console, 1, 14, 2, console)
- console[1]=string.sub(input, 1, 36)
- end
- function consoleInput()
- input = read()
- addLnCon(input)
- input = input .. " "
- cmd = ""
- args = {}
- i = 1
- for w in input:gmatch("(.-) ") do
- if tonumber(w) ~= nil then
- w = tonumber(w)
- end
- if i==1 then
- cmd = w
- else
- table.insert(args, w)
- end
- i=i+1
- end
- commandRunner(cmd, 0, args)
- end
- function drawFloors()
- surf:drawString("Floors:", 1, 0, colors.black, colors.purple)
- i=1
- lFloor=nil
- for i,floor in pairs(database) do
- if floor.cur == localId then
- lFloor = i
- end
- end
- for id,floor in pairs(database) do
- if id == lFloor then
- surf:drawLine(1,2,11,2,colors.purple)
- surf:drawString(floor.name,2,2,colors.purple,colors.black)
- else
- surf:drawLine(1,i*2+2,11,i*2+2,colors.gray)
- buttons[floor.name] = {tl={2,i*2+3}, br={12,i*2+3}, fun=function(name) commandRunner("getFloors", -1, {}) selected=name end}
- surf:drawString(floor.name, 2,i*2+2, colors.gray, colors.lightGray)
- i=i+1
- end
- end
- end
- function go()
- dep=nil
- dest=nil
- for i,floor in pairs(database) do
- if floor.cur == localId then
- dep = i
- end
- if floor.name == selected then
- dest = i
- end
- end
- if dep==nil then addLnCon("ERROR: Could not locate you!") return end
- if dest==nil then addLnCon("ERROR: Could not locate destination!") return end
- if dep==dest then
- addLnCon("ERROR: Cannot go to current floor!")
- return
- end
- addLnCon("requestTransport 0 " .. dep .. " " .. dest)
- commandRunner("requestTransport", 0, {dep, dest})
- commandRunner("getFloors", 0, {})
- end
- function confirmation()
- confirm = surface.create(21, 6, colors.gray)
- confirm:drawString((" Confirm travel to"), 0,1,colors.gray,colors.lightGray)
- confirm:drawString("\""..selected.."\"", 2,2,colors.gray,colors.lightGray)
- confirm:drawLine(1,4,8,4,colors.lightGray)
- confirm:drawLine(11,4,19,4,colors.purple)
- confirm:drawString("cancel", 2,4,colors.lightGray,colors.black)
- confirm:drawString("confirm",12,4,colors.purple,colors.black)
- surf.overwrite=true
- surf:drawSurface(confirm, 15, 6)
- buttons["cancel"] = {tl={17,11},br={24,11}, fun=(function(name) selected=nil end)}
- buttons["confirm"] = {tl={27,11},br={34,11}, fun=(function(name) go() selected=nil end)}
- end
- function draw()
- buttons = {}
- surf:drawSurfaceSmall(bg, 0, 0)
- drawCon()
- drawFloors()
- if selected ~= nil then
- confirmation()
- surf:output()
- sleep(1)
- else
- surf:output()
- term.setTextColor(colors.lightGray)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(21,18)
- term.setCursorBlink(true)
- consoleInput()
- end
- end
- drawBg()
- commandRunner("getFloors", 0, {})
- while true do
- parallel.waitForAny(draw,checkEvents)
- end
Advertisement
Add Comment
Please, Sign In to add comment