]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/common.qc
0bdc6672fbf550daa7d2d66b54bdb30cf69b99d3
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / common.qc
1 #include "../../common/command/command.qh"
2 #include "common.qh"
3 #include "../_all.qh"
4
5 #include "../scores.qh"
6
7 #include "../../common/notifications.qh"
8 #include "../../common/counting.qh"
9
10
11 // ====================================================
12 //  Shared code for server commands, written by Samual
13 //  Last updated: December 27th, 2011
14 // ====================================================
15
16 // select the proper prefix for usage and other messages
17 string GetCommandPrefix(entity caller)
18 {
19         if(caller)
20                 return "cmd";
21         else
22                 return "sv_cmd";
23 }
24
25 // if client return player nickname, or if server return admin nickname
26 string GetCallerName(entity caller)
27 {
28         if(caller)
29                 return caller.netname;
30         else
31                 return admin_name(); //((autocvar_sv_adminnick != "") ? autocvar_sv_adminnick : autocvar_hostname);
32 }
33
34 // verify that the client provided is acceptable for kicking
35 float VerifyKickableEntity(entity client)
36 {
37         if (!IS_REAL_CLIENT(client))
38                 return CLIENT_NOT_REAL;
39         return CLIENT_ACCEPTABLE;
40 }
41
42 // verify that the client provided is acceptable for use
43 float VerifyClientEntity(entity client, float must_be_real, float must_be_bots)
44 {
45         if (!IS_CLIENT(client))
46                 return CLIENT_DOESNT_EXIST;
47         else if(must_be_real && !IS_REAL_CLIENT(client))
48                 return CLIENT_NOT_REAL;
49         else if(must_be_bots && !IS_BOT_CLIENT(client))
50                 return CLIENT_NOT_BOT;
51
52         return CLIENT_ACCEPTABLE;
53 }
54
55 // if the client is not acceptable, return a string to be used for error messages
56 string GetClientErrorString_color(float clienterror, string original_input, string col)
57 {
58         switch(clienterror)
59         {
60                 case CLIENT_DOESNT_EXIST: { return strcat(col, "Client '", original_input, col, "' doesn't exist"); }
61                 case CLIENT_NOT_REAL: { return strcat(col, "Client '", original_input, col, "' is not real"); }
62                 case CLIENT_NOT_BOT: { return strcat(col, "Client '", original_input, col, "' is not a bot"); }
63                 default: { return "Incorrect usage of GetClientErrorString"; }
64         }
65 }
66
67 // is this entity number even in the possible range of entities?
68 float VerifyClientNumber(float tmp_number)
69 {
70         if((tmp_number < 1) || (tmp_number > maxclients))
71                 return false;
72         else
73                 return true;
74 }
75
76 entity GetIndexedEntity(float argc, float start_index)
77 {
78         entity tmp_player, selection;
79         float tmp_number, index;
80         string tmp_string;
81
82         next_token = -1;
83         index = start_index;
84         selection = world;
85
86         if(argc > start_index)
87         {
88                 if(substring(argv(index), 0, 1) == "#")
89                 {
90                         tmp_string = substring(argv(index), 1, -1);
91                         ++index;
92
93                         if(tmp_string != "") // is it all one token? like #1
94                         {
95                                 tmp_number = stof(tmp_string);
96                         }
97                         else if(argc > index) // no, it's two tokens? # 1
98                         {
99                                 tmp_number = stof(argv(index));
100                                 ++index;
101                         }
102                         else
103                                 tmp_number = 0;
104                 }
105                 else // maybe it's ONLY a number?
106                 {
107                         tmp_number = stof(argv(index));
108                         ++index;
109                 }
110
111                 if(VerifyClientNumber(tmp_number))
112                 {
113                         selection = edict_num(tmp_number); // yes, it was a number
114                 }
115                 else // no, maybe it's a name?
116                 {
117                         FOR_EACH_CLIENT(tmp_player)
118                                 if (strdecolorize(tmp_player.netname) == strdecolorize(argv(start_index)))
119                                         selection = tmp_player;
120
121                         index = (start_index + 1);
122                 }
123         }
124
125         next_token = index;
126         //print(strcat("start_index: ", ftos(start_index), ", next_token: ", ftos(next_token), ", edict: ", ftos(num_for_edict(selection)), ".\n"));
127         return selection;
128 }
129
130 // find a player which matches the input string, and return their entity
131 entity GetFilteredEntity(string input)
132 {
133         entity tmp_player, selection;
134         float tmp_number;
135
136         if(substring(input, 0, 1) == "#")
137                 tmp_number = stof(substring(input, 1, -1));
138         else
139                 tmp_number = stof(input);
140
141         if(VerifyClientNumber(tmp_number))
142         {
143                 selection = edict_num(tmp_number);
144         }
145         else
146         {
147                 selection = world;
148                 FOR_EACH_CLIENT(tmp_player)
149                         if (strdecolorize(tmp_player.netname) == strdecolorize(input))
150                                 selection = tmp_player;
151         }
152
153         return selection;
154 }
155
156 // same thing, but instead return their edict number
157 float GetFilteredNumber(string input)
158 {
159         entity selection = GetFilteredEntity(input);
160         float output;
161
162         output = num_for_edict(selection);
163
164         return output;
165 }
166
167 // switch between sprint and print depending on whether the receiver is the server or a player
168 void print_to(entity to, string input)
169 {
170     if(to)
171         sprint(to, strcat(input, "\n"));
172     else
173         print(input, "\n");
174 }
175
176 // ==========================================
177 //  Supporting functions for common commands
178 // ==========================================
179
180 // used by CommonCommand_timeout() and CommonCommand_timein() to handle game pausing and messaging and such.
181 void timeout_handler_reset()
182 {
183         timeout_caller = world;
184         timeout_time = 0;
185         timeout_leadtime = 0;
186
187         remove(self);
188 }
189
190 void timeout_handler_think()
191 {
192         entity tmp_player;
193
194         switch(timeout_status)
195         {
196                 case TIMEOUT_ACTIVE:
197                 {
198                         if(timeout_time > 0) // countdown is still going
199                         {
200                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_ENDING, timeout_time);
201
202                                 if(timeout_time == autocvar_sv_timeout_resumetime) // play a warning sound when only <sv_timeout_resumetime> seconds are left
203                                         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_PREPARE);
204
205                                 self.nextthink = time + TIMEOUT_SLOWMO_VALUE; // think again in one second
206                                 timeout_time -= 1; // decrease the time counter
207                         }
208                         else // time to end the timeout
209                         {
210                                 timeout_status = TIMEOUT_INACTIVE;
211
212                                 // reset the slowmo value back to normal
213                                 cvar_set("slowmo", ftos(orig_slowmo));
214
215                                 // unlock the view for players so they can move around again
216                                 FOR_EACH_REALPLAYER(tmp_player)
217                                         tmp_player.fixangle = false;
218
219                                 timeout_handler_reset();
220                         }
221
222                         return;
223                 }
224
225                 case TIMEOUT_LEADTIME:
226                 {
227                         if(timeout_leadtime > 0) // countdown is still going
228                         {
229                                 Send_Notification(NOTIF_ALL, world, MSG_CENTER, CENTER_TIMEOUT_BEGINNING, timeout_leadtime);
230
231                                 self.nextthink = time + 1; // think again in one second
232                                 timeout_leadtime -= 1; // decrease the time counter
233                         }
234                         else // time to begin the timeout
235                         {
236                                 timeout_status = TIMEOUT_ACTIVE;
237
238                                 // set the slowmo value to the timeout default slowmo value
239                                 cvar_set("slowmo", ftos(TIMEOUT_SLOWMO_VALUE));
240
241                                 // reset all the flood variables
242                                 FOR_EACH_CLIENT(tmp_player)
243                                         tmp_player.nickspamcount = tmp_player.nickspamtime = tmp_player.floodcontrol_chat =
244                                         tmp_player.floodcontrol_chatteam = tmp_player.floodcontrol_chattell =
245                                         tmp_player.floodcontrol_voice = tmp_player.floodcontrol_voiceteam = 0;
246
247                                 // copy .v_angle to .lastV_angle for every player in order to fix their view during pause (see PlayerPreThink)
248                                 FOR_EACH_REALPLAYER(tmp_player)
249                                         tmp_player.lastV_angle = tmp_player.v_angle;
250
251                                 self.nextthink = time; // think again next frame to handle it under TIMEOUT_ACTIVE code
252                         }
253
254                         return;
255                 }
256
257
258                 case TIMEOUT_INACTIVE:
259                 default:
260                 {
261                         timeout_handler_reset();
262                         return;
263                 }
264         }
265 }
266
267
268
269 // ===================================================
270 //  Common commands used in both sv_cmd.qc and cmd.qc
271 // ===================================================
272
273 void CommonCommand_cvar_changes(float request, entity caller)
274 {
275         switch(request)
276         {
277                 case CMD_REQUEST_COMMAND:
278                 {
279                         print_to(caller, cvar_changes);
280                         return; // never fall through to usage
281                 }
282
283                 default:
284                 case CMD_REQUEST_USAGE:
285                 {
286                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_changes"));
287                         print_to(caller, "  No arguments required.");
288                         print_to(caller, "See also: ^2cvar_purechanges^7");
289                         return;
290                 }
291         }
292 }
293
294 void CommonCommand_cvar_purechanges(float request, entity caller)
295 {
296         switch(request)
297         {
298                 case CMD_REQUEST_COMMAND:
299                 {
300                         print_to(caller, cvar_purechanges);
301                         return; // never fall through to usage
302                 }
303
304                 default:
305                 case CMD_REQUEST_USAGE:
306                 {
307                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " cvar_purechanges"));
308                         print_to(caller, "  No arguments required.");
309                         print_to(caller, "See also: ^2cvar_changes^7");
310                         return;
311                 }
312         }
313 }
314
315 void CommonCommand_info(float request, entity caller, float argc)
316 {
317         switch(request)
318         {
319                 case CMD_REQUEST_COMMAND:
320                 {
321                         string command = builtin_cvar_string(strcat("sv_info_", argv(1)));
322
323                         if(command)
324                                 wordwrap_sprint(command, 1000);
325                         else
326                                 print_to(caller, "ERROR: unsupported info command");
327
328                         return; // never fall through to usage
329                 }
330
331                 default:
332                 case CMD_REQUEST_USAGE:
333                 {
334                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " info request"));
335                         print_to(caller, "  Where 'request' is the suffixed string appended onto the request for cvar.");
336                         return;
337                 }
338         }
339 }
340
341 void CommonCommand_ladder(float request, entity caller)
342 {
343         switch(request)
344         {
345                 case CMD_REQUEST_COMMAND:
346                 {
347                         print_to(caller, ladder_reply);
348                         return; // never fall through to usage
349                 }
350
351                 default:
352                 case CMD_REQUEST_USAGE:
353                 {
354                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " ladder"));
355                         print_to(caller, "  No arguments required.");
356                         return;
357                 }
358         }
359 }
360
361 void CommonCommand_lsmaps(float request, entity caller)
362 {
363         switch(request)
364         {
365                 case CMD_REQUEST_COMMAND:
366                 {
367                         print_to(caller, lsmaps_reply);
368                         return; // never fall through to usage
369                 }
370
371                 default:
372                 case CMD_REQUEST_USAGE:
373                 {
374                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " lsmaps"));
375                         print_to(caller, "  No arguments required.");
376                         return;
377                 }
378         }
379 }
380
381 void CommonCommand_printmaplist(float request, entity caller)
382 {
383         switch(request)
384         {
385                 case CMD_REQUEST_COMMAND:
386                 {
387                         print_to(caller, maplist_reply);
388                         return; // never fall through to usage
389                 }
390
391                 default:
392                 case CMD_REQUEST_USAGE:
393                 {
394                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " printmaplist"));
395                         print_to(caller, "  No arguments required.");
396                         return;
397                 }
398         }
399 }
400
401 void CommonCommand_rankings(float request, entity caller)
402 {
403         switch(request)
404         {
405                 case CMD_REQUEST_COMMAND:
406                 {
407                         print_to(caller, rankings_reply);
408                         return; // never fall through to usage
409                 }
410
411                 default:
412                 case CMD_REQUEST_USAGE:
413                 {
414                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " rankings"));
415                         print_to(caller, "  No arguments required.");
416                         return;
417                 }
418         }
419 }
420
421 void CommonCommand_records(float request, entity caller)
422 {
423         switch(request)
424         {
425                 case CMD_REQUEST_COMMAND:
426                 {
427                         for(int i = 0; i < 10; ++i)
428                                 if(records_reply[i] != "")
429                                         print_to(caller, records_reply[i]);
430
431                         return; // never fall through to usage
432                 }
433
434                 default:
435                 case CMD_REQUEST_USAGE:
436                 {
437                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " records"));
438                         print_to(caller, "  No arguments required.");
439                         return;
440                 }
441         }
442 }
443
444 void CommonCommand_teamstatus(float request, entity caller)
445 {
446         switch(request)
447         {
448                 case CMD_REQUEST_COMMAND:
449                 {
450                         Score_NicePrint(caller);
451                         return; // never fall through to usage
452                 }
453
454                 default:
455                 case CMD_REQUEST_USAGE:
456                 {
457                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " teamstatus"));
458                         print_to(caller, "  No arguments required.");
459                         return;
460                 }
461         }
462 }
463
464 void CommonCommand_time(float request, entity caller)
465 {
466         switch(request)
467         {
468                 case CMD_REQUEST_COMMAND:
469                 {
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")));
477                         return;
478                 }
479
480                 default:
481                 case CMD_REQUEST_USAGE:
482                 {
483                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " time"));
484                         print_to(caller, "  No arguments required.");
485                         return;
486                 }
487         }
488 }
489
490 void CommonCommand_timein(float request, entity caller)
491 {
492         switch(request)
493         {
494                 case CMD_REQUEST_COMMAND:
495                 {
496                         if(!caller || autocvar_sv_timeout)
497                         {
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."); }
500
501                                 else // everything should be okay, continue aborting timeout
502                                 {
503                                         switch(timeout_status)
504                                         {
505                                                 case TIMEOUT_LEADTIME:
506                                                 {
507                                                         timeout_status = TIMEOUT_INACTIVE;
508                                                         timeout_time = 0;
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"));
511                                                         return;
512                                                 }
513
514                                                 case TIMEOUT_ACTIVE:
515                                                 {
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"));
519                                                         return;
520                                                 }
521
522                                                 default: dprint("timeout status was inactive, but this code was executed anyway?"); return;
523                                         }
524                                 }
525                         }
526                         else { print_to(caller, "^1Timeins are not allowed to be called, enable them with sv_timeout 1.\n"); }
527
528                         return; // never fall through to usage
529                 }
530
531                 default:
532                 case CMD_REQUEST_USAGE:
533                 {
534                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timein"));
535                         print_to(caller, "  No arguments required.");
536                         return;
537                 }
538         }
539 }
540
541 void CommonCommand_timeout(float request, entity caller) // DEAR GOD THIS COMMAND IS TERRIBLE.
542 {
543         switch(request)
544         {
545                 case CMD_REQUEST_COMMAND:
546                 {
547                         if(!caller || autocvar_sv_timeout)
548                         {
549                                 float last_possible_timeout = ((autocvar_timelimit * 60) - autocvar_sv_timeout_leadtime - 1);
550
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!"); }
558
559                                 else // everything should be okay, proceed with starting the timeout
560                                 {
561                                         if(caller) { caller.allowed_timeouts -= 1; }
562
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)
564
565                                         timeout_status = TIMEOUT_LEADTIME;
566                                         timeout_caller = caller;
567                                         timeout_time = autocvar_sv_timeout_length;
568                                         timeout_leadtime = autocvar_sv_timeout_leadtime;
569
570                                         timeout_handler = spawn();
571                                         timeout_handler.think = timeout_handler_think;
572                                         timeout_handler.nextthink = time; // always let the entity think asap
573
574                                         Send_Notification(NOTIF_ALL, world, MSG_ANNCE, ANNCE_TIMEOUT);
575                                 }
576                         }
577                         else { print_to(caller, "^1Timeouts are not allowed to be called, enable them with sv_timeout 1.\n"); }
578
579                         return; // never fall through to usage
580                 }
581
582                 default:
583                 case CMD_REQUEST_USAGE:
584                 {
585                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " timeout"));
586                         print_to(caller, "  No arguments required.");
587                         return;
588                 }
589         }
590 }
591
592 void CommonCommand_who(float request, entity caller, float argc)
593 {
594         switch(request)
595         {
596                 case CMD_REQUEST_COMMAND:
597                 {
598                         float total_listed_players, is_bot;
599                         entity tmp_player;
600
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;
604
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"));
608
609                         total_listed_players = 0;
610                         FOR_EACH_CLIENT(tmp_player)
611                         {
612                                 is_bot = (IS_BOT_CLIENT(tmp_player));
613
614                                 if(is_bot)
615                                 {
616                                         tmp_netaddress = "null/botclient";
617                                         tmp_crypto_idfp = "null/botclient";
618                                 }
619                                 else if(privacy)
620                                 {
621                                         tmp_netaddress = "hidden";
622                                         tmp_crypto_idfp = "hidden";
623                                 }
624                                 else
625                                 {
626                                         tmp_netaddress = tmp_player.netaddress;
627                                         tmp_crypto_idfp = tmp_player.crypto_idfp;
628                                 }
629
630                                 print_to(caller, sprintf(strreplace(" ", separator, " #%-3d %-20.20s %-5d %-3d %-9s %-16s %s "),
631                                         num_for_edict(tmp_player),
632                                         tmp_player.netname,
633                                         tmp_player.ping,
634                                         tmp_player.ping_packetloss,
635                                         process_time(1, time - tmp_player.jointime),
636                                         tmp_netaddress,
637                                         tmp_crypto_idfp));
638
639                                 ++total_listed_players;
640                         }
641
642                         print_to(caller, strcat("Finished listing ", ftos(total_listed_players), " client(s) out of ", ftos(maxclients), " slots."));
643
644                         return; // never fall through to usage
645                 }
646
647                 default:
648                 case CMD_REQUEST_USAGE:
649                 {
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.");
652                         return;
653                 }
654         }
655 }
656
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)
660 {
661         switch(request)
662         {
663                 case CMD_REQUEST_COMMAND:
664                 {
665
666                         return; // never fall through to usage
667                 }
668
669                 default:
670                 case CMD_REQUEST_USAGE:
671                 {
672                         print_to(caller, strcat("\nUsage:^3 ", GetCommandPrefix(caller), " "));
673                         print_to(caller, "  No arguments required.");
674                         return;
675                 }
676         }
677 }
678 */