Advertisement
klindley

manager

May 10th, 2023 (edited)
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. -- Manager
  2. local storage = {}
  3.  
  4. function addStorage(sender, resource)
  5. print(("Attempting to add %s from %d"):format(resource, sender))
  6.  
  7. if not storage[resource] then
  8. storage[resource] = {}
  9. end
  10.  
  11. table.insert(storage[resource], sender)
  12.  
  13. print(("Storage table is now: %s"):format(textutils.serialize(storage)))
  14.  
  15. local message = {command = "ack"}
  16. rednet.send(sender, textutils.serialize(message))
  17. end
  18.  
  19. function handleCommunications()
  20. local sender, message = rednet.receive()
  21.  
  22. -- message { command = "storage", resource = "gravel|cobblestone|andesite|etc"}
  23. -- message { command = "request", resource = "gravel|cobblestone|andesite|etc", amount = 64}
  24. local actualMessage = textutils.unserialize(message)
  25.  
  26. if actualMessage.command == "storage" then
  27. addStorage(sender, actualMessage.resource)
  28. elseif actualMessage.command == "request" then
  29. handleRequest(sender, actualMessage.resource, actualMessage.amount)
  30. elseif actualMessage.command == "bye" then
  31. handleGoodbye(sender)
  32. elseif actualMessage.command == "helo" then
  33. handleHello(sender)
  34. end
  35. end
  36.  
  37. function handleGoodbye(sender)
  38. print(("Attempting goodbye for %d"):format(sender))
  39. for resource, list in pairs(storage) do
  40. for index, which in ipairs(list) do
  41. if which == sender then
  42. print(("Removing sender %d from %s"):format(sender, resource))
  43. table.remove(storage[resource], index)
  44. end
  45. end
  46. end
  47.  
  48. local message = {command="ack"}
  49. rednet.send(sender, textutils.serialize(message))
  50. end
  51.  
  52. function handleHello(sender)
  53. print(("Received hello from %d"):format(sender))
  54.  
  55. local message = {command = "hello"}
  56. rednet.send(sender, textutils.serialize(message))
  57. end
  58.  
  59. function handleRequest(sender, resource, amount)
  60. print(("Request received for %d %s from %d"):format(amount, resource, sender))
  61.  
  62. if not storage[resource] then return end
  63. local resourceCount = table.maxn(storage[resource])
  64. if resourceCount > 0 then
  65. local perHost = math.ceil(amount / resourceCount)
  66.  
  67. for _, host in ipairs(storage[resource]) do
  68. local message = {command = "request", amount = perHost}
  69. rednet.send(host, textutils.serialize(message))
  70. end
  71. end
  72.  
  73. local message = {command = "ack"}
  74. rednet.send(sender, textutils.serialize(message))
  75. end
  76.  
  77. function main()
  78. peripheral.find("modem", rednet.open)
  79.  
  80. while true do
  81. handleCommunications()
  82. end
  83. end
  84.  
  85. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement