Theudas

Untitled

Sep 7th, 2015
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.17 KB | None | 0 0
  1.         ---------------------------------------------
  2.         --      Tank module for caftNanny
  3.         --      by demethan
  4.         --      www.breakfastcraft.com
  5.         ---------------------------------------------
  6.  
  7.         -- variables
  8.  
  9.     local containers={}
  10.     local version = 1
  11.  
  12.     local installer = "Ja5bQSqT"
  13.     local token = '0'
  14.     local module_name = ''
  15.     local username = ''
  16.     local tankArray = {}
  17.     local names = {}
  18.     local mon = term
  19.  
  20.     -- write text to the terminal screen
  21.     function draw_text_term(x, y, text, text_color, bg_color)
  22.         mon.setTextColor(text_color)
  23.         mon.setBackgroundColor(bg_color)
  24.         mon.setCursorPos(x,y)
  25.         mon.write(text)
  26.     end
  27.  
  28.     -- draw a line on the terminal screen
  29.     function draw_line_term(x, y, length, color)
  30.         mon.setBackgroundColor(color)
  31.         mon.setCursorPos(x,y)
  32.         mon.write(string.rep(" ", length))
  33.     end
  34.  
  35.     function bars()
  36.         draw_line_term(1, 1, 51, colors.lime)
  37.         draw_line_term(1, 19, 51, colors.lime)
  38.         draw_text_term(15, 1, 'CraftNanny Fluid Module', colors.gray, colors.lime)
  39.         draw_text_term(10, 19, 'www.craftnanny.org', colors.gray, colors.lime)
  40.     end
  41.  
  42.     function terminal_screen()
  43.         mon.clear()
  44.    
  45.         bars()
  46.         draw_text_term(1, 2, 'Module: ', colors.lime, colors.black)
  47.         draw_text_term(10, 2, module_name, colors.white, colors.black)
  48.         draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  49.         draw_text_term(8, 3, username, colors.white, colors.black)
  50.         draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  51.    
  52.         --draw_text_term(2, 8, "I dont know what to put here...", colors.white, colors.black)
  53.     end
  54.  
  55.     -- retrieves token from local text file
  56.     function load_config()
  57.         sr = fs.open("config.txt", "r")
  58.         token = sr.readLine()
  59.         module_name = sr.readLine()
  60.         username = sr.readLine()
  61.         type = sr.readLine()
  62.         sr.close()
  63.     end
  64.  
  65.     -- called for new installations and when the scanner needs to be updated
  66.     function run_installer()
  67.         if fs.exists("install") then
  68.                 fs.delete("install")
  69.         end
  70.         shell.run("pastebin get "..installer.." install")
  71.         sleep(1)
  72.         shell.run("install")
  73.     end
  74.  
  75.  
  76.  
  77.  
  78.         ------  Start module specific code ---------
  79.  
  80.  
  81.     ------ TANK Class, lets do it oop style baby --------
  82.  
  83.     Tank = {
  84.         name="", fluidName="Nothing", fluidAmount=0, peripheral="", fluidCapacity=0, fluidPercent=0
  85.     }
  86.  
  87.     -- creator class
  88.     function Tank:new (o)
  89.             o = o or {}   -- create object if user does not provide one
  90.             setmetatable(o, self)
  91.             self.__index = self
  92.             return o
  93.     end
  94.    
  95.     --calculate how much filled we are
  96.     function Tank:calcPercent()
  97.         if self.fluidAmount > 0 then
  98.             return round((self.fluidAmount/self.fluidCapacity*100),2)
  99.         else
  100.             return 0
  101.         end
  102.     end
  103.    
  104.     -- read the pressurepipes tanks
  105.     function Tank:readPPTank()
  106.         self.fluidCapacity  = self.p.getCapacity()
  107.  
  108.         if(self.p.hasFluid()) then
  109.             local fluid         = self.p.getFluid()
  110.                    
  111.             self.fluidAmount    = fluid["amount"]
  112.             self.fluidName      = fluid["name"]
  113.             self.fluidPercent   = self:calcPercent()
  114.         else
  115.             self.fluidName      = "nothing"
  116.             self.Amount         = 0
  117.         end
  118.     end
  119.    
  120.     -- read the standard bc tanks
  121.     function Tank:readStandardTank()
  122.         local tankTbl       = self.p.getTankInfo()
  123.         self.fluidCapacity  = tankTbl[1].capacity
  124.         contentsTbl         = tankTbl[1].contents or {["rawName"]="nothing",["amount"]=0}
  125.         self.fluidName      = contentsTbl.rawName
  126.         self.fluidAmount    = contentsTbl.amount    
  127.         self.fluidPercent   = self:calcPercent()
  128.     end
  129.    
  130.     -- find out what kind of tank is there
  131.     function Tank:readTank()
  132.         if t.p.isConnected then
  133.             self:readPPTank()
  134.         elseif t.p.getTankInfo then
  135.             t:readStandardTank()
  136.         end
  137.     end
  138.     ------ // Tank Class -------
  139.  
  140.     function phone_home(t)
  141.         response = http.post("http://craftnanny.org/code/fluid.php",
  142.             "token="..token.."&id="..os.getComputerID().."&tank_name="..t.name.."&fluid_type="..t.fluidName.."&percent="..t.fluidPercent)              
  143.         return_string = response.readAll()
  144.            
  145.         if tonumber(return_string) > version then
  146.             run_installer()
  147.         end
  148.     end
  149.  
  150.     function writeScreen(t, i)
  151.         draw_text_term(1, 2+i*3, t.fluidName..": ",colors.white,colors.black)
  152.         graphBar= round((((mon.getSize()-10)*t.fluidPercent)/100),0)
  153.         i = i-1
  154.         if graphBar < 50 then
  155.             draw_line_term(10, 6+i*3, graphBar , colors.green)
  156.             draw_line_term(10+graphBar,6+i*3,mon.getSize()-graphBar-10,colors.red)
  157.             draw_text_term(1, 6+i*3, t.fluidPercent.." % ",colors.lime,colors.black)
  158.             mon.setBackgroundColor(colors.black)
  159.         else
  160.             draw_line_term(10, 6+i*3, graphBar-10 , colors.green)
  161.             draw_text_term(1, 6+i*3, t.fluidPercent.." % ",colors.lime,colors.black)
  162.             mon.setBackgroundColor(colors.black)
  163.         end      
  164.     end
  165.        
  166.     local function filter(name)
  167.         table.insert(names, name)
  168.         return true
  169.     end
  170.        
  171.     function getModems()
  172.         local m = {}
  173.        
  174.         -- get pp Tanks
  175.         peripheral.find("tank_dataport", filter)
  176.         for i = 1, #names, 1 do
  177.             local t = Tank:new{}
  178.             print(names[i])
  179.             t.p = peripheral.wrap(names[i])
  180.             t.name = "Tank"..os.getComputerID()
  181.             table.insert(tankArray, t)
  182.         end
  183.        
  184.         -- get standard Tanks
  185.         local x = peripheral.getNames()
  186.         for i = 1, #x, 1 do
  187.             if pcall(peripheral.wrap(x[i]).getTankInfo) then
  188.                 local t = Tank:new{}
  189.                 t.p = peripheral.wrap(x[i])
  190.                 t.name = "Tank"..os.getComputerID()
  191.                 table.insert(tankArray, t)
  192.             end
  193.         end
  194.     end
  195.        
  196.  
  197.     function round(num, idp)
  198.         local mult = 10^(idp or 0)
  199.         return math.floor(num * mult + 0.5) / mult
  200.     end
  201.        
  202.     function updateTankInfo()
  203.         for i = 1, #tankArray, 1 do
  204.             t = tankArray[i]
  205.             t:readTank()
  206.             writeScreen(t, i)
  207.             phone_home(t)
  208.         end
  209.     end
  210.  
  211.     function notanks()
  212.         -- relavent error msg
  213.     end
  214.  
  215.     -- Main Loop
  216.     function start_loop()
  217.         terminal_screen()
  218.         getModems()
  219.            
  220.         while true do
  221.             updateTankInfo()
  222.             -- main active status with server
  223.             --sleep(30)
  224.             return true
  225.         end
  226.     end
  227.  
  228.     function start()
  229.         mon.clear()
  230.         mon.setCursorPos(1,1)
  231.        
  232.         -- find a monitor to print onto
  233.         local monitor = peripheral.find("monitor")
  234.         if monitor then
  235.         mon = monitor
  236.         end
  237.                
  238.         --turn mainloop on if we are logged in
  239.         if fs.exists("config.txt") then
  240.             load_config()
  241.             start_loop()
  242.         else
  243.             run_installer()
  244.         end
  245.     end
  246.  
  247.     start()
Advertisement
Add Comment
Please, Sign In to add comment