Skip to content

Commit

Permalink
Merge pull request #792 from MarechJ/bug/chat_commands
Browse files Browse the repository at this point in the history
Fix rcon chat commands
  • Loading branch information
FlorianSW authored Dec 17, 2024
2 parents 17d49a2 + dc0242a commit 4a01f8e
Showing 1 changed file with 0 additions and 55 deletions.
55 changes: 0 additions & 55 deletions rcon/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,61 +251,6 @@ def chat_rcon_command(
do_run_commands(rcon, commands)


def replace_params(ctx: dict[str, str], args: list[str], v: Any) -> Any:
"""
Replaces arguments (from args) and message parameters (from ctx) into the provided parameter value (v).
The parameter value can be a str, list or dict, the arguments and message parameters are going to be replaced at
any appropriate value:
* in lists: for each element of the list; if a list item is a list or dict, each item is processed independently to
ensure nested values are covered as well
* in dicts: for each value, but not for keys. If a value is a list or dict, it is going to be processed independently
just like list items are as well.
The return value has the same type as the passed in value, just with all arguments in values being replaced with their
expected context or parameters.
:param ctx:
:param args:
:param v:
:return:
"""
for i, a in enumerate(args):
if isinstance(v, str):
v = format_message_string(v.replace(f"${i + 1}", a), context=ctx)
elif isinstance(v, list):
for li, lv in enumerate(v):
v[li] = replace_params(ctx, args, lv)
elif isinstance(v, dict):
for k, kv in v:
v[k] = replace_params(ctx, args, kv)
return v

def max_arg_index(p: Any) -> int:
"""
Counts the highest argument index that occurs in any value. If $2 and $5 is used in any of the values, the return
value of this function will be 5.
Each item of a list, as well as each value of a dict key-value pair, is evaluated independently and the highest
argument index is returned from any nested value.
:param p:
:return:
"""
max_count = 0
if isinstance(p, list):
for v in p:
a = max_arg_index(v)
if a > max_count:
max_count = a
elif isinstance(p, str):
for a in ARG_RE.findall(p):
if int(a) > max_count:
max_count = int(a)
elif isinstance(p, dict):
for _, v in p:
a = max_arg_index(v)
if a > max_count:
max_count = a

return max_count


def chat_help_command(rcon: Rcon, command: BaseChatCommand, ctx: dict[str, str]):
player_id = ctx[MessageVariableContext.player_id.value]
description = command.description
Expand Down

0 comments on commit 4a01f8e

Please sign in to comment.