Advertisement
SkyNetCloud

Player Module 2

Mar 10th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------------------------------------------
  2. -- Player Module for CraftNanny 2
  3. --  Update By SkyNetCloud
  4. --  craftnanny.skynetcloud.xyz
  5. --  2019-03-11 
  6. --      - Replace old Url
  7. ---------------------------------------------------
  8.  
  9.  
  10. -- pastebin for installer
  11. local installer = "WGtZnprm"
  12. -- players ignored by senors
  13. local allowedPlayerArray={}
  14. -- inventory arrays to compare
  15. local snapShot={}
  16. local snapShot2={}
  17. local itemString={}
  18. -- players currently inside sensor range
  19. local flag={}
  20. -- counter for scanning animation
  21. local heart = 0
  22. -- counter for phone_home()
  23. local time = 0
  24. -- user token to send to server
  25. local token = '0'
  26. -- this scanners name
  27. local scanner = ''
  28. -- owenrs username on website
  29. local username = ''
  30. -- currently installed version
  31. local version = 3
  32.  
  33. --Cleanup
  34. if fs.exists("log.txt") then   
  35.     ok,msg = pcall (fs.delete,"log.txt")
  36.         if msg~=nil then
  37.             os.reboot()
  38.         end
  39. end
  40.  
  41. -- write text to the terminal screen
  42. function draw_text_term(x, y, text, text_color, bg_color)
  43.   term.setTextColor(text_color)
  44.   term.setBackgroundColor(bg_color)
  45.   term.setCursorPos(x,y)
  46.   write(text)
  47. end
  48.  
  49. -- draw a line on the terminal screen
  50. function draw_line_term(x, y, length, color)
  51.     term.setBackgroundColor(color)
  52.     term.setCursorPos(x,y)
  53.     term.write(string.rep(" ", length))
  54. end
  55.  
  56. function bars()
  57.     draw_line_term(1, 1, 51, colors.lime)
  58.     draw_line_term(1, 19, 51, colors.lime)
  59.     draw_text_term(20, 1, 'Base Scanner', colors.gray, colors.lime)
  60.     draw_text_term(13, 19, 'craftnanny.skynetcloud.xyz', colors.gray, colors.lime)
  61. end
  62.  
  63. function scanner_screen()
  64.     term.clear()
  65.    
  66.     -- scanner info
  67.     bars()
  68.     draw_text_term(1, 2, 'Scanner: ', colors.lime, colors.black)
  69.     draw_text_term(10, 2, scanner, colors.white, colors.black)
  70.     draw_text_term(1, 3, 'Owner: ', colors.lime, colors.black)
  71.     draw_text_term(8, 3, username, colors.white, colors.black)
  72.     draw_text_term(1, 4 , string.rep("-", 51), colors.lime, colors.black)
  73.    
  74.     -- scanning graphuc
  75.     heart = heart + 1
  76.     draw_text_term(3, 10, 'Scanning ', colors.red, colors.black)
  77.     draw_text_term(12, 10 , string.rep(".", heart), colors.red, colors.black)
  78.     if heart == 15 then
  79.         heart = 1
  80.     end
  81. end
  82.  
  83. -- retrieves token from local text file
  84. -- called at startup if config.txt exists
  85. -- token is used to authorize the scanner to post to users log
  86. function load_config()
  87.   sr = fs.open("config.txt", "r")
  88.     token = sr.readLine()
  89.     scanner = sr.readLine()
  90.     username = sr.readLine()
  91.   sr.close()
  92. end
  93.  
  94. -- called for new installations and when the scanner needs to be updated
  95. function run_installer()
  96.     if fs.exists("install") then
  97.         fs.delete("install")
  98.       end
  99.       shell.run("pastebin get "..installer.." install")
  100.       sleep(1)
  101.       shell.run("install")
  102. end
  103.  
  104. -- called every 30 seconds when scanner is running
  105. -- tells the server that the scanner is online
  106. -- checks version and automtically updates
  107. function phone_home()
  108.     response = http.post("https://craftnanny.skynetcloud.xyz/code/ping.php",
  109.                 "token="..token.."&id="..os.getComputerID())
  110.     current_version = response.readAll()
  111.  
  112.     if tonumber(current_version) > version then
  113.             run_installer()
  114.     end
  115. end
  116.  
  117. function round(what, precision)
  118.    if what==nil then return 0 end
  119.    return math.floor(what*math.pow(10,precision)+0.5) / math.pow(10,precision)
  120. end
  121.  
  122. function record()
  123.     term.setCursorPos(1,1)
  124.     players=s.getPlayers()
  125.     for num,player in pairs(players) do
  126.         for p,ign in pairs(player) do
  127.             if p=="name" then
  128.                 playerData = s.getPlayerByName(ign)
  129.                 data = playerData.all()
  130.                 inventory=data.player.inventory
  131.                 if not allowedPlayerArray[ign] then -- check the allow array
  132.                     draw_text_term(3, 7, ign.." is not on the allowed list        ", colors.red, colors.black)
  133.                     invArray={} -- fresh copy
  134.                     for a,b in pairs(inventory) do --getting player inventory
  135.                         slot=b.all()
  136.                         invArray[slot.name]=slot.qty
  137.                     end
  138.                     if flag[ign]==nil or not flag[ign] then --recording first inventory scan
  139.                         itemString=""
  140.                         snapShot[ign]= invArray
  141.                         draw_text_term(3, 7, ign..": Initial Snapshot             ", colors.green, colors.black)
  142.                         post(ign,1," has entered sensor range")
  143.                         flag[ign]=true -- set player flag for later processing
  144.                     else
  145.                         snapShot2[ign]= invArray
  146.                         draw_text_term(3, 7, ign..": Updated Snapshot             ", colors.green, colors.black)
  147.                         found,Items=compare(snapShot[ign],snapShot2[ign])
  148.  
  149.                         if found then
  150.                             for i,v in pairs(Items) do --building string
  151.                                 if v~="" or v~=nil then
  152.                                     itemString=" "..v..":"..i..itemString
  153.                                     draw_text_term(6, 7, ign..itemString.." is extra since last snapshot of inventory", colors.orange, colors.black)
  154.                                     post(ign,3,itemString)
  155.                                 end
  156.                             end
  157.                             snapShot[ign]=snapShot2[ign]--inventory has been logged, can now reset the initial snapshot
  158.                             itemString=""
  159.                         else
  160.                             --print(ign," - No item found")
  161.                         end
  162.                         sleep(1)
  163.                        
  164.                         if snapShot2[ign]~= nil then --recoding newer inventory until player leaves.
  165.                         snapShot2[ign]= nil
  166.                     end
  167.                 end    
  168.  
  169.                 end
  170.  
  171.  
  172.             end
  173.  
  174.         end
  175.     end
  176.     --Cleanup
  177.     playerData={}
  178.     data={}
  179.     inventory={}
  180.     found=nil
  181.  
  182. end
  183.  
  184. -- iterate through all players with an active flag
  185. -- see if they're still in range of the scanner
  186. function leaveCheck()  
  187.     for ign,v in pairs(flag) do
  188.     --  print("Did ",ign," leave?")
  189.         local ok,msg=pcall(function ()s.getPlayerByName(ign) end)
  190.         --print(msg) --debug
  191.         if not ok and flag[ign] then
  192.             --print(ign," has left.")
  193.             flag[ign]=false
  194.             snapShot[ign]=nil
  195.             snapShot2[ign]=nil
  196.             post(ign,2," has left sensor range")
  197.         end
  198.     end
  199. end
  200.  
  201. -- records a log entry on the server
  202. -- passes to server:
  203.     -- token and computer ID (used to verify source)
  204.     -- event type: 1 = player entering, 2 = player leaving, 3 = inventory change
  205.     -- ign, players name
  206.     -- discription of event
  207.    
  208.    
  209. function logging(ign, event, description)
  210.     if peripheral.getType("right")== "monitor" then
  211.         mon = peripheral.wrap("right")
  212.     elseif peripheral.getType("left")~="monitor" then
  213.         mon = peripheral.wrap("left")
  214.     else
  215.         return
  216.     end
  217.   if mon then
  218.     mon.clear()
  219.     mon.setTextScale(0.5)
  220.     monMaxX,monMaxY = mon.getSize()
  221.     print(monMaxX,monMaxY)
  222.     if monMaxX < 61 then
  223.         mon.write("monitor too small. Min 4 wide")
  224.         return
  225.     end
  226.     sw = fs.open("log.txt",fs.exists("log.txt") and "a" or "w")
  227.     line=ign.." "..description
  228.     sw.writeLine(os.day().."/"..os.time()..": "..ign.." "..description)
  229.     sw.close()
  230.     sr = fs.open("log.txt","r")
  231.     monY=1
  232.     msg="starting log..."
  233.     while msg~=nil do
  234.         ok,msg = pcall(sr.readLine)
  235.         mon.setCursorPos(1,monY)
  236.         mon.write(msg)
  237.         monY=monY+1
  238.     end
  239.     sr.close()
  240. end
  241.  
  242. end
  243.    
  244.    
  245.    
  246. -- e.g. post('tom', 2, ' has left sensor range')
  247. function post(ign, event, description)  
  248.  
  249.             http.post("https://craftnanny.skynetcloud.xyz/code/log.php",
  250.             "token="..token.."&ign="..ign.."&id="..os.getComputerID().."&event="..event.."&description="..description)
  251.             logging(ign, event, description)
  252. end
  253.  
  254. function tablelength(T) --get real count of table
  255.   local count = 0
  256.   for _ in pairs(T) do count = count + 1 end
  257.   return count
  258. end
  259.  
  260. function compare(t1,t2)
  261.     local ai = {}
  262.     local r = {}
  263.     table.sort(t1) --sorting by name
  264.     table.sort(t2) --sorting by name
  265.    
  266.     for k,v in pairs(t2) do
  267.         r[k] = v; ai[v]=true
  268.     end
  269.     for k,v in pairs(t1) do
  270.         if ai[v]~=nil then   --if item match, remove it from temp table r
  271.             r[k]=nil  
  272.         end
  273.     end
  274.    
  275.     if tablelength(r) > 0 then --if there are items left in r
  276.          return true,r
  277.     else
  278.         return false,nil
  279.     end
  280. end
  281.  
  282. function start_recording()
  283.     -- main loop
  284.     while true do
  285.         -- run scan
  286.         ok,msg=pcall(record)
  287.         if not ok then
  288.             print(msg)
  289.             break
  290.         end
  291.         leaveCheck()
  292.        
  293.         -- animate screen and delay
  294.         scanner_screen()   
  295.         sleep(0.5)
  296.         scanner_screen()   
  297.         sleep(0.5)
  298.        
  299.         -- main active status with server
  300.         time = time + 1
  301.         if time > 30 then
  302.             time=0
  303.             phone_home()
  304.         end
  305.     end
  306. end
  307.  
  308. function start()
  309.     s=peripheral.wrap("top")
  310.     heart=0
  311.     term.clear()
  312.     term.setCursorPos(1,1)
  313.    
  314.   if fs.exists("config.txt") then
  315.       load_config()
  316.       start_recording()
  317.   else
  318.       run_installer()
  319.   end
  320. end
  321.  
  322. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement