Advertisement
jared314

Redstone Module

Aug 6th, 2015
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.24 KB | None | 0 0
  1. -- CraftNanny
  2. -- Redstone module
  3. local version = 2
  4.  
  5. -- pastebin for installer
  6. local installer = "Q8ah3K9S"
  7. local time = 0
  8. local token = '0'
  9. -- this scanners name
  10. local module_name = ''
  11. -- owenrs username on website
  12. local username = ''
  13. local type = ''
  14.  
  15. local top = ''
  16. local bottom = ''
  17. local front = ''
  18. local back = ''
  19. local left = ''
  20. local right = ''
  21.  
  22. local top_input = 0
  23. local bottom_input = 0
  24. local front_input = 0
  25. local back_input = 0
  26. local left_input = 0
  27. local right_input = 0
  28.  
  29. -- write text to the terminal screen
  30. function draw_text_term(x, y, text, text_color, bg_color)
  31.   term.setTextColor(text_color)
  32.   term.setBackgroundColor(bg_color)
  33.   term.setCursorPos(x,y)
  34.   write(text)
  35. end
  36.  
  37. -- draw a line on the terminal screen
  38. function draw_line_term(x, y, length, color)
  39.     term.setBackgroundColor(color)
  40.     term.setCursorPos(x,y)
  41.     term.write(string.rep(" ", length))
  42. end
  43.  
  44. function bars()
  45.     draw_line_term(1, 1, 51, colors.lime)
  46.     draw_line_term(1, 19, 51, colors.lime)
  47.     draw_text_term(15, 1, 'CraftNanny Redstone Module', colors.gray, colors.lime)
  48.     draw_text_term(10, 19, 'www.craftnanny.org', colors.gray, colors.lime)
  49. end
  50.  
  51. function terminal_screen()
  52.     term.clear()
  53.    
  54.     bars()
  55.     draw_text_term(1, 2, 'Module: ', colors.lime, colors.black)
  56.     draw_text_term(10, 2, module_name, colors.white, colors.black)
  57.     draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  58.     draw_text_term(8, 3, username, colors.white, colors.black)
  59.     draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  60.    
  61.     draw_text_term(2, 6, 'Top: '..top, colors.white, colors.black)
  62.     draw_text_term(2, 7, 'Bottom: '..bottom, colors.white, colors.black)
  63.     draw_text_term(2, 8, 'Front: '..front, colors.white, colors.black)
  64.     draw_text_term(2, 9, 'Back: '..back, colors.white, colors.black)
  65.     draw_text_term(2, 10, 'Right: '..right, colors.white, colors.black)
  66.     draw_text_term(2, 11, 'Left: '..left, colors.white, colors.black)
  67. end
  68.  
  69. -- retrieves token from local text file
  70. function load_config()
  71.   sr = fs.open("config.txt", "r")
  72.     token = sr.readLine()
  73.     module_name = sr.readLine()
  74.     username = sr.readLine()
  75.     type = sr.readLine()
  76.   sr.close()
  77. end
  78.  
  79. -- called for new installations and when the scanner needs to be updated
  80. function run_installer()
  81.     if fs.exists("install") then
  82.         fs.delete("install")
  83.       end
  84.       shell.run("pastebin get "..installer.." install")
  85.       sleep(1)
  86.       shell.run("install")
  87. end
  88.  
  89.  
  90. ------  Start module specific code ---------
  91.  
  92.  
  93. function string:split(delimiter)
  94.   local result = { }
  95.   local from  = 1
  96.   local delim_from, delim_to = string.find( self, delimiter, from  )
  97.   while delim_from do
  98.     table.insert( result, string.sub( self, from , delim_from-1 ) )
  99.     from  = delim_to + 1
  100.     delim_from, delim_to = string.find( self, delimiter, from  )
  101.   end
  102.   table.insert( result, string.sub( self, from  ) )
  103.   return result
  104. end
  105.  
  106. function phone_home()
  107.     getInputs()
  108.  
  109.     response = http.post("http://craftnanny.org/code/redstone.php",
  110.                 "token="..token.."&id="..os.getComputerID().."&top_input="..top_input.."&bottom_input="..bottom_input.."&front_input="..front_input.."&back_input="..back_input.."&left_input="..left_input.."&right_input="..right_input)     
  111.     return_string = response.readAll()
  112.    
  113.     result_array = string.split(return_string,",")
  114.     current_version = result_array[1]
  115.    
  116.     if tonumber(result_array[2]) == 1 then
  117.         rs.setOutput('top', true)
  118.         top = 'true'
  119.     else
  120.         rs.setOutput('top', false)
  121.         top = 'false'
  122.     end
  123.     if tonumber(result_array[3]) == 1 then
  124.         rs.setOutput('bottom', true)
  125.         bottom = 'true'
  126.     else
  127.         rs.setOutput('bottom', false)
  128.         bottom = 'false'
  129.     end
  130.     if tonumber(result_array[4]) == 1 then
  131.         rs.setOutput('back', true)
  132.         back = 'true'
  133.     else
  134.         rs.setOutput('back', false)
  135.         back = 'false'
  136.     end
  137.     if tonumber(result_array[5]) == 1 then
  138.         rs.setOutput('front', true)
  139.         front = 'true'
  140.     else
  141.         rs.setOutput('front', false)
  142.         front = 'false'
  143.     end
  144.     if tonumber(result_array[6]) == 1 then
  145.         rs.setOutput('left', true)
  146.         left = 'true'
  147.     else
  148.         rs.setOutput('left', false)
  149.         left = 'false'
  150.     end
  151.     if tonumber(result_array[7]) == 1 then
  152.         rs.setOutput('right', true)
  153.         right = 'true'
  154.     else
  155.         rs.setOutput('right', false)
  156.         right = 'false'
  157.     end
  158.  
  159.     if tonumber(current_version) > version then
  160.             run_installer()
  161.     end
  162. end
  163.  
  164. function getInputs()
  165.     if rs.getInput('top') then
  166.         top_input = 1
  167.     else
  168.         top_input = 0
  169.     end
  170.     if rs.getInput('bottom') then
  171.         bottom_input = 1
  172.     else
  173.         bottom_input = 0
  174.     end
  175.     if rs.getInput('front') then
  176.         front_input = 1
  177.     else
  178.         front_input = 0
  179.     end
  180.     if rs.getInput('back') then
  181.         back_input = 1
  182.     else
  183.         back_input = 0
  184.     end
  185.     if rs.getInput('left') then
  186.         left_input = 1
  187.     else
  188.         left_input = 0
  189.     end
  190.     if rs.getInput('right') then
  191.         right_input = 1
  192.     else
  193.         right_input = 0
  194.     end
  195.    
  196. end
  197.  
  198. ------  End module specific code ---------
  199.  
  200.  
  201. function start_loop()
  202.     phone_home()
  203.     while true do
  204.         terminal_screen()
  205.        
  206.         -- main active status with server
  207.         time = time + 1
  208.         if time > 30 then
  209.             time=0
  210.             phone_home()
  211.         end
  212.         sleep(1)
  213.     end
  214. end
  215.  
  216. function start()
  217.     term.clear()
  218.     term.setCursorPos(1,1)
  219.    
  220.   if fs.exists("config.txt") then
  221.       load_config()
  222.       start_loop()
  223.   else
  224.       run_installer()
  225.   end
  226. end
  227.  
  228. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement