Advertisement
glitchdetector

EMC List

Oct 14th, 2023 (edited)
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. -- Computercraft Program
  2. -- by glitchdetector
  3.  
  4. function ReadableNumber(num, places, full)
  5.     num = tonumber(num)
  6.     if not num then return 0 end
  7.     local ret
  8.     local placeValue = ("%%.%df"):format(places or 0)
  9.     local isNegative = num < 0
  10.     num = math.abs(num)
  11.     if num >= 1000000000000 then
  12.         ret = placeValue:format(num / 1000000000000) .. (full and " trillion" or "T") -- trillion
  13.     elseif num >= 1000000000 then
  14.         ret = placeValue:format(num / 1000000000) .. (full and " billion" or "B") -- billion
  15.     elseif num >= 1000000 then
  16.         ret = placeValue:format(num / 1000000) .. (full and " million" or "M") -- million
  17.     elseif num >= 1000 and not full then
  18.         ret = placeValue:format(num / 1000) .. "k" -- thousand
  19.     elseif num >= 1 then
  20.         ret = num -- hundreds
  21.     else
  22.         ret = num
  23.     end
  24.     return (isNegative and "- " or "") .. ret
  25. end
  26.  
  27. function GetEMC(link)
  28.     local items = link.list()
  29.     if not items then return 0 end
  30.     if not items[1] then return 0 end
  31.     return items[1].count or 0
  32. end
  33.  
  34. function ClearScreen(monitor)
  35.     monitor.clear()
  36.     monitor.setCursorPos(1, 1)
  37. end
  38.  
  39. function NextLine(monitor)
  40.     local x, y = monitor.getCursorPos()
  41.     monitor.setCursorPos(1, y + 1)
  42. end
  43.  
  44. function WriteCenterAligned(monitor, text)
  45.     local w, h = monitor.getSize()
  46.     local x, y = monitor.getCursorPos()
  47.     monitor.setCursorPos(math.floor(w / 2 - #text / 2), y)
  48.     monitor.write(text)
  49. end
  50.  
  51. function WriteLeftAligned(monitor, text)
  52.     local x, y = monitor.getCursorPos()
  53.     monitor.setCursorPos(1, y)
  54.     monitor.write(text)
  55. end
  56.  
  57. function WriteRightAligned(monitor, text)
  58.     local w, h = monitor.getSize()
  59.     local x, y = monitor.getCursorPos()
  60.     monitor.setCursorPos(w - #text + 1, y)
  61.     monitor.write(text)
  62. end
  63.  
  64. -- Program Code
  65.  
  66. local Users = {
  67.     {"top", "Glitch"},
  68.     {"right", "Collins"},
  69. }
  70.  
  71. for _, user in next, Users do
  72.     user[3] = peripheral.wrap(user[1])
  73. end
  74.  
  75. local monitor = peripheral.wrap("monitor_0")
  76. local last = 0
  77. local timespan = 5
  78. local timespanFactor = 60 / timespan
  79.  
  80. local history = {}
  81. function CalculateHistory(new)
  82.     table.insert(history, new)
  83.     while #history > timespanFactor do
  84.         table.remove(history, 1)
  85.     end
  86.     local sum = 0
  87.     for _, v in next, history do
  88.         sum = sum + v
  89.     end
  90.     return sum / #history
  91. end
  92.  
  93. while true do
  94.     ClearScreen(monitor)
  95.     WriteCenterAligned(monitor, "Energy Matter Covalence")
  96.     NextLine(monitor)
  97.     local sum = 0
  98.     for n, user in next, Users do
  99.         WriteLeftAligned(monitor, user[2])
  100.         local EMC = GetEMC(user[3])
  101.         sum = sum + EMC
  102.         WriteRightAligned(monitor, ReadableNumber(EMC, 2, false) .. " EMC")
  103.         NextLine(monitor)
  104.     end
  105.     WriteCenterAligned(monitor, ReadableNumber(sum, 2, true) .. " EMC")
  106.  
  107.     NextLine(monitor)
  108.     local difference = (sum - last)
  109.     local isNeg = difference < 0
  110.     if isNeg then
  111.         monitor.setTextColour(colors.red)
  112.     else
  113.         monitor.setTextColour(colors.green)
  114.     end
  115.     last = sum
  116.     WriteCenterAligned(monitor, (isNeg and "" or "+ ") .. ReadableNumber(CalculateHistory(difference), 2, true) .. " / minute")
  117.     monitor.setTextColour(colors.white)
  118.  
  119.     sleep(timespan)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement