]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/vote.qc
fix map validation for chmap
[divverent/nexuiz.git] / data / qcsrc / server / vote.qc
1 float VoteCheckNasty(string cmd)
2 {
3         if(strstrofs(cmd, ";", 0) >= 0)
4                 return TRUE;
5         if(strstrofs(cmd, "\n", 0) >= 0)
6                 return TRUE;
7         if(strstrofs(cmd, "\r", 0) >= 0)
8                 return TRUE;
9         if(strstrofs(cmd, "$", 0) >= 0)
10                 return TRUE;
11         return FALSE;
12 }
13
14 entity GetKickVoteVictim(string vote, string cmd, entity caller)
15 {
16         float tokens;
17         float n, t;
18         string ns;
19         entity e;
20
21         tokens = tokenize_sane(vote);
22         ns = "";
23
24         if(tokens >= 2)
25                 if(substring(argv(1), 0, 1) == "#")
26                 {
27                         ns = substring(argv(1), 1, 999);
28                         t = 2;
29                 }
30
31         if(tokens >= 3)
32                 if(argv(1) == "#")
33                 {
34                         ns = argv(2);
35                         t = 3;
36                 }
37
38         if(ns != "")
39         {
40                 GetKickVoteVictim_reason = substring(vote, argv_start_index(t), argv_end_index(-1) - argv_start_index(t));
41
42                 n = stof(ns);
43                 if(ns == ftos(n)) if(n >= 1) if(n <= maxclients)
44                 {
45                         e = edict_num(n);
46                         if(clienttype(e) == CLIENTTYPE_REAL)
47                         {
48                                 GetKickVoteVictim_newcommand = strcat(argv(0), " # ", ns);
49                                 return e;
50                         }
51                 }
52         }
53
54         print_to(caller, strcat("Usage: ", cmd, " ", argv(0), " #playernumber (as in \"status\")\n"));
55         return world;
56 }
57
58 string RemapVote_display;
59 string RemapVote_vote;
60 float RemapVote(string vote, string cmd, entity e)
61 {
62         float vote_argc;
63         entity victim;
64         vote_argc = tokenize_sane(vote);
65
66         print(">>", vote, "<<\n");
67
68         if(!VoteAllowed(argv(0)))
69         {
70                 return FALSE;
71         }
72
73         // VoteAllowed tokenizes!
74         vote_argc = tokenize_sane(vote);
75
76         // remap chmap to gotomap (forces intermission)
77         if(vote_argc < 2)
78                 if(argv(0) == "chmap" || argv(0) == "gotomap" || argv(0) == "kick" || argv(0) == "kickban") // won't work without arguments
79                         return FALSE;
80         if(argv(0) == "chmap")
81         {
82                 vote = strcat("gotomap ", substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)));
83                 vote_argc = tokenize_sane(vote);
84         }
85         if(argv(0) == "gotomap")
86         {
87                 if(!(vote = ValidateMap(substring(vote, argv_start_index(1), argv_end_index(-1) - argv_start_index(1)), e)))
88                         return FALSE;
89                 vote = strcat("gotomap ", vote);
90                 vote_argc = tokenize_sane(vote); // ValidateMap may have done some stuff to it
91         }
92
93         // make kick and kickban votes a bit nicer (and reject them if formatted badly)
94         if(argv(0) == "kick" || argv(0) == "kickban")
95         {
96                 if(!(victim = GetKickVoteVictim(vote, cmd, e)))
97                         return FALSE;
98                 RemapVote_vote = GetKickVoteVictim_newcommand;
99                 RemapVote_display = strcat("^1", vote, " (^7", victim.netname, "^1): ", GetKickVoteVictim_reason);
100         }
101         else
102         {
103                 RemapVote_vote = vote;
104                 RemapVote_display = strzone(strcat("^1", vote));
105         }
106
107         return TRUE;
108 }
109
110 float GameCommand_Vote(string s, entity e) {
111         tokenize_sane(s);
112         if(argv(0) == "help") {
113                 print_to(e, "  vote COMMANDS ARGUMENTS. See 'vote help' for more info.");
114                 return TRUE;
115         } else if(argv(0) == "vote") {
116                 if(argv(1) == "") {
117                         print_to(e, "^1You have to supply a vote command. See help for more info.");
118                 } else if(argv(1) == "help") {
119                         VoteHelp(e);
120                 } else if(argv(1) == "status") {
121                         if(votecalled) {
122                                 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
123                         } else {
124                                 print_to(e, "^1No vote called.");
125                         }
126                 } else if(argv(1) == "call") {
127                         if(!e || cvar("sv_vote_call")) {
128                                 if(cvar("sv_vote_nospectators") && e.classname != "player") {
129                                         print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
130                                 }
131                                 else if(timeoutStatus) { //don't allow a vote call during a timeout
132                                         print_to(e, "^1Error: You can not call a vote while a timeout is active.");
133                                 }
134                                 else if(votecalled) {
135                                         print_to(e, "^1There is already a vote called.");
136                                 } else {
137                                         local string vote;
138                                         vote = VoteParse(s);
139                                         if(vote == "") {
140                                                 print_to(e, "^1Your vote is empty. See help for more info.");
141                                         } else if(e
142                                                 && time < e.vote_next) {
143                                                         print_to(e, strcat("^1You have to wait ^2", ftos(e.vote_next - time), "^1 seconds before you can again call a vote."));
144                                         } else if(VoteCheckNasty(vote)) {
145                                                 print_to(e, "Syntax error in command. See help for more info.");
146                                         } else if(RemapVote(vote, "vcall", e)) {
147                                                 votecalledvote = strzone(RemapVote_vote);
148                                                 votecalledvote_display = strzone(RemapVote_display);
149                                                 votecalled = TRUE;
150                                                 votecalledmaster = FALSE;
151                                                 votefinished = time + cvar("sv_vote_timeout");
152                                                 votecaller = e; // remember who called the vote
153                                                 if(e) {
154                                                         e.vote_vote = 1; // of course you vote yes
155                                                         e.vote_next = time + cvar("sv_vote_wait");
156                                                 }
157                                                 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
158                                                 if(cvar("sv_eventlog"))
159                                                         GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
160                                                 VoteCount(); // needed if you are the only one
161                                                 Nagger_VoteChanged();
162                                         } else {
163                                                 print_to(e, "^1This vote is not ok. See help for more info.");
164                                         }
165                                 }
166                         } else {
167                                 print_to(e, "^1Vote calling is NOT allowed.");
168                         }
169                 } else if(argv(1) == "stop") {
170                         if(!votecalled) {
171                                 print_to(e, "^1No vote called.");
172                         } else if(e == votecaller) { // the votecaller can stop a vote
173                                 VoteStop(e);
174                         } else if(!e) { // server admin / console can too
175                                 VoteStop(e);
176                         } else if(e.vote_master) { // masters can too
177                                 VoteStop(e);
178                         } else {
179                                 print_to(e, "^1You are not allowed to stop that Vote.");
180                         }
181                 } else if(argv(1) == "master") {
182                         if(cvar("sv_vote_master")) {
183                                 if(votecalled) {
184                                         print_to(e, "^1There is already a vote called.");
185                                 } else {
186                                         votecalled = TRUE;
187                                         votecalledmaster = TRUE;
188                                         votecalledvote = strzone("XXX");
189                                         votecalledvote_display = strzone("^3master");
190                                         votefinished = time + cvar("sv_vote_timeout");
191                                         votecaller = e; // remember who called the vote
192                                         if(e) {
193                                                 e.vote_vote = 1; // of course you vote yes
194                                                 e.vote_next = time + cvar("sv_vote_wait");
195                                         }
196                                         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
197                                         if(cvar("sv_eventlog"))
198                                                 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
199                                         VoteCount(); // needed if you are the only one
200                                         Nagger_VoteChanged();
201                                 }
202                         } else {
203                                 print_to(e, "^1Vote to become master is NOT allowed.");
204                         }
205                 } else if(argv(1) == "do") {
206                         if(!e || e.vote_master) {
207                                 local string dovote;
208                                 dovote = VoteParse(s);
209                                 if(dovote == "") {
210                                         print_to(e, "^1Your command was empty. See help for more info.");
211                                 } else if(VoteCheckNasty(dovote)) {
212                                         print_to(e, "Syntax error in command. See help for more info.");
213                                 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
214                                         bprint("\{1}^2* ^3", VoteNetname(e), "^2 used his ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
215                                         if(cvar("sv_eventlog"))
216                                                 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
217                                         localcmd(strcat(RemapVote_vote, "\n"));
218                                 } else {
219                                         print_to(e, "^1This command is not ok. See help for more info.");
220                                 }
221                         } else {
222                                 print_to(e, "^1You are NOT a master.  You might need to login or vote to become master first. See help for more info.");
223                         }
224                 } else if(argv(1) == "login") {
225                         local string masterpwd;
226                         masterpwd = cvar_string("sv_vote_master_password");
227                         if(masterpwd != "") {
228                                 local float granted;
229                                 granted = (masterpwd == argv(2));
230                                 if (e)
231                                         e.vote_master = granted;
232                                 if(granted) {
233                                         print("Accepted master login from ", VoteNetname(e), "\n");
234                                         bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
235                                         if(cvar("sv_eventlog"))
236                                                 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
237                                 }
238                                 else
239                                         print("REJECTED master login from ", VoteNetname(e), "\n");
240                         }
241                         else
242                                 print_to(e, "^1Login to become master is NOT allowed.");
243                 } else if(argv(1) == "yes") {
244                         if(!votecalled) {
245                                 print_to(e, "^1No vote called.");
246                         } else if (!e) {
247                                 print_to(e, "^1You can't vote from the server console.");
248                         } else if(e.vote_vote == 0
249                                   || cvar("sv_vote_change")) {
250                                 print_to(e, "^1You accepted the vote.");
251                                 e.vote_vote = 1;
252                                 centerprint_expire(e, CENTERPRIO_VOTE);
253                                 if(!cvar("sv_vote_singlecount")) {
254                                         VoteCount();
255                                 }
256                         } else {
257                                 print_to(e, "^1You have already voted.");
258                         }
259                 } else if(argv(1) == "no") {
260                         if(!votecalled) {
261                                 print_to(e, "^1No vote called.");
262                         } else if (!e) {
263                                 print_to(e, "^1You can't vote from the server console.");
264                         } else if(e.vote_vote == 0
265                                   || cvar("sv_vote_change")) {
266                                 print_to(e, "^1You rejected the vote.");
267                                 e.vote_vote = -1;
268                                 centerprint_expire(e, CENTERPRIO_VOTE);
269                                 if(!cvar("sv_vote_singlecount")) {
270                                         VoteCount();
271                                 }
272                         } else {
273                                 print_to(e, "^1You have already voted.");
274                         }
275                 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
276                         if(!votecalled) {
277                                 print_to(e, "^1No vote called.");
278                         } else if (!e) {
279                                 print_to(e, "^1You can't vote from the server console.");
280                         } else if(e.vote_vote == 0
281                                   || cvar("sv_vote_change")) {
282                                 print_to(e, "^1You abstained from your vote.");
283                                 e.vote_vote = -2;
284                                 centerprint_expire(e, CENTERPRIO_VOTE);
285                                 if(!cvar("sv_vote_singlecount")) {
286                                         VoteCount();
287                                 }
288                         } else {
289                                 print_to(e, "^1You have already voted.");
290                         }
291                 } else {
292                         // ignore this?
293                         print_to(e, "^1Unknown vote command.");
294                 }
295                 return TRUE;
296         }
297         return FALSE;
298 }
299
300 void VoteHelp(entity e) {
301         local string vmasterdis;
302         if(!cvar("sv_vote_master")) {
303                 vmasterdis = " ^1(disabled)";
304         }
305
306         local string vlogindis;
307         if("" == cvar_string("sv_vote_master_password")) {
308                 vlogindis = " ^1(disabled)";
309         }
310
311         local string vcalldis;
312         if(!cvar("sv_vote_call")) {
313                 vcalldis = " ^1(disabled)";
314         }
315
316         print_to(e, "^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 login^7\" \"^2cmd vote do ^3COMMAND ARGUMENTS^7\" \"^2cmd vote yes^7\" \"^2cmd vote no^7\" \"^2cmd vote abstain^7\" \"^2cmd vote dontcare^7\".");
317         print_to(e, "^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\" \"^2vlogin^7\" \"^2vdo ^3COMMAND ARGUMENTS^7\" \"^2vyes^7\" \"^2vno^7\" \"^2abstain^7\" \"^2vdontcare^7\".");
318         print_to(e, "^7\"^2help^7\" shows this info.");
319         print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
320         print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
321         print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
322         print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
323         print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
324         print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
325         print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
326         print_to(e, "^7If enough of the players vote yes the vote is accepted.");
327         print_to(e, "^7If enough of the players vote no the vote is rejected.");
328         print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
329         print_to(e, "^7You can call a vote for or execute these commands:");
330         print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
331 }
332
333 string VoteNetname(entity e)
334 {
335         if(e) {
336                 return e.netname;
337         } else {
338                 if(cvar_string("sv_adminnick") != "") {
339                         return cvar_string("sv_adminnick");
340                 } else {
341                         return cvar_string("hostname");
342                 }
343         }
344 }
345
346 string ValidateMap(string m, entity e)
347 {
348         m = MapInfo_FixName(m);
349         if(!m)
350         {
351                 print_to(e, "This map is not available on this server.");
352                 return string_null;
353         }
354         if(!cvar("sv_vote_override_mostrecent"))
355                 if(Map_IsRecent(m))
356                 {
357                         print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
358                         return string_null;
359                 }
360         if(!MapInfo_CheckMap(m))
361         {
362                 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
363                 return string_null;
364         }
365
366         return m;
367 }
368
369
370 void VoteThink() {
371         if(votefinished > 0) // a vote was called
372         if(time > votefinished) // time is up
373         {
374                 VoteCount();
375         }
376 }
377
378 string VoteParse(string all) {
379         return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
380 }
381
382 float VoteAllowed(string votecommand) {
383         tokenize_sane(cvar_string("sv_vote_commands"));
384         local float index;
385         index = 0;
386         while(argv(index) != "") {
387                 if(votecommand == argv(index)) {
388                         return TRUE;
389                 }
390                 ++index;
391         }
392         return FALSE;
393 }
394
395 void VoteReset() {
396         local entity player;
397
398         FOR_EACH_CLIENT(player)
399         {
400                 player.vote_vote = 0;
401                 centerprint_expire(player, CENTERPRIO_VOTE);
402         }
403
404         if(votecalled)
405         {
406                 strunzone(votecalledvote);
407                 strunzone(votecalledvote_display);
408         }
409
410         votecalled = FALSE;
411         votecalledmaster = FALSE;
412         votefinished = 0;
413         Nagger_VoteChanged();
414 }
415
416 void VoteAccept() {
417         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
418         if(votecalledmaster)
419         {
420                 if(votecaller) {
421                         votecaller.vote_master = 1;
422                 }
423         } else {
424                 localcmd(strcat(votecalledvote, "\n"));
425         }
426         if(votecaller) {
427                 votecaller.vote_next = 0; // people like your votes,
428                                           // no wait for next vote
429         }
430         VoteReset();
431 }
432
433 void VoteReject() {
434         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
435         VoteReset();
436 }
437
438 void VoteTimeout() {
439         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
440         VoteReset();
441 }
442
443 void VoteStop(entity stopper) {
444         bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
445         if(cvar("sv_eventlog"))
446                 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
447         if(stopper == votecaller) {
448                 // no wait for next vote so you can correct your vote
449                 if(votecaller) {
450                         votecaller.vote_next = 0;
451                 }
452         }
453         VoteReset();
454 }
455
456 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
457 {
458         string s;
459         if(mincount >= 0)
460         {
461                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
462                 s = strcat(s, ftos(nocount), "^2 (^1");
463                 s = strcat(s, ftos(mincount), "^2 needed), ^1");
464                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
465                 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
466         }
467         else
468         {
469                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
470                 s = strcat(s, ftos(nocount), "^2, ^1");
471                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
472                 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
473         }
474         bprint(s);
475         if(cvar("sv_eventlog"))
476         {
477                 s = strcat(":vote:v", result, ":", ftos(yescount));
478                 s = strcat(s, ":", ftos(nocount));
479                 s = strcat(s, ":", ftos(abstaincount));
480                 s = strcat(s, ":", ftos(notvoters));
481                 s = strcat(s, ":", ftos(mincount));
482                 GameLogEcho(s);
483         }
484 }
485
486 void VoteCount() {
487         local float playercount;
488         playercount = 0;
489         local float yescount;
490         yescount = 0;
491         local float nocount;
492         nocount = 0;
493         local float abstaincount;
494         abstaincount = 0;
495         local entity player;
496         //same for real players
497         local float realplayercount;
498         local float realplayeryescount;
499         local float realplayernocount;
500         local float realplayerabstaincount;
501         realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
502
503         FOR_EACH_REALCLIENT(player)
504         {
505                 if(player.vote_vote == -1) {
506                         ++nocount;
507                 } else if(player.vote_vote == 1) {
508                         ++yescount;
509                 } else if(player.vote_vote == -2) {
510                         ++abstaincount;
511                 }
512                 ++playercount;
513                 //do the same for real players
514                 if(player.classname == "player") {
515                         if(player.vote_vote == -1) {
516                                 ++realplayernocount;
517                         } else if(player.vote_vote == 1) {
518                                 ++realplayeryescount;
519                         } else if(player.vote_vote == -2) {
520                                 ++realplayerabstaincount;
521                         }
522                         ++realplayercount;
523                 }
524         }
525
526         //in tournament mode, if we have at least one player then don't make the vote dependent on spectators (so specs don't have to press F1)
527         if(cvar("sv_vote_nospectators"))
528         if(realplayercount > 0) {
529                 yescount = realplayeryescount;
530                 nocount = realplayernocount;
531                 abstaincount = realplayerabstaincount;
532                 playercount = realplayercount;
533         }
534
535
536         if(votecalledmaster
537            && playercount == 1) {
538                 // if only one player is on the server becoming vote
539                 // master is not allowed.  This could be used for
540                 // trolling or worse. 'self' is the user who has
541                 // called the vote because this function is called
542                 // by SV_ParseClientCommand. Maybe all voting should
543                 // be disabled for a single player?
544                 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
545                 if(votecaller) {
546                         votecaller.vote_next = 0;
547                 }
548                 VoteReset();
549         } else {
550                 float votefactor;
551                 votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
552                 if(yescount > (playercount - abstaincount) * votefactor)
553                 {
554                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
555                         VoteAccept();
556                 }
557                 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
558                 {
559                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
560                         VoteReject();
561                 }
562                 else if(time > votefinished)
563                 {
564                         if(cvar("sv_vote_simple_majority"))
565                         {
566                                 string result;
567                                 if(yescount > (yescount + nocount) * votefactor)
568                                         result = "yes";
569                                 else if(yescount + nocount > 0)
570                                         result = "no";
571                                 else
572                                         result = "timeout";
573                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((yescount + nocount) * votefactor) + 1, result);
574                                 if(result == "yes")
575                                         VoteAccept();
576                                 else if(result == "no")
577                                         VoteReject();
578                                 else
579                                         VoteTimeout();
580                         }
581                         else
582                         {
583                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
584                                 VoteTimeout();
585                         }
586                 }
587         }
588
589         Nagger_VoteCountChanged();
590 }