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