]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
say_team to centerprint
[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, 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                         self.classname = "observer";
306                         PutClientInServer();
307                 }
308         } else if(argv(0) == "join") {
309                 if(!cvar("g_arena"))
310                 if (self.classname != "player")
311                 {
312                         self.classname = "player";
313                         self.frags = 0;
314                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
315                         PutClientInServer();
316                 }
317         } else if( argv(0) == "selectteam" ) {
318                 if( !cvar("teamplay") ) {
319                         sprint( self, "selecteam can only be used in teamgames\n");
320                 } else if(cvar("g_campaign")) {
321                         //JoinBestTeam(self, 0);
322                 } else if( argv(1) == "none" ) {
323                         SV_ChangeTeam( 0 );
324                 } else if( argv(1) == "red" ) {
325                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
326                 } else if( argv(1) == "blue" ) {
327                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
328                 } else if( argv(1) == "pink" ) {
329                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
330                 } else if( argv(1) == "yellow" ) {
331                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
332                 } else if( argv(1) == "auto" ) {
333                         self.team = -1;
334                         JoinBestTeam( self, 0 );
335                 } else {
336                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
337                 }
338         } else if(argv(0) == "ready") {
339                 if(cvar("sv_ready_restart"))
340                 {
341                         self.ready = TRUE;
342                         bprint(self.netname, "^2 is ready\n");
343                         ReadyCount();
344                 }
345         } else if(argv(0) == "say") {
346                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
347                 //clientcommand(self, formatmessage(s));
348         } else if(argv(0) == "say_team") {
349                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
350                 //clientcommand(self, formatmessage(s));
351         } else {
352                 clientcommand(self,s);
353         }
354 }
355
356 float ValidateMap(string vote)
357 {
358         string ext;
359         
360         tokenize(vote);
361         if(argv(0) == "map" || argv(0) == "changelevel")
362                 ext = ".bsp";
363         else if(argv(0) == "chmap")
364                 ext = ".mapcfg";
365         else if(argv(0) == "gotomap")
366                 ext = ".mapcfg";
367         else
368                 return TRUE;
369
370         if(!TryFile(strcat("maps/", argv(1), ext)))
371         {
372                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
373                 return FALSE;
374         }
375         return TRUE;
376 }
377
378
379 void VoteThink() {
380         if(votefinished > 0 // a vote was called
381             && time > votefinished) // time is up
382         {
383                 VoteCount();
384         }
385 }
386
387 string VoteParse() {
388         local float index;
389         index = 3;
390         local string vote;
391         vote = argv(2);
392         while(argv(index) != "") {
393                 vote = strcat(vote, " ", argv(index));
394                 index++;
395         }
396
397         // necessary for some of the string operations
398         vote = strzone(vote);
399
400         // now we remove some things that could be misused
401         index = 0;
402         local float found;
403         found = FALSE;
404         local float votelength;
405         votelength = strlen(vote);
406         while(!found && index < votelength)
407         {
408                 local string badchar;
409                 badchar = substring(vote, index, 1);
410                 if(badchar == ";"
411                    || badchar == "\r"
412                    || badchar == "\n")
413                 {
414                         found = TRUE;
415                 } else {
416                         index++;
417                 }
418         }
419         return substring(vote, 0, index);
420 }
421
422 float VoteAllowed(string votecommand) {
423         tokenize(cvar_string("sv_vote_commands"));
424         local float index;
425         index = 0;
426         while(argv(index) != "") {
427                 local string allowed;
428                 allowed = argv(index);
429                 if(votecommand == allowed) {
430                         return TRUE;
431                 }
432                 index++;
433         }
434         return FALSE;
435 }
436
437 void VoteReset() {
438         local string searchclass;
439         searchclass = "player";
440
441         while (TRUE)
442         {
443                 local entity player;
444                 player = find(player, classname, searchclass);
445                 while(player)
446                 {
447                         player.vote_vote = 0;
448                         player = find(player, classname, searchclass);
449                 }
450
451                 if("player" == searchclass) {
452                         searchclass = "observer";
453                 } else if("observer" == searchclass) {
454                         searchclass = "spectator";
455                 } else {
456                         break;
457                 }
458         }
459
460         votecalled = FALSE;
461         votecalledmaster = FALSE;
462         votefinished = 0;
463 }
464
465 void VoteAccept() {
466         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was accepted.\n"));
467         if(votecalledmaster)
468         {
469                 votecaller.vote_master = 1;
470         } else {
471                 localcmd(strcat(votecalledvote, "\n"));
472         }
473         votecaller.vote_next = 0; // people like your votes, no wait for next vote
474         VoteReset();
475 }
476
477 void VoteReject() {
478         bprint(strcat("^2The vote for \"^1", votecalledvote, "^2\" from \"^7", votecaller.netname, "^2\" was rejected.\n"));
479         VoteReset();
480 }
481
482 void VoteTimeout() {
483         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" did timeout.\n"));
484         VoteReset();
485 }
486
487 void VoteStop(entity stopper) {
488         bprint(strcat("^5The vote for \"^1", votecalledvote, "^5\" from \"^7", votecaller.netname, "^5\" was stopped by \"^5", stopper.netname, "^5\".\n"));
489         if(stopper == votecaller) {
490                 // no wait for next vote so you can correct your vote
491                 votecaller.vote_next = 0;
492         }
493         VoteReset();
494 }
495
496 void VoteCount() {
497         local float playercount;
498         playercount = 0;
499         local float yescount;
500         yescount = 0;
501         local float nocount;
502         nocount = 0;
503         local string searchclass;
504         searchclass = "player";
505
506         while (TRUE)
507         {
508                 local entity player;
509                 player = find(player, classname, searchclass);
510
511                 while(player)
512                 {
513                         if(clienttype(player) != CLIENTTYPE_BOT) {
514                                 if(player.vote_vote < 0) {
515                                         nocount++;
516                                 } else if(player.vote_vote > 0) {
517                                         yescount++;
518                                 }
519                                 playercount++;
520                         }
521                         player = find(player, classname, searchclass);
522                 }
523
524                 if("player" == searchclass) {
525                         searchclass = "observer";
526                 } else if("observer" == searchclass) {
527                         searchclass = "specator";
528                 } else {
529                         break;
530                 }
531         }
532
533         if((playercount == 1) && votecalledmaster) {
534                 // if only one player is on the server becoming vote
535                 // master is not allowed.  This could be used for
536                 // trolling or worse. 'self' is the user who has
537                 // called the vote because this function is called
538                 // by SV_ParseClientCommand. Maybe all voting should
539                 // be disabled for a single player?
540                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
541                 votecaller.vote_next = 0;
542                 VoteReset();
543         } else if((playercount / 2) < yescount) { // vote accepted
544                 VoteAccept();
545         } else if((playercount / 2) < nocount) { // vote rejected
546                 VoteReject();
547         } else if(time > votefinished) { // vote timedout
548                 VoteTimeout();
549         } // else still running
550 }
551
552
553 void ReadyCount()
554 {
555         local entity e;
556         local float r, p;
557
558         e = find(world, classname, "player");
559
560         while(e)
561         {
562                 if(clienttype(e) == CLIENTTYPE_REAL)
563                 {
564                         p += 1;
565                         if(e.ready) r += 1;
566                 }
567                 e = find(e, classname, "player");
568         }
569
570         if(p && r == p)
571         {
572                 bprint("^1Server is restarting...\n");
573                 localcmd("restart\n");
574         }
575 }