2015-10-01 23:27:05 +02:00
|
|
|
--
|
2017-07-06 19:23:23 +02:00
|
|
|
-- Player listing mod
|
2015-10-01 23:27:05 +02:00
|
|
|
-- By Kaadmy, for Pixture
|
|
|
|
--
|
2019-08-28 17:31:41 +02:00
|
|
|
local S = minetest.get_translator("player_list")
|
2015-10-01 23:27:05 +02:00
|
|
|
|
2017-07-06 19:23:23 +02:00
|
|
|
player_list = {}
|
2015-10-01 23:27:05 +02:00
|
|
|
|
|
|
|
-- current players format:
|
|
|
|
-- {<playername> = <last connect(if connected, or nil)>}
|
2017-07-06 19:23:23 +02:00
|
|
|
player_list.players = {}
|
2015-10-01 23:27:05 +02:00
|
|
|
|
|
|
|
local function divmod(f, d)
|
|
|
|
return {math.floor(f / d), f % d}
|
|
|
|
end
|
|
|
|
|
|
|
|
local function prettytime(time)
|
|
|
|
local a = divmod(time, 60)
|
|
|
|
local seconds = a[2]
|
|
|
|
local minutes = a[1]
|
|
|
|
|
|
|
|
local b = divmod(minutes, 60)
|
|
|
|
local hours = b[1]
|
|
|
|
minutes = b[2]
|
|
|
|
|
|
|
|
local c = divmod(hours, 24)
|
|
|
|
local days = c[1]
|
|
|
|
hours = c[2]
|
|
|
|
|
|
|
|
local str = ""
|
|
|
|
if days ~= 0 then str = str .. days .. "d " end
|
|
|
|
if hours ~= 0 then str = str .. hours .. "h " end
|
|
|
|
if minutes ~= 0 then str = str .. minutes .. "m " end
|
|
|
|
str = str .. seconds .. "s"
|
|
|
|
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_joinplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2017-07-06 19:23:23 +02:00
|
|
|
player_list.players[name] = minetest.get_gametime()
|
2015-10-01 23:27:05 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local function on_leaveplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2017-07-06 19:23:23 +02:00
|
|
|
player_list.players[name] = minetest.get_gametime()
|
2015-10-01 23:27:05 +02:00
|
|
|
end
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
|
|
|
minetest.register_on_leaveplayer(on_leaveplayer)
|
2015-10-01 23:27:05 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_chatcommand(
|
2015-10-01 23:27:05 +02:00
|
|
|
"plist",
|
|
|
|
{
|
2019-08-28 20:49:04 +02:00
|
|
|
params = "[ all | recent ]",
|
2019-08-28 17:31:41 +02:00
|
|
|
description = S("List current, recent, or all players since the last server restart"),
|
2015-10-03 01:13:36 +02:00
|
|
|
func = function(player_name, param)
|
2017-05-12 04:29:55 +02:00
|
|
|
local time = minetest.get_gametime()
|
2015-10-01 23:27:05 +02:00
|
|
|
|
2015-10-01 23:55:18 +02:00
|
|
|
local str = ""
|
|
|
|
|
|
|
|
if param == "all" then
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(player_name, S("Players:"))
|
2015-10-01 23:55:18 +02:00
|
|
|
elseif param == "recent" then
|
2019-08-28 17:31:41 +02:00
|
|
|
str = str .. S("Recent players: ")
|
2015-10-01 23:55:18 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
str = str .. S("Connected players: ")
|
2015-10-01 23:55:18 +02:00
|
|
|
end
|
2015-10-01 23:27:05 +02:00
|
|
|
|
|
|
|
local player_count = 0
|
2017-07-06 19:23:23 +02:00
|
|
|
for name, jointime in pairs(player_list.players) do
|
2017-05-12 04:29:55 +02:00
|
|
|
local plyr = minetest.get_player_by_name(name)
|
2015-10-01 23:55:18 +02:00
|
|
|
|
|
|
|
if param == "all" then
|
|
|
|
if plyr ~= nil then
|
|
|
|
player_count = player_count + 1
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(S("@1: connected for @2", player_name, name, prettytime(time - jointime)))
|
2015-10-01 23:55:18 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(S("@1, @2: last seen @3 ago", player_name, name, prettytime(time - jointime)))
|
2015-10-01 23:55:18 +02:00
|
|
|
end
|
2017-07-06 19:23:23 +02:00
|
|
|
elseif param == "recent" then
|
|
|
|
if plyr == nil then -- Only show players that were connected but are currently disconnected
|
|
|
|
player_count = player_count + 1
|
|
|
|
|
|
|
|
if player_count == 1 then
|
|
|
|
str = str .. name
|
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
-- TODO: Translate comma
|
2017-07-06 19:23:23 +02:00
|
|
|
str = str .. ", " .. name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif plyr ~= nil then
|
|
|
|
player_count = player_count + 1
|
|
|
|
|
|
|
|
if player_count == 1 then
|
|
|
|
str = str .. name
|
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
-- TODO: Translate comma
|
2017-07-06 19:23:23 +02:00
|
|
|
str = str .. ", " .. name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-01 23:27:05 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.chat_send_player(player_name, str)
|
2015-10-01 23:55:18 +02:00
|
|
|
|
|
|
|
if param == "recent" then
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(player_name, S("Count: @1", player_count))
|
2015-10-01 23:55:18 +02:00
|
|
|
else
|
2019-08-28 17:31:41 +02:00
|
|
|
minetest.chat_send_player(player_name, S("Count: @1", player_count))
|
2015-10-01 23:55:18 +02:00
|
|
|
end
|
2019-06-23 21:05:52 +02:00
|
|
|
return true
|
2015-10-01 23:27:05 +02:00
|
|
|
end
|
|
|
|
})
|
2015-10-01 23:55:18 +02:00
|
|
|
|
2017-07-06 19:23:23 +02:00
|
|
|
default.log("mod:player_list", "loaded")
|