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