akashi9210

tank_display

Dec 3rd, 2024 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. -- Programm: Tank-Daten empfangen und als Text anzeigen
  2. -- Voraussetzungen:
  3. -- 1. ComputerCraft Tweaked installiert.
  4. -- 2. Ein Modem (z. B. Wireless Modem) am Computer angebracht und aktiviert.
  5. -- 3. Ein Monitor (z. B. Advanced Monitor) am Computer angebracht und aktiviert.
  6.  
  7. -- Konfiguration
  8. local modemSide = "right" -- Seite, an der das Modem angebracht ist
  9. local monitorSide = "top" -- Seite, an der der Monitor angeschlossen ist
  10. local channel = 1 -- Kanal, auf dem die Daten empfangen werden
  11. local monitorScale = 1 -- Textgröße des Monitors
  12.  
  13. -- Globale Tabelle zur Speicherung aller Tanks, gruppiert nach Flüssigkeit
  14. local tanksByFluid = {}
  15.  
  16. -- Funktionen
  17. local function getFluidType(fluidName)
  18. local separator = string.find(fluidName, ":")
  19. if separator then
  20. return string.sub(fluidName, separator + 1)
  21. end
  22. return fluidName
  23. end
  24.  
  25. local function formatNumberWithThousandsSeparator(number)
  26. local formatted = tostring(number)
  27. while true do
  28. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1.%2")
  29. if k == 0 then break end
  30. end
  31. return formatted
  32. end
  33.  
  34. local function displayTankData(monitor, tanksByFluid)
  35. monitor.setBackgroundColor(colors.black)
  36. monitor.clear()
  37. monitor.setTextColor(colors.white)
  38.  
  39. local row = 1
  40. for fluid, tank in pairs(tanksByFluid) do
  41. monitor.setCursorPos(1, row)
  42. monitor.write(string.format("Flüssigkeit: %s", fluid))
  43. row = row + 1
  44.  
  45. local percentage = (tank.amount / tank.capacity) * 100
  46. local amountFormatted = formatNumberWithThousandsSeparator(tank.amount)
  47. local capacityFormatted = formatNumberWithThousandsSeparator(tank.capacity)
  48. local text = string.format(" %s/%s mb (%.1f%%)", amountFormatted, capacityFormatted, percentage)
  49. monitor.setCursorPos(1, row)
  50. monitor.write(text)
  51. row = row + 1
  52. end
  53. end
  54.  
  55. -- Hauptprogramm
  56. print("Starte Monitor...")
  57. local modem = peripheral.wrap(modemSide)
  58. local monitor = peripheral.wrap(monitorSide)
  59.  
  60. if not modem then
  61. error("Modem nicht gefunden!")
  62. end
  63. if not monitor then
  64. error("Monitor nicht gefunden!")
  65. end
  66.  
  67. monitor.setTextScale(monitorScale)
  68. modem.open(channel)
  69.  
  70. while true do
  71. local event, _, senderChannel, _, message = os.pullEvent("modem_message")
  72. if senderChannel == channel then
  73. print("Nachricht empfangen: " .. tostring(message)) -- Debug-Ausgabe
  74. local success, data = pcall(function()
  75. return type(message) == "table" and message or textutils.unserialize(message)
  76. end)
  77.  
  78. if success and data and type(data) == "table" then
  79. -- Verarbeitung der empfangenen Daten
  80. if data.capacity and data.amount and data.fluid then
  81. local fluidType = getFluidType(data.fluid)
  82. tanksByFluid[fluidType] = {
  83. capacity = data.capacity,
  84. amount = data.amount,
  85. fluid = data.fluid
  86. }
  87. else
  88. for key, value in pairs(data) do
  89. if type(value) == "table" and value.capacity and value.amount and value.fluid then
  90. local fluidType = getFluidType(value.fluid)
  91. tanksByFluid[fluidType] = {
  92. capacity = value.capacity,
  93. amount = value.amount,
  94. fluid = value.fluid
  95. }
  96. end
  97. end
  98. end
  99.  
  100. displayTankData(monitor, tanksByFluid)
  101. else
  102. print("Ungültige Nachricht empfangen. Nachricht: " .. tostring(message)) -- Ausführlichere Fehlerausgabe
  103. end
  104. end
  105. end
  106.  
Advertisement
Add Comment
Please, Sign In to add comment