1 // ====================================================
2 // Shared code for server commands, written by Samual
3 // Last updated: December 17th, 2011
4 // ====================================================
6 string GetCommandPrefix(entity caller)
14 string GetCallerName(entity caller)
17 return caller.netname;
19 return ((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
22 string GetClientErrorString(float clienterror, string original_input)
26 case CLIENT_DOESNT_EXIST: { return strcat("Client '", original_input, "' doesn't exist"); }
27 case CLIENT_NOT_REAL: { return strcat("Client '", original_input, "' is not real"); }
28 case CLIENT_IS_BOT: { return strcat("Client '", original_input, "' is a bot"); }
29 default: { return "Incorrect usage of GetClientErrorString"; }
33 float VerifyClientNumber(float tmp_number)
35 if((tmp_number < 1) || (tmp_number > maxclients))
41 float VerifyClientEntity(entity client, float must_be_real, float allow_bots)
43 if not(client.flags & FL_CLIENT)
44 return CLIENT_DOESNT_EXIST;
45 else if(must_be_real && (clienttype(client) != CLIENTTYPE_REAL))
46 return CLIENT_NOT_REAL;
47 else if(!allow_bots && (clienttype(client) == CLIENTTYPE_BOT))
50 return CLIENT_ACCEPTABLE;
53 // find a player which matches the input string, and return their entity
54 entity GetFilteredEntity(string input)
56 entity tmp_player, selection;
59 if(substring(input, 0, 1) == "#")
60 tmp_number = stof(substring(input, 1, -1));
62 tmp_number = stof(input);
64 if(VerifyClientNumber(tmp_number))
66 selection = edict_num(tmp_number);
70 FOR_EACH_CLIENT(tmp_player)
71 if (strdecolorize(tmp_player.netname) == strdecolorize(input))
72 selection = tmp_player;
78 // same thing, but instead return their edict number
79 float GetFilteredNumber(string input)
81 entity selection = GetFilteredEntity(input);
84 if(selection) { output = num_for_edict(selection); }
86 print(strcat("input: ", input, ", output: ", ftos(output), ",\n")); // todo remove after done debugging
90 // switch between sprint and print depending on whether the reciever is the server or a player
91 void print_to(entity to, string input)
94 sprint(to, strcat(input, "\n"));
100 // ===================================================
101 // Common commands used in both sv_cmd.qc and cmd.qc
102 // ===================================================
104 void CommonCommand_cvar_changes(float request, entity caller)
108 case CMD_REQUEST_COMMAND:
110 print_to(caller, cvar_changes);
111 return; // never fall through to usage
115 case CMD_REQUEST_USAGE:
117 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
118 print_to(caller, " No arguments required.");
119 print_to(caller, "See also: ^2cvar_purechanges^7");
125 void CommonCommand_cvar_purechanges(float request, entity caller)
129 case CMD_REQUEST_COMMAND:
131 print_to(caller, cvar_purechanges);
132 return; // never fall through to usage
136 case CMD_REQUEST_USAGE:
138 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
139 print_to(caller, " No arguments required.");
140 print_to(caller, "See also: ^2cvar_changes^7");
146 void CommonCommand_info(float request, entity caller, float argc) // todo: figure out how this works?
150 case CMD_REQUEST_COMMAND:
154 command = builtin_cvar_string(strcat("sv_info_", argv(1)));
156 wordwrap_sprint(command, 1111); // why 1111?
158 print_to(caller, "ERROR: unsupported info command");
160 return; // never fall through to usage
164 case CMD_REQUEST_USAGE:
166 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
167 print_to(caller, " Where 'request' is the suffixed string appended onto the request for cvar.");
173 void CommonCommand_ladder(float request, entity caller)
177 case CMD_REQUEST_COMMAND:
179 print_to(caller, ladder_reply);
180 return; // never fall through to usage
184 case CMD_REQUEST_USAGE:
186 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
187 print_to(caller, " No arguments required.");
193 void CommonCommand_lsmaps(float request, entity caller)
197 case CMD_REQUEST_COMMAND:
199 print_to(caller, lsmaps_reply);
200 return; // never fall through to usage
204 case CMD_REQUEST_USAGE:
206 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
207 print_to(caller, " No arguments required.");
213 void CommonCommand_lsnewmaps(float request, entity caller)
217 case CMD_REQUEST_COMMAND:
219 print_to(caller, lsnewmaps_reply);
220 return; // never fall through to usage
224 case CMD_REQUEST_USAGE:
226 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsnewmaps"));
227 print_to(caller, " No arguments required.");
233 void CommonCommand_maplist(float request, entity caller)
237 case CMD_REQUEST_COMMAND:
239 print_to(caller, maplist_reply);
240 return; // never fall through to usage
244 case CMD_REQUEST_USAGE:
246 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " maplist"));
247 print_to(caller, " No arguments required.");
253 void GameCommand_rankings(float request) // this is OLD.... jeez.
257 case CMD_REQUEST_COMMAND:
259 strunzone(rankings_reply);
260 rankings_reply = strzone(getrankings());
261 print(rankings_reply);
266 case CMD_REQUEST_USAGE:
268 print("\nUsage:^3 sv_cmd rankings");
269 print(" No arguments required.");
275 void CommonCommand_rankings(float request, entity caller)
279 case CMD_REQUEST_COMMAND:
281 print_to(caller, rankings_reply);
282 return; // never fall through to usage
286 case CMD_REQUEST_USAGE:
288 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
289 print_to(caller, " No arguments required.");
295 void CommonCommand_records(float request, entity caller) // TODO: Isn't this flooding with the sprint messages? Old code, but perhaps bad?
299 case CMD_REQUEST_COMMAND:
303 for(i = 0; i < 10; ++i)
304 print_to(caller, records_reply[i]);
306 return; // never fall through to usage
310 case CMD_REQUEST_USAGE:
312 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
313 print_to(caller, " No arguments required.");
319 void CommonCommand_teamstatus(float request, entity caller)
323 case CMD_REQUEST_COMMAND:
325 Score_NicePrint(caller);
326 return; // never fall through to usage
330 case CMD_REQUEST_USAGE:
332 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
333 print_to(caller, " No arguments required.");
339 void CommonCommand_time(float request, entity caller)
343 case CMD_REQUEST_COMMAND:
345 print_to(caller, strcat("time = ", ftos(time), "\n"));
346 print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART)), "\n"));
347 print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME)), "\n"));
348 print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES)), "\n"));
349 print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME)), "\n"));
350 print_to(caller, strcat("localtime = ", strftime(TRUE, "%a %b %e %H:%M:%S %Z %Y"), "\n")); // todo: Why is strftime broken? is engine problem, I think.
351 print_to(caller, strcat("gmtime = ", strftime(FALSE, "%a %b %e %H:%M:%S %Z %Y"), "\n"));
356 case CMD_REQUEST_USAGE:
358 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
359 print_to(caller, " No arguments required.");
365 void CommonCommand_timein(float request, entity caller) // todo entirely re-write this
369 case CMD_REQUEST_COMMAND:
371 if(caller.flags & FL_CLIENT)
373 if(autocvar_sv_timeout)
376 return print_to(caller, "^7Error: There is no active timeout which could be aborted!");
377 if (caller != timeoutInitiator)
378 return print_to(caller, "^7Error: You may not abort the active timeout. Only the player who called it can do that!");
380 if (timeoutStatus == 1)
382 remainingTimeoutTime = timeoutStatus = 0;
383 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
384 bprint(strcat("^7The timeout was aborted by ", caller.netname, " !\n"));
386 else if (timeoutStatus == 2)
388 //only shorten the remainingTimeoutTime if it makes sense
389 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) )
391 bprint(strcat("^1Attention: ^7", caller.netname, " resumed the game! Prepare for battle!\n"));
392 remainingTimeoutTime = autocvar_sv_timeout_resumetime;
393 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
396 print_to(caller, "^7Error: Your resumegame call was discarded!");
400 return; // never fall through to usage
404 case CMD_REQUEST_USAGE:
406 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
407 print_to(caller, " No arguments required.");
413 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
417 case CMD_REQUEST_COMMAND:
419 if(caller.flags & FL_CLIENT)
421 if(autocvar_sv_timeout)
423 if(caller.classname == "player")
426 print_to(caller, "^7Error: you can not call a timeout while a vote is active!");
429 if (inWarmupStage && !g_warmup_allow_timeout)
430 return print_to(caller, "^7Error: You can not call a timeout in warmup-stage!");
431 if (time < game_starttime )
432 return print_to(caller, "^7Error: You can not call a timeout while the map is being restarted!");
434 if (timeoutStatus != 2) {
435 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
436 if (autocvar_timelimit) {
437 //a timelimit was used
439 myTl = autocvar_timelimit;
441 float lastPossibleTimeout;
442 lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
444 if (lastPossibleTimeout < time - game_starttime)
445 return print_to(caller, "^7Error: It is too late to call a timeout now!");
449 //player may not call a timeout if he has no calls left
450 if (caller.allowedTimeouts < 1)
451 return print_to(caller, "^7Error: You already used all your timeout calls for this map!");
454 //now all required checks are passed
455 caller.allowedTimeouts -= 1;
456 bprint(caller.netname, " ^7called a timeout (", ftos(caller.allowedTimeouts), " timeouts left)!\n"); //write a bprint who started the timeout (and how many he has left)
457 remainingTimeoutTime = autocvar_sv_timeout_length;
458 remainingLeadTime = autocvar_sv_timeout_leadtime;
459 timeoutInitiator = caller;
460 if (timeoutStatus == 0) { //if another timeout was already active, don't change its status (which was 1 or 2) to 1, only change it to 1 if no timeout was active yet
462 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
463 timeoutHandler = spawn();
464 timeoutHandler.think = timeoutHandler_Think;
466 timeoutHandler.nextthink = time; //always let the entity think asap
468 //inform all connected clients about the timeout call
469 Announce("timeoutcalled");
473 print_to(caller, "^7Error: only players can call a timeout!");
476 return; // never fall through to usage
480 case CMD_REQUEST_USAGE:
482 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
483 print_to(caller, " No arguments required.");
489 void CommonCommand_who(float request, entity caller, float argc)
493 case CMD_REQUEST_COMMAND:
495 float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds, is_bot;
498 string separator = strcat((argv(1) ? argv(1) : " "), "^7");
499 float privacy = (caller && autocvar_sv_status_privacy);
501 print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : string_null), ":"));
502 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "),
503 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
505 FOR_EACH_CLIENT(tmp_player)
507 is_bot = (clienttype(tmp_player) == CLIENTTYPE_BOT);
509 tmp_hours = tmp_minutes = tmp_seconds = 0;
511 tmp_seconds = floor(time - tmp_player.jointime);
512 tmp_minutes = floor(tmp_seconds / 60);
513 tmp_hours = floor(tmp_minutes / 60);
515 if(tmp_minutes) { tmp_seconds -= (tmp_minutes * 60); }
516 if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
518 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20.20s %-5d %-3d %-9s %-16s %s "),
519 strcat("#", ftos(num_for_edict(tmp_player))),
522 tmp_player.ping_packetloss,
523 sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
524 (is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.netaddress)),
525 (is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.crypto_idfp))));
527 ++total_listed_players;
530 print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
532 return; // never fall through to usage
536 case CMD_REQUEST_USAGE:
538 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
539 print_to(caller, " Where 'separator' is the optional string to separate the values with, default is a space.");
545 /* use this when creating a new command, making sure to place it in alphabetical order.
546 void CommonCommand_(float request, entity caller)
550 case CMD_REQUEST_COMMAND:
553 return; // never fall through to usage
557 case CMD_REQUEST_USAGE:
559 print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
560 print_to(caller, " No arguments required.");