Sparkybearbomb

Create Vault Display Server

Dec 3rd, 2024 (edited)
3,155
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Set the vault and monitor peripherals
  2. function monitorSearch()
  3.    local names = peripheral.getNames()
  4.    local i, name
  5.    for i, name in pairs(names) do
  6.       if peripheral.getType(name) == "monitor" then
  7.          return peripheral.wrap(name)
  8.       else
  9.          --return null
  10.       end
  11.    end
  12. end
  13.  
  14. function modemSearch()
  15.    local names = peripheral.getNames()
  16.    local i, name
  17.    for i, name in pairs(names) do
  18.       if peripheral.getType(name) == "modem" then
  19.          return peripheral.wrap(name)
  20.       else
  21.          --return null
  22.       end
  23.    end
  24. end
  25.  
  26.  
  27.  
  28. -- Function to draw a colored border on the monitor
  29. function drawBorder()
  30.    -- Define simpler border characters
  31.    local horizontalBorder = "-"
  32.    local verticalBorder = "|"
  33.    local topLeftCorner = "+"
  34.    local topRightCorner = "+"
  35.    local bottomLeftCorner = "+"
  36.    local bottomRightCorner = "+"
  37.  
  38.    -- Set the color for the border (e.g., colors.red)
  39.    monitor.setTextColor(colors.red)
  40.  
  41.    -- Draw top border
  42.    monitor.setCursorPos(1, 1)
  43.    monitor.write(topLeftCorner .. string.rep(horizontalBorder, width - 2) .. topRightCorner)
  44.  
  45.    -- Draw bottom border
  46.    monitor.setCursorPos(1, height)
  47.    monitor.write(bottomLeftCorner .. string.rep(horizontalBorder, width - 2) .. bottomRightCorner)
  48.  
  49.    -- Draw left and right borders
  50.    for y = 2, height - 1 do
  51.        monitor.setCursorPos(1, y)
  52.        monitor.write(verticalBorder)
  53.        monitor.setCursorPos(width, y)
  54.        monitor.write(verticalBorder)
  55.    end
  56.  
  57.    -- Reset color to white for the rest of the text
  58.    monitor.setTextColor(colors.white)
  59. end
  60.  
  61. -- Function to add or update entries
  62. function addOrUpdateEntry(name, value, pc)
  63.    oreValues[pc] = {oreName = name, qty = value}
  64. end
  65.  
  66. function receiveData()
  67.    local event, side, channel, replyChannel, message, distance
  68.    event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  69.    if((channel == 1) and (message == "Hello")) then
  70.       if (type(oreValues[replyChannel]) ~= "table") then
  71.          print("New Device Found")
  72.          modem.transmit(replyChannel, 1, "Confirmed")
  73.       else
  74.          print("Conflict Detected, Ignoring")
  75.       end
  76.    elseif(channel == 2) then
  77.       charPos = string.find(message,":")
  78.       oreName = string.sub(message,(charPos+1))
  79.       qty = string.sub(message,0,(charPos-1))
  80.       print(oreName.." "..qty)
  81.       addOrUpdateEntry(oreName,qty,replyChannel)
  82.       modem.transmit(replyChannel, 2, "Okay")
  83.    end
  84. end
  85.  
  86. function displayUpdate()
  87.    monitor.setTextScale(1)
  88.    width, height = monitor.getSize()
  89.    monitor.clear()
  90.    drawBorder()
  91.    dispName = "Ore Collection Values"
  92.    wPos = math.floor((width - string.len(dispName)) / 2) + 1
  93.    hPos = 2
  94.    monitor.setCursorPos(wPos,hPos)
  95.    monitor.write(dispName)
  96.    
  97.    if next(oreValues) == nil then
  98.       hPos = hPos + 2
  99.       dispName = "Empty"
  100.       wPos = math.floor((width - string.len(dispName)) / 2) + 1
  101.       monitor.setCursorPos(wPos,hPos)
  102.       monitor.write(dispName)
  103.       return
  104.    end
  105.    
  106.    local totalOreValues = {}
  107.    for pc, entry in pairs(oreValues) do
  108.       local oreName = entry.oreName
  109.       local qty = tonumber(entry.qty) or 0
  110.       if not totalOreValues[oreName] then
  111.          totalOreValues[oreName] = 0
  112.       end
  113.       totalOreValues[oreName] = totalOreValues[oreName] + qty
  114.    end
  115.    
  116.    for oreName, totalQty in pairs(totalOreValues) do
  117.       hPos = hPos + 2
  118.       dispName = oreName.. " = ".. totalQty
  119.       wPos = math.floor((width - string.len(dispName)) / 2) + 1
  120.       monitor.setCursorPos(wPos,hPos)
  121.       monitor.write(dispName)
  122.    end
  123. end
  124.  
  125. -- Attach Peripherals
  126. monitor = monitorSearch()
  127. modem = modemSearch()
  128.  
  129. -- Open Modem Channels
  130. modem.open(1)
  131. modem.open(2)
  132.  
  133. -- Create the table
  134. oreValues = {}
  135. -- Set text scale
  136.  
  137.  
  138. while true do
  139.    parallel.waitForAll(receiveData,displayUpdate)
  140. end
  141.  
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment