Advertisement
Krutoy242

Sensor Alert (ComputerCraft)

Apr 6th, 2015
321
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. K_VERSION = 0.093 -- Version of program. Need for auto-update
  2.  
  3.  
  4. --===========================================================
  5. -- Inline help functions
  6. --===========================================================
  7. local function clearScreen()     term.clear(); term.setCursorPos(1,1) end
  8. local function tblLen(tbl)       local n=0;       for _,_  in pairs(tbl) do n = n + 1 end return n end
  9. local function makeSet(list)     local set = {};  for _, v in pairs(list) do set[v] = true end return set end
  10. local function pressAnyKey()     local event, param1 = os.pullEvent ("key") end
  11. local function getChar(str, pos) return string.sub(str, pos, pos) end
  12.  
  13. -- Redefine global function for faster acces
  14. local floor = math.floor
  15. local ceil  = math.ceil
  16. local insert= table.insert
  17. local min   = math.min
  18. local max   = math.max
  19.  
  20. local scrW, scrH = term.getSize() -- Get screen params
  21. local source = "http://pastebin.com/raw.php?i=dsQ8c2UR"
  22.  
  23.  
  24. -- ==============================
  25. -- Auto-update
  26. -- Download and replace running file
  27. -- ==============================
  28. local function autoUpdate()
  29.   -- Get version of last
  30.   if not http then return end
  31.   local httpResponce = http.get(source)
  32.   local allText = httpResponce.readAll()
  33.   httpResponce.close()
  34.  
  35.   local newVersion = 0
  36.   local _,verPos = string.find(allText, 'K_VERSION *= *')
  37.   if verPos then
  38.     newVersion = tonumber(string.match(allText, '%d+[%.?%d*]*', verPos+1))
  39.   end
  40.  
  41.   -- Compare and replace
  42.   if K_VERSION < newVersion then
  43.     local sFile = shell.getRunningProgram()
  44.     local f = fs.open(sFile, "w")
  45.     f.write(allText)
  46.     f.close()
  47.     clearScreen()
  48.     print("New version downloaded!")
  49.     print("Rebooting to apply changes...")
  50.     sleep(0.1)
  51.     os.reboot()
  52.     sleep(10)
  53.   end
  54. end
  55.  
  56.  
  57. -- ################################################################################## --
  58. -- ##                                   Main                                       ## --
  59. -- ################################################################################## --
  60. local function setAllOutput(state)
  61.   for k,v in pairs(redstone.getSides()) do
  62.     redstone.setOutput(v, state)
  63.   end
  64. end
  65.  
  66.  
  67. function main()
  68.  
  69.   -- Update on background
  70.   autoUpdate()
  71.  
  72.   local sensor = peripheral.find('openperipheral_sensor')
  73.   local maxPlayers = 1
  74.   local whitelist = {}
  75.   local pl
  76.  
  77.   while true do
  78.     -- Start the timer
  79.     local timer
  80.     timer = os.startTimer( 3 )
  81.    
  82.     while true do
  83.       local sEvent, p1, p2, p3, p4, p5 = os.pullEvent()
  84.      
  85.       if sEvent == "key" then
  86.         if p1 == keys.minus then
  87.           maxPlayers = math.max(0, maxPlayers - 1)
  88.         elseif p1 == keys.equals then
  89.           maxPlayers = maxPlayers + 1
  90.         elseif p1 == keys.insert then
  91.           if pl then
  92.             for k,v in pairs(pl) do
  93.               whitelist[v.name] = true
  94.             end
  95.           end
  96.         elseif p1 == keys.delete then
  97.           whitelist = {}
  98.         end
  99.       elseif sEvent == "timer" and p1 == timer then
  100.         pl = sensor.getPlayers()
  101.         local catchedPlayers = {}
  102.          
  103.         clearScreen()
  104.         print("Players found:")
  105.         for k,v in pairs(pl) do
  106.           print(v.name)
  107.           if not whitelist[v.name] then
  108.             table.insert(catchedPlayers, v.name)
  109.           end
  110.         end
  111.        
  112.        
  113.         term.setCursorPos(scrW-20, 1)
  114.         write("Whitelisted players:")
  115.         local i=1
  116.         for k,v in pairs(whitelist) do
  117.           term.setCursorPos(scrW-20, 1+i)
  118.           write(k)
  119.           i=i+1
  120.         end
  121.        
  122.         if #catchedPlayers>maxPlayers then
  123.           setAllOutput(true)
  124.         else
  125.           setAllOutput(false)
  126.         end
  127.        
  128.         term.setCursorPos(1, scrH-3)
  129.         write("MaxPlayers: "..maxPlayers)
  130.         term.setCursorPos(1, scrH-2)
  131.         print("Press -/+ to change max player")
  132.         print("Press [insert] to add all players to whitelist")
  133.         write("Press [delete] to clear whitelist")
  134.      
  135.         break
  136.       end
  137.     end
  138.   end
  139. end
  140.  
  141. main()
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement