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