Advertisement
MrFoxit

Energy Cell Reader3 : Server

Mar 27th, 2025 (edited)
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.40 KB | None | 0 0
  1. local mon = peripheral.find("monitor")
  2. if not mon then
  3.     print("Aucun monitor détecté")
  4.     return
  5. end
  6.  
  7. local modemSide
  8. for _, side in ipairs(peripheral.getNames()) do
  9.     if peripheral.getType(side) == "modem" then
  10.         modemSide = side
  11.         break
  12.     end
  13. end
  14. if not modemSide then
  15.     print("Aucun modem détecté")
  16.     return
  17. end
  18. rednet.open(modemSide)
  19.  
  20.  
  21. local cells = {}
  22. for _, name in ipairs(peripheral.getNames()) do
  23.     local p = peripheral.wrap(name)
  24.     if p.getEnergy and p.getEnergyCapacity then
  25.         table.insert(cells, {
  26.             obj = p,
  27.             lastEnergy = p.getEnergy()
  28.         })
  29.     end
  30. end
  31.  
  32. if #cells == 0 then
  33.     print("Aucune Energy Cell détectée")
  34.     return
  35. end
  36.  
  37. mon.setTextScale(0.5)
  38. local w, h = mon.getSize()
  39.  
  40. -- === BARRE AVEC COULEUR DYNAMIQUE ===
  41. function getBarColor(percent)
  42.     if percent < 0.3 then
  43.         return colors.red
  44.     elseif percent < 0.6 then
  45.         return colors.orange
  46.     else
  47.         return colors.lime
  48.     end
  49. end
  50.  
  51. function drawBar(x, y, percent, width)
  52.     local filled = math.floor(percent * width)
  53.     local empty = width - filled
  54.  
  55.     mon.setCursorPos(x, y)
  56.     mon.setBackgroundColor(getBarColor(percent))
  57.     mon.write(string.rep(" ", filled))
  58.  
  59.     mon.setBackgroundColor(colors.gray)
  60.     mon.write(string.rep(" ", empty))
  61.  
  62.     mon.setBackgroundColor(colors.black)
  63. end
  64.  
  65. function clearLine(y)
  66.     mon.setCursorPos(1, y)
  67.     mon.setBackgroundColor(colors.black)
  68.     mon.write(string.rep(" ", w))
  69. end
  70.  
  71. function formatTime(seconds)
  72.     if seconds <= 0 then return "--" end
  73.     local m = math.floor(seconds / 60)
  74.     local s = seconds % 60
  75.     return string.format("%d min %02d sec", m, s)
  76. end
  77.  
  78. while true do
  79.     local totalEnergy = 0
  80.     local totalCapacity = 0
  81.     local totalDelta = 0
  82.  
  83.     for i, cell in ipairs(cells) do
  84.         local y = i * 2 - 1 -- espace entre chaque ligne
  85.         local energy = cell.obj.getEnergy()
  86.         local capacity = cell.obj.getEnergyCapacity()
  87.         local percent = energy / capacity
  88.         local delta = energy - cell.lastEnergy
  89.         cell.lastEnergy = energy
  90.  
  91.         totalEnergy = totalEnergy + energy
  92.         totalCapacity = totalCapacity + capacity
  93.         totalDelta = totalDelta + delta
  94.  
  95.         local nomCellule = string.format("Cellule %-2d", i)
  96.  
  97.         -- Efface ligne
  98.         clearLine(y)
  99.  
  100.         -- Titre
  101.         mon.setCursorPos(1, y)
  102.         mon.setTextColor(colors.yellow)
  103.         mon.write(string.format("[%s]", nomCellule))
  104.  
  105.         -- Barre graphique
  106.         drawBar(10, y, percent, 20)
  107.  
  108.         -- Pourcentage
  109.         mon.setCursorPos(32, y)
  110.         mon.setTextColor(colors.white)
  111.         mon.write(string.format("%3d%%", percent * 100))
  112.  
  113.         -- RF/t
  114.         mon.setCursorPos(40, y)
  115.         if delta > 0 then
  116.             mon.setTextColor(colors.lime)
  117.         elseif delta < 0 then
  118.             mon.setTextColor(colors.red)
  119.         else
  120.             mon.setTextColor(colors.gray)
  121.         end
  122.         mon.write(string.format("%+4d RF/t ", delta))
  123.     end
  124.  
  125.     -- === GLOBAL BAR ===
  126.     local globalPercent = totalEnergy / totalCapacity
  127.     local globalBarY = h - 1
  128.  
  129.     clearLine(globalBarY)
  130.     clearLine(h)
  131.  
  132.     drawBar(10, globalBarY, globalPercent, 20)
  133.  
  134.     mon.setCursorPos(1, globalBarY)
  135.     mon.setTextColor(colors.cyan)
  136.     mon.write("[GLOBAL]")
  137.  
  138.     mon.setCursorPos(32, globalBarY)
  139.     mon.setTextColor(colors.white)
  140.     mon.write(string.format("%3d%%", globalPercent * 100))
  141.  
  142.     local estTime = "--"
  143.     if totalDelta ~= 0 then
  144.         local missing = totalDelta > 0 and (totalCapacity - totalEnergy) or totalEnergy
  145.         local seconds = missing / math.abs(totalDelta)
  146.         estTime = formatTime(math.floor(seconds))
  147.     end
  148.  
  149.     mon.setCursorPos(1, h)
  150.     mon.setTextColor(colors.lightGray)
  151.     mon.write("Estimation : " .. estTime .. "          ")
  152.  
  153.     sleep(1)
  154.  
  155.     local data = {
  156.         cells = {},
  157.         globalPercent = globalPercent,
  158.         estTime = estTime
  159.     }
  160.    
  161.     for i, cell in ipairs(cells) do
  162.         local energy = cell.obj.getEnergy()
  163.         local capacity = cell.obj.getEnergyCapacity()
  164.         local percent = energy / capacity
  165.         local delta = energy - cell.lastEnergy
  166.         table.insert(data.cells, {
  167.             id = i,
  168.             percent = percent,
  169.             delta = delta
  170.         })
  171.     end
  172.     rednet.broadcast(data, "energy_display")
  173.  
  174.  
  175.  
  176. end
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement