Advertisement
SkyNetCloud

fluid

Mar 22nd, 2024 (edited)
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | Gaming | 0 0
  1. ---------------------------------------------
  2. --  Tank module for caftNanny
  3. --  by demethan
  4. --  www.breakfastcraft.com
  5. --  www.craftnanny.org
  6. --  2015 08 12  demethan:
  7. --      -fixed modem support
  8. --      -did some error magement
  9. --      -added visual bar  
  10. ---------------------------------------------
  11.  
  12. -- variables
  13.  
  14. local containers={}
  15. local version = 2
  16.  
  17. local installer = "Q8ah3K9S"
  18. local token = '0'
  19. local module_name = ''
  20. local username = ''
  21. local type = ''
  22.  
  23. -- write text to the terminal screen
  24. function draw_text_term(x, y, text, text_color, bg_color)
  25.   term.setTextColor(text_color)
  26.   term.setBackgroundColor(bg_color)
  27.   term.setCursorPos(x,y)
  28.   write(text)
  29. end
  30.  
  31. -- draw a line on the terminal screen
  32. function draw_line_term(x, y, length, color)
  33.     term.setBackgroundColor(color)
  34.     term.setCursorPos(x,y)
  35.     term.write(string.rep(" ", length))
  36. end
  37.  
  38. function bars()
  39.     draw_line_term(1, 1, 51, colors.lime)
  40.     draw_line_term(1, 19, 51, colors.lime)
  41.     draw_text_term(15, 1, 'CraftNanny Fluid Module', colors.gray, colors.lime)
  42.     draw_text_term(10, 19, 'www.craftnanny.org', colors.gray, colors.lime)
  43. end
  44.  
  45. function terminal_screen()
  46.     term.clear()
  47.    
  48.     bars()
  49.     draw_text_term(1, 2, 'Module: ', colors.lime, colors.black)
  50.     draw_text_term(10, 2, module_name, colors.white, colors.black)
  51.     draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  52.     draw_text_term(8, 3, username, colors.white, colors.black)
  53.     draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  54.    
  55.     --draw_text_term(2, 8, "I dont know what to put here...", colors.white, colors.black)
  56. end
  57.  
  58. -- retrieves token from local text file
  59. function load_config()
  60.   sr = fs.open("config.txt", "r")
  61.     token = sr.readLine()
  62.     module_name = sr.readLine()
  63.     username = sr.readLine()
  64.     type = sr.readLine()
  65.   sr.close()
  66. end
  67.  
  68. -- called for new installations and when the scanner needs to be updated
  69. function run_installer()
  70.     if fs.exists("install") then
  71.         fs.delete("install")
  72.       end
  73.       shell.run("pastebin get "..installer.." install")
  74.       sleep(1)
  75.       shell.run("install")
  76. end
  77.  
  78.  
  79. ------  Start module specific code ---------
  80.  
  81.  
  82. function phone_home(tank_name, fluid_type, percent)
  83.     response = http.post("https://craftnanny.org/code/fluid.php",
  84.                 "token="..token.."&id="..os.getComputerID().."&tank_name="..tank_name.."&fluid_type="..fluid_type.."&percent="..percent)       
  85.     return_string = response.readAll()
  86.    
  87.     if tonumber(return_string) > version then
  88.             run_installer()
  89.     end
  90. end
  91.  
  92.  
  93. -- function findPeripheralSide()
  94. --     -- Define the list of faces to check
  95. --     local faces = {"left", "right", "bottom", "top", "back"}
  96.  
  97. --     -- Iterate through each face
  98. --     for _, face in ipairs(faces) do
  99. --         if peripheral.isPresent(face) then
  100. --             return true, face  -- Return if a peripheral is found on this face
  101. --         end
  102. --     end
  103.  
  104. --     return false, ""  -- Return false if no peripheral is found
  105. -- end
  106.  
  107. function round(num, decimals)
  108.     local mult = 10^(decimals or 0)
  109.     return math.floor(num * mult + 0.5) / mult
  110. end
  111.  
  112. function capitalizeAndRemoveUnderscores(str)
  113.     return str:gsub("([^_]+)([^_]*)", function(first, rest)
  114.         first = first:gsub("^%l", string.upper) -- Capitalize the first letter of the first word
  115.         rest = rest:gsub("^%l", string.upper)   -- Capitalize the first letter of the second word
  116.         return first:gsub("_", "") .. rest:gsub("_", "") -- Concatenate the words after removing underscores
  117.     end)
  118. end
  119.  
  120.  
  121.  
  122. function getTankInformation(t, tankName)
  123.     local tnk = peripheral.wrap(t)
  124.     local okLiquid, msg = pcall(tnk.getInfo)
  125.  
  126.     if okLiquid then
  127.         local amount = msg.amount
  128.         local capacity = msg.capacity
  129.         local fluid = tostring(msg.fluid):gsub("^[^:]*:", "")  -- Remove anything before the first colon
  130.         fluid = capitalizeAndRemoveUnderscores(fluid) -- Capitalize the first letter
  131.         local percent = round((amount / capacity * 100), 2)
  132.  
  133.        
  134.         phone_home(tankName, fluid, percent)
  135.        
  136.         print(fluid, ":")
  137.         local graphBar = round(((term.getSize() * percent) / 100), 0)
  138.         if graphBar < 50 then
  139.             draw_line_term(6, 7, graphBar , colors.green)
  140.             draw_line_term(6 + graphBar, 7, term.getSize() - graphBar - 6, colors.red)
  141.             draw_text_term(1, 7, percent .. " % ", colors.lime, colors.black)
  142.             term.setBackgroundColor(colors.black)
  143.         else
  144.             draw_line_term(6, 7, graphBar - 6 , colors.green)
  145.             draw_text_term(1, 7, percent .. " % ", colors.lime, colors.black)
  146.             term.setBackgroundColor(colors.black)
  147.         end
  148.         return true
  149.     else
  150.         return false
  151.     end
  152. end
  153.  
  154.  
  155. function start_loop()
  156.     local side
  157.     local tanks = peripheral.getNames()
  158.     local faces = {"left", "right", "bottom", "top", "back"}
  159.  
  160.     -- Find the side where a peripheral is present
  161.     for _, face in ipairs(faces) do
  162.         if peripheral.isPresent(face) then
  163.             side = face
  164.             break
  165.         end
  166.     end
  167.  
  168.     -- -- If no peripheral is found, exit the loop
  169.     -- if not side then
  170.     --     print("No tank storage found")
  171.     --     print("Please check your modems")
  172.     --     return
  173.     -- end
  174.  
  175.     while true do
  176.         terminal_screen()
  177.  
  178.         if #tanks > 2 then
  179.             print("Only one device is supported")
  180.             break
  181.         elseif #tanks == 2 then
  182.             for _, tank in pairs(tanks) do
  183.                 if tank ~= side then
  184.                     ok = getTankInformation(tank, tank)          
  185.                 end
  186.             end
  187.         else
  188.             ok = getTankInformation(side, "Tank"..os.getComputerID())
  189.         end
  190.  
  191.         if not ok then
  192.             print("No tank storage found")
  193.             print("Do you have the right module?")
  194.             print("Please check your modems")
  195.             break
  196.         end
  197.  
  198.         -- Main active status with server
  199.         sleep(30)
  200.     end
  201. end
  202.  
  203.  
  204. function start()
  205.     term.clear()
  206.     term.setCursorPos(1,1)
  207.    
  208.   if fs.exists("config.txt") then
  209.       load_config()
  210.       start_loop()
  211.   else
  212.       run_installer()
  213.   end
  214. end
  215.  
  216. start()
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement