1 // ====================================================
2 // Shared code for server commands, written by Samual
3 // Last updated: December 19th, 2011
4 // ====================================================
6 // select the proper prefix for usage and other messages
7 string GetCommandPrefix(entity caller)
15 // if client return player nickname, or if server return admin nickname
16 string GetCallerName(entity caller)
19 return caller.netname;
21 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
24 // verify that the client provided is acceptable for use
25 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
27 if not(client.flags & FL_CLIENT)
28 return CLIENT_DOESNT_EXIST;
29 else if(must_be_real && (clienttype(client) != CLIENTTYPE_REAL))
30 return CLIENT_NOT_REAL;
31 else if(must_be_bots && (clienttype(client) != CLIENTTYPE_BOT))
32 return CLIENT_NOT_BOT;
34 return CLIENT_ACCEPTABLE;
37 // if the client is not acceptable, return a string to be used for error messages
38 string GetClientErrorString(float clienterror, string original_input)
42 case CLIENT_DOESNT_EXIST: { return strcat("Client '", original_input, "' doesn't exist"); }
43 case CLIENT_NOT_REAL: { return strcat("Client '", original_input, "' is not real"); }
44 case CLIENT_NOT_BOT: { return strcat("Client '", original_input, "' is not a bot"); }
45 default: { return "Incorrect usage of GetClientErrorString"; }
49 // is this entity number even in the possible range of entities?
50 float VerifyClientNumber(float tmp_number)
52 if((tmp_number < 1) || (tmp_number > maxclients))
58 // find a player which matches the input string, and return their entity
59 entity GetFilteredEntity(string input)
61 entity tmp_player, selection;
64 if(substring(input, 0, 1) == "#")
65 tmp_number = stof(substring(input, 1, -1));
67 tmp_number = stof(input);
69 if(VerifyClientNumber(tmp_number))
71 selection = edict_num(tmp_number);
75 FOR_EACH_CLIENT(tmp_player)
76 if (strdecolorize(tmp_player.netname) == strdecolorize(input))
77 selection = tmp_player;
83 // same thing, but instead return their edict number
84 float GetFilteredNumber(string input)
86 entity selection = GetFilteredEntity(input);
89 if(selection) { output = num_for_edict(selection); }
91 print(strcat("input: ", input, ", output: ", ftos(output), ",\n")); // todo remove after done debugging
95 // switch between sprint and print depending on whether the reciever is the server or a player
96 void print_to(entity to, string input)
99 sprint(to, strcat(input, "\n"));
105 // ===================================================
106 // Common commands used in both sv_cmd.qc and cmd.qc
107 // ===================================================
109 void CommonCommand_cvar_changes(float request, entity caller)
113 case CMD_REQUEST_COMMAND:
115 print_to(caller, cvar_changes);
116 return; // never fall through to usage
120 case CMD_REQUEST_USAGE:
122 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
123 print_to(caller, " No arguments required.");
124 print_to(caller, "See also: ^2cvar_purechanges^7");
130 void CommonCommand_cvar_purechanges(float request, entity caller)
134 case CMD_REQUEST_COMMAND:
136 print_to(caller, cvar_purechanges);
137 return; // never fall through to usage
141 case CMD_REQUEST_USAGE:
143 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
144 print_to(caller, " No arguments required.");
145 print_to(caller, "See also: ^2cvar_changes^7");
151 void CommonCommand_info(float request, entity caller, float argc)
155 case CMD_REQUEST_COMMAND:
157 string command = builtin_cvar_string(strcat("sv_info_", argv(1)));
160 wordwrap_sprint(command, 1000);
162 print_to(caller, "ERROR: unsupported info command");
164 return; // never fall through to usage
168 case CMD_REQUEST_USAGE:
170 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
171 print_to(caller, " Where 'request' is the suffixed string appended onto the request for cvar.");
177 void CommonCommand_ladder(float request, entity caller)
181 case CMD_REQUEST_COMMAND:
183 print_to(caller, ladder_reply);
184 return; // never fall through to usage
188 case CMD_REQUEST_USAGE:
190 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
191 print_to(caller, " No arguments required.");
197 void CommonCommand_lsmaps(float request, entity caller)
201 case CMD_REQUEST_COMMAND:
203 print_to(caller, lsmaps_reply);
204 return; // never fall through to usage
208 case CMD_REQUEST_USAGE:
210 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
211 print_to(caller, " No arguments required.");
217 void CommonCommand_lsnewmaps(float request, entity caller)
221 case CMD_REQUEST_COMMAND:
223 print_to(caller, lsnewmaps_reply);
224 return; // never fall through to usage
228 case CMD_REQUEST_USAGE:
230 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsnewmaps"));
231 print_to(caller, " No arguments required.");
237 void CommonCommand_maplist(float request, entity caller)
241 case CMD_REQUEST_COMMAND:
243 print_to(caller, maplist_reply);
244 return; // never fall through to usage
248 case CMD_REQUEST_USAGE:
250 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " maplist"));
251 print_to(caller, " No arguments required.");
257 void GameCommand_rankings(float request) // this is OLD.... jeez.
261 case CMD_REQUEST_COMMAND:
263 strunzone(rankings_reply);
264 rankings_reply = strzone(getrankings());
265 print(rankings_reply);
270 case CMD_REQUEST_USAGE:
272 print("\nUsage:^3 sv_cmd rankings");
273 print(" No arguments required.");
279 void CommonCommand_rankings(float request, entity caller)
283 case CMD_REQUEST_COMMAND:
285 print_to(caller, rankings_reply);
286 return; // never fall through to usage
290 case CMD_REQUEST_USAGE:
292 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
293 print_to(caller, " No arguments required.");
299 void CommonCommand_records(float request, entity caller)
303 case CMD_REQUEST_COMMAND:
307 for(i = 0; i < 10; ++i)
308 print_to(caller, records_reply[i]);
310 return; // never fall through to usage
314 case CMD_REQUEST_USAGE:
316 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
317 print_to(caller, " No arguments required.");
323 void CommonCommand_teamstatus(float request, entity caller)
327 case CMD_REQUEST_COMMAND:
329 Score_NicePrint(caller);
330 return; // never fall through to usage
334 case CMD_REQUEST_USAGE:
336 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
337 print_to(caller, " No arguments required.");
343 void CommonCommand_time(float request, entity caller)
347 case CMD_REQUEST_COMMAND:
349 print_to(caller, strcat("time = ", ftos(time)));
350 print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
351 print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
352 print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
353 print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
354 print_to(caller, strcat("localtime = ", strftime(TRUE, "%a %b %e %H:%M:%S %Z %Y"))); // todo: Why is strftime broken? is engine problem, I think.
355 print_to(caller, strcat("gmtime = ", strftime(FALSE, "%a %b %e %H:%M:%S %Z %Y")));
360 case CMD_REQUEST_USAGE:
362 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
363 print_to(caller, " No arguments required.");
369 void CommonCommand_timein(float request, entity caller) // todo entirely re-write this
373 case CMD_REQUEST_COMMAND:
375 if(caller.flags & FL_CLIENT)
377 if(autocvar_sv_timeout)
380 return print_to(caller, "^7Error: There is no active timeout which could be aborted!");
381 if (caller != timeoutInitiator)
382 return print_to(caller, "^7Error: You may not abort the active timeout. Only the player who called it can do that!");
384 if (timeoutStatus == 1)
386 remainingTimeoutTime = timeoutStatus = 0;
387 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
388 bprint(strcat("^7The timeout was aborted by ", caller.netname, " !\n"));
390 else if (timeoutStatus == 2)
392 //only shorten the remainingTimeoutTime if it makes sense
393 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) )
395 bprint(strcat("^1Attention: ^7", caller.netname, " resumed the game! Prepare for battle!\n"));
396 remainingTimeoutTime = autocvar_sv_timeout_resumetime;
397 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
400 print_to(caller, "^7Error: Your resumegame call was discarded!");
404 return; // never fall through to usage
408 case CMD_REQUEST_USAGE:
410 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
411 print_to(caller, " No arguments required.");
417 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
421 case CMD_REQUEST_COMMAND:
423 if(autocvar_sv_timeout)
425 if(vote_called) { print_to(caller, "^7Error: You can not call a timeout while a vote is active."); }
426 else if(inWarmupStage && !g_warmup_allow_timeout) { print_to(caller, "^7Error: You can not call a timeout in warmup-stage."); }
427 else if(time < game_starttime) { print_to(caller, "^7Error: You can not call a timeout while the map is being restarted."); }
428 else if(caller && (caller.allowedTimeouts < 1)) { print_to(caller, "^7Error: You already used all your timeout calls for this map."); }
429 else if(caller && (caller.classname != "player")) { print_to(caller, "^7Error: You must be a player to call a timeout."); }
431 else // everything should be okay, proceed with starting the timeout
433 if (timeoutStatus != 2) {
434 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
435 if (autocvar_timelimit) {
436 //a timelimit was used
438 myTl = autocvar_timelimit;
440 float lastPossibleTimeout;
441 lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
443 if (lastPossibleTimeout < time - game_starttime)
444 return print_to(caller, "^7Error: It is too late to call a timeout now!");
448 if(caller) { caller.allowedTimeouts -= 1; }
450 bprint(GetCallerName(caller), " ^7called a timeout", (caller ? strcat(" (", ftos(caller.allowedTimeouts), " timeouts left)") : string_null), "!\n"); // write a bprint who started the timeout (and how many they have left)
452 remainingTimeoutTime = autocvar_sv_timeout_length;
453 remainingLeadTime = autocvar_sv_timeout_leadtime;
455 timeoutInitiator = caller;
457 // if another timeout was already active, don't change its status (which was 1 or 2) to 1
458 if (timeoutStatus == 0)
461 timeoutHandler = spawn();
462 timeoutHandler.think = timeoutHandler_Think;
465 timeoutHandler.nextthink = time; //always let the entity think asap
467 Announce("timeoutcalled");
470 return; // never fall through to usage
474 case CMD_REQUEST_USAGE:
476 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
477 print_to(caller, " No arguments required.");
483 void CommonCommand_who(float request, entity caller, float argc)
487 case CMD_REQUEST_COMMAND:
489 float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds, is_bot;
492 float privacy = (caller && autocvar_sv_status_privacy);
493 string separator = strreplace("%", " ", strcat((argv(1) ? argv(1) : " "), "^7"));
494 string tmp_netaddress, tmp_crypto_idfp;
496 print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : string_null), ":"));
497 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
498 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
500 FOR_EACH_CLIENT(tmp_player)
502 is_bot = (clienttype(tmp_player) == CLIENTTYPE_BOT);
506 tmp_netaddress = "null/botclient";
507 tmp_crypto_idfp = "null/botclient";
511 tmp_netaddress = "hidden";
512 tmp_crypto_idfp = "hidden";
516 tmp_netaddress = tmp_player.netaddress;
517 tmp_crypto_idfp = tmp_player.crypto_idfp;
520 tmp_hours = tmp_minutes = tmp_seconds = 0;
522 tmp_seconds = floor(time - tmp_player.jointime);
523 tmp_minutes = floor(tmp_seconds / 60);
524 tmp_hours = floor(tmp_minutes / 60);
526 if(tmp_minutes) { tmp_seconds -= (tmp_minutes * 60); }
527 if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
529 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
530 num_for_edict(tmp_player),
533 tmp_player.ping_packetloss,
534 sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
538 ++total_listed_players;
541 print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
543 return; // never fall through to usage
547 case CMD_REQUEST_USAGE:
549 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
550 print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
556 /* use this when creating a new command, making sure to place it in alphabetical order.
557 void CommonCommand_(float request, entity caller)
561 case CMD_REQUEST_COMMAND:
564 return; // never fall through to usage
568 case CMD_REQUEST_USAGE:
570 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
571 print_to(caller, " No arguments required.");