Damaged

AE Monitor

Apr 3rd, 2014
255
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.49 KB | None | 0 0
  1. local aeStorage = peripheral.wrap("bottom")
  2. local bigDisplay = peripheral.wrap("top")
  3. local firstTopTen = peripheral.wrap("left")
  4. local secondTopTen = peripheral.wrap("right")
  5. local leftColor = 8192 -- green
  6. local rightColor = 2048 -- blue
  7. local overlapColor = 16 -- yellow
  8.  
  9. local graphWindow = {}
  10. local x
  11. local y
  12. local JSON = (loadfile "JSON.lua")()
  13. JSON.strictTypes = true
  14. function printDebug(textString)
  15.    print(textString)
  16. end
  17.  
  18. function initDisplays()
  19.    bigDisplay.clear()
  20.    bigDisplay.setTextScale(.5)
  21.    firstTopTen.clear()
  22.    secondTopTen.clear()
  23. end
  24.  
  25. function num2Text( inputNumber, textLength )
  26.    inputNumber = tostring( inputNumber )
  27.    if string.len(inputNumber ) < textLength then
  28.       inputNumber = string.rep( " " , textLength - string.len( inputNumber ) )..inputNumber
  29.    elseif string.len(inputNumber ) > textLength then
  30.       inputNumber = "ERR: String too long"
  31.    end
  32.    return inputNumber
  33. end
  34.  
  35. function valueToScale( maxValue, inputValue )
  36.    return graphWindow.ylen - math.ceil( inputValue / ( maxValue / ( graphWindow.ylen - 1 ) ) )
  37. end
  38.  
  39. function scaleToValue( maxValue, inputScale )
  40.    return maxValue - math.floor( ( maxValue / ( graphWindow.ylen - 1 ) ) * ( inputScale - 1 ) )
  41. end
  42.  
  43. function drawScale( maxYLeft, maxYRight )
  44.    local leftLen = string.len( tostring( maxYLeft ) )
  45.    local rightLen = string.len( tostring( maxYRight ) )
  46.    printDebug(textutils.serialize(graphWindow))
  47.    for i = graphWindow.ylen, graphWindow.y, - 1 do
  48.       bigDisplay.setCursorPos( 1 , i )
  49.       bigDisplay.setTextColor(leftColor)
  50.       bigDisplay.write( num2Text( scaleToValue(maxYLeft, i), leftLen ).."-|" )
  51.       bigDisplay.setCursorPos( graphWindow.xlen + graphWindow.x , i )
  52.       bigDisplay.setTextColor(rightColor)
  53.       bigDisplay.write( "|-"..num2Text( scaleToValue(maxYRight, i), rightLen ) )
  54.    end
  55.    bigDisplay.setCursorPos( graphWindow.x , y )
  56.    bigDisplay.setTextColor(1)
  57.    bigDisplay.write( string.rep( "-", graphWindow.xlen ) )
  58. end
  59.  
  60. local leftGraphTable
  61. local rightGraphTable
  62.  
  63. function buildGraph( leftArray, rightArray )
  64.    leftGraphTable = {}
  65.    rightGraphTable = {}
  66.    local lefti = #leftArray
  67.    local righti = #rightArray
  68.    for i = graphWindow.xlen - 1 , 1, - 1 do
  69.       if lefti == 0 or righti == 0 then break end
  70.       leftGraphTable[i] = valueToScale(math.max(unpack(leftArray)), leftArray[lefti] )
  71.       rightGraphTable[i] = valueToScale(math.max(unpack(rightArray)), rightArray[righti] )
  72.       lefti = lefti - 1
  73.       righti = righti - 1
  74.    end
  75. end
  76.    
  77. function drawGraph()
  78.    for i = graphWindow.xlen - 1 , 1, - 1 do
  79.       if not leftGraphTable[i] or not rightGraphTable[i] then break end
  80.       for l = 1, graphWindow.ylen do
  81.          bigDisplay.setCursorPos( i + graphWindow.x, l)
  82.          bigDisplay.write(" ")
  83.       end
  84.       bigDisplay.setCursorPos( i + graphWindow.x, leftGraphTable[i])
  85.       if leftGraphTable[i] == rightGraphTable[i] then
  86.          -- If the table values are the same, draw them as an overlap
  87.          bigDisplay.setTextColor( overlapColor )
  88.          bigDisplay.write("=")
  89.       else
  90.          -- Otherwise, draw one
  91.          bigDisplay.setTextColor( leftColor )
  92.          bigDisplay.write("-")
  93.          -- Then the other
  94.          bigDisplay.setCursorPos( i + graphWindow.x, rightGraphTable[i])
  95.          bigDisplay.setTextColor( rightColor )
  96.          bigDisplay.write("-")
  97.       end
  98.    end
  99. end
  100.    
  101. function rotateHistory( inputArray )
  102.    local outputArray
  103.    if not inputArray or #inputArray < ( graphWindow.xlen + 3 ) then
  104.       outputArray = inputArray
  105.    else
  106.       for i = 2, #inputArray do
  107.          outputArray[i-1] = inputArray[i]
  108.       end
  109.       inputArray[#inputArray] = nil
  110.    end
  111.    return outputArray
  112. end
  113.  
  114. local historyType = {}
  115. local historyCount = {}
  116. if fs.exists("AE_History") then
  117.    local recoveredHistory = fs.open("AE_History", "r")
  118.    local historyData = recoveredHistory.readLine()
  119.    printDebug(historyData)
  120.    if historyData then
  121.       historyData = JSON:decode(historyData)
  122.       printDebug(textutils.serialize(historyData))
  123.       historyType = historyData["Type"]
  124.       historyCount = historyData["Count"]
  125.       printDebug("Recovered old data.")
  126.    end
  127.    recoveredHistory.close()
  128. end  
  129. initDisplays()
  130. while true do
  131.    -- Initialize displays, draw the graph axis on bigDisplay and grab AE data
  132.    -- local allItems = aeStorage.getAvailableItems()
  133.    table.insert( historyType, aeStorage.getRemainingItemTypes() + aeStorage.getStoredItemTypes() )
  134.    table.insert( historyCount, aeStorage.getRemainingItemCount() + aeStorage.getStoredItemCount() )
  135.    fs.delete("AE_History")
  136.    recoveredHistory = fs.open("AE_History", "w")
  137.    local tempTable = {
  138.       Type = historyType ;
  139.       Count = historyCount ;
  140.    }
  141.    recoveredHistory.write( JSON:encode( tempTable ) )
  142.    recoveredHistory.close()
  143.    local leftLen = string.len( tostring( math.max(unpack(historyCount)) ) )
  144.    local rightLen = string.len( tostring( math.max(unpack(historyType)) ) )
  145.    x, y = bigDisplay.getSize()
  146.    graphWindow.x = leftLen + 3
  147.    graphWindow.y = 1
  148.    graphWindow.xlen = ( x - ( leftLen + 2 ) ) - ( rightLen + 2 )
  149.    graphWindow.ylen = y - 1
  150.    buildGraph( historyCount, historyType )
  151.    drawScale( math.max(unpack(historyCount)), math.max(unpack(historyType)) )
  152.    drawGraph()
  153.    -- Draw graphs
  154.    printDebug(textutils.serialize(historyType).." - "..textutils.serialize(historyCount))
  155.    sleep(60)
  156.    -- historyCount = rotateHistory( historyCount )
  157.    -- historyType = rotateHistory( historyType )
  158. end
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment