Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3.     UU Monitor by JJJan
  4.     ____________________________________________________________________________
  5.  
  6.     * Monitor, Sensor Peripheral and Applied Energetics Sensor Card required
  7.  
  8.     * The Script automatically detects the monitor and sensor
  9.  
  10.     * You may switch the displayed time intervall by typing
  11.       sec, min, hour, day in the console
  12.     ____________________________________________________________________________
  13.  
  14.     Git: https://git.bitrain.co/computercraft/computer
  15.     Bugs: https://git.bitrain.co/computercraft/computer/issues
  16.  
  17. ]]--
  18.  
  19. os.loadAPI("ocs/apis/sensor")
  20.  
  21. sleep(2) -- Prevents crashing due to slow chunk loading
  22.  
  23. local sensorcard    = nil
  24. local monitor       = nil
  25. local position      = nil
  26. local item          = "Iron%20Ingot"
  27.  
  28. local wait          = 5
  29. local multiplier    = 1
  30. local scale         = "SEC"
  31. local data          = {}
  32. local body          = nil
  33. local stored        = 0
  34. local elapsed       = 0
  35. local time          = 0
  36. local tps           = 0
  37.  
  38. local function initialize()
  39.     -- Autodetect Function by JJJan
  40.     for i, side in pairs(redstone.getSides()) do
  41.         if peripheral.isPresent(side) then
  42.             type = peripheral.getType(side)
  43.             if type == "monitor" then
  44.                 monitor = peripheral.wrap(side)
  45.                 monitor.setTextScale(0.5)
  46.             end
  47.             if type == "sensor" then
  48.                 if position then
  49.                     error()
  50.                 end
  51.                 sensorcard = sensor.wrap(side)
  52.                 targets = sensorcard.getTargets()
  53.                 for key, value in pairs(targets) do
  54.                     if value.RawName == "tile.appeng.mb.mewirelessaccesspoint" then
  55.                         position = key
  56.                     end
  57.                 end
  58.             end
  59.         end
  60.     end
  61. end
  62.  
  63. local function round(num, idp)
  64.     local mult = 10^(idp or 0)
  65.     return math.floor(num * mult + 0.5) / mult
  66. end
  67.  
  68. local function format(n)
  69.     -- Format Function by JJJan
  70.     if n >= 1000000 then
  71.         return round(n/1000000, 2) .. " M"
  72.     elseif n >= 1000 then
  73.         return round(n/1000, 2) .. " K"
  74.     else
  75.         return round(n, 2)
  76.     end
  77. end
  78.  
  79. local function getInput()
  80.     while true do
  81.         input = read()
  82.  
  83.         if input then
  84.             if input == "sec" then
  85.                 multiplier = 1
  86.                 scale = string.upper(input)
  87.             elseif input == "min" then
  88.                 multiplier = 60
  89.                 scale = string.upper(input)
  90.             elseif input == "hour" then
  91.                 multiplier = 3600
  92.                 scale = string.upper(input)
  93.             elseif input == "day" then
  94.                 multiplier = 86400
  95.                 scale = string.upper(input)
  96.             end
  97.         end
  98.  
  99.         sleep(0.6)
  100.     end
  101. end
  102.  
  103. local function tick()
  104.     while true do
  105.  
  106.         if not monitor then
  107.             print("No Monitor found!")
  108.             initialize()
  109.             break
  110.         end
  111.  
  112.         if not position then
  113.             if not sensorcard then
  114.                 print("No Sensor found!")
  115.                 initialize()
  116.             else
  117.                 print("No Target found!")
  118.             end
  119.  
  120.             break
  121.         end
  122.  
  123.         w, h = monitor.getSize()
  124.         data = sensorcard.getTargetDetails(position)
  125.         for key, value in ipairs(data["Items"]) do
  126.             if(value.Name == item) then
  127.                 request = http.get("http://cc.nghty.co/seconds")
  128.                 body = request.readAll()
  129.                 request.close()
  130.                 count = value.Size - stored
  131.                 elapsed = body - time
  132.                 tps = (wait * 20)/elapsed
  133.                 stored = value.Size
  134.                 time = body
  135.                 monitor.clear()
  136.                 monitor.setTextColor(256)
  137.                 monitor.setCursorPos(w/6 - string.len("Iron/" .. scale)/2, h/2 - 1)
  138.                 monitor.write("Iron/" .. scale)
  139.                 monitor.setTextColor(1)
  140.                 monitor.setCursorPos(w/6 - string.len(tostring(format((count/elapsed) * multiplier)))/2, h/2 + 2)
  141.                 monitor.write(format((count/elapsed) * multiplier))
  142.                 monitor.setTextColor(256)
  143.                 monitor.setCursorPos(w/2 - string.len("TPS")/2, h/2 - 1)
  144.                 monitor.write("TPS")
  145.                 if tps > 18 then
  146.                     monitor.setTextColor(32)
  147.                 elseif tps <= 18 and tps >= 15 then
  148.                     monitor.setTextColor(2)
  149.                 else
  150.                     monitor.setTextColor(16384)
  151.                 end
  152.                 monitor.setCursorPos(w/2 - string.len(tostring(round(tps, 2)))/2, h/2 + 2)
  153.                 monitor.write(round(tps, 2))
  154.                 monitor.setTextColor(256)
  155.                 monitor.setCursorPos(5*w/6 - string.len("STORED")/2, h/2 - 1)
  156.                 monitor.write("STORED")
  157.                 monitor.setTextColor(1)
  158.  
  159.                 monitor.setCursorPos(5*w/6 - string.len(tostring(format(stored)))/2, h/2 + 2)
  160.                 monitor.write(format(stored))
  161.             end
  162.         end
  163.         sleep(wait)
  164.     end
  165. end
  166.  
  167. initialize()
  168.  
  169. while true do
  170.  
  171.   parallel.waitForAny(tick, getInput)
  172.  
  173.   sleep(1)
  174.  
  175. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement