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