]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
fix: misleading dprint, error message for missing map.txt, timelimit/fraglimit alignment
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 float ValidateMap(string vote);
3
4 void Say(entity source, float teamsay, string msgin)
5 {
6         string msgstr, colorstr;
7         entity head;
8
9         msgin = formatmessage(msgin);
10
11         if(msgin == "")
12                 return;
13
14         if(source.team == COLOR_TEAM1)
15                 colorstr = "^1";
16         else if(source.team == COLOR_TEAM2)
17                 colorstr = "^4";
18         else if(source.team == COLOR_TEAM3)
19                 colorstr = "^6";
20         else if(source.team == COLOR_TEAM4)
21                 colorstr = "^3";
22         else
23                 colorstr = "^7";
24
25         if(!teams_matter)
26                 teamsay = FALSE;
27
28         if(source.classname != "player") // observers can't
29                 teamsay = FALSE;
30
31         // how can we prevent the message from appearing in a listen server?
32         // for now, just give "say" back and only handle say_team
33         if(!teamsay)
34         {
35                 clientcommand(self, strcat("say ", msgin));
36                 return;
37         }
38
39         if(teamsay)
40                 msgstr = strcat(colorstr, "(^3", source.netname, colorstr, ") ^7", msgin, "\n");
41         else
42                 msgstr = strcat("^3", source.netname, "^7: ", msgin, "\n");
43
44         head = find(world, classname, "player");
45         while(head)
46         {
47                 if(clienttype(head) == CLIENTTYPE_REAL)
48                         if(!teamsay || (head.team == source.team))
49                         {
50                                 sprint(head, msgstr);
51                                 stuffcmd(head, "play2 misc/talk.wav\n");
52                         }
53                 head = find(head, classname, "player");
54         }
55
56         if(!teamsay)
57         {
58                 head = find(world, classname, "observer");
59                 while(head)
60                 {
61                         if(clienttype(head) == CLIENTTYPE_REAL)
62                         {
63                                 sprint(head, msgstr);
64                                 stuffcmd(head, "play2 misc/talk.wav\n");
65                         }
66                         head = find(head, classname, "observer");
67                 }
68                 head = find(world, classname, "spectator");
69                 while(head)
70                 {
71                         if(clienttype(head) == CLIENTTYPE_REAL)
72                         {
73                                 sprint(head, msgstr);
74                                 stuffcmd(head, "play2 misc/talk.wav\n");
75                         }
76                         head = find(head, classname, "spectator");
77                 }
78                 ServerConsoleEcho(substring(msgstr, 0, strlen(msgstr) - 1), TRUE);
79         }
80 }
81
82 void SV_ParseClientCommand(string s) {
83         local float index;
84
85         tokenize(s);
86
87         if(argv(0) == "clogin") {
88                 if(cvar("sv_clientcommands")) {
89                         if(self.adminstatus < -5) {
90                                 sprint(self, "Too many unsuccessful tries.\n");
91                         } else if(argv(1) == cvar_string("sv_clientcommands_password")) {
92                                 self.adminstatus = 1;
93                                 sprint(self, "You now have remote admin status.\n");
94                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " received admin status"), TRUE);
95                         } else {
96                                 sprint(self, "Wrong password.\n");
97                                 // use of -- produces compiler warning in the if() line???
98                                 self.adminstatus = self.adminstatus - 1;
99                                 if(self.adminstatus == 0)
100                                 {
101                                         sprint(self, "You lost remote admin status.\n");
102                                         ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " lost admin status"), TRUE);
103                                 }
104                         }
105                 } else {
106                         sprint(self, "Clientside commands NOT allowed.\n");
107                 }
108         } else if(argv(0) == "ccmd") {
109                 if(cvar("sv_clientcommands")) {
110                         if(self.adminstatus > 0) {
111                                 local string command;
112                                 command = argv(1);
113                                 index = 2;
114                                 while(argv(index) != "") {
115                                         command = strcat(command, " ", argv(index));
116                                         index++;
117                                 }
118                                 command = strzone(command);
119                                 ServerConsoleEcho(strcat("ClientCommands: ", self.netname, " issued command '", command, "'"), TRUE);
120                                 localcmd(strcat(command, "\n"));
121                                 strunzone(command);
122                         } else
123                                 sprint(self, "You don't have remote admin status.\n");
124                 } else {
125                         sprint(self, "Clientside commands NOT allowed.\n");
126                 }
127         } else if(argv(0) == "vote") {
128                 if(argv(1) == "help") {
129                         local string vmasterdis;
130                         if(!cvar("sv_vote_master")) {
131                                 vmasterdis = " ^1(disabled)";
132                         }
133                         local string vcalldis;
134                         if(!cvar("sv_vote_call")) {
135                                 vcalldis = " ^1(disabled)";
136                         }
137                         sprint(self, "^7You can use voting with \"^2cmd vote help^7\" \"^2cmd vote status^7\" \"^2cmd vote call ^3COMMAND ARGUMENTS^7\" \"^2cmd vote stop^7\" \"^2cmd vote master^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\".\n");
138                         sprint(self, "^7Or if your version is up to date you can use these aliases \"^2vhelp^7\" \"^2vstatus^7\" \"^2vcall ^3COMMAND ARGUMENTS^7\" \"^2vstop^7\" \"^2vmaster^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\".\n");
139                         sprint(self, "^7\"^2help^7\" shows this info.\n");
140                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
141                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
142                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
143                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
144                         sprint(self, "^7\"^2do^7\" If you are a master you can execute a command without a vote. See the list of allowed commands.\n");
145                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
146                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
147                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
148                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
149                         sprint(self, "^7You can call a vote for or execute these commands:\n");
150                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
151                 } else if(argv(1) == "status") {
152                         if(votecalled) {
153                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
154                         } else {
155                                 sprint(self, "^1No vote called.\n");
156                         }
157                 } else if(argv(1) == "call") {
158                         if(cvar("sv_vote_call")) {
159                                 if(votecalled) {
160                                         sprint(self, "^1There is already a vote called.\n");
161                                 } else {
162                                         local string vote;
163                                         vote = VoteParse();
164                                         if(vote == "") {
165                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
166                                         } else if(time < self.vote_next) {
167                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
168                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
169                                                 if(!ValidateMap(vote))
170                                                         return;
171                                                 votecalled = TRUE;
172                                                 votecalledmaster = FALSE;
173                                                 // remap chmap to gomap (forces intermission)
174                                                 if(strlen(vote) >= 6)
175                                                         if(substring(vote, 0, 6) == "chmap ")
176                                                                 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
177                                                 votecalledvote = strzone(vote);
178                                                 votecaller = self; // remember who called the vote
179                                                 votefinished = time + cvar("sv_vote_timeout");
180                                                 votecaller.vote_vote = 1; // of course you vote yes
181                                                 votecaller.vote_next = time + cvar("sv_vote_wait");
182                                                 bprint(strcat("^3Vote for \"^1", votecalledvote, "^3\" called by \"^7", votecaller.netname, "^3\".\n"));
183                                                 VoteCount(); // needed if you are the only one
184                                         } else {
185                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
186                                         }
187                                 }
188                         } else {
189                                 sprint(self, "^1Vote calling is NOT allowed.\n");
190                         }
191                 } else if(argv(1) == "stop") {
192                         if(!votecalled) {
193                                 sprint(self, "^1No vote called.\n");
194                         } else if(self == votecaller
195                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
196                                 VoteStop(self);
197                         } else {
198                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
199                         }
200                 } else if(argv(1) == "master") {
201                         if(cvar("sv_vote_master")) {
202                                 if(votecalled) {
203                                         sprint(self, "^1There is already a vote called.\n");
204                                 } else {
205                                         votecalled = TRUE;
206                                         votecalledmaster = TRUE;
207                                         votecalledvote = strzone("^3master");
208                                         votecaller = self; // remember who called the vote
209                                         votefinished = time + cvar("sv_vote_timeout");
210                                         votecaller.vote_vote = 1; // of course you vote yes
211                                         votecaller.vote_next = time + cvar("sv_vote_wait");
212                                         bprint(strcat("\"^3", votecaller.netname, "^3\" called a vote to become ^3master^3.\n"));
213                                         VoteCount(); // needed if you are the only one
214                                 }
215                         } else {
216                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
217                         }
218                 } else if(argv(1) == "do") {
219                         if(self.vote_master) {
220                                 local string dovote;
221                                 dovote = VoteParse();
222                                 if(dovote == "") {
223                                         sprint(self, "^1Your command was empty. See help for more info.\n");
224                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
225                                         bprint("\"^7", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
226                                         localcmd(strcat(dovote, "\n"));
227                                 } else {
228                                         sprint(self, "^1This command is not ok. See help for more info.\n");
229                                 }
230                         } else {
231                                 sprint(self, "^1You are NOT a master.\n");
232                         }
233                 } else if(argv(1) == "yes") {
234                         if(!votecalled) {
235                                 sprint(self, "^1No vote called.\n");
236                         } else if(self.vote_vote == 0
237                                   || cvar("sv_vote_change")) {
238                                 sprint(self, "^1You accepted the vote.\n");
239                                 self.vote_vote = 1;
240                                 if(!cvar("sv_vote_singlecount")) {
241                                         VoteCount();
242                                 }
243                         } else {
244                                 sprint(self, "^1You have already voted.\n");
245                         }
246                 } else if(argv(1) == "no") {
247                         if(!votecalled) {
248                                 sprint(self, "^1No vote called.\n");
249                         } else if(self.vote_vote == 0
250                                   || cvar("sv_vote_change")) {
251                                 sprint(self, "^1You rejected the vote.\n");
252                                 self.vote_vote = -1;
253                                 if(!cvar("sv_vote_singlecount")) {
254                                         VoteCount();
255                                 }
256                         } else {
257                                 sprint(self, "^1You have already voted.\n");
258                         }
259                 } else {
260                         // ignore this?
261                         sprint(self, "^1Unknown vote command.\n");
262                 }
263         } else if(argv(0) == "autoswitch") {
264                 // be backwards compatible with older clients (enabled)
265                 self.autoswitch = ("0" != argv(1));
266                 local string autoswitchmsg;
267                 if (self.autoswitch) {
268                         autoswitchmsg = "on";
269                 } else {
270                         autoswitchmsg = "off";
271                 }
272                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
273         } else if(argv(0) == "clientversion") {
274                 if (argv(1) == "$gameversion") {
275                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
276                         // either that or someone wants to be funny
277                         self.version = 1;
278                 } else {
279                         self.version = stof(argv(1));
280                 }
281                 if(self.version != cvar("gameversion")) 
282                 {
283                         self.classname = "observer";
284                         self.frags = -2;
285                         PutClientInServer();
286                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
287                         //JoinBestTeam(self, 0);
288                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
289                         self.classname = "observer";
290                         stuffcmd(self,"menu_showteamselect\n");
291                 }
292         } else if(argv(0) == "reportcvar") {
293                 GetCvars(1);
294         } else if(argv(0) == "spectate") {
295                 if(cvar("g_lms") || cvar("g_arena"))
296                         return; // don't allow spectating in lms, unless player runs out of lives
297                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
298                         self.classname = "observer";
299                         PutClientInServer();
300                 }
301         } else if(argv(0) == "join") {
302                 if(!cvar("g_arena"))
303                 if (self.classname != "player")
304                 {
305                         self.classname = "player";
306                         self.frags = 0;
307                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
308                         PutClientInServer();
309                 }
310         } else if( argv(0) == "selectteam" ) {
311                 if( !cvar("teamplay") ) {
312                         sprint( self, "selecteam can only be used in teamgames\n");
313                 } else if(cvar("g_campaign")) {
314                         //JoinBestTeam(self, 0);
315                 } else if( argv(1) == "none" ) {
316                         SV_ChangeTeam( 0 );
317                 } else if( argv(1) == "red" ) {
318                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
319                 } else if( argv(1) == "blue" ) {
320                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
321                 } else if( argv(1) == "pink" ) {
322                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
323                 } else if( argv(1) == "yellow" ) {
324                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
325                 } else if( argv(1) == "auto" ) {
326                         self.team = -1;
327                         JoinBestTeam( self, 0 );
328                 } else {
329                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
330                 }
331         } else if(argv(0) == "ready") {
332                 if(cvar("sv_ready_restart"))
333                 {
334                         self.ready = TRUE;
335                         bprint(self.netname, "^2 is ready\n");
336                         ReadyCount();
337                 }
338         } else if(argv(0) == "say") {
339                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
340                 //clientcommand(self, formatmessage(s));
341         } else if(argv(0) == "say_team") {
342                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
343                 //clientcommand(self, formatmessage(s));
344         } else {
345                 clientcommand(self,s);
346         }
347 }
348
349 float ValidateMap(string vote)
350 {
351         string ext;
352         
353         tokenize(vote);
354         if(argv(0) == "map" || argv(0) == "changelevel")
355                 ext = ".bsp";
356         else if(argv(0) == "chmap")
357                 ext = ".mapcfg";
358         else if(argv(0) == "gotomap")
359                 ext = ".mapcfg";
360         else
361                 return TRUE;
362
363         if(!TryFile(strcat("maps/", argv(1), ext)))
364         {
365                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
366                 return FALSE;
367         }
368         return TRUE;
369 }
370
371
372 void VoteThink() {
373         if(votefinished > 0 // a vote was called
374             && time > votefinished) // time is up
375         {
376                 VoteCount();
377         }
378 }
379
380 string VoteParse() {
381         local float index;
382         index = 3;
383         local string vote;
384         vote = argv(2);
385         while(argv(index) != "") {
386                 vote = strcat(vote, " ", argv(index));
387                 index++;
388         }
389
390         // necessary for some of the string operations
391         vote = strzone(vote);
392
393         // now we remove some things that could be misused
394         index = 0;
395         local float found;
396         found = FALSE;
397         local float votelength;
398         votelength = strlen(vote);
399         while(!found && index < votelength)
400         {
401                 local string badchar;
402                 badchar = substring(vote, index, 1);
403                 if(badchar == ";"
404                    || badchar == "\r"
405                    || badchar == "\n")
406                 {
407                         found = TRUE;
408                 } else {
409                         index++;
410                 }
411         }
412         return substring(vote, 0, index);
413 }
414
415 float VoteAllowed(string votecommand) {
416         tokenize(cvar_string("sv_vote_commands"));
417         local float index;
418         index = 0;
419         while(argv(index) != "") {
420                 local string allowed;
421                 allowed = argv(index);
422                 if(votecommand == allowed) {
423                         return TRUE;
424                 }
425                 index++;
426         }
427         return FALSE;
428 }
429
430 void VoteReset() {
431         local string searchclass;
432         searchclass = "player";
433
434         while (TRUE)
435         {
436                 local entity player;
437                 player = find(player, classname, searchclass);
438                 while(player)
439                 {
440                         player.vote_vote = 0;
441                         player = find(player, classname, searchclass);
442                 }
443
444                 if("player" == searchclass) {
445                         searchclass = "observer";
446                 } else if("observer" == searchclass) {
447                         searchclass = "spectator";
448                 } else {
449                         break;
450                 }
451         }
452
453         votecalled = FALSE;
454         votecalledmaster = FALSE;
455         votefinished = 0;
456 }
457
458 void VoteAccept() {
459         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
460         if(votecalledmaster)
461         {
462                 votecaller.vote_master = 1;
463         } else {
464                 localcmd(strcat(votecalledvote, "\n"));
465         }
466         votecaller.vote_next = 0; // people like your votes, no wait for next vote
467         VoteReset();
468 }
469
470 void VoteReject() {
471         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
472         VoteReset();
473 }
474
475 void VoteTimeout() {
476         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
477         VoteReset();
478 }
479
480 void VoteStop(entity stopper) {
481         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
482         if(stopper == votecaller) {
483                 // no wait for next vote so you can correct your vote
484                 votecaller.vote_next = 0;
485         }
486         VoteReset();
487 }
488
489 void VoteCount() {
490         local float playercount;
491         playercount = 0;
492         local float yescount;
493         yescount = 0;
494         local float nocount;
495         nocount = 0;
496         local string searchclass;
497         searchclass = "player";
498
499         while (TRUE)
500         {
501                 local entity player;
502                 player = find(player, classname, searchclass);
503
504                 while(player)
505                 {
506                         if(clienttype(player) != CLIENTTYPE_BOT) {
507                                 if(player.vote_vote < 0) {
508                                         nocount++;
509                                 } else if(player.vote_vote > 0) {
510                                         yescount++;
511                                 }
512                                 playercount++;
513                         }
514                         player = find(player, classname, searchclass);
515                 }
516
517                 if("player" == searchclass) {
518                         searchclass = "observer";
519                 } else if("observer" == searchclass) {
520                         searchclass = "specator";
521                 } else {
522                         break;
523                 }
524         }
525
526         if((playercount == 1) && votecalledmaster) {
527                 // if only one player is on the server becoming vote
528                 // master is not allowed.  This could be used for
529                 // trolling or worse. 'self' is the user who has
530                 // called the vote because this function is called
531                 // by SV_ParseClientCommand. Maybe all voting should
532                 // be disabled for a single player?
533                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
534                 votecaller.vote_next = 0;
535                 VoteReset();
536         } else if((playercount / 2) < yescount) { // vote accepted
537                 VoteAccept();
538         } else if((playercount / 2) < nocount) { // vote rejected
539                 VoteReject();
540         } else if(time > votefinished) { // vote timedout
541                 VoteTimeout();
542         } // else still running
543 }
544
545
546 void ReadyCount()
547 {
548         local entity e;
549         local float r, p;
550
551         e = find(world, classname, "player");
552
553         while(e)
554         {
555                 if(clienttype(e) == CLIENTTYPE_REAL)
556                 {
557                         p += 1;
558                         if(e.ready) r += 1;
559                 }
560                 e = find(e, classname, "player");
561         }
562
563         if(p && r == p)
564         {
565                 bprint("^1Server is restarting...\n");
566                 localcmd("restart\n");
567         }
568 }