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