MrKey2b

mecpumr

Oct 30th, 2023 (edited)
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.04 KB | None | 0 0
  1. mon = peripheral.wrap("right")
  2. me = peripheral.wrap("right")
  3.  
  4. data = {
  5.     cpus = 0,
  6.     oldCpus = 0,
  7.     crafting = 0,
  8.     bytes = 0,
  9.     bytesUsed = 0
  10. }
  11.  
  12. local firstStart = true
  13.  
  14. local label = "ME Crafting CPUs"
  15.  
  16. local monX, monY
  17.  
  18. os.loadAPI("mecpus/api/bars.lua")
  19.  
  20. function prepareMon()
  21.     mon.clear()
  22.     monX, monY = mon.getSize()
  23.     if monX < 38 or monY < 25 then
  24.         error("Monitor is too small, we need a size of 39x and 26y minimum.")
  25.     end
  26.     mon.setBackgroundColor(colors.black)
  27.     mon.setCursorPos((monX/2)-(#label/2),1)
  28.     mon.setTextScale(1)
  29.     mon.write(label)
  30.     mon.setCursorPos(1,1)
  31.     drawBox(2, monX - 1, 3, monY - 10, "CPU's", colors.gray, colors.lightGray)
  32.     drawBox(2, monX - 1, monY - 8, monY - 1, "Stats", colors.gray, colors.lightGray)
  33.     addBars()
  34. end
  35.  
  36. function addBars()
  37.     cpus = me.getCraftingCPUs()
  38.     for i=1, #cpus do
  39.         x = 3*i
  40.         full = (cpus[i].storage/65536) + cpus[i].coProcessors
  41.         bars.add(""..i,"ver", full, cpus[i].coProcessors, 1+x, 5, 2, monY - 16, colors.purple, colors.lightBlue)
  42.         mon.setCursorPos(x+1, monY - 11)
  43.         --mon.write(string.format(i))
  44.     end
  45.     bars.construct(mon)
  46.     bars.screen()
  47. end
  48.  
  49.  
  50. function drawBox(xMin, xMax, yMin, yMax, title, bcolor, tcolor)
  51.     mon.setBackgroundColor(bcolor)
  52.     for xPos = xMin, xMax, 1 do
  53.         mon.setCursorPos(xPos, yMin)
  54.         mon.write(" ")
  55.     end
  56.     for yPos = yMin, yMax, 1 do
  57.         mon.setCursorPos(xMin, yPos)
  58.         mon.write(" ")
  59.         mon.setCursorPos(xMax, yPos)
  60.         mon.write(" ")
  61.  
  62.     end
  63.     for xPos = xMin, xMax, 1 do
  64.         mon.setCursorPos(xPos, yMax)
  65.         mon.write(" ")
  66.     end
  67.     mon.setCursorPos(xMin+2, yMin)
  68.     mon.setBackgroundColor(colors.black)
  69.     mon.setTextColor(tcolor)
  70.     mon.write(" ")
  71.     mon.write(title)
  72.     mon.write(" ")
  73.     mon.setTextColor(colors.white)
  74. end
  75.  
  76. function clear(xMin,xMax, yMin, yMax)
  77.     mon.setBackgroundColor(colors.black)
  78.     for xPos = xMin, xMax, 1 do
  79.         for yPos = yMin, yMax, 1 do
  80.             mon.setCursorPos(xPos, yPos)
  81.             mon.write(" ")
  82.         end
  83.     end
  84. end
  85.  
  86. function tablelength(T)
  87.     local count = 0
  88.     for _ in pairs(T) do count = count + 1 end
  89.     return count
  90. end
  91.  
  92. function getUsage()
  93.     return (data.crafting * 100) / data.cpus
  94. end
  95.  
  96. function comma_value(n) -- credit http://richard.warburton.it
  97.     local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
  98.     return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
  99. end
  100.  
  101. function updateStats()
  102.     clear(3,monX - 3,monY - 5,monY - 2)
  103.     print("CPUs: ".. data.cpus)
  104.     print("busy: ".. data.crafting)
  105.     mon.setCursorPos(4,monY-6)
  106.     mon.write("CPUs: ".. data.cpus)
  107.     mon.setCursorPos(4,monY-5)
  108.     mon.write("Busy: ".. data.crafting)
  109.     mon.setCursorPos(4,monY-4)
  110.     mon.write("Busy in percent: ".. math.floor(getUsage()) .."%")
  111.     mon.setCursorPos(4,monY-3)
  112.     if monX > 39 then
  113.         mon.write("Bytes(Total|Used): ".. comma_value(data.bytes) .." | ".. comma_value(data.bytesUsed))
  114.     else
  115.         mon.write("Bytes(Total|Used):")
  116.         mon.setCursorPos(4,monY-2)
  117.         mon.write(comma_value(data.bytes) .." | ".. comma_value(data.bytesUsed))
  118.     end
  119.     if tablelength(bars.getBars()) ~= data.cpus then
  120.         clear(3,monX - 3,4,monY - 12)
  121.         shell.run("reboot")
  122.     end
  123.     oldCpus = cpus
  124.     firstStart = false
  125. end
  126.  
  127. prepareMon()
  128.  
  129. while true do
  130.     cpus = {}
  131.     for k in pairs(me.getCraftingCPUs()) do
  132.         table.insert(cpus, k)
  133.     end
  134.     data.cpus = 0
  135.     data.crafting = 0
  136.     data.bytes = 0
  137.     data.bytesUsed = 0
  138.     table.sort(cpus)
  139.     for i = 1, #cpus do
  140.         local k, v = cpus[i], me.getCraftingCPUs()[cpus[i]]
  141.         data.cpus = data.cpus+1
  142.         data.bytes = data.bytes + v.storage
  143.         if v.isBusy then
  144.             data.bytesUsed = data.bytesUsed + v.storage
  145.             data.crafting = data.crafting+1
  146.         end
  147.         -- print(i, v.coProcessors, v.isBusy, v.storage/65536)
  148.     end
  149.     updateStats()
  150.     sleep(2)
  151. end
Advertisement
Add Comment
Please, Sign In to add comment