Theudas

Fluid Module

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