optimize track_players/init.lua

This commit is contained in:
HybridDog 2016-08-13 10:04:08 +00:00
parent 9b6427fa2e
commit 059d013481

View File

@ -2,31 +2,27 @@ local time_interval = 1.0
local fifo_path = "/tmp/mt_players_fifo" local fifo_path = "/tmp/mt_players_fifo"
function players_data() function players_data()
local ps = {} local ps = minetest.get_connected_players()
for _, player in ipairs(minetest.get_connected_players()) do local pcount = #ps
local pos = player:getpos() if pcount == 0 then
local pname = player:get_player_name() return "[]\n"
local data = { end
name = pname, for i = 1,pcount do
x = pos.x, local player = ps[i]
y = pos.y, local data = player:getpos()
z = pos.z } data.name = player:get_player_name()
table.insert(ps, data) ps[i] = data
end end
if table.getn(ps) == 0 then return minetest.write_json(ps) .. "\n"
return '[]\n'
end
return minetest.write_json(ps) .. '\n'
end end
function time_interval_func() function time_interval_func()
local players = players_data() local fifo = io.open(fifo_path, "w")
local fifo = io.open(fifo_path, 'w') if fifo then
if (fifo ~= nil) then fifo:write(players_data())
fifo:write(players) fifo:close()
fifo:close() end
end minetest.after(time_interval, time_interval_func)
minetest.after(time_interval, time_interval_func)
end end
minetest.after(time_interval, time_interval_func) minetest.after(time_interval, time_interval_func)