Advertisement
Koridev

Untitled

Mar 25th, 2025
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.04 KB | Gaming | 0 0
  1. -- client.lua
  2. -- AE2-Client, empfängt Daten per rednet und zeigt AE2 UI an
  3.  
  4. -------------------------
  5. -- 1) KONFIG
  6. -------------------------
  7. local monSide     = "right"      -- Monitor-Seite
  8. local modemSide   = "top"       -- Wireless Modem-Seite
  9. local BOX_W       = 14           -- Breite Kästchen
  10. local BOX_H       = 4            -- Höhe Kästchen
  11. local ITEMS_PER_PAGE   = 10      -- Items in Detailansicht
  12. local REDNET_PROTOCOL  = "AE2_DATA"  -- Muss zum Server passen
  13. local DATA_TIMEOUT     = 15      -- Sekunden, bis wir "keine Daten" anzeigen
  14.  
  15. -------------------------
  16. -- 2) Peripherie-Abfrage
  17. -------------------------
  18. local mon = peripheral.wrap(monSide)
  19. if not mon then
  20.   error("Monitor an Seite '"..monSide.."' nicht gefunden!")
  21. end
  22.  
  23. local modem = peripheral.wrap(modemSide)
  24. if not modem then
  25.   error("Modem an Seite '"..modemSide.."' nicht gefunden!")
  26. end
  27. rednet.open(modemSide)
  28.  
  29. mon.setTextScale(0.5)
  30.  
  31. -------------------------
  32. -- 3) Globale Variablen
  33. -------------------------
  34. local width, height = mon.getSize()
  35. local currentScreen = "main"
  36. local currentPage   = 1
  37.  
  38. -- Daten vom Server
  39. local serverData = {
  40.   cells       = {},  -- { {usedBytes=..., totalBytes=...}, ... }
  41.   itemCount   = 0,
  42.   freePercent = 0,
  43.   items       = {},  -- Komplette Liste (sortiert)
  44. }
  45.  
  46. -------------------------
  47. -- 4) Hilfsfunktionen
  48. -------------------------
  49. local function clear()
  50.   mon.setBackgroundColor(colors.black)
  51.   mon.clear()
  52.   mon.setCursorPos(1,1)
  53. end
  54.  
  55. local function centerText(y, text, color)
  56.   color = color or colors.white
  57.   mon.setTextColor(color)
  58.   local x = math.floor((width - #text) / 2)
  59.   mon.setCursorPos(x+1, y)
  60.   mon.write(text)
  61. end
  62.  
  63. local function getColorForUsage(used, total)
  64.   if total == 0 then
  65.     return colors.lime
  66.   end
  67.   local percent = used / total
  68.   if percent >= 0.9 then
  69.     return colors.red
  70.   elseif percent >= 0.5 then
  71.     return colors.yellow
  72.   else
  73.     return colors.lime
  74.   end
  75. end
  76.  
  77. -------------------------
  78. -- 4.1) Box-Zeichnung (Cell UI)
  79. -------------------------
  80. local function drawCellBox(x, y, w, h, cellIndex, usedB, totalB)
  81.   mon.setCursorPos(x, y)
  82.   mon.setTextColor(colors.white)
  83.   mon.write("+")
  84.   for i = 1, w-2 do
  85.     mon.write("-")
  86.   end
  87.   mon.write("+")
  88.  
  89.   for row = 1, h-2 do
  90.     mon.setCursorPos(x, y+row)
  91.     mon.write("|")
  92.     mon.setCursorPos(x+w-1, y+row)
  93.     mon.write("|")
  94.   end
  95.  
  96.   mon.setCursorPos(x, y+h-1)
  97.   mon.write("+")
  98.   for i = 1, w-2 do
  99.     mon.write("-")
  100.   end
  101.   mon.write("+")
  102.  
  103.   -- Info
  104.   mon.setCursorPos(x+1, y+1)
  105.   mon.setTextColor(colors.white)
  106.   mon.write("Cell "..cellIndex)
  107.  
  108.   local percent = 0
  109.   if totalB > 0 then
  110.     percent = math.floor((usedB / totalB)*100)
  111.   end
  112.   local usageColor = getColorForUsage(usedB, totalB)
  113.   mon.setTextColor(usageColor)
  114.   mon.setCursorPos(x+1, y+2)
  115.   mon.write(usedB.."/"..totalB.." ("..percent.."%)")
  116. end
  117.  
  118. -------------------------
  119. -- 5) Hauptmenü zeichnen
  120. -------------------------
  121. local function drawMainScreen()
  122.   clear()
  123.   centerText(1, "AE2 Cells Overview (Page "..currentPage..")", colors.cyan)
  124.  
  125.   local cells = serverData.cells or {}
  126.   local columns = math.floor(width / BOX_W)
  127.   local rows    = math.floor((height - 3) / BOX_H)
  128.  
  129.   if columns < 1 or rows < 1 then
  130.     centerText(math.floor(height/2), "Monitor zu klein!", colors.red)
  131.     return
  132.   end
  133.  
  134.   local boxesPerPage = columns * rows
  135.   local totalCells   = #cells
  136.   local totalPages   = math.ceil(totalCells / boxesPerPage)
  137.  
  138.   -- "globale" totalPages Updaten, damit wir blättern können
  139.   _G.totalPages = totalPages
  140.  
  141.   local startCell = (currentPage - 1)*boxesPerPage + 1
  142.   local endCell   = math.min(startCell + boxesPerPage - 1, totalCells)
  143.  
  144.   local idx = startCell
  145.   for row=0, rows-1 do
  146.     for col=0, columns-1 do
  147.       if idx > endCell then break end
  148.       local c = cells[idx]
  149.       local usedB  = c.usedBytes  or 0
  150.       local totalB = c.totalBytes or 0
  151.  
  152.       local x = col*BOX_W + 1
  153.       local y = row*BOX_H + 2
  154.       drawCellBox(x, y, BOX_W, BOX_H, idx, usedB, totalB)
  155.       idx = idx + 1
  156.     end
  157.     if idx > endCell then break end
  158.   end
  159.  
  160.   -- Infozeile (Cells, Items, Frei)
  161.   mon.setCursorPos(2, height-1)
  162.   mon.setTextColor(colors.white)
  163.   local msg = string.format("Cells: %d  Items: %d  Frei: %d%%",
  164.     #cells, serverData.itemCount, serverData.freePercent)
  165.   mon.write(msg)
  166.  
  167.   -- Seitenbuttons
  168.   mon.setCursorPos(2, height)
  169.   if currentPage > 1 then
  170.     mon.write("[ < ]")
  171.   end
  172.   mon.setCursorPos(width-5, height)
  173.   if currentPage < totalPages then
  174.     mon.write("[ > ]")
  175.   end
  176. end
  177.  
  178. -------------------------
  179. -- 5.1) Detail-Items
  180. -------------------------
  181. local function drawCellDetails(cellIndex)
  182.   clear()
  183.   centerText(1, "Cell "..cellIndex.." - Top Items", colors.cyan)
  184.  
  185.   local items = serverData.items or {}
  186.   local y = 3
  187.   local shown = 0
  188.  
  189.   for _, it in ipairs(items) do
  190.     if y >= height then break end
  191.     mon.setCursorPos(2, y)
  192.     mon.setTextColor(colors.white)
  193.     mon.write((it.displayName or "?")..": "..(it.amount or 0))
  194.     y = y + 1
  195.     shown = shown + 1
  196.     if shown >= ITEMS_PER_PAGE then
  197.       break
  198.     end
  199.   end
  200.  
  201.   mon.setCursorPos(2, height)
  202.   mon.setTextColor(colors.gray)
  203.   mon.write("[ Zurueck ]")
  204. end
  205.  
  206. -------------------------
  207. -- 6) Klick -> Welche Cell?
  208. -------------------------
  209. local function getCellIndexFromClick(x, y)
  210.   local cells = serverData.cells or {}
  211.   local columns = math.floor(width / BOX_W)
  212.   local rows    = math.floor((height - 3) / BOX_H)
  213.   local boxesPerPage = columns * rows
  214.  
  215.   local totalCells = #cells
  216.   local totalPages = math.ceil(totalCells / boxesPerPage)
  217.   _G.totalPages = totalPages  -- for global reference
  218.  
  219.   local startCell = (currentPage - 1)*boxesPerPage + 1
  220.   local endCell   = math.min(startCell + boxesPerPage - 1, totalCells)
  221.  
  222.   local col = math.floor((x-1)/BOX_W)
  223.   local row = math.floor((y-2)/BOX_H)
  224.   if col<0 or col>=columns or row<0 or row>=rows then
  225.     return nil
  226.   end
  227.  
  228.   local cellOffset = row*columns + col
  229.   local actualIndex = startCell + cellOffset
  230.   if actualIndex <= endCell then
  231.     return actualIndex
  232.   end
  233.   return nil
  234. end
  235.  
  236. -------------------------
  237. -- 7) Haupt-Loop
  238. -------------------------
  239. local refreshTimer = os.startTimer(1) -- alle 1s checken, ob neue Daten reingekommen sind
  240.  
  241. -- Wir speichern Zeitstempel, wann wir zuletzt Daten erhielten
  242. local lastDataTime = os.epoch("utc")
  243.  
  244. -- Wir zeigen init
  245. drawMainScreen()
  246.  
  247. while true do
  248.   local event, p1, p2, p3, p4 = os.pullEvent()
  249.  
  250.   if event == "rednet_message" then
  251.     local senderID, message, protocol = p1, p2, p3
  252.     if protocol == REDNET_PROTOCOL and type(message)=="table" then
  253.       -- Neue Daten vom Server
  254.       serverData = message
  255.       lastDataTime = os.epoch("utc")
  256.       if currentScreen == "main" then
  257.         drawMainScreen()
  258.       end
  259.     end
  260.  
  261.   elseif event == "monitor_touch" then
  262.     local x, y = p2, p3
  263.     if currentScreen == "main" then
  264.       -- Blättern?
  265.       if y == height then
  266.         if x>=2 and x<=5 and currentPage>1 then
  267.           currentPage = currentPage - 1
  268.           drawMainScreen()
  269.         elseif x>=width-4 and x<=width and currentPage<_G.totalPages then
  270.           currentPage = currentPage + 1
  271.           drawMainScreen()
  272.         end
  273.       else
  274.         -- Klick auf Cell => Detail
  275.         local cellIndex = getCellIndexFromClick(x,y)
  276.         if cellIndex then
  277.           currentScreen = "cell"
  278.           drawCellDetails(cellIndex)
  279.         end
  280.       end
  281.     elseif currentScreen == "cell" then
  282.       -- Klick auf [Zurueck]
  283.       if y == height then
  284.         currentScreen = "main"
  285.         drawMainScreen()
  286.       end
  287.     end
  288.  
  289.   elseif event == "timer" then
  290.     if p1 == refreshTimer then
  291.       -- Prüfe, ob wir zu lange keine Daten hatten
  292.       local now = os.epoch("utc")
  293.       if now - lastDataTime > (DATA_TIMEOUT*1000) then
  294.         -- Zu lange keine Daten => Anzeige leeren
  295.         clear()
  296.         centerText(math.floor(height/2), "Keine Daten vom Server!", colors.red)
  297.       end
  298.       refreshTimer = os.startTimer(1) -- Timer neu
  299.     end
  300.   end
  301. end
  302.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement