3 #include "../../common/counting.qh"
6 // ====================================================
7 // Shared code for server commands, written by Samual
8 // Last updated: December 27th, 2011
9 // ====================================================
11 // select the proper prefix for usage and other messages
12 string GetCommandPrefix(entity caller)
20 // if client return player nickname, or if server return admin nickname
21 string GetCallerName(entity caller)
24 return caller.netname;
26 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
29 // verify that the client provided is acceptable for kicking
30 float VerifyKickableEntity(entity client)
32 if (!IS_REAL_CLIENT(client))
33 return CLIENT_NOT_REAL;
34 return CLIENT_ACCEPTABLE;
37 // verify that the client provided is acceptable for use
38 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
40 if (!IS_CLIENT(client))
41 return CLIENT_DOESNT_EXIST;
42 else if(must_be_real && !IS_REAL_CLIENT(client))
43 return CLIENT_NOT_REAL;
44 else if(must_be_bots && !IS_BOT_CLIENT(client))
45 return CLIENT_NOT_BOT;
47 return CLIENT_ACCEPTABLE;
50 // if the client is not acceptable, return a string to be used for error messages
51 string GetClientErrorString(float clienterror, string original_input)
55 case CLIENT_DOESNT_EXIST: { return strcat("Client '", original_input, "' doesn't exist"); }
56 case CLIENT_NOT_REAL: { return strcat("Client '", original_input, "' is not real"); }
57 case CLIENT_NOT_BOT: { return strcat("Client '", original_input, "' is not a bot"); }
58 default: { return "Incorrect usage of GetClientErrorString"; }
62 // is this entity number even in the possible range of entities?
63 float VerifyClientNumber(float tmp_number)
65 if((tmp_number < 1) || (tmp_number > maxclients))
71 entity GetIndexedEntity(float argc, float start_index)
73 entity tmp_player, selection;
74 float tmp_number, index;
81 if(argc > start_index)
83 if(substring(argv(index), 0, 1) == "#")
85 tmp_string = substring(argv(index), 1, -1);
88 if(tmp_string != "") // is it all one token? like #1
90 tmp_number = stof(tmp_string);
92 else if(argc > index) // no, it's two tokens? # 1
94 tmp_number = stof(argv(index));
100 else // maybe it's ONLY a number?
102 tmp_number = stof(argv(index));
106 if(VerifyClientNumber(tmp_number))
108 selection = edict_num(tmp_number); // yes, it was a number
110 else // no, maybe it's a name?
112 FOR_EACH_CLIENT(tmp_player)
113 if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index)))
114 selection = tmp_player;
116 index = (start_index + 1);
121 //print(strcat("start_index: ", ftos(start_index), ", next_token: ", ftos(next_token), ", edict: ", ftos(num_for_edict(selection)), ".\n"));
125 // find a player which matches the input string, and return their entity
126 entity GetFilteredEntity(string input)
128 entity tmp_player, selection;
131 if(substring(input, 0, 1) == "#")
132 tmp_number = stof(substring(input, 1, -1));
134 tmp_number = stof(input);
136 if(VerifyClientNumber(tmp_number))
138 selection = edict_num(tmp_number);
143 FOR_EACH_CLIENT(tmp_player)
144 if (strdecolorize(tmp_player.netname) == strdecolorize(input))
145 selection = tmp_player;
151 // same thing, but instead return their edict number
152 float GetFilteredNumber(string input)
154 entity selection = GetFilteredEntity(input);
157 output = num_for_edict(selection);
162 // switch between sprint and print depending on whether the receiver is the server or a player
163 void print_to(entity to, string input)
166 sprint(to, strcat(input, "\n"));
171 // ==========================================
172 // Supporting functions for common commands
173 // ==========================================
175 // used by CommonCommand_timeout() and CommonCommand_timein() to handle game pausing and messaging and such.
176 void timeout_handler_reset()
178 timeout_caller = world;
180 timeout_leadtime = 0;
185 void timeout_handler_think()
189 switch(timeout_status)
193 if(timeout_time > 0) // countdown is still going
195 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_ENDING, timeout_time);
197 if(timeout_time == autocvar_sv_timeout_resumetime) // play a warning sound when only <sv_timeout_resumetime> seconds are left
198 Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_PREPARE);
200 self.nextthink = time + TIMEOUT_SLOWMO_VALUE; // think again in one second
201 timeout_time -= 1; // decrease the time counter
203 else // time to end the timeout
205 timeout_status = TIMEOUT_INACTIVE;
207 // reset the slowmo value back to normal
208 cvar_set("slowmo", ftos(orig_slowmo));
210 // unlock the view for players so they can move around again
211 FOR_EACH_REALPLAYER(tmp_player)
212 tmp_player.fixangle = false;
214 timeout_handler_reset();
220 case TIMEOUT_LEADTIME:
222 if(timeout_leadtime > 0) // countdown is still going
224 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_BEGINNING, timeout_leadtime);
226 self.nextthink = time + 1; // think again in one second
227 timeout_leadtime -= 1; // decrease the time counter
229 else // time to begin the timeout
231 timeout_status = TIMEOUT_ACTIVE;
233 // set the slowmo value to the timeout default slowmo value
234 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
236 // reset all the flood variables
237 FOR_EACH_CLIENT(tmp_player)
238 tmp_player.nickspamcount = tmp_player.nickspamtime = tmp_player.floodcontrol_chat =
239 tmp_player.floodcontrol_chatteam = tmp_player.floodcontrol_chattell =
240 tmp_player.floodcontrol_voice = tmp_player.floodcontrol_voiceteam = 0;
242 // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
243 FOR_EACH_REALPLAYER(tmp_player)
244 tmp_player.lastV_angle = tmp_player.v_angle;
246 self.nextthink = time; // think again next frame to handle it under TIMEOUT_ACTIVE code
253 case TIMEOUT_INACTIVE:
256 timeout_handler_reset();
264 // ===================================================
265 // Common commands used in both sv_cmd.qc and cmd.qc
266 // ===================================================
268 void CommonCommand_cvar_changes(float request, entity caller)
272 case CMD_REQUEST_COMMAND:
274 print_to(caller, cvar_changes);
275 return; // never fall through to usage
279 case CMD_REQUEST_USAGE:
281 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
282 print_to(caller, " No arguments required.");
283 print_to(caller, "See also: ^2cvar_purechanges^7");
289 void CommonCommand_cvar_purechanges(float request, entity caller)
293 case CMD_REQUEST_COMMAND:
295 print_to(caller, cvar_purechanges);
296 return; // never fall through to usage
300 case CMD_REQUEST_USAGE:
302 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
303 print_to(caller, " No arguments required.");
304 print_to(caller, "See also: ^2cvar_changes^7");
310 void CommonCommand_info(float request, entity caller, float argc)
314 case CMD_REQUEST_COMMAND:
316 string command = builtin_cvar_string(strcat("sv_info_", argv(1)));
319 wordwrap_sprint(command, 1000);
321 print_to(caller, "ERROR: unsupported info command");
323 return; // never fall through to usage
327 case CMD_REQUEST_USAGE:
329 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
330 print_to(caller, " Where 'request' is the suffixed string appended onto the request for cvar.");
336 void CommonCommand_ladder(float request, entity caller)
340 case CMD_REQUEST_COMMAND:
342 print_to(caller, ladder_reply);
343 return; // never fall through to usage
347 case CMD_REQUEST_USAGE:
349 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
350 print_to(caller, " No arguments required.");
356 void CommonCommand_lsmaps(float request, entity caller)
360 case CMD_REQUEST_COMMAND:
362 print_to(caller, lsmaps_reply);
363 return; // never fall through to usage
367 case CMD_REQUEST_USAGE:
369 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
370 print_to(caller, " No arguments required.");
376 void CommonCommand_printmaplist(float request, entity caller)
380 case CMD_REQUEST_COMMAND:
382 print_to(caller, maplist_reply);
383 return; // never fall through to usage
387 case CMD_REQUEST_USAGE:
389 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " printmaplist"));
390 print_to(caller, " No arguments required.");
396 void CommonCommand_rankings(float request, entity caller)
400 case CMD_REQUEST_COMMAND:
402 print_to(caller, rankings_reply);
403 return; // never fall through to usage
407 case CMD_REQUEST_USAGE:
409 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
410 print_to(caller, " No arguments required.");
416 void CommonCommand_records(float request, entity caller)
420 case CMD_REQUEST_COMMAND:
422 for(int i = 0; i < 10; ++i)
423 if(records_reply[i] != "")
424 print_to(caller, records_reply[i]);
426 return; // never fall through to usage
430 case CMD_REQUEST_USAGE:
432 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
433 print_to(caller, " No arguments required.");
439 void CommonCommand_teamstatus(float request, entity caller)
443 case CMD_REQUEST_COMMAND:
445 Score_NicePrint(caller);
446 return; // never fall through to usage
450 case CMD_REQUEST_USAGE:
452 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
453 print_to(caller, " No arguments required.");
459 void CommonCommand_time(float request, entity caller)
463 case CMD_REQUEST_COMMAND:
465 print_to(caller, strcat("time = ", ftos(time)));
466 print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
467 print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
468 print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
469 print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
470 print_to(caller, strcat("localtime = ", strftime(true, "%a %b %e %H:%M:%S %Z %Y")));
471 print_to(caller, strcat("gmtime = ", strftime(false, "%a %b %e %H:%M:%S %Z %Y")));
476 case CMD_REQUEST_USAGE:
478 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
479 print_to(caller, " No arguments required.");
485 void CommonCommand_timein(float request, entity caller)
489 case CMD_REQUEST_COMMAND:
491 if(!caller || autocvar_sv_timeout)
493 if (!timeout_status) { print_to(caller, "^7Error: There is no active timeout called."); }
494 else if(caller && (caller != timeout_caller)) { print_to(caller, "^7Error: You are not allowed to stop the active timeout."); }
496 else // everything should be okay, continue aborting timeout
498 switch(timeout_status)
500 case TIMEOUT_LEADTIME:
502 timeout_status = TIMEOUT_INACTIVE;
504 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
505 bprint(strcat("^7The timeout was aborted by ", GetCallerName(caller), " !\n"));
511 timeout_time = autocvar_sv_timeout_resumetime;
512 timeout_handler.nextthink = time; // timeout_handler has to take care of it immediately
513 bprint(strcat("^1Attention: ^7", GetCallerName(caller), " resumed the game! Prepare for battle!\n"));
517 default: dprint("timeout status was inactive, but this code was executed anyway?"); return;
521 else { print_to(caller, "^1Timeins are not allowed to be called, enable them with sv_timeout 1.\n"); }
523 return; // never fall through to usage
527 case CMD_REQUEST_USAGE:
529 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
530 print_to(caller, " No arguments required.");
536 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
540 case CMD_REQUEST_COMMAND:
542 if(!caller || autocvar_sv_timeout)
544 float last_possible_timeout = ((autocvar_timelimit * 60) - autocvar_sv_timeout_leadtime - 1);
546 if(timeout_status) { print_to(caller, "^7Error: A timeout is already active."); }
547 else if(vote_called) { print_to(caller, "^7Error: You can not call a timeout while a vote is active."); }
548 else if(warmup_stage && !g_warmup_allow_timeout) { print_to(caller, "^7Error: You can not call a timeout in warmup-stage."); }
549 else if(time < game_starttime) { print_to(caller, "^7Error: You can not call a timeout while the map is being restarted."); }
550 else if(caller && (caller.allowed_timeouts < 1)) { print_to(caller, "^7Error: You already used all your timeout calls for this map."); }
551 else if(caller && !IS_PLAYER(caller)) { print_to(caller, "^7Error: You must be a player to call a timeout."); }
552 else if((autocvar_timelimit) && (last_possible_timeout < time - game_starttime)) { print_to(caller, "^7Error: It is too late to call a timeout now!"); }
554 else // everything should be okay, proceed with starting the timeout
556 if(caller) { caller.allowed_timeouts -= 1; }
558 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)
560 timeout_status = TIMEOUT_LEADTIME;
561 timeout_caller = caller;
562 timeout_time = autocvar_sv_timeout_length;
563 timeout_leadtime = autocvar_sv_timeout_leadtime;
565 timeout_handler = spawn();
566 timeout_handler.think = timeout_handler_think;
567 timeout_handler.nextthink = time; // always let the entity think asap
569 Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_TIMEOUT);
572 else { print_to(caller, "^1Timeouts are not allowed to be called, enable them with sv_timeout 1.\n"); }
574 return; // never fall through to usage
578 case CMD_REQUEST_USAGE:
580 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
581 print_to(caller, " No arguments required.");
587 void CommonCommand_who(float request, entity caller, float argc)
591 case CMD_REQUEST_COMMAND:
593 float total_listed_players, is_bot;
596 float privacy = (caller && autocvar_sv_status_privacy);
597 string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
598 string tmp_netaddress, tmp_crypto_idfp;
600 print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : ""), ":"));
601 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
602 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
604 total_listed_players = 0;
605 FOR_EACH_CLIENT(tmp_player)
607 is_bot = (IS_BOT_CLIENT(tmp_player));
611 tmp_netaddress = "null/botclient";
612 tmp_crypto_idfp = "null/botclient";
616 tmp_netaddress = "hidden";
617 tmp_crypto_idfp = "hidden";
621 tmp_netaddress = tmp_player.netaddress;
622 tmp_crypto_idfp = tmp_player.crypto_idfp;
625 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
626 num_for_edict(tmp_player),
629 tmp_player.ping_packetloss,
630 process_time(1, time - tmp_player.jointime),
634 ++total_listed_players;
637 print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
639 return; // never fall through to usage
643 case CMD_REQUEST_USAGE:
645 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
646 print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
652 /* use this when creating a new command, making sure to place it in alphabetical order... also,
653 ** ADD ALL NEW COMMANDS TO commands.cfg WITH PROPER ALIASES IN THE SAME FASHION!
654 void CommonCommand_(float request, entity caller)
658 case CMD_REQUEST_COMMAND:
661 return; // never fall through to usage
665 case CMD_REQUEST_USAGE:
667 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
668 print_to(caller, " No arguments required.");