Advertisement
kaibochan

remoteRS.lua

Mar 9th, 2023 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.40 KB | None | 0 0
  1. local remoteRSProtocol = "remoteRS"
  2.  
  3. local function throwError(sentFrom, parameters)
  4.     local errorMessage = parameters["errorMessage"]
  5.     error(errorMessage)
  6. end
  7.  
  8. local function selectItemFromList(sentFrom, parameters)
  9.     local itemList = parameters["itemList"]
  10.  
  11.     --cursor position of beginning of highlighted index text
  12.     --and end cursor position, so we can read whether user clicked
  13.     --on this selection or not
  14.     local itemSelectBounds = {}
  15.  
  16.     local firstCursorPos = {}
  17.     local secondCursorPos = {}
  18.     local currentTextColor = term.getTextColor()
  19.  
  20.     for index, item in ipairs(itemList) do
  21.         --print indexed list with index highlighted red
  22.         term.setTextColor(colors.red)
  23.  
  24.         firstCursorPos.x, firstCursorPos.y = term.getCursorPos()
  25.         term.write("["..index.."]")
  26.         secondCursorPos.x, secondCursorPos.y = term.getCursorPos()
  27.  
  28.         term.setTextColor(currentTextColor)
  29.  
  30.         print("\t"..item.displayName)
  31.  
  32.         table.insert(itemSelectBounds, index, {["left"] = firstCursorPos, ["right"] = secondCursorPos})
  33.     end
  34.  
  35.     print("Select an item from the list above")
  36.     local selection
  37.     local button, x, y
  38.  
  39.     --debug
  40.     for index, bound in ipairs(itemSelectBounds) do
  41.         print(index, bound.left.x, bound.right.x, bound.left.y, bound.right.y)
  42.     end
  43.  
  44.     while not selection do
  45.         _, button, x, y = os.pullEvent("mouse_click")
  46.  
  47.         --if user clicks left button then search for which item they clicked
  48.         --using the itemSelectBounds table
  49.         if button == 1 then
  50.             --debug
  51.             print(button, x, y)
  52.  
  53.             for index, bound in ipairs(itemSelectBounds) do
  54.  
  55.                 --check if mouse_click was within bounds of this item index
  56.                 if x >= bound.left.x and x <= bound.right.x
  57.                 and y >= bound.left.y and y <= bound.right.y then
  58.                     selection = index
  59.                     break
  60.                 end
  61.             end
  62.         end
  63.     end
  64.  
  65.     local message = "select "..selection
  66.     rednet.send(sentFrom, message, remoteRSProtocol)
  67. end
  68.  
  69. local function displayQuery(sentFrom, parameters)
  70.     local queryResult = parameters["queryResult"]
  71.  
  72.     for index, item in ipairs(queryResult) do
  73.         print(item.amount, item.displayName)
  74.     end
  75. end
  76.  
  77. local function display(sentFrom, parameters)
  78.     local toDisplay = parameters["toDisplay"]
  79.     print(toDisplay)
  80. end
  81.  
  82. --create parameter objects (a pair of name and type)
  83. local function parameter(name, type)
  84.     return {["name"] = name, ["type"] = type}
  85. end
  86.  
  87. local actions = {
  88.     ["error"] = {
  89.         ["function"] = throwError,
  90.         ["parameters"] = {
  91.             parameter("errorMessage", "string")
  92.         },
  93.     },
  94.     ["select"] = {
  95.         ["function"] = selectItemFromList,
  96.         ["parameters"] = {
  97.             parameter("itemList", "table")
  98.         },
  99.     },
  100.     ["query"] = {
  101.         ["function"] = displayQuery,
  102.         ["parameters"] = {
  103.             parameter("queryResult", "table")
  104.         },
  105.     },
  106.     ["display"] = {
  107.         ["function"] = display,
  108.         ["parameters"] = {
  109.             parameter("toDisplay", "string")
  110.         },
  111.     },
  112. }
  113.  
  114. local function openModem()
  115.     local modem = peripheral.find("modem")
  116.     local modemLocation = peripheral.getName(modem)
  117.    
  118.     rednet.open(modemLocation)
  119. end
  120.  
  121. --quotes are placed around parameters to ensure they are
  122. --grouped properly when parsed out by recieving parties
  123. local function packageArguments()
  124.     local message = arg[1]
  125.  
  126.     for index = 2, #arg do
  127.         message = message.." ".."\""..arg[index].."\""
  128.     end
  129.  
  130.     return message
  131. end
  132.  
  133. function main()
  134.     if not fs.exists("com.lua") then
  135.         error("com.lua is necessary for the operation of this program")
  136.     end
  137.  
  138.     os.loadAPI("com.lua")
  139.  
  140.     --should have at least one argument besides the program call
  141.     if #arg == 0 then
  142.         error("Expected at least one argument: action")
  143.     end
  144.  
  145.     local message = packageArguments()
  146.  
  147.     openModem()
  148.  
  149.     rednet.broadcast(message, remoteRSProtocol)
  150.     local sentFrom, reply = rednet.receive(remoteRSProtocol, 5)
  151.    
  152.     if not reply then
  153.         return
  154.     end
  155.  
  156.     local action, parameters = com.parseMessage(actions, sentFrom, reply, remoteRSProtocol)
  157.    
  158.     if action and parameters then
  159.         actions[action]["function"](sentFrom, parameters)
  160.     end
  161. end
  162.  
  163. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement