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