Taw_

Untitled

Aug 24th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local chest = peripheral.find("chest")
  2. local me = peripheral.find("aemultipart")
  3. local mon = peripheral.find("monitor")
  4. local dataMax = 300
  5. local readsPerSecond = 2
  6. local items = {}
  7.  
  8. local resolution = "min"
  9. local scale = 60
  10.  
  11. local endings = {"K","M","B","T","Q","WTF"}
  12.  
  13. local timer = os.startTimer(1/readsPerSecond)
  14.  
  15. function linReg(data)
  16.     if #data == 0 then return 0,0 end
  17.     local i
  18.     local x,y,x2,y2,xy = 0,0,0,0,0
  19.     for i=1,#data do
  20.         x = x + i
  21.         y = y + data[i]
  22.         x2 = x2 + i*i
  23.         y2 = y2 + data[i]*data[i]
  24.         xy = xy + i*data[i]
  25.     end
  26.     local a,b = (x*x2-x*xy)/(#data*x2-x*x),(#data*xy-x*y)/(#data*x2-x*x)
  27.     if a ~= a then a = 0 end
  28.     if b ~= b then b = 0 end
  29.     return a,b
  30. end
  31.  
  32. function buildItemList()
  33.     chest = peripheral.find("chest")
  34.     me = peripheral.find("aemultipart")
  35.     if not chest or not me then
  36.         return
  37.     end
  38.     local list = {}
  39.     local stacks = chest.getAllStacks()
  40.     for k,v in pairs(stacks) do
  41.         local a = v.all()
  42.         local data = {
  43.             current = -1,
  44.             slope = 0,
  45.             name = a.display_name,
  46.             id = a.id,
  47.             dmg = a.dmg,
  48.             count = {}
  49.         }
  50.         if items and items[a.dmg.."~"..a.id] then
  51.             data.count = items[a.dmg.."~"..a.id].count
  52.         end
  53.         if #data.count >= dataMax then table.remove(data.count,1) end
  54.         local value = nil
  55.         local ok = pcall(function() value = me.getItemDetail(a) end)
  56.         if ok then
  57.             data.current = value.basic().qty
  58.         end
  59.         table.insert(data.count,data.current)
  60.         local b,m = linReg(data.count)
  61.         data.slope = m
  62.         list[a.dmg.."~"..a.id] = data
  63.     end
  64.     items = list
  65. end
  66.  
  67. function round(n)
  68.     if n%2 ~= 0.5 then
  69.         return math.floor(n+0.5)
  70.     end
  71.     return n-0.5
  72. end
  73.  
  74. function adjustNumber(n)
  75.     if n == 0 then return 0 end
  76.     local d = math.floor(math.log10(math.abs(n)))+1
  77.     local adj = 4
  78.     if d > 4 or d < 2 then adj = 3 end
  79.     n = math.floor(n/math.pow(10,d-adj))*math.pow(10,d-adj)
  80.     if d > 4 then n = (n * math.pow(10,-math.floor((d-1)/3)*3))..(endings[math.floor((d-1)/3)] or "") end
  81.     return n
  82. end
  83.  
  84. function getScaledSlope(slope)
  85.     return round(slope*readsPerSecond*getScale()*100)/100
  86. end
  87.  
  88. function getScale()
  89.     return scale
  90. end
  91.  
  92. function getScaledUnits()
  93.     return resolution
  94. end
  95.  
  96. function showStats()
  97.     term.redirect(mon)
  98.     local r=1
  99.     local w,h = term.getSize()
  100.     for k,v in pairs(items) do
  101.         term.setCursorPos(1,r)
  102.         if #v.count < dataMax/3 then
  103.             term.setTextColor(colors.gray)
  104.         elseif #v.count < dataMax*2/3 then
  105.             term.setTextColor(colors.lightGray)
  106.         else
  107.             term.setTextColor(colors.white)
  108.         end
  109.         term.clearLine()
  110.         term.write(#v.count.." ")
  111.         term.setCursorPos(5,r)
  112.         if term.isColor() then term.setTextColor(colors.yellow) else term.setTextColor(colors.white) end
  113.         term.write(v.name..": ")
  114.         term.setTextColor(colors.white)
  115.         term.write(adjustNumber(v.current).." ")
  116.         if term.isColor() then term.setTextColor(colors.red) else term.setTextColor(colors.lightGray) end
  117.         term.write("("..adjustNumber(getScaledSlope(v.slope)).." / "..getScaledUnits()..")")
  118.         r = r + 1
  119.     end
  120.     while r <= h do
  121.         term.setCursorPos(1,r)
  122.         term.clearLine()
  123.         r = r + 1
  124.     end
  125.     term.setTextColor(colors.white)
  126.     term.setCursorPos(2,h)
  127.     term.write("^")
  128.     term.setCursorPos(5,h)
  129.     term.write("v")
  130.     if getScaledUnits() == "sec" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
  131.     term.setCursorPos(w-15,h)
  132.     term.write("sec")
  133.     if getScaledUnits() == "min" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
  134.     term.setCursorPos(w-9,h)
  135.     term.write("min")
  136.     if getScaledUnits() == "hr" then term.setTextColor(colors.green) else term.setTextColor(colors.red) end
  137.     term.setCursorPos(w-3,h)
  138.     term.write("hr")
  139. end
  140.  
  141. while true do
  142.     event = {os.pullEvent()}
  143.     if event[1] == "monitor_touch" then
  144.         local x,y = event[3],event[4]
  145.         local w,h = mon.getSize()
  146.         term.setCursorPos(x,y)
  147.         term.write("o")
  148.         if y == h and x >= 1 and x <= 3 then
  149.                 --scroll up
  150.         elseif y == h and x >= 4 and x <= 6 then
  151.                 --scroll down
  152.         elseif y == h and x >= w-16 and x <= w - 12 then
  153.             resolution = "sec"
  154.             scale = 1
  155.         elseif y == h and x >= w-10 and x <= w - 6 then
  156.             resolution = "min"
  157.             scale = 60
  158.         elseif y == h and x >= w - 4 and x <= w then
  159.             resolution = "hr"
  160.             scale = 3600
  161.         end
  162.     elseif event[1] == "timer" then
  163.         if event[2] == timer then
  164.             buildItemList()
  165.             showStats()
  166.             timer = os.startTimer(1/readsPerSecond)
  167.         end
  168.     end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment