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