4 #include "../../dpdefs/progsdefs.qh"
5 #include "../../dpdefs/dpextensions.qh"
6 #include "../../common/util.qh"
7 #include "../../common/counting.qh"
8 #include "../../common/command/shared_defs.qh"
9 #include "../autocvars.qh"
11 #include "../../common/notifications.qh"
14 #include "../scores.qh"
17 // ====================================================
18 // Shared code for server commands, written by Samual
19 // Last updated: December 27th, 2011
20 // ====================================================
22 // select the proper prefix for usage and other messages
23 string GetCommandPrefix(entity caller)
31 // if client return player nickname, or if server return admin nickname
32 string GetCallerName(entity caller)
35 return caller.netname;
37 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
40 // verify that the client provided is acceptable for use
41 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
43 if (!IS_CLIENT(client))
44 return CLIENT_DOESNT_EXIST;
45 else if(must_be_real && !IS_REAL_CLIENT(client))
46 return CLIENT_NOT_REAL;
47 else if(must_be_bots && !IS_BOT_CLIENT(client))
48 return CLIENT_NOT_BOT;
50 return CLIENT_ACCEPTABLE;
53 // if the client is not acceptable, return a string to be used for error messages
54 string GetClientErrorString(float clienterror, string original_input)
58 case CLIENT_DOESNT_EXIST: { return strcat("Client '", original_input, "' doesn't exist"); }
59 case CLIENT_NOT_REAL: { return strcat("Client '", original_input, "' is not real"); }
60 case CLIENT_NOT_BOT: { return strcat("Client '", original_input, "' is not a bot"); }
61 default: { return "Incorrect usage of GetClientErrorString"; }
65 // is this entity number even in the possible range of entities?
66 float VerifyClientNumber(float tmp_number)
68 if((tmp_number < 1) || (tmp_number > maxclients))
74 entity GetIndexedEntity(float argc, float start_index)
76 entity tmp_player, selection;
77 float tmp_number, index;
84 if(argc > start_index)
86 if(substring(argv(index), 0, 1) == "#")
88 tmp_string = substring(argv(index), 1, -1);
91 if(tmp_string != "") // is it all one token? like #1
93 tmp_number = stof(tmp_string);
95 else if(argc > index) // no, it's two tokens? # 1
97 tmp_number = stof(argv(index));
103 else // maybe it's ONLY a number?
105 tmp_number = stof(argv(index));
109 if(VerifyClientNumber(tmp_number))
111 selection = edict_num(tmp_number); // yes, it was a number
113 else // no, maybe it's a name?
115 FOR_EACH_CLIENT(tmp_player)
116 if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index)))
117 selection = tmp_player;
119 index = (start_index + 1);
124 //print(strcat("start_index: ", ftos(start_index), ", next_token: ", ftos(next_token), ", edict: ", ftos(num_for_edict(selection)), ".\n"));
128 // find a player which matches the input string, and return their entity
129 entity GetFilteredEntity(string input)
131 entity tmp_player, selection;
134 if(substring(input, 0, 1) == "#")
135 tmp_number = stof(substring(input, 1, -1));
137 tmp_number = stof(input);
139 if(VerifyClientNumber(tmp_number))
141 selection = edict_num(tmp_number);
146 FOR_EACH_CLIENT(tmp_player)
147 if (strdecolorize(tmp_player.netname) == strdecolorize(input))
148 selection = tmp_player;
154 // same thing, but instead return their edict number
155 float GetFilteredNumber(string input)
157 entity selection = GetFilteredEntity(input);
160 output = num_for_edict(selection);
165 // switch between sprint and print depending on whether the receiver is the server or a player
166 void print_to(entity to, string input)
169 sprint(to, strcat(input, "\n"));
174 // ==========================================
175 // Supporting functions for common commands
176 // ==========================================
178 // used by CommonCommand_timeout() and CommonCommand_timein() to handle game pausing and messaging and such.
179 void timeout_handler_reset()
181 timeout_caller = world;
183 timeout_leadtime = 0;
188 void timeout_handler_think()
192 switch(timeout_status)
196 if(timeout_time > 0) // countdown is still going
198 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_ENDING, timeout_time);
200 if(timeout_time == autocvar_sv_timeout_resumetime) // play a warning sound when only <sv_timeout_resumetime> seconds are left
201 Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_PREPARE);
203 self.nextthink = time + TIMEOUT_SLOWMO_VALUE; // think again in one second
204 timeout_time -= 1; // decrease the time counter
206 else // time to end the timeout
208 timeout_status = TIMEOUT_INACTIVE;
210 // reset the slowmo value back to normal
211 cvar_set("slowmo", ftos(orig_slowmo));
213 // unlock the view for players so they can move around again
214 FOR_EACH_REALPLAYER(tmp_player)
215 tmp_player.fixangle = false;
217 timeout_handler_reset();
223 case TIMEOUT_LEADTIME:
225 if(timeout_leadtime > 0) // countdown is still going
227 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_BEGINNING, timeout_leadtime);
229 self.nextthink = time + 1; // think again in one second
230 timeout_leadtime -= 1; // decrease the time counter
232 else // time to begin the timeout
234 timeout_status = TIMEOUT_ACTIVE;
236 // set the slowmo value to the timeout default slowmo value
237 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
239 // reset all the flood variables
240 FOR_EACH_CLIENT(tmp_player)
241 tmp_player.nickspamcount = tmp_player.nickspamtime = tmp_player.floodcontrol_chat =
242 tmp_player.floodcontrol_chatteam = tmp_player.floodcontrol_chattell =
243 tmp_player.floodcontrol_voice = tmp_player.floodcontrol_voiceteam = 0;
245 // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
246 FOR_EACH_REALPLAYER(tmp_player)
247 tmp_player.lastV_angle = tmp_player.v_angle;
249 self.nextthink = time; // think again next frame to handle it under TIMEOUT_ACTIVE code
256 case TIMEOUT_INACTIVE:
259 timeout_handler_reset();
267 // ===================================================
268 // Common commands used in both sv_cmd.qc and cmd.qc
269 // ===================================================
271 void CommonCommand_cvar_changes(float request, entity caller)
275 case CMD_REQUEST_COMMAND:
277 print_to(caller, cvar_changes);
278 return; // never fall through to usage
282 case CMD_REQUEST_USAGE:
284 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
285 print_to(caller, " No arguments required.");
286 print_to(caller, "See also: ^2cvar_purechanges^7");
292 void CommonCommand_cvar_purechanges(float request, entity caller)
296 case CMD_REQUEST_COMMAND:
298 print_to(caller, cvar_purechanges);
299 return; // never fall through to usage
303 case CMD_REQUEST_USAGE:
305 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
306 print_to(caller, " No arguments required.");
307 print_to(caller, "See also: ^2cvar_changes^7");
313 void CommonCommand_info(float request, entity caller, float argc)
317 case CMD_REQUEST_COMMAND:
319 string command = builtin_cvar_string(strcat("sv_info_", argv(1)));
322 wordwrap_sprint(command, 1000);
324 print_to(caller, "ERROR: unsupported info command");
326 return; // never fall through to usage
330 case CMD_REQUEST_USAGE:
332 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
333 print_to(caller, " Where 'request' is the suffixed string appended onto the request for cvar.");
339 void CommonCommand_ladder(float request, entity caller)
343 case CMD_REQUEST_COMMAND:
345 print_to(caller, ladder_reply);
346 return; // never fall through to usage
350 case CMD_REQUEST_USAGE:
352 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
353 print_to(caller, " No arguments required.");
359 void CommonCommand_lsmaps(float request, entity caller)
363 case CMD_REQUEST_COMMAND:
365 print_to(caller, lsmaps_reply);
366 return; // never fall through to usage
370 case CMD_REQUEST_USAGE:
372 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
373 print_to(caller, " No arguments required.");
379 void CommonCommand_printmaplist(float request, entity caller)
383 case CMD_REQUEST_COMMAND:
385 print_to(caller, maplist_reply);
386 return; // never fall through to usage
390 case CMD_REQUEST_USAGE:
392 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " printmaplist"));
393 print_to(caller, " No arguments required.");
399 void CommonCommand_rankings(float request, entity caller)
403 case CMD_REQUEST_COMMAND:
405 print_to(caller, rankings_reply);
406 return; // never fall through to usage
410 case CMD_REQUEST_USAGE:
412 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
413 print_to(caller, " No arguments required.");
419 void CommonCommand_records(float request, entity caller)
423 case CMD_REQUEST_COMMAND:
427 for(i = 0; i < 10; ++i)
428 if(records_reply[i] != "")
429 print_to(caller, records_reply[i]);
431 return; // never fall through to usage
435 case CMD_REQUEST_USAGE:
437 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
438 print_to(caller, " No arguments required.");
444 void CommonCommand_teamstatus(float request, entity caller)
448 case CMD_REQUEST_COMMAND:
450 Score_NicePrint(caller);
451 return; // never fall through to usage
455 case CMD_REQUEST_USAGE:
457 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
458 print_to(caller, " No arguments required.");
464 void CommonCommand_time(float request, entity caller)
468 case CMD_REQUEST_COMMAND:
470 print_to(caller, strcat("time = ", ftos(time)));
471 print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
472 print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
473 print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
474 print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
475 print_to(caller, strcat("localtime = ", strftime(true, "%a %b %e %H:%M:%S %Z %Y")));
476 print_to(caller, strcat("gmtime = ", strftime(false, "%a %b %e %H:%M:%S %Z %Y")));
481 case CMD_REQUEST_USAGE:
483 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
484 print_to(caller, " No arguments required.");
490 void CommonCommand_timein(float request, entity caller)
494 case CMD_REQUEST_COMMAND:
496 if(!caller || autocvar_sv_timeout)
498 if (!timeout_status) { print_to(caller, "^7Error: There is no active timeout called."); }
499 else if(caller && (caller != timeout_caller)) { print_to(caller, "^7Error: You are not allowed to stop the active timeout."); }
501 else // everything should be okay, continue aborting timeout
503 switch(timeout_status)
505 case TIMEOUT_LEADTIME:
507 timeout_status = TIMEOUT_INACTIVE;
509 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
510 bprint(strcat("^7The timeout was aborted by ", GetCallerName(caller), " !\n"));
516 timeout_time = autocvar_sv_timeout_resumetime;
517 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
518 bprint(strcat("^1Attention: ^7", GetCallerName(caller), " resumed the game! Prepare for battle!\n"));
522 default: dprint("timeout status was inactive, but this code was executed anyway?"); return;
526 else { print_to(caller, "^1Timeins are not allowed to be called, enable them with sv_timeout 1.\n"); }
528 return; // never fall through to usage
532 case CMD_REQUEST_USAGE:
534 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
535 print_to(caller, " No arguments required.");
541 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
545 case CMD_REQUEST_COMMAND:
547 if(!caller || autocvar_sv_timeout)
549 float last_possible_timeout = ((autocvar_timelimit * 60) - autocvar_sv_timeout_leadtime - 1);
551 if(timeout_status) { print_to(caller, "^7Error: A timeout is already active."); }
552 else if(vote_called) { print_to(caller, "^7Error: You can not call a timeout while a vote is active."); }
553 else if(warmup_stage && !g_warmup_allow_timeout) { print_to(caller, "^7Error: You can not call a timeout in warmup-stage."); }
554 else if(time < game_starttime) { print_to(caller, "^7Error: You can not call a timeout while the map is being restarted."); }
555 else if(caller && (caller.allowed_timeouts < 1)) { print_to(caller, "^7Error: You already used all your timeout calls for this map."); }
556 else if(caller && !IS_PLAYER(caller)) { print_to(caller, "^7Error: You must be a player to call a timeout."); }
557 else if((autocvar_timelimit) && (last_possible_timeout < time - game_starttime)) { print_to(caller, "^7Error: It is too late to call a timeout now!"); }
559 else // everything should be okay, proceed with starting the timeout
561 if(caller) { caller.allowed_timeouts -= 1; }
563 bprint(GetCallerName(caller), " ^7called a timeout", (caller ? strcat(" (", ftos(caller.allowed_timeouts), " timeout(s) left)") : ""), "!\n"); // write a bprint who started the timeout (and how many they have left)
565 timeout_status = TIMEOUT_LEADTIME;
566 timeout_caller = caller;
567 timeout_time = autocvar_sv_timeout_length;
568 timeout_leadtime = autocvar_sv_timeout_leadtime;
570 timeout_handler = spawn();
571 timeout_handler.think = timeout_handler_think;
572 timeout_handler.nextthink = time; // always let the entity think asap
574 Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_TIMEOUT);
577 else { print_to(caller, "^1Timeouts are not allowed to be called, enable them with sv_timeout 1.\n"); }
579 return; // never fall through to usage
583 case CMD_REQUEST_USAGE:
585 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
586 print_to(caller, " No arguments required.");
592 void CommonCommand_who(float request, entity caller, float argc)
596 case CMD_REQUEST_COMMAND:
598 float total_listed_players, is_bot;
601 float privacy = (caller && autocvar_sv_status_privacy);
602 string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
603 string tmp_netaddress, tmp_crypto_idfp;
605 print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : ""), ":"));
606 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
607 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
609 total_listed_players = 0;
610 FOR_EACH_CLIENT(tmp_player)
612 is_bot = (IS_BOT_CLIENT(tmp_player));
616 tmp_netaddress = "null/botclient";
617 tmp_crypto_idfp = "null/botclient";
621 tmp_netaddress = "hidden";
622 tmp_crypto_idfp = "hidden";
626 tmp_netaddress = tmp_player.netaddress;
627 tmp_crypto_idfp = tmp_player.crypto_idfp;
630 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
631 num_for_edict(tmp_player),
634 tmp_player.ping_packetloss,
635 process_time(1, time - tmp_player.jointime),
639 ++total_listed_players;
642 print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
644 return; // never fall through to usage
648 case CMD_REQUEST_USAGE:
650 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
651 print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
657 /* use this when creating a new command, making sure to place it in alphabetical order... also,
658 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
659 void CommonCommand_(float request, entity caller)
663 case CMD_REQUEST_COMMAND:
666 return; // never fall through to usage
670 case CMD_REQUEST_USAGE:
672 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
673 print_to(caller, " No arguments required.");
681 // ==================================
682 // Macro system for common commands
683 // ==================================
685 // Do not hard code aliases for these, instead create them in commands.cfg... also: keep in alphabetical order, please ;)
686 #define COMMON_COMMANDS(request,caller,arguments,command) \
687 COMMON_COMMAND("cvar_changes", CommonCommand_cvar_changes(request, caller), "Prints a list of all changed server cvars") \
688 COMMON_COMMAND("cvar_purechanges", CommonCommand_cvar_purechanges(request, caller), "Prints a list of all changed gameplay cvars") \
689 COMMON_COMMAND("info", CommonCommand_info(request, caller, arguments), "Request for unique server information set up by admin") \
690 COMMON_COMMAND("ladder", CommonCommand_ladder(request, caller), "Get information about top players if supported") \
691 COMMON_COMMAND("lsmaps", CommonCommand_lsmaps(request, caller), "List maps which can be used with the current game mode") \
692 COMMON_COMMAND("printmaplist", CommonCommand_printmaplist(request, caller), "Display full server maplist reply") \
693 COMMON_COMMAND("rankings", CommonCommand_rankings(request, caller), "Print information about rankings") \
694 COMMON_COMMAND("records", CommonCommand_records(request, caller), "List top 10 records for the current map") \
695 COMMON_COMMAND("teamstatus", CommonCommand_teamstatus(request, caller), "Show information about player and team scores") \
696 COMMON_COMMAND("time", CommonCommand_time(request, caller), "Print different formats/readouts of time") \
697 COMMON_COMMAND("timein", CommonCommand_timein(request, caller), "Resume the game from being paused with a timeout") \
698 COMMON_COMMAND("timeout", CommonCommand_timeout(request, caller), "Call a timeout which pauses the game for certain amount of time unless unpaused") \
699 COMMON_COMMAND("vote", VoteCommand(request, caller, arguments, command), "Request an action to be voted upon by players") \
700 COMMON_COMMAND("who", CommonCommand_who(request, caller, arguments), "Display detailed client information about all players") \
703 void CommonCommand_macro_help(entity caller)
705 #define COMMON_COMMAND(name,function,description) \
706 { print_to(caller, strcat(" ^2", name, "^7: ", description)); }
708 COMMON_COMMANDS(0, caller, 0, "");
709 #undef COMMON_COMMAND
714 float CommonCommand_macro_command(float argc, entity caller, string command)
716 #define COMMON_COMMAND(name,function,description) \
717 { if(name == strtolower(argv(0))) { function; return true; } }
719 COMMON_COMMANDS(CMD_REQUEST_COMMAND, caller, argc, command);
720 #undef COMMON_COMMAND
725 float CommonCommand_macro_usage(float argc, entity caller)
727 #define COMMON_COMMAND(name,function,description) \
728 { if(name == strtolower(argv(1))) { function; return true; } }
730 COMMON_COMMANDS(CMD_REQUEST_USAGE, caller, argc, "");
731 #undef COMMON_COMMAND
736 void CommonCommand_macro_write_aliases(float fh)
738 #define COMMON_COMMAND(name,function,description) \
739 { CMD_Write_Alias("qc_cmd_svcmd", name, description); }
741 COMMON_COMMANDS(0, world, 0, "");
742 #undef COMMON_COMMAND