]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/command/common.qc
Now sv_cmd.qc uses common commands too -- plus some extra fixes.
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / command / common.qc
1 // ====================================================
2 //  Shared code for server commands, written by Samual
3 //  Last updated: December 13th, 2011
4 // ====================================================
5
6 // find a player which matches the input string, and return their entity number
7 float GetFilteredNumber(string input)
8 {
9         entity tmp_player, selection;
10         float output, matches;
11         
12         // check and see if we can get a number from input like "#3" or "3" 
13         if(substring(input, 0, 1) == "#")
14                 output = stof(substring(input, 1, -1));
15         else
16                 output = stof(input);
17                 
18         // if we can't, check and see if we can match the input to the netname of any player in the game
19         if not(output) 
20         {
21                 FOR_EACH_CLIENT(tmp_player)
22                         if (strdecolorize(tmp_player.netname) == strdecolorize(input))
23                                 selection = tmp_player;
24
25                 if (selection) { output = num_for_edict(selection); }
26         }
27                 
28         print(strcat("input: ", input, ", output: ", ftos(output), ",\n"));
29         return output;
30 }
31
32 // switch between sprint and print depending on whether the reciever is the server or a player
33 void print_to(entity to, string input)
34 {
35     if(to)
36         sprint(to, strcat(input, "\n"));
37     else
38         print(input, "\n");
39 }
40
41
42 // ===================================================
43 //  Common commands used in both sv_cmd.qc and cmd.qc
44 // ===================================================
45
46 void CommonCommand_cvar_changes(float request)
47 {
48         switch(request)
49         {
50                 case CMD_REQUEST_COMMAND:
51                 {
52                         print_to(self, cvar_changes);
53                         return; // never fall through to usage
54                 }
55                         
56                 default:
57                 case CMD_REQUEST_USAGE:
58                 {
59                         print_to(self, "\nUsage:^3 sv_cmd cvar_changes\n");
60                         print_to(self, "  No arguments required.\n");
61                         //print_to(self, "See also: ^2cvar_purechanges^7\n");
62                         return;
63                 }
64         }
65 }
66
67 void CommonCommand_cvar_purechanges(float request)
68 {
69         switch(request)
70         {
71                 case CMD_REQUEST_COMMAND:
72                 {
73                         print_to(self, cvar_purechanges);
74                         return; // never fall through to usage
75                 }
76                         
77                 default:
78                 case CMD_REQUEST_USAGE:
79                 {
80                         print_to(self, "\nUsage:^3 sv_cmd cvar_purechanges\n");
81                         print_to(self, "  No arguments required.\n");
82                         //print_to(self, "See also: ^2cvar_changes^7\n");
83                         return;
84                 }
85         }
86 }
87
88 void CommonCommand_info(float request, float argc)
89 {       
90         switch(request)
91         {
92                 case CMD_REQUEST_COMMAND:
93                 {
94                         string command;
95                         
96                         command = builtin_cvar_string(strcat("sv_info_", argv(1))); 
97                         if(command)
98                                 wordwrap_sprint(command, 1111); // why 1111?
99                         else
100                                 print_to(self, "ERROR: unsupported info command\n");
101                                 
102                         return; // never fall through to usage
103                 }
104                         
105                 default:
106                 case CMD_REQUEST_USAGE:
107                 {
108                         print_to(self, "\nUsage:^3 cmd info request\n");
109                         print_to(self, "  Where 'request' is the suffixed string appended onto the request for cvar.\n");
110                         return;
111                 }
112         }
113 }
114
115 void CommonCommand_ladder(float request)
116 {
117         switch(request)
118         {
119                 case CMD_REQUEST_COMMAND:
120                 {
121                         print_to(self, ladder_reply);
122                         return; // never fall through to usage
123                 }
124                         
125                 default:
126                 case CMD_REQUEST_USAGE:
127                 {
128                         print_to(self, "\nUsage:^3 cmd ladder\n");
129                         print_to(self, "  No arguments required.\n");
130                         return;
131                 }
132         }
133 }
134
135 void CommonCommand_lsmaps(float request)
136 {
137         switch(request)
138         {
139                 case CMD_REQUEST_COMMAND:
140                 {
141                         print_to(self, lsmaps_reply);
142                         return; // never fall through to usage
143                 }
144                         
145                 default:
146                 case CMD_REQUEST_USAGE:
147                 {
148                         print_to(self, "\nUsage:^3 cmd lsmaps\n");
149                         print_to(self, "  No arguments required.\n");
150                         return;
151                 }
152         }
153 }
154
155 void CommonCommand_lsnewmaps(float request)
156 {
157         switch(request)
158         {
159                 case CMD_REQUEST_COMMAND:
160                 {
161                         print_to(self, lsnewmaps_reply);
162                         return; // never fall through to usage
163                 }
164                         
165                 default:
166                 case CMD_REQUEST_USAGE:
167                 {
168                         print_to(self, "\nUsage:^3 cmd lsnewmaps\n");
169                         print_to(self, "  No arguments required.\n");
170                         return;
171                 }
172         }
173 }
174
175 void CommonCommand_maplist(float request)
176 {
177         switch(request)
178         {
179                 case CMD_REQUEST_COMMAND:
180                 {
181                         print_to(self, maplist_reply);
182                         return; // never fall through to usage
183                 }
184                         
185                 default:
186                 case CMD_REQUEST_USAGE:
187                 {
188                         print_to(self, "\nUsage:^3 cmd maplist\n");
189                         print_to(self, "  No arguments required.\n");
190                         return;
191                 }
192         }
193 }
194
195 void GameCommand_rankings(float request) // this is OLD.... jeez.
196 {
197         switch(request)
198         {
199                 case CMD_REQUEST_COMMAND:
200                 {
201                         strunzone(rankings_reply);
202                         rankings_reply = strzone(getrankings());
203                         print(rankings_reply);
204                         return;
205                 }
206                         
207                 default:
208                 case CMD_REQUEST_USAGE:
209                 {
210                         print("\nUsage:^3 sv_cmd rankings\n");
211                         print("  No arguments required.\n");
212                         return;
213                 }
214         }
215 }
216
217 void CommonCommand_rankings(float request)
218 {
219         switch(request)
220         {
221                 case CMD_REQUEST_COMMAND:
222                 {
223                         print_to(self, rankings_reply);
224                         return; // never fall through to usage
225                 }
226                         
227                 default:
228                 case CMD_REQUEST_USAGE:
229                 {
230                         print_to(self, "\nUsage:^3 cmd rankings\n");
231                         print_to(self, "  No arguments required.\n");
232                         return;
233                 }
234         }
235 }
236
237 void CommonCommand_records(float request) // TODO: Isn't this flooding with the sprint messages? Old code, but perhaps bad?
238 {       
239         switch(request)
240         {
241                 case CMD_REQUEST_COMMAND:
242                 {
243                         float i;
244                         
245                         for(i = 0; i < 10; ++i)
246                                 print_to(self, records_reply[i]);
247                                 
248                         return; // never fall through to usage
249                 }
250                         
251                 default:
252                 case CMD_REQUEST_USAGE:
253                 {
254                         print_to(self, "\nUsage:^3 cmd records\n");
255                         print_to(self, "  No arguments required.\n");
256                         return;
257                 }
258         }
259 }
260
261 void CommonCommand_teamstatus(float request)
262 {
263         switch(request)
264         {
265                 case CMD_REQUEST_COMMAND:
266                 {
267                         Score_NicePrint(self);
268                         return; // never fall through to usage
269                 }
270                         
271                 default:
272                 case CMD_REQUEST_USAGE:
273                 {
274                         print_to(self, "\nUsage:^3 cmd teamstatus\n");
275                         print_to(self, "  No arguments required.\n");
276                         return;
277                 }
278         }
279 }
280
281
282 void CommonCommand_timein(float request)
283 {
284         switch(request)
285         {
286                 case CMD_REQUEST_COMMAND:
287                 {
288                         if(self.flags & FL_CLIENT)
289                         {
290                                 if(autocvar_sv_timeout)
291                                 {
292                                         if (!timeoutStatus)
293                                                 return print_to(self, "^7Error: There is no active timeout which could be aborted!\n");
294                                         if (self != timeoutInitiator)
295                                                 return print_to(self, "^7Error: You may not abort the active timeout. Only the player who called it can do that!\n");
296                                                 
297                                         if (timeoutStatus == 1) 
298                                         {
299                                                 remainingTimeoutTime = timeoutStatus = 0;
300                                                 timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
301                                                 bprint(strcat("^7The timeout was aborted by ", self.netname, " !\n"));
302                                         }
303                                         else if (timeoutStatus == 2) 
304                                         {
305                                                 //only shorten the remainingTimeoutTime if it makes sense
306                                                 if( remainingTimeoutTime > (autocvar_sv_timeout_resumetime + 1) ) 
307                                                 {
308                                                         bprint(strcat("^1Attention: ^7", self.netname, " resumed the game! Prepare for battle!\n"));
309                                                         remainingTimeoutTime = autocvar_sv_timeout_resumetime;
310                                                         timeoutHandler.nextthink = time; //timeoutHandler has to take care of it immediately
311                                                 }
312                                                 else
313                                                         print_to(self, "^7Error: Your resumegame call was discarded!\n");
314                                         }
315                                 }
316                         }
317                         return; // never fall through to usage
318                 }
319                         
320                 default:
321                 case CMD_REQUEST_USAGE:
322                 {
323                         print_to(self, "\nUsage:^3 cmd timein\n");
324                         print_to(self, "  No arguments required.\n");
325                         return;
326                 }
327         }
328 }
329
330 void CommonCommand_timeout(float request) // DEAR GOD THIS COMMAND IS TERRIBLE.
331 {
332         switch(request)
333         {
334                 case CMD_REQUEST_COMMAND:
335                 {
336                         if(self.flags & FL_CLIENT)
337                         {
338                                 if(autocvar_sv_timeout) 
339                                 {
340                                         if(self.classname == "player") 
341                                         {
342                                                 if(votecalled)
343                                                         print_to(self, "^7Error: you can not call a timeout while a vote is active!\n");
344                                                 else
345                                                 {
346                                                         if (inWarmupStage && !g_warmup_allow_timeout)
347                                                                 return print_to(self, "^7Error: You can not call a timeout in warmup-stage!\n");
348                                                         if (time < game_starttime )
349                                                                 return print_to(self, "^7Error: You can not call a timeout while the map is being restarted!\n");
350                                                                 
351                                                         if (timeoutStatus != 2) {
352                                                                 //if the map uses a timelimit make sure that timeout cannot be called right before the map ends
353                                                                 if (autocvar_timelimit) {
354                                                                         //a timelimit was used
355                                                                         float myTl;
356                                                                         myTl = autocvar_timelimit;
357
358                                                                         float lastPossibleTimeout;
359                                                                         lastPossibleTimeout = (myTl*60) - autocvar_sv_timeout_leadtime - 1;
360
361                                                                         if (lastPossibleTimeout < time - game_starttime)
362                                                                                 return print_to(self, "^7Error: It is too late to call a timeout now!\n");
363                                                                 }
364                                                         }
365                                                         
366                                                         //player may not call a timeout if he has no calls left
367                                                         if (self.allowedTimeouts < 1)
368                                                                 return print_to(self, "^7Error: You already used all your timeout calls for this map!\n");
369                                                                 
370                                                                 
371                                                         //now all required checks are passed
372                                                         self.allowedTimeouts -= 1;
373                                                         bprint(self.netname, " ^7called a timeout (", ftos(self.allowedTimeouts), " timeouts left)!\n"); //write a bprint who started the timeout (and how many he has left)
374                                                         remainingTimeoutTime = autocvar_sv_timeout_length;
375                                                         remainingLeadTime = autocvar_sv_timeout_leadtime;
376                                                         timeoutInitiator = self;
377                                                         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
378                                                                 timeoutStatus = 1;
379                                                                 //create the timeout indicator which centerprints the information to all players and takes care of pausing/unpausing
380                                                                 timeoutHandler = spawn();
381                                                                 timeoutHandler.think = timeoutHandler_Think;
382                                                         }
383                                                         timeoutHandler.nextthink = time; //always let the entity think asap
384
385                                                         //inform all connected clients about the timeout call
386                                                         Announce("timeoutcalled");
387                                                 }
388                                         }
389                                         else
390                                                 print_to(self, "^7Error: only players can call a timeout!\n");
391                                 }
392                         }
393                         return; // never fall through to usage
394                 }
395                         
396                 default:
397                 case CMD_REQUEST_USAGE:
398                 {
399                         print_to(self, "\nUsage:^3 cmd timeout\n");
400                         print_to(self, "  No arguments required.\n");
401                         return;
402                 }
403         }
404 }
405
406 void CommonCommand_who(float request)
407 {
408         switch(request)
409         {
410                 case CMD_REQUEST_COMMAND:
411                 {
412                         float total_listed_players, tmp_hours, tmp_minutes, tmp_seconds;
413                         entity tmp_player;
414                         //string tmp_player_name;
415                         
416                         print_to(self, strcat("List of client information", (autocvar_sv_status_privacy ? " (some data is hidden for privacy)" : string_null), ":\n"));
417                         print_to(self, sprintf(" %-4s %-20s %-5s %-3s %-9s %-16s %s\n", "ent", "nickname", "ping", "pl", "time", "ip", "crypto_id"));
418                         
419                         FOR_EACH_CLIENT(tmp_player)
420                         {
421                                 //tmp_player_name = strlimitedlen(tmp_player.netname, "...", TRUE, 20);
422                                 
423                                 tmp_hours = tmp_minutes = tmp_seconds = 0;
424                                 
425                                 tmp_seconds = (time - tmp_player.jointime);
426                                 tmp_minutes = (tmp_seconds / 60);
427                                 
428                                 if(tmp_minutes)
429                                 {
430                                         tmp_seconds -= (tmp_minutes * 60);
431                                         tmp_hours = (tmp_minutes / 60);
432                                         
433                                         if(tmp_hours) { tmp_minutes -= (tmp_hours * 60); }
434                                 }
435                                 
436                                 print_to(self, sprintf(" %-4s %-20s %-5d %-3d %-9s %-16s %s\n", 
437                                         strcat("#", ftos(num_for_edict(tmp_player))), 
438                                         tmp_player.netname, //strcat(tmp_player_name, sprintf("%*s", (20 - strlen(strdecolorize(tmp_player_name))), "")),
439                                         tmp_player.ping, tmp_player.ping_packetloss, 
440                                         sprintf("%02d:%02d:%02d", tmp_hours, tmp_minutes, tmp_seconds),
441                                         (autocvar_sv_status_privacy ? "hidden" : tmp_player.netaddress),
442                                         (autocvar_sv_status_privacy ? "hidden" : tmp_player.crypto_idfp)));
443                                         
444                                 ++total_listed_players;
445                         }
446                         
447                         print_to(self, strcat("Finished listing ", ftos(total_listed_players), " client(s). \n"));
448                         
449                         return; // never fall through to usage
450                 }
451                         
452                 default:
453                 case CMD_REQUEST_USAGE:
454                 {
455                         print_to(self, "\nUsage:^3 cmd who\n");
456                         print_to(self, "  No arguments required.\n");
457                         return;
458                 }
459         }
460 }
461
462 /* use this when creating a new command, making sure to place it in alphabetical order.
463 void CommonCommand_(float request)
464 {
465         switch(request)
466         {
467                 case CMD_REQUEST_COMMAND:
468                 {
469                         
470                         return; // never fall through to usage
471                 }
472                         
473                 default:
474                 case CMD_REQUEST_USAGE:
475                 {
476                         print_to(self, "\nUsage:^3 cmd \n");
477                         print_to(self, "  No arguments required.\n");
478                         return;
479                 }
480         }
481 }
482 */