Out-Feu

spybot.lua

Oct 13th, 2024 (edited)
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | Gaming | 0 0
  1. --[[
  2.  
  3. SpyBot program
  4. By Out-Feu
  5.  
  6. version 1.0.2
  7.  
  8. Free to distribute/alter
  9. so long as proper credit to original
  10. author is maintained.
  11.  
  12. Simply connect a player detector in any way and start this program
  13.  
  14. --]]
  15.  
  16. string.splitLine = function(self)
  17.  local tab = {}
  18.  for str in self:gmatch("([^\n]*)\n?") do
  19.   table.insert(tab, str)
  20.  end
  21.  return tab
  22. end
  23.  
  24. table.find = function(self, search)
  25.  for index, value in ipairs(self) do
  26.   if value == search then
  27.    return index
  28.   end
  29.  end
  30.  return nil
  31. end
  32.  
  33. function updateTempFile()
  34.  if tempFile == '' then
  35.   return
  36.  end
  37.  if next(players) == nil then
  38.   fs.delete(tempFile)
  39.  else
  40.   file = fs.open(tempFile, "w")
  41.   for index, value in ipairs(players) do
  42.    file.writeLine(value)
  43.   end
  44.   file.close()
  45.  end
  46. end
  47.  
  48. function getLogText(join, player)
  49.  local logTime = os.date("%x %X", os.epoch("local") / 1000)
  50.  local logText
  51.  if join then
  52.   logText = "[" .. logTime .. "] " .. player .. " joined"
  53.  else
  54.   logText = "[" .. logTime .. "] " .. player .. " left"
  55.  end
  56.  return logText
  57. end
  58.  
  59. function updatePlayerFile(join, player)
  60.  if player == '' then
  61.   return
  62.  end
  63.  local file
  64.  if singleFile then
  65.   file = fs.open(playerFile, "a")
  66.  else
  67.   file = fs.open(playerFile .. "/" .. player .. ".txt" , "a")
  68.  end
  69.  local logText = getLogText(join, player)
  70.  file.writeLine(logText)
  71.  print(logText)
  72.  file.close()
  73. end
  74.  
  75. function updatePlayerList(addPlayer, player)
  76.  local index = table.find(players, player)
  77.  updatePlayerFile(addPlayer, player)
  78.  if addPlayer and index == nil then
  79.   table.insert(players, player)
  80.   updateTempFile()
  81.  elseif not addPlayer and index ~= nil then
  82.   table.remove(players, index)
  83.   updateTempFile()
  84.  end
  85. end
  86.  
  87. function initPlayers()
  88.  local onlinePlayers = detector.getOnlinePlayers()
  89.  for index, value in ipairs(onlinePlayers) do
  90.   updatePlayerList(true, value)
  91.  end
  92. end
  93.  
  94. function removeTempPlayers()
  95.  if not fs.exists(tempFile) then
  96.   return
  97.  end
  98.  local file = fs.open(tempFile, "r")
  99.  local fileContent = file.readAll()
  100.  local tempPlayers = fileContent:splitLine()
  101.  for index, value in ipairs(tempPlayers) do
  102.   updatePlayerFile(false, value)
  103.  end
  104.  file.close()
  105.  updateTempFile()
  106. end
  107.  
  108. per = peripheral.getNames()
  109. for k, v in pairs(per) do
  110.  if peripheral.getType(v) == "playerDetector" then
  111.   detector = peripheral.wrap(v)
  112.  end
  113. end
  114. per = nil
  115.  
  116. singleFile = false -- Store the data inside a single file instead of using a file for each player
  117. playerFile = "players" -- Name of the file or directory in which the data in stored
  118. tempFile = "currentPlayers.txt" -- Name of the file in which are stored the names of the currently connected players
  119.  
  120. --------------------------------------------------------------------------------
  121.  
  122. if detector == nil then
  123.  printError("Player detector not found")
  124.  return
  125. end
  126.  
  127. players = {}
  128.  
  129. removeTempPlayers()
  130. initPlayers()
  131.  
  132. repeat --main loop
  133.  
  134.  -- Wait for a player to join or leave
  135.  local event, user, dim
  136.  repeat
  137.   event, player, dim = os.pullEvent()
  138.  until event == "playerJoin" or event == "playerLeave"
  139.  
  140.  if event == "playerJoin" then
  141.   updatePlayerList(true, player)
  142.  elseif event == "playerLeave" then
  143.   updatePlayerList(false, player)
  144.  end
  145.  
  146. until false
Advertisement
Add Comment
Please, Sign In to add comment