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