]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/clientcommands.qc
vote message in four lines again
[divverent/nexuiz.git] / data / qcsrc / server / clientcommands.qc
1 void ReadyCount();
2 float ValidateMap(string vote);
3 void(entity e) DropFlag;
4 string MapVote_Suggest(string m);
5
6 void Say(entity source, float teamsay, string msgin)
7 {
8         string msgstr, colorstr, cmsgstr;
9         entity head;
10
11         msgin = formatmessage(msgin);
12
13         if(msgin == "")
14                 return;
15
16         colorstr = Team_ColorCode(source.team);
17
18         if(!teams_matter)
19                 teamsay = FALSE;
20
21         if(intermission_running)
22                 teamsay = FALSE;
23
24         if(source.classname != "player") // observers can't
25                 teamsay = FALSE;
26
27         // how can we prevent the message from appearing in a listen server?
28         // for now, just give "say" back and only handle say_team
29         if(!teamsay)
30         {
31                 clientcommand(self, strcat("say ", msgin));
32                 return;
33         }
34
35         if(teamsay)
36         {
37                 msgstr = strzone(strcat("\{1}\{13}", colorstr, "(^3", source.netname, colorstr, ") ^7", msgin, "\n"));
38                 cmsgstr = strcat(colorstr, "(^3", source.netname, colorstr, ")\n^7", wordwrap(msgin, 50));
39         }
40         else
41                 msgstr = strzone(strcat("\{1}^3", source.netname, "^7: ", msgin, "\n"));
42
43         if(teamsay)
44         {
45                 FOR_EACH_REALPLAYER(head)
46                 {
47                         if(head.team == source.team)
48                         {
49                                 sprint(head, msgstr);
50                                 centerprint(head, cmsgstr);
51                         }
52                 }
53         }
54         else
55         {
56                 bprint(msgstr);
57                 //ServerConsoleEcho(substring(msgstr, 1, strlen(msgstr) - 2), TRUE);
58         }
59
60         strunzone(msgstr);
61 }
62
63 void SV_ParseClientCommand(string s) {
64         local string cmd;
65
66         tokenize(s);
67
68         if(argv(0) == "vote") {
69                 if(argv(1) == "help") {
70                         local string vmasterdis;
71                         if(!cvar("sv_vote_master")) {
72                                 vmasterdis = " ^1(disabled)";
73                         }
74                         local string vcalldis;
75                         if(!cvar("sv_vote_call")) {
76                                 vcalldis = " ^1(disabled)";
77                         }
78                         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");
79                         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");
80                         sprint(self, "^7\"^2help^7\" shows this info.\n");
81                         sprint(self, "^7\"^2status^7\" shows if there is a vote called and who called it.\n");
82                         sprint(self, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7\n"));
83                         sprint(self, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.\n");
84                         sprint(self, strcat("^7\"^2master^7\" is used to call a vote to become a master.", vmasterdis, "^7\n"));
85                         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");
86                         sprint(self, "^7\"^2yes^7\" and \"^2no^7\" to make your vote.\n");
87                         sprint(self, "^7If more then 50% of the players vote yes the vote is accepted.\n");
88                         sprint(self, "^7If more then 50% of the players vote no the vote is rejected.\n");
89                         sprint(self, strcat("^7The vote will end after ", cvar_string("sv_vote_timeout"), "^7 seconds.\n"));
90                         sprint(self, "^7You can call a vote for or execute these commands:\n");
91                         sprint(self, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7\n"));
92                 } else if(argv(1) == "status") {
93                         if(votecalled) {
94                                 sprint(self, strcat("^7Vote for \"^1", votecalledvote, "^7\" called by \"^7", votecaller.netname, "^7\".\n"));
95                         } else {
96                                 sprint(self, "^1No vote called.\n");
97                         }
98                 } else if(argv(1) == "call") {
99                         if(cvar("sv_vote_call")) {
100                                 if(votecalled) {
101                                         sprint(self, "^1There is already a vote called.\n");
102                                 } else {
103                                         local string vote;
104                                         vote = VoteParse();
105                                         if(vote == "") {
106                                                 sprint(self, "^1Your vote is empty. See help for more info.\n");
107                                         } else if(time < self.vote_next) {
108                                                 sprint(self, strcat("^1You have to wait ^2", ftos(self.vote_next - time), "^1 seconds before you can again call a vote.\n"));
109                                         } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
110                                                 if(!ValidateMap(vote))
111                                                         return;
112                                                 votecalled = TRUE;
113                                                 votecalledmaster = FALSE;
114                                                 // remap chmap to gotomap (forces intermission)
115                                                 if(strlen(vote) >= 6)
116                                                         if(substring(vote, 0, 6) == "chmap ")
117                                                                 vote = strcat("gotomap ", substring(vote, 6, strlen(vote) - 6));
118                                                 votecalledvote = strzone(vote);
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("\{1}^2* ^3", votecaller.netname, "^2 calls a vote for ^1", votecalledvote, "\n"));
124                                                 VoteCount(); // needed if you are the only one
125                                         } else {
126                                                 sprint(self, "^1This vote is not ok. See help for more info.\n");
127                                         }
128                                 }
129                         } else {
130                                 sprint(self, "^1Vote calling is NOT allowed.\n");
131                         }
132                 } else if(argv(1) == "stop") {
133                         if(!votecalled) {
134                                 sprint(self, "^1No vote called.\n");
135                         } else if(self == votecaller) { // the votecaller can stop a vote
136                                 VoteStop(self);
137                         } else {
138                                 sprint(self, "^1You are not allowed to stop that Vote.\n");
139                         }
140                 } else if(argv(1) == "master") {
141                         if(cvar("sv_vote_master")) {
142                                 if(votecalled) {
143                                         sprint(self, "^1There is already a vote called.\n");
144                                 } else {
145                                         votecalled = TRUE;
146                                         votecalledmaster = TRUE;
147                                         votecalledvote = strzone("^3master");
148                                         votecaller = self; // remember who called the vote
149                                         votefinished = time + cvar("sv_vote_timeout");
150                                         votecaller.vote_vote = 1; // of course you vote yes
151                                         votecaller.vote_next = time + cvar("sv_vote_wait");
152                                         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2 calls a vote to become ^3master^2.\n"));
153                                         VoteCount(); // needed if you are the only one
154                                 }
155                         } else {
156                                 sprint(self, "^1Vote to become master is NOT allowed.\n");
157                         }
158                 } else if(argv(1) == "do") {
159                         if(argv(2) == "login") {
160                                 local string masterpwd;
161                                 masterpwd = cvar_string("sv_vote_master_password");
162                                 if(masterpwd != "") {
163                                         self.vote_master = (masterpwd == argv(3));
164                                         if(self.vote_master) {
165                                                 ServerConsoleEcho(strcat("Accepted master login from ", self.netname), TRUE);
166                                                 bprint(strcat("\{1}^2* ^3", self.netname, "^2 logged in as ^3master^2\n"));
167                                         }
168                                         else
169                                                 ServerConsoleEcho(strcat("REJECTED master login from ", self.netname), TRUE);
170                                 }
171                                 else
172                                         sprint(self, "^1You are NOT a master.\n");
173                         } else if(self.vote_master) {
174                                 local string dovote;
175                                 dovote = VoteParse();
176                                 if(dovote == "") {
177                                         sprint(self, "^1Your command was empty. See help for more info.\n");
178                                 } else if(VoteAllowed(strcat(argv(2)))) { // strcat seems to be necessary
179                                         if(!ValidateMap(dovote))
180                                                 return;
181                                         // remap chmap to gotomap (forces intermission)
182                                         if(strlen(dovote) >= 6)
183                                                 if(substring(dovote, 0, 6) == "chmap ")
184                                                         vote = strcat("gotomap ", substring(dovote, 6, strlen(dovote) - 6));
185                                         bprint("\{1}^2* ^3", strcat(self.netname, "^2 used his ^3master^2 status to do \"^2", dovote, "^2\".\n"));
186                                         localcmd(strcat(dovote, "\n"));
187                                 } else {
188                                         sprint(self, "^1This command is not ok. See help for more info.\n");
189                                 }
190                         } else {
191                                 sprint(self, "^1You are NOT a master.\n");
192                         }
193                 } else if(argv(1) == "yes") {
194                         if(!votecalled) {
195                                 sprint(self, "^1No vote called.\n");
196                         } else if(self.vote_vote == 0
197                                   || cvar("sv_vote_change")) {
198                                 sprint(self, "^1You accepted the vote.\n");
199                                 self.vote_vote = 1;
200                                 if(!cvar("sv_vote_singlecount")) {
201                                         VoteCount();
202                                 }
203                         } else {
204                                 sprint(self, "^1You have already voted.\n");
205                         }
206                 } else if(argv(1) == "no") {
207                         if(!votecalled) {
208                                 sprint(self, "^1No vote called.\n");
209                         } else if(self.vote_vote == 0
210                                   || cvar("sv_vote_change")) {
211                                 sprint(self, "^1You rejected the vote.\n");
212                                 self.vote_vote = -1;
213                                 if(!cvar("sv_vote_singlecount")) {
214                                         VoteCount();
215                                 }
216                         } else {
217                                 sprint(self, "^1You have already voted.\n");
218                         }
219                 } else {
220                         // ignore this?
221                         sprint(self, "^1Unknown vote command.\n");
222                 }
223         } else if(argv(0) == "autoswitch") {
224                 // be backwards compatible with older clients (enabled)
225                 self.autoswitch = ("0" != argv(1));
226                 local string autoswitchmsg;
227                 if (self.autoswitch) {
228                         autoswitchmsg = "on";
229                 } else {
230                         autoswitchmsg = "off";
231                 }
232                 sprint(self, strcat("^1autoswitch turned ", autoswitchmsg, "\n"));
233         } else if(argv(0) == "clientversion") {
234                 if (argv(1) == "$gameversion") {
235                         //versionmsg = "^1client is too old to get versioninfo.\nUPDATE!!! (http://www.nexuiz.com)^8";
236                         // either that or someone wants to be funny
237                         self.version = 1;
238                 } else {
239                         self.version = stof(argv(1));
240                 }
241                 if(self.version != cvar("gameversion")) 
242                 {
243                         self.classname = "observer";
244                         self.frags = -2;
245                         PutClientInServer();
246                 } else if(cvar("g_campaign") || cvar("g_balance_teams")) {
247                         //JoinBestTeam(self, 0);
248                 } else if(cvar("teamplay") && !cvar("sv_spectate")) {
249                         self.classname = "observer";
250                         stuffcmd(self,"menu_showteamselect\n");
251                 }
252         } else if(argv(0) == "reportcvar") {
253                 GetCvars(1);
254         } else if(argv(0) == "spectate") {
255                 if(cvar("g_lms") || cvar("g_arena"))
256                         return; // don't allow spectating in lms, unless player runs out of lives
257                 if(self.classname == "player" && cvar("sv_spectate") == 1) {
258                         if(self.flagcarried)
259                                 DropFlag(self.flagcarried);
260                         DistributeFragsAmongTeam(self, self.team, 1.0);
261                         self.classname = "observer";
262                         PutClientInServer();
263                 }
264         } else if(argv(0) == "join") {
265                 if(!cvar("g_arena"))
266                 if (self.classname != "player")
267                 {
268                         self.classname = "player";
269                         self.frags = 0;
270                         bprint (strcat("^4", self.netname, "^4 is playing now\n"));
271                         PutClientInServer();
272                 }
273         } else if( argv(0) == "selectteam" ) {
274                 if( !cvar("teamplay") ) {
275                         sprint( self, "selecteam can only be used in teamgames\n");
276                 } else if(cvar("g_campaign")) {
277                         //JoinBestTeam(self, 0);
278                 } else if( argv(1) == "none" ) {
279                         SV_ChangeTeam( 0 );
280                 } else if( argv(1) == "red" ) {
281                         SV_ChangeTeam( COLOR_TEAM1 - 1 );
282                 } else if( argv(1) == "blue" ) {
283                         SV_ChangeTeam( COLOR_TEAM2 - 1 );
284                 } else if( argv(1) == "pink" ) {
285                         SV_ChangeTeam( COLOR_TEAM3 - 1 );
286                 } else if( argv(1) == "yellow" ) {
287                         SV_ChangeTeam( COLOR_TEAM4 - 1 );
288                 } else if( argv(1) == "auto" ) {
289                         self.team = -1;
290                         JoinBestTeam( self, 0 );
291                 } else {
292                         sprint( self, strcat( "selectteam none/red/blue/pink/yellow/auto - \"", argv(1), "\" not recognised\n" ) );
293                 }
294         } else if(argv(0) == "ready") {
295                 if(cvar("sv_ready_restart"))
296                 {
297                         self.ready = TRUE;
298                         bprint(self.netname, "^2 is ready\n");
299                         ReadyCount();
300                 }
301         } else if(argv(0) == "maplist") {
302                 local float i, n;
303                 local string col;
304                 n = tokenize(cvar_string("g_maplist"));
305                 sprint(self, "^7Maps in list: ");
306                 for(i = 0; i < n; ++i)
307                 {
308                         if(math_mod(i, 2))
309                                 col = "^2";
310                         else
311                                 col = "^3";
312                         sprint(self, strcat(col, argv(i), " "));
313                 }
314                 sprint(self, "\n");
315         } else if(argv(0) == "teamstatus") {
316                 PrintScoreboard(self);
317         } else if(argv(0) == "say") {
318                 Say(self, FALSE, substring(s, 4, strlen(s) - 4));
319                 //clientcommand(self, formatmessage(s));
320         } else if(argv(0) == "say_team") {
321                 Say(self, TRUE, substring(s, 9, strlen(s) - 9));
322                 //clientcommand(self, formatmessage(s));
323         } else if(argv(0) == "info") {
324                 cmd = cvar_string(strcat("sv_info_", argv(1)));
325                 if(cmd == "")
326                         sprint(self, "ERROR: unsupported info command\n");
327                 else
328                         wordwrap_sprint(cmd, 1111);
329         } else if(argv(0) == "suggestmap") {
330                 sprint(self, strcat(MapVote_Suggest(argv(1)), "\n"));
331         } else {
332                 cmd = argv(0);
333                 /* checks not needed any more since DP has separated clientcommands and regular commands
334                 if(cmd != "status")
335                 if(cmd != "name")
336                 //if(cmd != "say") // handled above
337                 //if(cmd != "say_team") // handled above
338                 if(cmd != "tell")
339                 if(cmd != "color")
340                 if(cmd != "kill")
341                 if(cmd != "pause")
342                 if(cmd != "kick")
343                 if(cmd != "ping")
344                 if(cmd != "pings")
345                 if(cmd != "ban")
346                 if(cmd != "pmodel")
347                 if(cmd != "rate")
348                 if(cmd != "playermodel")
349                 if(cmd != "playerskin")
350                 if(cmd != "god") if(cmd != "notarget") if(cmd != "fly") if(cmd != "give") if(cmd != "noclip")
351                 {
352                         ServerConsoleEcho(strcat("WARNING: Invalid clientcommand by ", self.netname, ": ", s), TRUE);
353                         return;
354                 }
355                 */
356                 clientcommand(self,s);
357         }
358 }
359
360 float ValidateMap(string vote)
361 {
362         string ext;
363         
364         tokenize(vote);
365         if(argv(0) == "map" || argv(0) == "changelevel")
366                 ext = ".bsp";
367         else if(argv(0) == "chmap")
368                 ext = ".mapcfg";
369         else if(argv(0) == "gotomap")
370                 ext = ".mapcfg";
371         else
372                 return TRUE;
373
374         if(!TryFile(strcat("maps/", argv(1), ext)))
375         {
376                 sprint(self, strcat("^1Invalid mapname, \"^3", argv(1), "^1\" does not exist on this server.\n"));
377                 return FALSE;
378         }
379         return TRUE;
380 }
381
382
383 void VoteThink() {
384         if(votefinished > 0 // a vote was called
385             && time > votefinished) // time is up
386         {
387                 VoteCount();
388         }
389 }
390
391 string VoteParse() {
392         local float index;
393         index = 3;
394         local string vote;
395         vote = argv(2);
396         while(argv(index) != "") {
397                 vote = strcat(vote, " ", argv(index));
398                 index++;
399         }
400
401         // necessary for some of the string operations
402         vote = strzone(vote);
403
404         // now we remove some things that could be misused
405         index = 0;
406         local float found;
407         found = FALSE;
408         local float votelength;
409         votelength = strlen(vote);
410         while(!found && index < votelength)
411         {
412                 local string badchar;
413                 badchar = substring(vote, index, 1);
414                 if(badchar == ";"
415                    || badchar == "\r"
416                    || badchar == "\n")
417                 {
418                         found = TRUE;
419                 } else {
420                         index++;
421                 }
422         }
423         return substring(vote, 0, index);
424 }
425
426 float VoteAllowed(string votecommand) {
427         tokenize(cvar_string("sv_vote_commands"));
428         local float index;
429         index = 0;
430         while(argv(index) != "") {
431                 local string allowed;
432                 allowed = argv(index);
433                 if(votecommand == allowed) {
434                         return TRUE;
435                 }
436                 index++;
437         }
438         return FALSE;
439 }
440
441 void VoteReset() {
442         local entity player;
443
444         FOR_EACH_CLIENT(player)
445         {
446                 player.vote_vote = 0;
447                 centerprint_expire(player, CENTERPRIO_VOTE);
448         }
449
450         votecalled = FALSE;
451         votecalledmaster = FALSE;
452         votefinished = 0;
453 }
454
455 void VoteAccept() {
456         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was accepted\n"));
457         if(votecalledmaster)
458         {
459                 votecaller.vote_master = 1;
460         } else {
461                 localcmd(strcat(votecalledvote, "\n"));
462         }
463         votecaller.vote_next = 0; // people like your votes, no wait for next vote
464         VoteReset();
465 }
466
467 void VoteReject() {
468         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 was rejected\n"));
469         VoteReset();
470 }
471
472 void VoteTimeout() {
473         bprint(strcat("\{1}^2* ^3", votecaller.netname, "^2's vote for ^1", votecalledvote, "^2 timed out\n"));
474         VoteReset();
475 }
476
477 void VoteStop(entity stopper) {
478         bprint(strcat("\{1}^2* ^3", stopper.netname, "^2 stopped ^3", votecaller.netname, "^2's vote\n"));
479         if(stopper == votecaller) {
480                 // no wait for next vote so you can correct your vote
481                 votecaller.vote_next = 0;
482         }
483         VoteReset();
484 }
485
486 void VoteNag() {
487         if(votecalled)
488                 if(self.vote_vote == 0)
489                         centerprint_atprio(self, CENTERPRIO_VOTE, strcat("^7^3", votecaller.netname, "^2 called a vote for ^1", votecalledvote, "\n\n^2You have not voted yet!\n^2HINT: By default, F1 is yes and F2 is no."));
490 }
491
492 void VoteCount() {
493         local float playercount;
494         playercount = 0;
495         local float yescount;
496         yescount = 0;
497         local float nocount;
498         nocount = 0;
499         local entity player;
500
501         FOR_EACH_REALCLIENT(player)
502         {
503                 if(player.vote_vote < 0) {
504                         nocount++;
505                 } else if(player.vote_vote > 0) {
506                         yescount++;
507                 }
508                 playercount++;
509         }
510
511         if((playercount == 1) && votecalledmaster) {
512                 // if only one player is on the server becoming vote
513                 // master is not allowed.  This could be used for
514                 // trolling or worse. 'self' is the user who has
515                 // called the vote because this function is called
516                 // by SV_ParseClientCommand. Maybe all voting should
517                 // be disabled for a single player?
518                 sprint(self, "^1You are the only player on this server so you can not become vote master.\n");
519                 votecaller.vote_next = 0;
520                 VoteReset();
521         } else if((playercount / 2) < yescount) { // vote accepted
522                 VoteAccept();
523         } else if((playercount / 2) < nocount) { // vote rejected
524                 VoteReject();
525         } else if(time > votefinished) { // vote timedout
526                 if(cvar("sv_vote_simple_majority"))
527                         if(yescount > nocount)
528                                 VoteAccept();
529                         else if(nocount > yescount)
530                                 VoteReject();
531                         else
532                                 VoteTimeout();
533                 else
534                         VoteTimeout();
535         } // else still running
536 }
537
538
539 void ReadyCount()
540 {
541         local entity e;
542         local float r, p;
543
544         FOR_EACH_REALPLAYER(e)
545         {
546                 p += 1;
547                 if(e.ready)
548                         r += 1;
549         }
550
551         if(p && r == p)
552         {
553                 bprint("^1Server is restarting...\n");
554                 localcmd("restart\n");
555         }
556 }