Advertisement
omr__

Visiters logger

Nov 12th, 2022 (edited)
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.32 KB | Gaming | 0 0
  1. --[[**************************************************************************************
  2. **                                                                                      **
  3. **                                  Visitor logger v1.1                                 **
  4. **              The script saves and display all players in configured range            **
  5. **                                                                                      **
  6. **                                                  Author                              **
  7. **                                                  discord:    om_r#3567               **
  8. **                                                  email:      max.kvy@gmail.com       **
  9. **                                                                                      **
  10. **      ---------------------------------------------------------------------------     **
  11. **      If you want to report a bug, suggest any ideas or just to support - use         **
  12. **      credentials above. I appreciate any feedback!                                   **
  13. **      ---------------------------------------------------------------------------     **
  14. **                                                                                      **
  15. **      Download                                                                        **
  16. **          "pastebin get QKPAfPkU visitors" and "visitors" to execute                  **
  17. **          or "pastebin get QKPAfPkU startup" - to autorun                             **
  18. **                                                                                      **
  19. **      Usage                                                                           **
  20. **          Place "Player Detector" from any side                                       **
  21. **          (optional) Place "Chat Box" from any side to receiv ig PMs                  **
  22. **                                                                                      **
  23. **      Settings (After start you will'be asked if you want to change them)             **
  24. **          Range                   => Player Detector scan range (default: 1000)       **
  25. **          Refresh time            => Delay between scans (default: 5)                 **
  26. **          Send PM's to username   => Player login to whom it sends PM's               **
  27. **                                                                                      **
  28. **************************************************************************************--]]
  29.  
  30. local scriptConfig = {
  31.     version         = 1.1,
  32.     startDelay      = 15,
  33.     configPath      = 'config',
  34.     playersListPath = 'users'  
  35. }
  36.  
  37. local settings = {
  38.     scanRange       = 1000,
  39.     refreshDelay    = 5,
  40.     sendToChat      = false,
  41.     messageToUser   = 'none'
  42. }
  43.  
  44. -- Support methods
  45. local utils = {}
  46. local detector = peripheral.find("playerDetector")
  47. local box = peripheral.find("chatBox")
  48.  
  49. utils.checkRequirements = function ()
  50.     if detector == nil then
  51.         error("Player Detector required")
  52.     end
  53.     if box ~= nil then
  54.      settings.sendToChat = true
  55.     end
  56. end
  57.  
  58. utils.getTablelength = function (T)
  59.     local count = 0
  60.     for _ in pairs(T) do count = count + 1 end
  61.     return count
  62. end
  63.  
  64. utils.loadTableFromFile = function (path)
  65.     local file = fs.open(path , "r")
  66.     if file == nil then return {} end
  67.     local data = file.readAll()
  68.     file.close()
  69.     return textutils.unserialize(data)
  70. end
  71.  
  72. utils.saveTableToFile = function (table, path)
  73.     local file = fs.open(path , "w")
  74.     file.write(textutils.serialize(table))
  75.     file.close()
  76. end
  77.  
  78. utils.restoreConfig = function ()
  79.     local savedConfig = utils.loadTableFromFile(scriptConfig.configPath)
  80.     if utils.getTablelength(savedConfig) ~= 0 then
  81.         settings = savedConfig
  82.     end
  83. end
  84.  
  85. utils.resetTerminal = function ()
  86.     term.setBackgroundColor(colours.cyan)
  87.     term.clear()
  88.     term.setCursorPos(1, 1)
  89. end
  90.  
  91. utils.readOption = function (message, defaultValue)
  92.     local x, y = term.getCursorPos()
  93.     print(message .. "? Default: " .. defaultValue)
  94.     input = read()
  95.     term.setCursorPos(x, y + 1)
  96.     term.clearLine()
  97.     term.setCursorPos(x, y)
  98.     term.clearLine()
  99.     if input ~= '' then
  100.         return input
  101.     else
  102.         return defaultValue
  103.      end
  104. end
  105.  
  106. -- Main methods
  107. local app = {}
  108.  
  109. app.displayAllDetectedPlayers = function ()
  110.     utils.resetTerminal()
  111.     local savedPlayers = utils.loadTableFromFile(scriptConfig.playersListPath)
  112.     print("Detected Players:")
  113.     print("")
  114.     for key, value in pairs(savedPlayers) do
  115.         print(" => " .. key)
  116.     end
  117. end
  118.  
  119. app.displayConfiguration = function ()
  120.     local x, y = term.getCursorPos()
  121.     local w, h = term.getSize()
  122.     term.setCursorPos(x, h - 2)
  123.    
  124.     local configure = utils.readOption("Do you want to configure (yes/no) (Use default after " .. scriptConfig.startDelay .. " seconds)", "no")
  125.  
  126.     if configure == 'yes' then
  127.         settings.scanRange = tonumber(utils.readOption("Range", settings.scanRange))
  128.         settings.refreshDelay = tonumber(utils.readOption("Refresh time", settings.refreshDelay))
  129.         if settings.sendToChat ~= nil then
  130.             settings.messageToUser = utils.readOption("Name of player to send notification", settings.messageToUser)
  131.         end
  132.        
  133.         term.setCursorPos(x, y)
  134.  
  135.         utils.saveTableToFile(settings, scriptConfig.configPath)
  136.         app.displayDescription()
  137.         print("")
  138.         print("Saving settings. Wait 3 seconds...")
  139.         os.sleep(3)
  140.         utils.resetTerminal()
  141.     end
  142. end
  143.  
  144. app.displayDescription = function ()    
  145.     utils.resetTerminal()
  146.     print("Save all players who visited you base VERSION #" .. scriptConfig.version)
  147.     print("")
  148.     print("Discord: om_r#3567")
  149.     print("--------------------------------------------------")
  150.     print("Current settings:")
  151.     print(" * Range => " .. settings.scanRange .. " blocks")
  152.     print(" * Refresh time => " .. settings.refreshDelay .. " seconds")
  153.     if settings.sendToChat ~= nil then
  154.         settings.sendToChat = settings.messageToUser
  155.     else
  156.         settings.sendToChat = "disabled"
  157.     end
  158.     print(" * Sends PM's to (Chat Box required) => " .. tostring(settings.sendToChat))
  159.     print("--------------------------------------------------")
  160.     print("Use \"edit users.lua\" to show full list")
  161. end
  162.  
  163. app.scanNewPlayers = function ()
  164.     local playersNearby = detector.getPlayersInRange(settings.scanRange)
  165.     local savedPlayers = utils.loadTableFromFile(scriptConfig.playersListPath)
  166.  
  167.     for k, playerName in pairs(playersNearby) do
  168.         if not savedPlayers[playerName] then
  169.             savedPlayers[playerName] = true
  170.             print('New player on base: ' .. playerName)
  171.             if settings.sendToChat ~= nil then
  172.                 box.sendMessageToPlayer('New player on base: ' .. playerName, settings.messageToUser, 'Base Protection Tool')
  173.             end
  174.         end
  175.     end
  176.    
  177.     utils.saveTableToFile(savedPlayers, scriptConfig.playersListPath)
  178. end
  179.  
  180. app.run = function()
  181.     utils.checkRequirements()
  182.     utils.restoreConfig()
  183.     app.displayDescription()
  184.     parallel.waitForAny(
  185.         function()
  186.             app.displayConfiguration()
  187.         end,
  188.         function()
  189.             sleep(scriptConfig.startDelay)
  190.         end
  191.     )
  192.    
  193.     while true do
  194.         app.displayAllDetectedPlayers()
  195.         app.scanNewPlayers()
  196.         os.sleep(settings.refreshDelay)
  197.     end
  198. end
  199.  
  200. app.run()
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement