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