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