shiroxxblank

hostServer

Jun 27th, 2022 (edited)
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.62 KB | None | 0 0
  1. ---@diagnostic disable: undefined-global, undefined-field, need-check-nil, lowercase-global
  2.  
  3. inventories = {}
  4. combinedInventories = {}
  5. turtleamount = 48
  6. totals = {}
  7. progressBarWidth = 10
  8.  
  9. monitor = peripheral.find("monitor")
  10. oldTerm = term.redirect(monitor)
  11.  
  12. table.reduce = function(list, fn, init)
  13.     local acc = init
  14.     for k, v in ipairs(list) do
  15.         if 1 == k and not init then
  16.             acc = v
  17.         else
  18.             acc = fn(acc, v)
  19.         end
  20.     end
  21.     return acc
  22. end
  23.  
  24. function tablelength(T)
  25.     local count = 0
  26.     for _ in pairs(T) do count = count + 1 end
  27.     return count
  28. end
  29.  
  30. function mysplit(inputstr, sep)
  31.     if sep == nil then
  32.         sep = "%s"
  33.     end
  34.     local t = {}
  35.     for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  36.         table.insert(t, str)
  37.     end
  38.     return t
  39. end
  40.  
  41. function drawLine(x, y, length, color)
  42.     oldBgColor = term.getBackgroundColor()
  43.     for i = x, (length + x) do
  44.         term.setCursorPos(i, y)
  45.         term.setBackgroundColor(color)
  46.         term.write(" ")
  47.     end
  48.     term.setBackgroundColor(oldBgColor)
  49. end
  50.  
  51. function listen()
  52.     peripheral.find("modem", rednet.open)
  53.     received = false
  54.     while not received do
  55.         local id, msg = rednet.receive()
  56.         if msg ~= nil or msg ~= false then
  57.             inventories[id] = msg[1]
  58.             totals[id] = msg[2]
  59.  
  60.             received = true
  61.         end
  62.     end
  63. end
  64.  
  65. function getPercentage()
  66.     local used = 0
  67.     local maximum = 0
  68.  
  69.     for name, amount in pairs(combinedInventories) do
  70.         used = used + amount
  71.     end
  72.  
  73.     for key, value in pairs(totals) do
  74.         maximum = maximum + value
  75.     end
  76.  
  77.     if (used / maximum) == 0 then return 0.0 end
  78.  
  79.     return (used / maximum)
  80. end
  81.  
  82. function updateMonitor()
  83.     monitor.clear()
  84.     local width, height = monitor.getSize()
  85.     percent = getPercentage()
  86.     w = math.floor(percent * progressBarWidth)
  87.  
  88.     drawLine(width / 2 - progressBarWidth / 2, height / 2, w , colors.white)
  89.  
  90.     local str = tostring(percent * 10):sub(1,5) .. "%"
  91.  
  92.     monitor.setCursorPos((width / 2 - progressBarWidth / 2) + (monitor.getTextScale() * #str), (height / 2) + 2)
  93.     monitor.write(str)
  94. end
  95.  
  96. while true do
  97.     combinedInventories = {}
  98.     listen()
  99.  
  100.     for key, value in pairs(inventories) do
  101.         for name, amount in pairs(value) do
  102.             if combinedInventories[name] ~= nil then
  103.                 combinedInventories[name] = combinedInventories[name] + amount
  104.             else
  105.                 combinedInventories[name] = amount
  106.             end
  107.         end
  108.     end
  109.  
  110.     updateMonitor()
  111. end
  112.  
Add Comment
Please, Sign In to add comment