Skip to content

Commit

Permalink
Clans: players must exist now (#1458). Closes #1421.
Browse files Browse the repository at this point in the history
* Fixed /clans.register localization
  • Loading branch information
ssdaniel24 authored Jun 13, 2024
1 parent b3b1d67 commit 08e918d
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 32 deletions.
3 changes: 2 additions & 1 deletion mods/lord/Player/clans/locale/clans.en.tr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Lists all existing clans. Blocked clans are marked with gray color.=Lists all ex
List of clans in format '<clan title> (<clan name>) — <status>':=List of clans in format '<clan title> (<clan name>) — <status>':
Shows given clan information.=Shows given clan information.
Clan @1 does not exist.=Clan @1 does not exist.
<clan name> <clan title> [list of players separated by space]=<clan name> <clan title> [list of players separated by space]
<leader name> <clan name> <clan title>=<leader name> <clan name> <clan title>
Register clan with given title and given players=Register clan with given title and given players
Didn't get enough arguments! See help.=Didn't get enough arguments! See help.
Clan '@1' successfully created. Leader: @2=Clan '@1' successfully created. Leader: @2
Expand All @@ -30,3 +30,4 @@ Toggles clan block.=Toggles clan block.
Clan @1 unblocked succesfully.=Clan @1 unblocked succesfully.
Clan @1 blocked succesfully.=Clan @1 blocked succesfully.
Clan=Clan
A player from given does not exist.=A player from given does not exist.
3 changes: 2 additions & 1 deletion mods/lord/Player/clans/locale/clans.ru.tr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Lists all existing clans. Blocked clans are marked with gray color.=Показы
List of clans in format '<clan title> (<clan name>) — <status>':=Список кланов виде '<название клана> (<id клана>) — <статус>':
Shows given clan information.=Показывает информацию о данном клане.
Clan @1 does not exist.=Клана @1 не существует.
<clan name> <clan title> [list of players separated by space]=<ID клана> <тайтл клана> [список игроков через пробел]
<leader name> <clan name> <clan title>=<имя лидера> <id клана> <название клана>
Register clan with given title and given players=Регистрация клана с данным тайтлом и данными игроками
Didn't get enough arguments! See help.=Получено недостаточно аргументов! Смотрите справку (/help).
Clan '@1' successfully created. Leader: @2=Клан '@1' успешно создан. Глава: @2
Expand All @@ -30,3 +30,4 @@ Toggles clan block.=Переключает блокировку клана.
Clan @1 unblocked succesfully.=Клан @1 успешно разблокирован.
Clan @1 blocked succesfully.=Клан @1 успешно заблокирован.
Clan=Клан
A player from given does not exist.=Один из данных игроков не существует.
3 changes: 2 additions & 1 deletion mods/lord/Player/clans/locale/template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Lists all existing clans. Blocked clans are marked with gray color.=
List of clans in format '<clan title> (<clan name>) — <status>':=
Shows given clan information.=
Clan @1 does not exist.=
<clan name> <clan title> [list of players separated by space]=
<leader name> <clan name> <clan title>=
Register clan with given title and given players=
Didn't get enough arguments! See help.=
Clan '@1' successfully created. Leader: @2=
Expand All @@ -30,3 +30,4 @@ Toggles clan block.=
Clan @1 unblocked succesfully.=
Clan @1 blocked succesfully.=
Clan=
A player from given does not exist.=
10 changes: 4 additions & 6 deletions mods/lord/Player/clans/src/clans.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ clans.err = {
[4] = "there is no such player in this clan",
[5] = "max players reached!",
[6] = "clan with this name is blocked",
[7] = "player not found",
[7] = "player does not exist",
}

--- @private
Expand Down Expand Up @@ -96,6 +96,7 @@ function clans.clan_players_add(clan_name, player_name)
if clan == nil then return false, clans.err[3] end
if clan.is_blocked then return false, clans.err[6] end
if #clan.players+1 > clans.max_players_in_clan then return false, clans.err[5] end
if not minetest.player_exists(player_name) then return false, clans.err[7] end

table.insert(clan.players, player_name)
clan_storage.set(clan)
Expand Down Expand Up @@ -196,18 +197,15 @@ end

local function register_join_or_leave_operations()

-- TODO: add events `on_clan_player_join` & `on_clan_player_leave`. #1431
-- TODO: move this into `nametag.lua` using that events
minetest.register_on_joinplayer(function(player, _)
if not player or not player:is_player() then return end

local clan = clans.clan_get_by_player(player)

if not clan then return end

Event.trigger(Event.Type.on_clan_player_join, clan.name, player:get_player_name())
Event.trigger(Event.Type.on_clan_player_join, clan, player:get_player_name())
clan_is_online_cache[clan.name] = true
nametag.for_player(player):segment("clan"):update(clan.title)
end)

minetest.register_on_leaveplayer(function(player, _)
Expand All @@ -216,7 +214,7 @@ local function register_join_or_leave_operations()
local clan = clans.clan_get_by_player(player)
if not clan then return end

Event.trigger(Event.Type.on_clan_player_leave, clan.name, player:get_player_name())
Event.trigger(Event.Type.on_clan_player_leave, clan, player:get_player_name())
clan_is_online_cache[clan.name] = nil -- reset cache (this will force recalc cache)
end)
end
Expand Down
52 changes: 29 additions & 23 deletions mods/lord/Player/clans/src/clans/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ local colorize = minetest.colorize
---@param clan clans.Clan
---@return string
local function get_beautiful_clan_str(clan)
local title_str = minetest.colorize(clans.COLOR, clan.title)
local id_str = minetest.colorize("grey", clan.name)
local status_str = minetest.colorize("green", "online")
local title_str = colorize(clans.COLOR, clan.title)
local id_str = colorize("grey", clan.name)
local status_str = colorize("green", "online")
if not clans.clan_is_online(clan.name) then
status_str = "offline"
if clan.is_blocked then
status_str = "blocked"
end
status_str = minetest.colorize("red", status_str)
status_str = colorize("red", status_str)
end
return string.format("%s (%s) — %s", title_str, id_str, status_str)
end
Expand All @@ -24,7 +24,7 @@ minetest.register_chatcommand("clans.list", {
func = function(_, _)
local msg = S("List of clans in format '<clan title> (<clan name>) — <status>':") .. "\n"
for _, clan in pairs(clans.list()) do
msg = msg .. get_beautiful_clan_str(clan) .."\n"
msg = msg .. "- "..get_beautiful_clan_str(clan).."\n"
end
return true, "\n"..msg.."\n"
end
Expand All @@ -47,16 +47,16 @@ local auth_handler = minetest.get_auth_handler()
---@return string|nil
local function get_beautiful_player_str(player_name)
local race = races.list[races.get_race(player_name)]
local race_name = minetest.colorize("chocolate", race.name)
local last_status = minetest.colorize("green", "online")
local race_name = colorize("chocolate", race.name)
local last_status = colorize("green", "online")
if not is_player_online(player_name) then
local auth = auth_handler.get_auth(player_name)
if not auth then
minetest.log("error", "[clans] can't get player auth for "..player_name)
return nil
end
local seen = os.date("(%H:%M %d.%m.%y)", auth.last_login) -- "(18:49 12.06.24)"
last_status = minetest.colorize("red", "offline ") .. minetest.colorize("grey", seen)
last_status = colorize("red", "offline ") .. colorize("grey", seen)
end
return string.format("%s (%s) — %s", player_name, race_name, last_status)
end
Expand All @@ -74,7 +74,7 @@ minetest.register_chatcommand("clans.show", {
for _, player_name in ipairs(clan.players) do
local player_str = get_beautiful_player_str(player_name)
if player_str then
msg = msg .. player_str .. "\n"
msg = msg .. "- "..player_str.."\n"
end
end
end
Expand Down Expand Up @@ -133,6 +133,23 @@ minetest.register_chatcommand("clans.delete", {
end
})


--- Gets localized string for given clans.err from /clans.players.add command
---@param err string @clans.err[some_num]
---@param clan_name string
---@return string @localized str for error
local function err2str(err, clan_name)
local enum = {
[2] = S("A player from given is already assigned to a clan."),
[3] = S("Clan @1 does not exist.", clan_name),
[5] = S("Too many players in clan (max is @1). Can't add player(s).", clans.max_players_in_clan),
[6] = S("Clan @1 is blocked!", clan_name),
[7] = S("A player from given does not exist."),
}
local idx = table.indexof(clans.err, err)
return enum[idx]
end

minetest.register_chatcommand("clans.players.add", {
params = S("<clan name> <list of players separated by space>"),
description = S("Add given players to given clan"),
Expand All @@ -147,20 +164,9 @@ minetest.register_chatcommand("clans.players.add", {

for i, param in ipairs(params) do
if i ~= 1 then
local is_executed, err = clans.clan_players_add(clan_name, param)
if not is_executed then
if err == clans.err[3] then
return false, S("Clan @1 does not exist.", clan_name)
elseif err == clans.err[2] then
return false, S("A player from given is already assigned to a clan.")
elseif err == clans.err[5] then
return false, S(
"Too many players in clan (max is @1). Can't add player(s).",
clans.max_players_in_clan
)
elseif err == clans.err[6] then
return false, S("Clan @1 is blocked!", clan_name)
end
local _, err = clans.clan_players_add(clan_name, param)
if err then
return false, err2str(err, clan_name)
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions mods/lord/Player/clans/src/clans/players/nametag.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ end)
clans.on_clan_player_removed(function(clan, player_name)
set_player_nametag(player_name)
end)

clans.on_clan_player_join(function(clan, player_name)
set_player_nametag(player_name, clan.title)
end)
2 changes: 2 additions & 0 deletions util/mt-ide-helper/minetest_types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,8 @@ function minetest.is_player(obj) end
--- (regardless of online status)
---
--- [View in lua_api.txt](https://github.com/minetest/minetest/blob/5.4.1/doc/lua_api.txt#L5639-L5640)
---@param name string
---@return boolean
function minetest.player_exists(name) end
--- * Replaces definition of a builtin hud element
--- * `name`: `"breath"` or `"health"`
Expand Down

0 comments on commit 08e918d

Please sign in to comment.