From 059d013481528c265a5d98ab3ee3ec6ab8d3d82a Mon Sep 17 00:00:00 2001 From: HybridDog Date: Sat, 13 Aug 2016 10:04:08 +0000 Subject: [PATCH] optimize track_players/init.lua --- mods/track_players/init.lua | 40 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/mods/track_players/init.lua b/mods/track_players/init.lua index 71b8aa0..a4b8fac 100644 --- a/mods/track_players/init.lua +++ b/mods/track_players/init.lua @@ -2,31 +2,27 @@ local time_interval = 1.0 local fifo_path = "/tmp/mt_players_fifo" function players_data() - local ps = {} - for _, player in ipairs(minetest.get_connected_players()) do - local pos = player:getpos() - local pname = player:get_player_name() - local data = { - name = pname, - x = pos.x, - y = pos.y, - z = pos.z } - table.insert(ps, data) - end - if table.getn(ps) == 0 then - return '[]\n' - end - return minetest.write_json(ps) .. '\n' + local ps = minetest.get_connected_players() + local pcount = #ps + if pcount == 0 then + return "[]\n" + end + for i = 1,pcount do + local player = ps[i] + local data = player:getpos() + data.name = player:get_player_name() + ps[i] = data + end + return minetest.write_json(ps) .. "\n" end function time_interval_func() - local players = players_data() - local fifo = io.open(fifo_path, 'w') - if (fifo ~= nil) then - fifo:write(players) - fifo:close() - end - minetest.after(time_interval, time_interval_func) + local fifo = io.open(fifo_path, "w") + if fifo then + fifo:write(players_data()) + fifo:close() + end + minetest.after(time_interval, time_interval_func) end minetest.after(time_interval, time_interval_func)