Advertisement
SkyNetCloud

player

Mar 19th, 2024 (edited)
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.57 KB | Gaming | 0 0
  1. -- Global Variables
  2. --allowedPlayerArray={["Topher"]=true,["nunley21"]=true,["Demethan"]=true,["waerloga"]=true}
  3.  
  4. -- pastebin for installer
  5. local installer = "P324jv87"
  6. -- players ignored by senors
  7. local allowedPlayerArray={}
  8. -- inventory arrays to compare
  9. -- players currently inside sensor range
  10. local flag={}
  11. -- counter for scanning animation
  12. local heart = 0
  13. -- counter for phone_home()
  14. local time = 0
  15. -- user token to send to server
  16. local token = '0'
  17. -- this scanners name
  18. local scanner = ''
  19. -- oweners username on website
  20. local username = ''
  21. -- currently installed version
  22. local version = 2
  23.  
  24. -- write text to the terminal screen
  25. function draw_text_term(x, y, text, text_color, bg_color)
  26.     term.setTextColor(text_color)
  27.     term.setBackgroundColor(bg_color)
  28.     term.setCursorPos(x,y)
  29.     write(text)
  30. end
  31.  
  32. -- draw a line on the terminal screen
  33. function draw_line_term(x, y, length, color)
  34.     term.setBackgroundColor(color)
  35.     term.setCursorPos(x,y)
  36.     term.write(string.rep(" ", length))
  37. end
  38.  
  39. function bars()
  40.     draw_line_term(1, 1, 51, colors.lime)
  41.     draw_line_term(1, 19, 51, colors.lime)
  42.     draw_text_term(20, 1, 'Base Scanner', colors.gray, colors.lime)
  43.     draw_text_term(13, 19, 'CraftNanny.org', colors.gray, colors.lime)
  44. end
  45.  
  46. function scanner_screen()
  47.     term.clear()
  48.    
  49.     -- scanner info
  50.     bars()
  51.     draw_text_term(1, 2, 'Scanner: ', colors.lime, colors.black)
  52.     draw_text_term(10, 2, scanner, colors.white, colors.black)
  53.     draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  54.     draw_text_term(8, 3, username, colors.white, colors.black)
  55.     draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  56.    
  57.     -- scanning graphic
  58.     heart = heart + 1
  59.     draw_text_term(3, 10, 'Scanning ', colors.red, colors.black)
  60.     draw_text_term(12, 10 , string.rep(".", heart), colors.red, colors.black)
  61.     if heart == 15 then
  62.         heart = 1
  63.     end
  64. end
  65.  
  66. -- retrieves token from local text file
  67. -- called at startup if config.txt exists
  68. -- token is used to authorize the scanner to post to users log
  69. function load_config()
  70.     sr = fs.open("config.txt", "r")
  71.     token = sr.readLine()
  72.     scanner = sr.readLine()
  73.     username = sr.readLine()
  74.     sr.close()
  75. end
  76.  
  77. -- called for new installations and when the scanner needs to be updated
  78. function run_installer()
  79.     if fs.exists("install") then
  80.         fs.delete("install")
  81.     end
  82.     shell.run("pastebin get "..installer.." install")
  83.     sleep(1)
  84.     shell.run("install")
  85. end
  86.  
  87. -- called every 30 seconds when scanner is running
  88. -- tells the server that the scanner is online
  89. -- checks version and automtically updates
  90. function phone_home()
  91.     response = http.post("https://craftnanny.org/code/ping.php",
  92.         "token="..token.."&id="..os.getComputerID())
  93.     current_version = response.readAll()
  94.  
  95.     if tonumber(current_version) > version then
  96.         run_installer()
  97.     end
  98. end
  99.  
  100. function round(what, precision)
  101.    if what==nil then return 0 end
  102.    return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
  103. end
  104.  
  105.  
  106.  
  107. function record()
  108.     -- Set the cursor position
  109.     term.setCursorPos(1, 1)
  110.    
  111.     -- Get online players
  112.     local players = s.getOnlinePlayers()
  113.    
  114.     -- Iterate over players
  115.     for _, player in ipairs(players) do
  116.         -- Check if player is in range
  117.         local in_range = s.getPlayersInRange(15)
  118.         if in_range then
  119.             -- Post message for each player in range
  120.             for _, ign in ipairs(in_range) do
  121.                 post(tostring(ign), 1, "Has entered sensor range")
  122.                 flag[ign] = true
  123.             end
  124.         end
  125.     end
  126. end
  127.  
  128.  
  129. -- iterate through all players with an active flag
  130. -- see if they're still in range of the scanner
  131. function leaveCheck()  
  132.     for ign,v in pairs(flag) do
  133.         local ok,msg = pcall(function() s.isPlayersInRange(15) end)
  134.         if not msg and flag[ign] then
  135.             flag[ign] = false
  136.             post(tostring(ign), 2, " has left sensor range")
  137.         end
  138.     end
  139. end
  140.  
  141.  
  142. -- e.g. post('tom', 2, ' has left sensor range')
  143. function post(ign, event, description)  
  144.     http.post("https://craftnanny.org/code/log.php",
  145.         "token="..token.."&ign="..ign.."&id="..os.getComputerID().."&event="..event.."&description="..description)
  146. end
  147.  
  148. function tablelength(T) --get real count of table
  149.     local count = 0
  150.     for _ in pairs(T) do
  151.         count = count + 1
  152.     end
  153.     return count
  154. end
  155.  
  156. function compare(t1,t2)
  157.     local ai = {}
  158.     local r = {}
  159.     table.sort(t1) --sorting by name
  160.     table.sort(t2) --sorting by name
  161.    
  162.     for k,v in pairs(t2) do
  163.         r[k] = v; ai[v]=true
  164.     end
  165.     for k,v in pairs(t1) do
  166.         if ai[v]~=nil then   --if item match, remove it from temp table r
  167.             r[k]=nil  
  168.         end
  169.     end
  170.    
  171.     if tablelength(r) > 0 then --if there are items left in r
  172.          return true,r
  173.     else
  174.         return false,nil
  175.     end
  176. end
  177.  
  178. function start_recording()
  179.     -- main loop
  180.  
  181.     local playersInRange = s.isPlayersInRange(15)
  182.     if playersInRange then -- If players are in range
  183.         -- run scan
  184.         ok, msg = pcall(record)
  185.     else
  186.         -- If no players are in range, run leaveCheck
  187.         leaveCheck()
  188.     end
  189.    
  190.     while true do
  191.         -- run scan
  192.         -- animate screen and delay
  193.         scanner_screen()   
  194.         sleep(0.5)
  195.         scanner_screen()   
  196.         sleep(0.5)
  197.        
  198.         -- main active status with server
  199.         time = time + 5
  200.         if time > 30 then
  201.             time=0
  202.             phone_home()
  203.         end
  204.     end
  205. end
  206.  
  207. function start()
  208.     s=peripheral.find("playerDetector")
  209.     heart=0
  210.     term.clear()
  211.     term.setCursorPos(1,1)
  212.    
  213.     if fs.exists("config.txt") then
  214.         load_config()
  215.         start_recording()
  216.     else
  217.         run_installer()
  218.     end
  219. end
  220.  
  221. start()
  222.  
Tags: lua
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement