Skip to content

Commit 7f304dd

Browse files
committed
Refactor repetitive string colorize code into a single function
1 parent 428b15b commit 7f304dd

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

amx/server/syscalls.lua

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,14 +1066,17 @@ function SendClientMessage(amx, player, r, g, b, a, message)
10661066
end
10671067

10681068
--replace colors
1069-
message = result:gsub("(=?{[0-9A-Fa-f]*})",
1069+
outputChatBox(colorizeString(message), player, r, g, b, true)
1070+
end
1071+
1072+
--replace colors
1073+
function colorizeString(string)
1074+
return string:gsub("(=?{[0-9A-Fa-f]*})",
10701075
function(colorMatches)
10711076
colorMatches = colorMatches:gsub("[{}]+", "") --replace the curly brackets with nothing
10721077
colorMatches = '#' .. colorMatches --Append to the beginning
10731078
return colorMatches
10741079
end)
1075-
1076-
outputChatBox(message, player, r, g, b, true)
10771080
end
10781081

10791082
function SendClientMessageToAll(amx, r, g, b, a, message)
@@ -1082,13 +1085,8 @@ function SendClientMessageToAll(amx, r, g, b, a, message)
10821085
end
10831086

10841087
--replace colors
1085-
message = result:gsub("(=?{[0-9A-Fa-f]*})",
1086-
function(colorMatches)
1087-
colorMatches = colorMatches:gsub("[{}]+", "") --replace the curly brackets with nothing
1088-
colorMatches = '#' .. colorMatches --Append to the beginning
1089-
return colorMatches
1090-
end)
1091-
1088+
message = colorizeString(message)
1089+
10921090
for i,data in pairs(g_Players) do
10931091
SendClientMessage(amx, data.elem, r, g, b, a, message)
10941092
end
@@ -1846,15 +1844,8 @@ function format(amx, outBuf, outBufSize, fmt, ...)
18461844
local result = fmt:format(unpack(args))
18471845

18481846
--replace colors
1849-
result = result:gsub("(=?{[0-9A-Fa-f]*})",
1850-
function(colorMatches)
1851-
colorMatches = colorMatches:gsub("[{}]+", "") --replace the curly brackets with nothing
1852-
colorMatches = '#' .. colorMatches --Append to the beginning
1853-
return colorMatches
1854-
end)
1855-
18561847
if #result+1 <= outBufSize then
1857-
writeMemString(amx, outBuf, result)
1848+
writeMemString(amx, outBuf, colorizeString(result))
18581849
end
18591850
end
18601851

0 commit comments

Comments
 (0)