force frags to zero when doing a cmd join
[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, "^1You can use voting with \"^2cmd vote help^1\" \"^2cmd vote status^1\" \"^2cmd vote call ^3COMMAND ARGUMENTS^1\" \"^2cmd vote stop^1\" \"^2cmd vote yes^1\" \"^2cmd vote no^1\".\n");
42                         sprint(self, "^1Or if your version is up to date you can use these aliases \"^2vhelp^1\" \"^2vstatus^1\" \"^2vcall ^3COMMAND ARGUMENTS^1\" \"^2vstop^1\" \"^2vyes^1\" \"^2vno^1\".\n");
43                         sprint(self, "^1\"^2help^1\" shows this info.\n");
44                         sprint(self, "^1\"^2status^1\" shows if there is a vote called and who called it.\n");
45                         sprint(self, "^1\"^2call^1\" is used to call a vote. See the list of allowed commands.\n");
46                         sprint(self, "^1If more then 50% of the players vote yes the vote is executed.\n");
47                         sprint(self, "^1If more then 50% of the players vote no the vote fails.\n");
48                         sprint(self, "^1\"^2stop^1\" can be used by the vote caller to stop a vote and maybe correct it.\n");
49                         sprint(self, "^1\"^2yes^1\" and \"^2no^1\" to make your vote.\n");
50                         sprint(self, "^1You can call a vote with these commands:\n");
51                         sprint(self, strcat("^1\"^2vcall^1\" ^3", cvar_string("sv_vote_allowed"), "^1 and further ^3arguments^1\n"));
52                 } else if(argv(1) == "status") {
53                         if(votecalled == "") {
54                                 sprint(self, "^1No vote called.\n");
55                         } else {
56                                 sprint(self, strcat("^7Vote for \"^1", votecalled, "^7\" called by \"", votecaller.netname, "^7\".\n"));
57                         }
58                 } else if(argv(1) == "call") {
59                         if(votecalled == "") {
60                                 local string vote;
61                                 vote = argv(2);
62                                 index = 3;
63                                 while(argv(index) != "") {
64                                         vote = strcat(vote, " ", argv(index));
65                                         index++;
66                                 }
67
68                                 // necessary for some of the string operations
69                                 vote = strzone(vote);
70
71                                 // now we remove some things that could be misused
72                                 index = 0;
73                                 local float found;
74                                 found = FALSE;
75                                 local float votelength;
76                                 votelength = strlen(vote);
77                                 while(!found && index < votelength)
78                                 {
79                                         local string badchar;
80                                         badchar = substring(vote, index, 1);
81                                         if(badchar == ";"
82                                            || badchar == "\n")
83                                         {
84                                                 found = TRUE;
85                                         } else {
86                                                 index++;
87                                         }
88                                 }
89                                 vote = substring(vote, 0, index);
90
91                                 if(vote == "") {
92                                         sprint(self, "^1You have to vote for something.\n");
93                                 } else if(time < self.vote_next) {
94                                         sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
95                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
96                                         votecalled = strzone(vote);
97                                         votecaller = self; // remember who called the vote
98                                         self.vote_vote = 1; // of course you vote yes
99                                         self.vote_finished = time + cvar("sv_vote_timeout");
100                                         self.vote_next = time + cvar("sv_vote_wait");
101                                         bprint(strcat("^3Vote for \"^1", votecalled, "^3\" called by \"", self.netname, "^3\".\n"));
102                                         VoteCount(); // needed if you are the only one
103                                 } else {
104                                         sprint(self, "^1This vote is not ok. See help for more info.\n");
105                                 }
106                         } else {
107                                 sprint(self, "^1There is already a vote called.\n");
108                         }
109                 } else if(argv(1) == "stop") {
110                         if(votecalled == "") {
111                                 sprint(self, "^1No vote called.\n");
112                         } else if(votecaller == self
113                                   || self.adminstatus > 0) { // the votecaller and admins can stop a vote
114                                 if(votecaller == self) {
115                                         // disable next votetimer so you can correct your vote
116                                         self.vote_next = 0;
117                                 }
118                                 VoteTimeout(votecaller);
119                         } else {
120                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
121                         }
122                 } else if(argv(1) == "yes") {
123                         if(votecalled == "") {
124                                 sprint(self, "^1No vote called.\n");
125                         } else if(self.vote_vote == 0
126                                   || cvar("sv_vote_change")) {
127                                 self.vote_vote = 1;
128                                 if(!cvar("sv_vote_singlecount")) {
129                                         VoteCount();
130                                 }
131                         } else {
132                                 sprint(self, "^1You have already voted.\n");
133                         }
134                 } else if(argv(1) == "no") {
135                         if(votecalled == "") {
136                                 sprint(self, "^1No vote called.\n");
137                         } else if(self.vote_vote == 0
138                                   || cvar("sv_vote_change")) {
139                                 self.vote_vote = -1;
140                                 if(!cvar("sv_vote_singlecount")) {
141                                         VoteCount();
142                                 }
143                         } else {
144                                 sprint(self, "^1You have already voted.\n");
145                         }
146                 } else {
147                         // ignore this?
148                         sprint(self, "^1Unknown vote command.\n");
149                 }
150         } else if(argv(0) == "autoswitch") {
151                 // be backwards compatible with older clients (enabled)
152                 self.autoswitch = ("0" != argv(1));
153                 local string autoswitchmsg;
154                 if (self.autoswitch) {
155                         autoswitchmsg = "on";
156                 } else {
157                         autoswitchmsg = "off";
158                 }
159                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
160         } else if(argv(0) == "clientversion") {
161                 local string versionmsg;
162                 if (argv(1) == "$g_nexuizversion_major") {
163                         versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
164                         // either that or someone wants to be funny
165                 } else if (stof(argv(1)) != cvar("g_nexuizversion_major")) {
166                         if(stof(argv(1)) < cvar("g_nexuizversion_major")) {
167                                 versionmsg = "^3Your client version is outdated.\nPlease update!!!^8";
168                         } else {
169                                 versionmsg = "^3This server is using an outdated Nexuiz version.\nThis can lead to problems.^8";
170                         }
171
172                 } else {
173                         versionmsg = "^2client version and server version are the same.^8";
174                 }
175                 self.versionmessage = strzone(versionmsg);
176         } else if(argv(0) == "spectate") {
177                 if(cvar("g_lms"))
178                         return; // don't allow spectating in lms, unless player runs out of lives
179                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
180                         self.classname = "observer";
181                         PutClientInServer();
182                 }
183         } else if(argv(0) == "join") {
184                 self.classname = "player";
185                 self.frags = 0;
186                 // TODO: I have no idea whether this is needed or not
187                 if(!cvar("g_lms")) {
188                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
189                 }
190                 PutClientInServer();
191         } else if( argv(0) == "selectteam" ) {
192                 if( argv(1) == "none" ) {
193                         SV_ChangeTeam( 0 );
194                         PutClientInServer();
195                 } else if( argv(1) == "red" ) {
196                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
197                 } else if( argv(1) == "blue" ) {
198                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
199                 } else if( argv(1) == "green" ) {
200                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
201                 } else if( argv(1) == "yellow" ) {
202                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
203                 } else if( argv(1) == "auto" ) {
204                         JoinBestTeam( self, 0 );
205                 } else {
206                         sprint( self, strcat( "selectteam none/red/blue/green/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
207                 }
208         } else {
209                 clientcommand(self,s);
210         }
211 }
212
213 float VoteAllowed(string votecommand) {
214         tokenize(cvar_string("sv_vote_allowed"));
215         local float index;
216         index = 0;
217         while(argv(index) != "") {
218                 local string allowed;
219                 allowed = argv(index);
220                 if(votecommand == allowed) {
221                         return TRUE;
222                 }
223                 index++;
224         }
225         return FALSE;
226 }
227
228 void VoteCount() {
229         local float playercount;
230         playercount = 0;
231         local float yescount;
232         yescount = 0;
233         local float nocount;
234         nocount = 0;
235         local entity player;
236         local entity voter;
237         player = find(player, classname, "player");
238         while(player)
239         {
240                 if(player.vote_vote < 0) {
241                         nocount++;
242                 } else if(player.vote_vote > 0) {
243                         yescount++;
244                 }
245
246                 if(self.vote_finished > 0) {
247                         voter = player;
248                 }
249
250                 playercount++;
251                 player = find(player, classname, "player");
252         }
253
254         if((playercount / 2) < yescount) { // vote passed
255                 VoteDo(voter);
256         } else if((playercount / 2) < nocount) { // vote rejected
257                 VoteTimeout(voter);
258         } // else still running
259 }
260
261 void VoteDo(entity voter) {
262         bprint(strcat("^2The vote for \"^1", votecalled, "^2\" from \"", voter.netname, "^2\" DID PASS.\n"));
263         localcmd(votecalled);
264         VoteReset();
265 }
266
267 void VoteTimeout(entity voter) {
268         bprint(strcat("^5The vote for \"^1", votecalled, "^5\" from \"", voter.netname, "^5\" did NOT pass.\n"));
269         VoteReset();
270 }
271
272 void VoteReset() {
273         local entity player;
274         player = find(player, classname, "player");
275         while(player)
276         {
277                 player.vote_vote = 0;
278                 player.vote_finished = 0;
279                 player = find(player, classname, "player");
280         }
281         votecalled = "";
282 }