Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local chest = peripheral.find("chest")
- local me = peripheral.find("aemultipart")
- local mon = peripheral.find("monitor")
- local dataMax = 300
- local readsPerSecond = 2
- local items = {}
- local resolution = "min"
- local scale = 60
- local endings = {"K","M","B","T","Q","WTF"}
- local timer = os.startTimer(1/readsPerSecond)
- function linReg(data)
- if #data == 0 then return 0,0 end
- local i
- local x,y,x2,y2,xy = 0,0,0,0,0
- for i=1,#data do
- x = x + i
- y = y + data[i]
- x2 = x2 + i*i
- y2 = y2 + data[i]*data[i]
- xy = xy + i*data[i]
- end
- local a,b = (x*x2-x*xy)/(#data*x2-x*x),(#data*xy-x*y)/(#data*x2-x*x)
- if a ~= a then a = 0 end
- if b ~= b then b = 0 end
- return a,b
- end
- function buildItemList()
- chest = peripheral.find("chest")
- me = peripheral.find("aemultipart")
- if not chest or not me then
- return
- end
- local list = {}
- local stacks = chest.getAllStacks()
- for k,v in pairs(stacks) do
- local a = v.all()
- local data = {
- current = -1,
- slope = 0,
- name = a.display_name,
- id = a.id,
- dmg = a.dmg,
- count = {}
- }
- if items and items[a.dmg.."~"..a.id] then
- data.count = items[a.dmg.."~"..a.id].count
- end
- if #data.count >= dataMax then table.remove(data.count,1) end
- local value = nil
- local ok = pcall(function() value = me.getItemDetail(a) end)
- if ok then
- data.current = value.basic().qty
- end
- table.insert(data.count,data.current)
- local b,m = linReg(data.count)
- data.slope = m
- list[a.dmg.."~"..a.id] = data
- end
- items = list
- end
- function round(n)
- if n%2 ~= 0.5 then
- return math.floor(n+0.5)
- end
- return n-0.5
- end
- function adjustNumber(n)
- if n == 0 then return 0 end
- local d = math.floor(math.log10(math.abs(n)))+1
- local adj = 4
- if d > 4 or d < 2 then adj = 3 end
- n = math.floor(n/math.pow(10,d-adj))*math.pow(10,d-adj)
- if d > 4 then n = (n * math.pow(10,-math.floor((d-1)/3)*3))..(endings[math.floor((d-1)/3)] or "") end
- return n
- end
- function getScaledSlope(slope)
- return round(slope*readsPerSecond*getScale()*100)/100
- end
- function getScale()
- return scale
- end
- function getScaledUnits()
- return resolution
- end
- function showStats()
- term.redirect(mon)
- local r=1
- local w,h = term.getSize()
- for k,v in pairs(items) do
- term.setCursorPos(1,r)
- if #v.count < dataMax/3 then
- term.setTextColor(colors.gray)
- elseif #v.count < dataMax*2/3 then
- term.setTextColor(colors.lightGray)
- else
- term.setTextColor(colors.white)
- end
- term.clearLine()
- term.write(#v.count.." ")
- term.setCursorPos(5,r)
- if term.isColor() then term.setTextColor(colors.yellow) else term.setTextColor(colors.white) end
- term.write(v.name..": ")
- term.setTextColor(colors.white)
- term.write(adjustNumber(v.current).." ")
- if term.isColor() then term.setTextColor(colors.red) else term.setTextColor(colors.lightGray) end
- term.write("("..adjustNumber(getScaledSlope(v.slope)).." / "..getScaledUnits()..")")
- r = r + 1
- end
- while r <= h do
- term.setCursorPos(1,r)
- term.clearLine()
- r = r + 1
- end
- term.setTextColor(colors.white)
- term.setCursorPos(2,h)
- term.write("^")
- term.setCursorPos(5,h)
- term.write("v")
- if getScaledUnits() == "sec" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
- term.setCursorPos(w-15,h)
- term.write("sec")
- if getScaledUnits() == "min" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
- term.setCursorPos(w-9,h)
- term.write("min")
- if getScaledUnits() == "hr" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
- term.setCursorPos(w-3,h)
- term.write("hr")
- end
- while true do
- event = {os.pullEvent()}
- if event[1] == "monitor_touch" then
- local x,y = event[3],event[4]
- local w,h = mon.getSize()
- term.setCursorPos(x,y)
- term.write("o")
- if y == h and x >= 1 and x <= 3 then
- --scroll up
- elseif y == h and x >= 4 and x <= 6 then
- --scroll down
- elseif y == h and x >= w-16 and x <= w - 12 then
- resolution = "sec"
- scale = 1
- elseif y == h and x >= w-10 and x <= w - 6 then
- resolution = "min"
- scale = 60
- elseif y == h and x >= w - 4 and x <= w then
- resolution = "hr"
- scale = 3600
- end
- elseif event[1] == "timer" then
- if event[2] == timer then
- buildItemList()
- showStats()
- timer = os.startTimer(1/readsPerSecond)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment