Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local aeStorage = peripheral.wrap("bottom")
- local bigDisplay = peripheral.wrap("top")
- local firstTopTen = peripheral.wrap("left")
- local secondTopTen = peripheral.wrap("right")
- local leftColor = 8192 -- green
- local rightColor = 2048 -- blue
- local overlapColor = 16 -- yellow
- local graphWindow = {}
- local x
- local y
- local JSON = (loadfile "JSON.lua")()
- JSON.strictTypes = true
- function printDebug(textString)
- print(textString)
- end
- function initDisplays()
- bigDisplay.clear()
- bigDisplay.setTextScale(.5)
- firstTopTen.clear()
- secondTopTen.clear()
- end
- function num2Text( inputNumber, textLength )
- inputNumber = tostring( inputNumber )
- if string.len(inputNumber ) < textLength then
- inputNumber = string.rep( " " , textLength - string.len( inputNumber ) )..inputNumber
- elseif string.len(inputNumber ) > textLength then
- inputNumber = "ERR: String too long"
- end
- return inputNumber
- end
- function valueToScale( minValue, maxValue, inputValue )
- return math.ceil( ( graphWindow.ylen - 1 ) - ( inputValue - minValue ) / ( ( maxValue - minValue ) / ( graphWindow.ylen - 2 ) ) )
- end
- function scaleToValue( minValue, maxValue, inputScale )
- return math.floor( maxValue - ( ( maxValue - minValue) / ( graphWindow.ylen - 1 ) ) * ( inputScale - 1 ) )
- end
- local maxYLeft
- local maxYRight
- local minYLeft
- local minYRight
- function drawScale( leftArray, rightArray )
- local leftLen = string.len( tostring( maxYLeft ) )
- local rightLen = string.len( tostring( maxYRight ) )
- for i = graphWindow.ylen, graphWindow.y, - 1 do
- bigDisplay.setCursorPos( 1 , i )
- bigDisplay.setTextColor(leftColor)
- bigDisplay.write( num2Text( scaleToValue(minYLeft, maxYLeft, i), leftLen ).."-|" )
- bigDisplay.setCursorPos( graphWindow.xlen + graphWindow.x , i )
- bigDisplay.setTextColor(rightColor)
- bigDisplay.write( "|-"..num2Text( scaleToValue(minYRight, maxYRight, i), rightLen ) )
- end
- bigDisplay.setCursorPos( graphWindow.x , y )
- bigDisplay.setTextColor(1)
- bigDisplay.write( string.rep( "-", graphWindow.xlen ) )
- end
- local leftGraphTable
- local rightGraphTable
- function buildGraph( leftArray, rightArray )
- maxYLeft = math.max(unpack(leftArray))
- maxYRight = math.max(unpack(rightArray))
- minYLeft = math.min(unpack(leftArray))
- minYRight = math.min(unpack(rightArray))
- local extendLeft = ( maxYLeft - minYLeft ) / 10
- local extendRight = ( maxYRight - minYRight ) / 10
- maxYLeft = math.ceil( maxYLeft + extendLeft )
- maxYRight = math.ceil( maxYRight + extendRight )
- minYLeft = math.max(math.floor( minYLeft - extendLeft ), 0 )
- minYRight = math.max(math.floor( minYRight - extendRight ), 0 )
- leftGraphTable = {}
- rightGraphTable = {}
- local lefti = #leftArray
- local righti = #rightArray
- for i = graphWindow.xlen - 1 , 1, - 1 do
- if lefti == 0 or righti == 0 then break end
- leftGraphTable[i] = valueToScale(minYLeft, maxYLeft, leftArray[lefti] )
- rightGraphTable[i] = valueToScale(minYRight, maxYRight, rightArray[righti] )
- lefti = lefti - 1
- righti = righti - 1
- end
- printDebug(textutils.serialize(leftGraphTable))
- printDebug(textutils.serialize(leftArray))
- end
- function drawGraph()
- for i = graphWindow.xlen - 1 , 1, - 1 do
- if not leftGraphTable[i] or not rightGraphTable[i] then break end
- for l = 1, graphWindow.ylen do
- bigDisplay.setCursorPos( i + graphWindow.x, l)
- bigDisplay.write(" ")
- end
- bigDisplay.setCursorPos( i + graphWindow.x, leftGraphTable[i])
- if leftGraphTable[i] == rightGraphTable[i] then
- -- If the table values are the same, draw them as an overlap
- bigDisplay.setTextColor( overlapColor )
- bigDisplay.write("=")
- else
- -- Otherwise, draw one
- bigDisplay.setTextColor( leftColor )
- bigDisplay.write("-")
- -- Then the other
- bigDisplay.setCursorPos( i + graphWindow.x, rightGraphTable[i])
- bigDisplay.setTextColor( rightColor )
- bigDisplay.write("-")
- end
- end
- end
- function rotateHistory( inputArray )
- local outputArray
- if not inputArray or #inputArray < ( graphWindow.xlen + 3 ) then
- outputArray = inputArray
- else
- for i = 2, #inputArray do
- outputArray[i-1] = inputArray[i]
- end
- inputArray[#inputArray] = nil
- end
- return outputArray
- end
- local historyType = {}
- local historyCount = {}
- if fs.exists("AE_History") then
- local recoveredHistory = fs.open("AE_History", "r")
- local historyData = recoveredHistory.readLine()
- printDebug(historyData)
- if historyData then
- historyData = JSON:decode(historyData)
- printDebug(textutils.serialize(historyData))
- historyType = historyData["Type"]
- historyCount = historyData["Count"]
- printDebug("Recovered old data.")
- end
- recoveredHistory.close()
- end
- initDisplays()
- while true do
- -- Initialize displays, draw the graph axis on bigDisplay and grab AE data
- -- local allItems = aeStorage.getAvailableItems()
- table.insert( historyType, aeStorage.getRemainingItemTypes() + aeStorage.getStoredItemTypes() )
- table.insert( historyCount, aeStorage.getRemainingItemCount() + aeStorage.getStoredItemCount() )
- fs.delete("AE_History")
- recoveredHistory = fs.open("AE_History", "w")
- local tempTable = {
- Type = historyType ;
- Count = historyCount ;
- }
- recoveredHistory.write( JSON:encode( tempTable ) )
- recoveredHistory.close()
- local leftLen = string.len( tostring( math.max(unpack(historyCount)) ) )
- local rightLen = string.len( tostring( math.max(unpack(historyType)) ) )
- x, y = bigDisplay.getSize()
- graphWindow.x = leftLen + 3
- graphWindow.y = 1
- graphWindow.xlen = ( x - ( leftLen + 2 ) ) - ( rightLen + 2 )
- graphWindow.ylen = y - 1
- buildGraph( historyCount, historyType )
- drawScale( historyCount, historyType )
- -- Draw graphs
- drawGraph()
- -- printDebug(textutils.serialize(historyType).." - "..textutils.serialize(historyCount))
- sleep(600)
- -- historyCount = rotateHistory( historyCount )
- -- historyType = rotateHistory( historyType )
- end
Advertisement
Add Comment
Please, Sign In to add comment