]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/common.qc
fix CommonCommand_time prints, and make autoscreenshots go to their own directly...
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / common.qc
1 // ====================================================
2 //  Shared code for server commands, written by Samual
3 //  Last updated: December 19th, 2011
4 // ====================================================
5
6 // select the proper prefix for usage and other messages
7 string GetCommandPrefix(entity caller)
8 {
9         if(caller)
10                 return "cmd";
11         else
12                 return "sv_cmd";
13 }
14
15 // if client return player nickname, or if server return admin nickname
16 string GetCallerName(entity caller)
17 {
18         if(caller)
19                 return caller.netname;
20         else
21                 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
22 }
23
24 // verify that the client provided is acceptable for use
25 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
26 {
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;
33                 
34         return CLIENT_ACCEPTABLE;
35 }
36
37 // if the client is not acceptable, return a string to be used for error messages
38 string GetClientErrorString(float clienterror, string original_input)
39 {
40         switch(clienterror)
41         {
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"; }
46         }
47 }
48
49 // is this entity number even in the possible range of entities?
50 float VerifyClientNumber(float tmp_number)
51 {
52         if((tmp_number < 1) || (tmp_number > maxclients))
53                 return FALSE;
54         else
55                 return TRUE;
56 }
57
58 // find a player which matches the input string, and return their entity
59 entity GetFilteredEntity(string input)
60 {
61         entity tmp_player, selection;
62         float tmp_number;
63         
64         if(substring(input, 0, 1) == "#")
65                 tmp_number = stof(substring(input, 1, -1));
66         else
67                 tmp_number = stof(input);
68         
69         if(VerifyClientNumber(tmp_number))
70         {
71                 selection = edict_num(tmp_number);
72         }
73         else
74         {
75                 FOR_EACH_CLIENT(tmp_player)
76                         if (strdecolorize(tmp_player.netname) == strdecolorize(input))
77                                 selection = tmp_player;
78         }
79         
80         return selection;
81 }
82
83 // same thing, but instead return their edict number
84 float GetFilteredNumber(string input)
85 {
86         entity selection = GetFilteredEntity(input);
87         float output;
88         
89         if(selection) { output = num_for_edict(selection); }
90
91         print(strcat("input: ", input, ", output: ", ftos(output), ",\n")); // todo remove after done debugging
92         return output;
93 }
94
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)
97 {
98     if(to)
99         sprint(to, strcat(input, "\n"));
100     else
101         print(input, "\n");
102 }
103
104
105 // ===================================================
106 //  Common commands used in both sv_cmd.qc and cmd.qc
107 // ===================================================
108
109 void CommonCommand_cvar_changes(float request, entity caller)
110 {
111         switch(request)
112         {
113                 case CMD_REQUEST_COMMAND:
114                 {
115                         print_to(caller, cvar_changes);
116                         return; // never fall through to usage
117                 }
118                         
119                 default:
120                 case CMD_REQUEST_USAGE:
121                 {
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");
125                         return;
126                 }
127         }
128 }
129
130 void CommonCommand_cvar_purechanges(float request, entity caller)
131 {
132         switch(request)
133         {
134                 case CMD_REQUEST_COMMAND:
135                 {
136                         print_to(caller, cvar_purechanges);
137                         return; // never fall through to usage
138                 }
139                         
140                 default:
141                 case CMD_REQUEST_USAGE:
142                 {
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");
146                         return;
147                 }
148         }
149 }
150
151 void CommonCommand_info(float request, entity caller, float argc) // todo: figure out how this works?
152 {       
153         switch(request)
154         {
155                 case CMD_REQUEST_COMMAND:
156                 {
157                         string command;
158                         
159                         command = builtin_cvar_string(strcat("sv_info_", argv(1))); 
160                         if(command)
161                                 wordwrap_sprint(command, 1111); // why 1111?
162                         else
163                                 print_to(caller, "ERROR: unsupported info command");
164                                 
165                         return; // never fall through to usage
166                 }
167                         
168                 default:
169                 case CMD_REQUEST_USAGE:
170                 {
171                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
172                         print_to(caller, "  Where 'request' is the suffixed string appended onto the request for cvar.");
173                         return;
174                 }
175         }
176 }
177
178 void CommonCommand_ladder(float request, entity caller)
179 {
180         switch(request)
181         {
182                 case CMD_REQUEST_COMMAND:
183                 {
184                         print_to(caller, ladder_reply);
185                         return; // never fall through to usage
186                 }
187                         
188                 default:
189                 case CMD_REQUEST_USAGE:
190                 {
191                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
192                         print_to(caller, "  No arguments required.");
193                         return;
194                 }
195         }
196 }
197
198 void CommonCommand_lsmaps(float request, entity caller)
199 {
200         switch(request)
201         {
202                 case CMD_REQUEST_COMMAND:
203                 {
204                         print_to(caller, lsmaps_reply);
205                         return; // never fall through to usage
206                 }
207                         
208                 default:
209                 case CMD_REQUEST_USAGE:
210                 {
211                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
212                         print_to(caller, "  No arguments required.");
213                         return;
214                 }
215         }
216 }
217
218 void CommonCommand_lsnewmaps(float request, entity caller)
219 {
220         switch(request)
221         {
222                 case CMD_REQUEST_COMMAND:
223                 {
224                         print_to(caller, lsnewmaps_reply);
225                         return; // never fall through to usage
226                 }
227                         
228                 default:
229                 case CMD_REQUEST_USAGE:
230                 {
231                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsnewmaps"));
232                         print_to(caller, "  No arguments required.");
233                         return;
234                 }
235         }
236 }
237
238 void CommonCommand_maplist(float request, entity caller)
239 {
240         switch(request)
241         {
242                 case CMD_REQUEST_COMMAND:
243                 {
244                         print_to(caller, maplist_reply);
245                         return; // never fall through to usage
246                 }
247                         
248                 default:
249                 case CMD_REQUEST_USAGE:
250                 {
251                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " maplist"));
252                         print_to(caller, "  No arguments required.");
253                         return;
254                 }
255         }
256 }
257
258 void GameCommand_rankings(float request) // this is OLD.... jeez.
259 {
260         switch(request)
261         {
262                 case CMD_REQUEST_COMMAND:
263                 {
264                         strunzone(rankings_reply);
265                         rankings_reply = strzone(getrankings());
266                         print(rankings_reply);
267                         return;
268                 }
269                         
270                 default:
271                 case CMD_REQUEST_USAGE:
272                 {
273                         print("\nUsage:^3 sv_cmd rankings");
274                         print("  No arguments required.");
275                         return;
276                 }
277         }
278 }
279
280 void CommonCommand_rankings(float request, entity caller)
281 {
282         switch(request)
283         {
284                 case CMD_REQUEST_COMMAND:
285                 {
286                         print_to(caller, rankings_reply);
287                         return; // never fall through to usage
288                 }
289                         
290                 default:
291                 case CMD_REQUEST_USAGE:
292                 {
293                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
294                         print_to(caller, "  No arguments required.");
295                         return;
296                 }
297         }
298 }
299
300 void CommonCommand_records(float request, entity caller) // TODO: Isn't this flooding with the sprint messages? Old code, but perhaps bad?
301 {       
302         switch(request)
303         {
304                 case CMD_REQUEST_COMMAND:
305                 {
306                         float i;
307                         
308                         for(i = 0; i < 10; ++i)
309                                 print_to(caller, records_reply[i]);
310                                 
311                         return; // never fall through to usage
312                 }
313                         
314                 default:
315                 case CMD_REQUEST_USAGE:
316                 {
317                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
318                         print_to(caller, "  No arguments required.");
319                         return;
320                 }
321         }
322 }
323
324 void CommonCommand_teamstatus(float request, entity caller)
325 {
326         switch(request)
327         {
328                 case CMD_REQUEST_COMMAND:
329                 {
330                         Score_NicePrint(caller);
331                         return; // never fall through to usage
332                 }
333                         
334                 default:
335                 case CMD_REQUEST_USAGE:
336                 {
337                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
338                         print_to(caller, "  No arguments required.");
339                         return;
340                 }
341         }
342 }
343
344 void CommonCommand_time(float request, entity caller)
345 {
346         switch(request)
347         {
348                 case CMD_REQUEST_COMMAND:
349                 {
350                         print_to(caller, strcat("time = ", ftos(time)));
351                         print_to(caller, strcat("frame start = ", ftos(gettime(GETTIME_FRAMESTART))));
352                         print_to(caller, strcat("realtime = ", ftos(gettime(GETTIME_REALTIME))));
353                         print_to(caller, strcat("hires = ", ftos(gettime(GETTIME_HIRES))));
354                         print_to(caller, strcat("uptime = ", ftos(gettime(GETTIME_UPTIME))));
355                         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.
356                         print_to(caller, strcat("gmtime = ", strftime(FALSE, "%a %b %e %H:%M:%S %Z %Y")));
357                         return;
358                 }
359                         
360                 default:
361                 case CMD_REQUEST_USAGE:
362                 {
363                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
364                         print_to(caller, "  No arguments required.");
365                         return;
366                 }
367         }
368 }
369
370 void CommonCommand_timein(float request, entity caller) // todo entirely re-write this
371 {
372         switch(request)
373         {
374                 case CMD_REQUEST_COMMAND:
375                 {
376                         if(caller.flags & FL_CLIENT)
377                         {
378                                 if(autocvar_sv_timeout)
379                                 {
380                                         if (!timeoutStatus)
381                                                 return print_to(caller, "^7Error: There is no active timeout which could be aborted!");
382                                         if (caller != timeoutInitiator)
383                                                 return print_to(caller, "^7Error: You may not abort the active timeout. Only the player who called it can do that!");
384                                                 
385                                         if (timeoutStatus == 1) 
386                                         {
387                                                 remainingTimeoutTime = timeoutStatus = 0;
388                                                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
389                                                 bprint(strcat("^7The timeout was aborted by ", caller.netname, " !\n"));
390                                         }
391                                         else if (timeoutStatus == 2) 
392                                         {
393                                                 //only shorten the remainingTimeoutTime if it makes sense
394                                                 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) ) 
395                                                 {
396                                                         bprint(strcat("^1Attention: ^7", caller.netname, " resumed the game! Prepare for battle!\n"));
397                                                         remainingTimeoutTime = autocvar_sv_timeout_resumetime;
398                                                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
399                                                 }
400                                                 else
401                                                         print_to(caller, "^7Error: Your resumegame call was discarded!");
402                                         }
403                                 }
404                         }
405                         return; // never fall through to usage
406                 }
407                         
408                 default:
409                 case CMD_REQUEST_USAGE:
410                 {
411                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
412                         print_to(caller, "  No arguments required.");
413                         return;
414                 }
415         }
416 }
417
418 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
419 {
420         switch(request)
421         {
422                 case CMD_REQUEST_COMMAND:
423                 {
424                         if(caller.flags & FL_CLIENT)
425                         {
426                                 if(autocvar_sv_timeout) 
427                                 {
428                                         if(caller.classname == "player") 
429                                         {
430                                                 if(vote_called)
431                                                         print_to(caller, "^7Error: you can not call a timeout while a vote is active!");
432                                                 else
433                                                 {
434                                                         if (inWarmupStage && !g_warmup_allow_timeout)
435                                                                 return print_to(caller, "^7Error: You can not call a timeout in warmup-stage!");
436                                                         if (time < game_starttime )
437                                                                 return print_to(caller, "^7Error: You can not call a timeout while the map is being restarted!");
438                                                                 
439                                                         if (timeoutStatus != 2) {
440                                                                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
441                                                                 if (autocvar_timelimit) {
442                                                                         //a timelimit was used
443                                                                         float myTl;
444                                                                         myTl = autocvar_timelimit;
445
446                                                                         float lastPossibleTimeout;
447                                                                         lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
448
449                                                                         if (lastPossibleTimeout < time - game_starttime)
450                                                                                 return print_to(caller, "^7Error: It is too late to call a timeout now!");
451                                                                 }
452                                                         }
453                                                         
454                                                         //player may not call a timeout if he has no calls left
455                                                         if (caller.allowedTimeouts < 1)
456                                                                 return print_to(caller, "^7Error: You already used all your timeout calls for this map!");
457                                                                 
458                                                                 
459                                                         //now all required checks are passed
460                                                         caller.allowedTimeouts -= 1;
461                                                         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)
462                                                         remainingTimeoutTime = autocvar_sv_timeout_length;
463                                                         remainingLeadTime = autocvar_sv_timeout_leadtime;
464                                                         timeoutInitiator = caller;
465                                                         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
466                                                                 timeoutStatus = 1;
467                                                                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
468                                                                 timeoutHandler = spawn();
469                                                                 timeoutHandler.think = timeoutHandler_Think;
470                                                         }
471                                                         timeoutHandler.nextthink = time; //always let the entity think asap
472
473                                                         //inform all connected clients about the timeout call
474                                                         Announce("timeoutcalled");
475                                                 }
476                                         }
477                                         else
478                                                 print_to(caller, "^7Error: only players can call a timeout!");
479                                 }
480                         }
481                         return; // never fall through to usage
482                 }
483                         
484                 default:
485                 case CMD_REQUEST_USAGE:
486                 {
487                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
488                         print_to(caller, "  No arguments required.");
489                         return;
490                 }
491         }
492 }
493
494 void CommonCommand_who(float request, entity caller, float argc)
495 {
496         switch(request)
497         {
498                 case CMD_REQUEST_COMMAND:
499                 {
500                         float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds, is_bot;
501                         entity tmp_player;                      
502                         
503                         string separator = strcat((argv(1) ? argv(1) : " "), "^7");
504                         float privacy = (caller && autocvar_sv_status_privacy);
505                         
506                         print_to(caller, strcat("List of client information", (privacy ? " (some data is hidden for privacy)" : string_null), ":"));
507                         print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20s %-5s %-3s %-9s %-16s %s "), 
508                                 "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
509                         
510                         FOR_EACH_CLIENT(tmp_player)
511                         {
512                                 is_bot = (clienttype(tmp_player) == CLIENTTYPE_BOT);
513                                 
514                                 tmp_hours = tmp_minutes = tmp_seconds = 0;
515                                 
516                                 tmp_seconds = floor(time - tmp_player.jointime);
517                                 tmp_minutes = floor(tmp_seconds / 60);
518                                 tmp_hours = floor(tmp_minutes / 60);
519
520                                 if(tmp_minutes) { tmp_seconds -= (tmp_minutes * 60); }                          
521                                 if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
522
523                                 print_to(caller, sprintf(strreplace(" ", separator, " %-4s %-20.20s %-5d %-3d %-9s %-16s %s "), 
524                                         strcat("#", ftos(num_for_edict(tmp_player))), 
525                                         tmp_player.netname,
526                                         tmp_player.ping, 
527                                         tmp_player.ping_packetloss, 
528                                         sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
529                                         (is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.netaddress)),
530                                         (is_bot ? "null/botclient" : (privacy ? "hidden" : tmp_player.crypto_idfp))));
531                                         
532                                 ++total_listed_players;
533                         }
534                         
535                         print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
536                         
537                         return; // never fall through to usage
538                 }
539                         
540                 default:
541                 case CMD_REQUEST_USAGE:
542                 {
543                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " who [separator]"));
544                         print_to(caller, "  Where 'separator' is the optional string to separate the values with, default is a space.");
545                         return;
546                 }
547         }
548 }
549
550 /* use this when creating a new command, making sure to place it in alphabetical order.
551 void CommonCommand_(float request, entity caller)
552 {
553         switch(request)
554         {
555                 case CMD_REQUEST_COMMAND:
556                 {
557                         
558                         return; // never fall through to usage
559                 }
560                         
561                 default:
562                 case CMD_REQUEST_USAGE:
563                 {
564                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
565                         print_to(caller, "  No arguments required.");
566                         return;
567                 }
568         }
569 }
570 */