From 58db9e7dd0052cdef8051c8081be462dbe0c4d2a Mon Sep 17 00:00:00 2001 From: ANAND Date: Mon, 14 Sep 2020 07:47:19 +0530 Subject: [PATCH] HUD score improvements (#656) * Move hud_score mod to 'other' modpack * hud_score: Reuse element when adding a new element with the same name --- mods/{pvp => other}/hud_score/README.md | 10 +++++----- mods/{pvp => other}/hud_score/init.lua | 26 +++++++++++++++++++------ mods/{pvp => other}/hud_score/mod.conf | 0 3 files changed, 25 insertions(+), 11 deletions(-) rename mods/{pvp => other}/hud_score/README.md (82%) rename mods/{pvp => other}/hud_score/init.lua (88%) rename mods/{pvp => other}/hud_score/mod.conf (100%) diff --git a/mods/pvp/hud_score/README.md b/mods/other/hud_score/README.md similarity index 82% rename from mods/pvp/hud_score/README.md rename to mods/other/hud_score/README.md index df65d90..f7ea570 100644 --- a/mods/pvp/hud_score/README.md +++ b/mods/other/hud_score/README.md @@ -28,15 +28,15 @@ Example definition: ## `players` table This is a table of tables, indexed by player names. This table holds the HUD -data of all online players. Each sub-table is a list of score tables, which -are added by `hud_score.new`. +data of all online players. Each sub-table is a list of HUD score elements, +which are added by `hud_score.new`. ```lua local players = { ["name"] = { - [1] = , - [2] = , - [3] = + [1] = , + [2] = , + [3] = ... }, ["name2"] = { diff --git a/mods/pvp/hud_score/init.lua b/mods/other/hud_score/init.lua similarity index 88% rename from mods/pvp/hud_score/init.lua rename to mods/other/hud_score/init.lua index 62acea3..7985805 100644 --- a/mods/pvp/hud_score/init.lua +++ b/mods/other/hud_score/init.lua @@ -50,24 +50,38 @@ local function update(name) end function hud_score.new(name, def) - local player = minetest.get_player_by_name(name) - if not player then - return - end - -- Verify HUD score element def if not name or not def or type(def) ~= "table" or not def.name or not def.value or not def.color then error("hud_score: Invalid HUD score element definition", 2) end + local player = minetest.get_player_by_name(name) + if not player then + return + end + -- Store element expiration time in def.time -- and append score element def to players[name] def.time = os.time() + duration if next_check > duration then next_check = duration end - table.insert(players[name], def) + + -- If a HUD score element with the same name exists already, + -- reuse it instead of creating a new element + local is_new = true + for i, hud_score_spec in ipairs(players[name]) do + if hud_score_spec.name == def.name then + is_new = false + players[name][i] = def + break + end + end + + if is_new then + table.insert(players[name], def) + end -- If more than `max` active elements, mark oldest element for deletion if #players[name] > max then diff --git a/mods/pvp/hud_score/mod.conf b/mods/other/hud_score/mod.conf similarity index 100% rename from mods/pvp/hud_score/mod.conf rename to mods/other/hud_score/mod.conf