Advertisement
kaibochan

serverRS.lua

Mar 9th, 2023 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.10 KB | None | 0 0
  1. --global peripherals to be used across the action functions
  2. local refindStorageBridge
  3. local inventoryManager
  4. local remoteRSProtocol = "remoteRS"
  5. local chestDirection = "north"
  6.  
  7. --create parameter objects (a pair of name and type)
  8. local function parameter(name, type)
  9.     return {["name"] = name, ["type"] = type}
  10. end
  11.  
  12. local function findMatchingItems(itemName)
  13.     local allItems = refindStorageBridge.listItems()
  14.     local matchingItems = {}
  15.  
  16.     for index, item in ipairs(allItems) do
  17.         if item.displayName:lower():find(itemName) then
  18.             local itemData = {
  19.                 ["name"] = item.name,
  20.                 ["displayName"] = item.displayName,
  21.                 ["amount"] = item.amount,
  22.             }
  23.             table.insert(matchingItems, itemData)
  24.         end
  25.     end
  26.  
  27.     return matchingItems
  28. end
  29.  
  30. local function evaluateSelection(selectionList, parameters)
  31.     local selection = parameters["selection"]
  32.     return selectionList[selection]
  33. end
  34.  
  35. local function subActions()
  36.     local actions = {
  37.         ["select"] = {
  38.             ["function"] = evaluateSelection,
  39.             ["parameters"] = {
  40.                 parameter("selection", "number")
  41.             },
  42.         },
  43.     }
  44.  
  45.     return actions
  46. end
  47.  
  48. local function request(sentFrom, parameters)
  49.     local actions = subActions()
  50.  
  51.     local itemName = parameters["itemName"]
  52.     local count = parameters["count"]
  53.  
  54.     local matchingItems = findMatchingItems(itemName)
  55.     local itemToSend
  56.    
  57.     --if more than one matching item, have user confirm which one
  58.     if #matchingItems > 1 then
  59.         local matchingItemsText = textutils.serialise(matchingItems)
  60.         local message = "select \""..matchingItemsText.."\""
  61.         rednet.send(sentFrom, message, remoteRSProtocol)
  62.  
  63.         local _, reply = rednet.receive(remoteRSProtocol, 15)
  64.        
  65.         --expected message: select [number]
  66.         local action, selectionParameter = com.parseMessage(actions, sentFrom, reply, remoteRSProtocol)
  67.  
  68.         if selectionParameter then
  69.             itemToSend = actions[action]["function"](matchingItems, selectionParameter)
  70.         end
  71.     elseif #matchingItems == 1 then
  72.         itemToSend = matchingItems[1]
  73.     else
  74.         local noItemsFoundText = "No items were found that match: "..itemName
  75.         local message = "display \""..noItemsFoundText.."\""
  76.         rednet.send(sentFrom, message, remoteRSProtocol)
  77.  
  78.         return
  79.     end
  80.  
  81.     itemToSend.count = count
  82.  
  83.     if itemToSend.count > itemToSend.amount then
  84.         local notEnoughItemsText = "There was not enough items to fulfill your request of "..itemToSend.count
  85.         local message = "display \""..notEnoughItemsText.."\""
  86.         rednet.send(sentFrom, message, remoteRSProtocol)
  87.     end
  88.  
  89.     print("itemToSend")
  90.     print(itemToSend.count, itemToSend.displayName)
  91.  
  92.     refindStorageBridge.exportItem(itemToSend, chestDirection)
  93.     inventoryManager.addItemToPlayer(chestDirection, itemToSend.amount, _, itemToSend.name)
  94. end
  95.  
  96. local function store(sentFrom, parameters)
  97.  
  98.     --debug message
  99.     print("action:\tstore")
  100.     print("parameters:")
  101.     for pName, pValue in pairs(parameters) do
  102.         print("\t"..pName..":\t"..pValue)
  103.     end
  104.  
  105. end
  106.  
  107. local function craft(sentFrom, parameters)
  108.  
  109.     --debug message
  110.     print("action:\tcraft")
  111.     print("parameters:")
  112.     for pName, pValue in pairs(parameters) do
  113.         print("\t"..pName..":\t"..pValue)
  114.     end
  115.  
  116. end
  117.  
  118. local function query(sentFrom, parameters)
  119.     local itemName = parameters["itemName"]
  120.  
  121.     local matchingItems = findMatchingItems(itemName)
  122.    
  123.     local message
  124.     if #matchingItems ~= 0 then
  125.         local matchingItemsText = textutils.serialise(matchingItems)
  126.         message = "query \""..matchingItemsText.."\""
  127.     else
  128.         local noItemsFoundText = "No items were found that match: "..itemName
  129.         message = "display \""..noItemsFoundText.."\""
  130.     end
  131.  
  132.     rednet.send(sentFrom, message, remoteRSProtocol)
  133. end
  134.  
  135. local function mainActions()
  136.     local actions = {
  137.         ["request"] = {
  138.             ["function"] = request,
  139.             ["parameters"] = {
  140.                 parameter("itemName", "string"),
  141.                 parameter("count", "number"),
  142.             },
  143.         },
  144.         ["store"] = {
  145.             ["function"] = store,
  146.             ["parameters"] = {
  147.                 parameter("itemName", "string"),
  148.                 parameter("count", "number"),
  149.             },
  150.         },
  151.         ["craft"] = {
  152.             ["function"] = craft,
  153.             ["parameters"] = {
  154.                 parameter("itemName", "string"),
  155.                 parameter("count", "number"),
  156.             },
  157.         },
  158.         ["query"] = {
  159.             ["function"] = query,
  160.             ["parameters"] = {
  161.                 parameter("itemName", "string"),
  162.             },
  163.         },
  164.     }
  165.  
  166.     return actions
  167. end
  168.  
  169. local function openModem()
  170.     local allPeripherals = peripheral.getNames()
  171.  
  172.     --search for specifically a wireless modem, not a wired one
  173.     local modem
  174.     local modemLocation
  175.     for index, peri in ipairs(allPeripherals) do
  176.         if peripheral.hasType(peri, "modem") then
  177.             modem = peripheral.wrap(peri)
  178.             if modem.isWireless() then
  179.                 modemLocation = peri
  180.             end
  181.         end
  182.     end
  183.  
  184.     if not modemLocation then
  185.         error("Wireless modem is necessary for the operation of this program")
  186.     end
  187.  
  188.     rednet.open(modemLocation)
  189. end
  190.  
  191. function main()
  192.     if not fs.exists("com.lua") then
  193.         error("com.lua is necessary for the operation of this program")
  194.     end
  195.  
  196.     os.loadAPI("com.lua")
  197.  
  198.     refindStorageBridge = peripheral.find("rsBridge")
  199.     inventoryManager = peripheral.find("inventoryManager")
  200.  
  201.     openModem()
  202.  
  203.     local actions = mainActions()
  204.  
  205.     while true do
  206.         local sentFrom, message = rednet.receive(remoteRSProtocol)
  207.         local action, parameters = com.parseMessage(actions, sentFrom, message, remoteRSProtocol)
  208.  
  209.         if action and parameters then
  210.             actions[action]["function"](sentFrom, parameters)
  211.         end
  212.     end
  213. end
  214.  
  215. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement