Skip to content

Commit 0e8e4e8

Browse files
committed
remove dmessage counting, remove scoretotal loop, use for instead of while to construct names
1 parent e997b53 commit 0e8e4e8

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

mods/ctf/ctf_modebase/ranking_commands.lua

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ end)
9797

9898
minetest.register_chatcommand("donate", {
9999
description = "Donate your match score to your teammate\nCan be used only once in 2.5 minutes",
100-
params = "<playername> [playernames] <score> [message]",
100+
params = "<name [name2 name3 ...]> <score> [message]",
101101
func = function(name, param)
102102
local current_mode = ctf_modebase:get_current_mode()
103103
if not current_mode or not ctf_modebase.match_started then
@@ -106,17 +106,18 @@ minetest.register_chatcommand("donate", {
106106

107107
local pnames, score, dmessage = {}, 0, ""
108108

109-
local pcount = 0
109+
local pcount, ismessage = 0, false
110110

111111
for p in string.gmatch(param, "%S+") do
112-
if #dmessage > 0 then
112+
if ismessage then
113113
dmessage = dmessage .. " " .. p
114114
elseif ctf_core.to_number(p) and score == 0 then
115115
score = p
116116
else
117117
local team = ctf_teams.get(p)
118118
if not team and pcount > 0 then
119119
dmessage = dmessage .. p
120+
ismessage = true
120121
else
121122
if pnames[p] then
122123
return false, "You cannot donate more than once to the same person."
@@ -126,7 +127,7 @@ minetest.register_chatcommand("donate", {
126127
return false, 'You cannot donate to yourself!'
127128
end
128129

129-
if not ctf_teams.get(p) then
130+
if not minetest.get_player_by_name(p) then
130131
return false, string.format("Player %s is not online!", p)
131132
end
132133

@@ -158,11 +159,7 @@ minetest.register_chatcommand("donate", {
158159
return false, "You can donate no more than 400 score!"
159160
end
160161

161-
local scoretotal = 0
162-
for pname, _ in pairs(pnames) do
163-
scoretotal = scoretotal + score
164-
end
165-
162+
local scoretotal = score * pcount
166163
local cur_score = math.min(
167164
current_mode.recent_rankings.get(name).score or 0,
168165
(current_mode.rankings:get(name) or {}).score or 0
@@ -181,23 +178,16 @@ minetest.register_chatcommand("donate", {
181178

182179
dmessage = (dmessage and dmessage ~= "") and (":" .. dmessage) or ""
183180

184-
local pname, names = next(pnames), ""
185-
while pname do
181+
local names = ""
182+
for pname, team in pairs(pnames) do
186183
current_mode.recent_rankings.add(pname, {score=score}, true)
187184
current_mode.recent_rankings.add(name, {score=-score}, true)
188-
189185
minetest.log("action", string.format(
190186
"Player '%s' donated %s score to player '%s'", name, score, pname
191187
))
192-
193-
names = names .. pname
194-
195-
pname = next(pnames, pname)
196-
if pname then
197-
names = names .. ", "
198-
end
188+
names = names .. pname .. ", "
199189
end
200-
190+
names = names:sub(1, -3)
201191
if pcount > 2 then
202192
names = string.gsub(names, ", (%S+)$", ", and %1")
203193
elseif pcount > 1 then

0 commit comments

Comments
 (0)