From a85e3c1a569304fde479dd9de593d8991fbe67c5 Mon Sep 17 00:00:00 2001 From: Samual Date: Wed, 28 Dec 2011 21:31:23 -0500 Subject: [PATCH] GetIndexedEntity(argc, start_index) -- allows for players to be described as: 1, #1, # 1, and by their nickname. --- qcsrc/server/command/cmd.qc | 4 +-- qcsrc/server/command/common.qc | 48 ++++++++++++++++++++++++++++++++++ qcsrc/server/command/common.qh | 5 +++- qcsrc/server/command/sv_cmd.qc | 18 ++++++------- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/qcsrc/server/command/cmd.qc b/qcsrc/server/command/cmd.qc index 449164e74..5c8d801ab 100644 --- a/qcsrc/server/command/cmd.qc +++ b/qcsrc/server/command/cmd.qc @@ -461,14 +461,14 @@ void ClientCommand_tell(float request, float argc, string command) { if(argc >= 3) { - entity tell_to = GetFilteredEntity(argv(1)); + entity tell_to = GetIndexedEntity(argc, 1); float tell_accepted = VerifyClientEntity(tell_to, TRUE, FALSE); if(tell_accepted > 0) // the target is a real client { if(tell_to != self) // and we're allowed to send to them :D { - Say(self, FALSE, tell_to, substring(command, argv_start_index(2), argv_end_index(-1) - argv_start_index(2)), TRUE); + Say(self, FALSE, tell_to, substring(command, argv_start_index(next_token), argv_end_index(-1) - argv_start_index(next_token)), TRUE); return; } else { print_to(self, "You can't ^2tell^7 a message to yourself."); return; } diff --git a/qcsrc/server/command/common.qc b/qcsrc/server/command/common.qc index 912e6e6ad..fae940495 100644 --- a/qcsrc/server/command/common.qc +++ b/qcsrc/server/command/common.qc @@ -55,6 +55,54 @@ float VerifyClientNumber(float tmp_number) return TRUE; } +entity GetIndexedEntity(float argc, float start_index) +{ + entity tmp_player, selection; + float tmp_number; + string tmp_string; + + next_token = -1; + + if(argc > start_index) + { + if(substring(argv(start_index), 0, 1) == "#") + { + tmp_string = substring(argv(start_index), 1, -1); + ++next_token; + + if(tmp_string) // is it all one token? like #1 + { + tmp_number = stof(tmp_string); + } + else if(argc > next_token) // no, it's two tokens? # 1 + { + tmp_number = stof(argv(next_token)); + ++next_token; + } + } + else // maybe it's ONLY a number? + { + tmp_number = stof(argv(start_index)); + ++next_token; + } + + if(VerifyClientNumber(tmp_number)) + { + selection = edict_num(tmp_number); // yes, it was a number + } + else // no, maybe it's a name? + { + next_token = (start_index + 1); + + FOR_EACH_CLIENT(tmp_player) + if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index))) + selection = tmp_player; + } + } + + return selection; +} + // find a player which matches the input string, and return their entity entity GetFilteredEntity(string input) { diff --git a/qcsrc/server/command/common.qh b/qcsrc/server/command/common.qh index 9f719892b..d3ee9cc27 100644 --- a/qcsrc/server/command/common.qh +++ b/qcsrc/server/command/common.qh @@ -32,4 +32,7 @@ float timeout_status; // (values: 0, 1, 2) contains whether a timeout is not act void timeout_handler_think(); // used by common/command/generic.qc:GenericCommand_dumpcommands to list all commands into a .txt file -void CommonCommand_macro_write_aliases(float fh); \ No newline at end of file +void CommonCommand_macro_write_aliases(float fh); + +// keep track of the next token to use for argc +float next_token; \ No newline at end of file diff --git a/qcsrc/server/command/sv_cmd.qc b/qcsrc/server/command/sv_cmd.qc index 34075853d..6385a78cc 100644 --- a/qcsrc/server/command/sv_cmd.qc +++ b/qcsrc/server/command/sv_cmd.qc @@ -196,7 +196,7 @@ void GameCommand_anticheat(float request, float argc) { case CMD_REQUEST_COMMAND: { - entity client = GetFilteredEntity(argv(1)); + entity client = GetIndexedEntity(argc, 1); float accepted = VerifyClientEntity(client, FALSE, FALSE); if(accepted > 0) @@ -495,7 +495,7 @@ void GameCommand_defer_clear(float request, float argc) if(argc == 2) { - client = GetFilteredEntity(argv(1)); + client = GetIndexedEntity(argc, 1); accepted = VerifyClientEntity(client, TRUE, FALSE); if(accepted > 0) @@ -1067,7 +1067,7 @@ void GameCommand_playerdemo(float request, float argc) // mostly legacy { case "read": { - client = GetFilteredEntity(argv(2)); + client = GetIndexedEntity(argc, 2); accepted = VerifyClientEntity(client, FALSE, TRUE); if not(accepted > 0) @@ -1077,13 +1077,13 @@ void GameCommand_playerdemo(float request, float argc) // mostly legacy } self = client; - playerdemo_open_read(argv(3)); + playerdemo_open_read(argv(next_token)); return; } case "write": { - client = GetFilteredEntity(argv(2)); + client = GetIndexedEntity(argc, 2); accepted = VerifyClientEntity(client, FALSE, FALSE); if not(accepted > 0) @@ -1093,7 +1093,7 @@ void GameCommand_playerdemo(float request, float argc) // mostly legacy } self = client; - playerdemo_open_write(argv(3)); + playerdemo_open_write(argv(next_token)); return; } @@ -1341,13 +1341,13 @@ void GameCommand_stuffto(float request, float argc) { if(argv(2)) { - entity client = GetFilteredEntity(argv(1)); + entity client = GetIndexedEntity(argc, 1)); float accepted = VerifyClientEntity(client, TRUE, FALSE); if(accepted > 0) { - stuffcmd(client, strcat("\n", argv(2), "\n")); - print(strcat("Command: \"", argv(2), "\" sent to ", GetCallerName(client), " (", argv(1) ,").\n")); + stuffcmd(client, strcat("\n", argv(next_token), "\n")); + print(strcat("Command: \"", argv(next_token), "\" sent to ", GetCallerName(client), " (", argv(1) ,").\n")); } else print("stuffto: ", GetClientErrorString(accepted, argv(1)), ".\n"); -- 2.39.2