]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/vote.qc
Merge remote branch 'origin/terencehill/misc_bugfixes'
[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, " ", cvar_string("g_ban_default_bantime"), " ", cvar_string("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         WriteShort(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         float argc;
133         argc = tokenize_console(s);
134         if(argv(0) == "help") {
135                 print_to(e, "  vote COMMANDS ARGUMENTS. See 'vhelp' for more info.");
136                 return TRUE;
137         } else if(argv(0) == "vote") {
138                 if(argv(1) == "") {
139                         print_to(e, "^1You have to supply a vote command. See 'vhelp' for more info.");
140                 } else if(argv(1) == "help") {
141                         VoteHelp(e);
142                 } else if(argv(1) == "status") {
143                         if(votecalled) {
144                                 print_to(e, strcat("^7Vote for ", votecalledvote_display, "^7 called by ^7", VoteNetname(votecaller), "^7."));
145                         } else {
146                                 print_to(e, "^1No vote called.");
147                         }
148                 } else if(argv(1) == "call") {
149                         if(!e || cvar("sv_vote_call")) {
150                                 if(cvar("sv_vote_nospectators") && e && e.classname != "player") {
151                                         print_to(e, "^1Error: Only players can call a vote."); // TODO invent a cvar name for allowing votes by spectators during warmup anyway
152                                 }
153                                 else if(timeoutStatus) { //don't allow a vote call during a timeout
154                                         print_to(e, "^1Error: You can not call a vote while a timeout is active.");
155                                 }
156                                 else if(votecalled) {
157                                         print_to(e, "^1There is already a vote called.");
158                                 } else {
159                                         local string vote;
160                                         vote = VoteParse(s, argc);
161                                         if(vote == "") {
162                                                 print_to(e, "^1Your vote is empty. See 'vhelp' for more info.");
163                                         } else if(e
164                                                 && time < e.vote_next) {
165                                                         print_to(e, strcat("^1You have to wait ^2", ftos(ceil(e.vote_next - time)), "^1 seconds before you can again call a vote."));
166                                         } else if(VoteCheckNasty(vote)) {
167                                                 print_to(e, "Syntax error in command. See 'vhelp' for more info.");
168                                         } else if(cvar("timelimit") == 0 && (vote == "extendmatchtime" || vote == "reducematchtime")) {
169                                                 print_to(e, "^1Match time can not be reduced or extended as it is infinite. See 'vhelp' for more info.");
170                                         } else if(RemapVote(vote, "vcall", e)) {
171                                                 votecalledvote = strzone(RemapVote_vote);
172                                                 votecalledvote_display = strzone(RemapVote_display);
173                                                 votecalled = TRUE;
174                                                 votecalledmaster = FALSE;
175                                                 votefinished = time + cvar("sv_vote_timeout");
176                                                 votecaller = e; // remember who called the vote
177                                                 if(e) {
178                                                         e.vote_vote = 1; // of course you vote yes
179                                                         e.vote_next = time + cvar("sv_vote_wait");
180                                                 }
181                                                 bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote for ", votecalledvote_display, "\n");
182                                                 if(cvar("sv_eventlog"))
183                                                         GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
184                                                 VoteCount(); // needed if you are the only one
185                                                 Nagger_VoteChanged();
186                                                 msg_entity = e;
187                                                 VoteDialog_UpdateHighlight(1);
188                                         } else {
189                                                 print_to(e, "^1This vote is not ok. See 'vhelp' for more info.");
190                                         }
191                                 }
192                         } else {
193                                 print_to(e, "^1Vote calling is NOT allowed.");
194                         }
195                 } else if(argv(1) == "stop") {
196                         if(!votecalled) {
197                                 print_to(e, "^1No vote called.");
198                         } else if(e == votecaller) { // the votecaller can stop a vote
199                                 VoteDialog_Reset();
200                                 VoteStop(e);
201                         } else if(!e) { // server admin / console can too
202                                 VoteDialog_Reset();
203                                 VoteStop(e);
204                         } else if(e.vote_master) { // masters can too
205                                 VoteDialog_Reset();
206                                 VoteStop(e);
207                         } else {
208                                 print_to(e, "^1You are not allowed to stop that vote.");
209                         }
210                 } else if(argv(1) == "master") {
211                         if(cvar("sv_vote_master")) {
212                                 if(votecalled) {
213                                         print_to(e, "^1There is already a vote called.");
214                                 } else {
215                                         votecalled = TRUE;
216                                         votecalledmaster = TRUE;
217                                         votecalledvote = strzone("XXX");
218                                         votecalledvote_display = strzone("^3master");
219                                         votefinished = time + cvar("sv_vote_timeout");
220                                         votecaller = e; // remember who called the vote
221                                         if(e) {
222                                                 e.vote_vote = 1; // of course you vote yes
223                                                 e.vote_next = time + cvar("sv_vote_wait");
224                                         }
225                                         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2 calls a vote to become ^3master^2.\n");
226                                         if(cvar("sv_eventlog"))
227                                                 GameLogEcho(strcat(":vote:vcall:", ftos(votecaller.playerid), ":", votecalledvote_display));
228                                         VoteCount(); // needed if you are the only one
229                                         Nagger_VoteChanged();
230                                 }
231                         } else {
232                                 print_to(e, "^1Vote to become master is NOT allowed.");
233                         }
234                 } else if(argv(1) == "do") {
235                         if(!e || e.vote_master) {
236                                 local string dovote;
237                                 dovote = VoteParse(s, argc);
238                                 if(dovote == "") {
239                                         print_to(e, "^1Your command was empty. See 'vhelp' for more info.");
240                                 } else if(VoteCheckNasty(dovote)) {
241                                         print_to(e, "Syntax error in command. See 'vhelp' for more info.");
242                                 } else if(RemapVote(dovote, "vdo", e)) { // strcat seems to be necessary
243                                         bprint("\{1}^2* ^3", VoteNetname(e), "^2 used their ^3master^2 status to do \"^2", RemapVote_display, "^2\".\n");
244                                         if(cvar("sv_eventlog"))
245                                                 GameLogEcho(strcat(":vote:vdo:", ftos(e.playerid), ":", RemapVote_display));
246                                         localcmd(strcat(RemapVote_vote, "\n"));
247                                 } else {
248                                         print_to(e, "^1This command is not ok. See 'vhelp' for more info.");
249                                 }
250                         } else {
251                                 print_to(e, "^1You are NOT a master.  You might need to login or vote to become master first. See 'vhelp' for more info.");
252                         }
253                 } else if(argv(1) == "login") {
254                         local string masterpwd;
255                         masterpwd = cvar_string("sv_vote_master_password");
256                         if(masterpwd != "") {
257                                 local float granted;
258                                 granted = (masterpwd == argv(2));
259                                 if (e)
260                                         e.vote_master = granted;
261                                 if(granted) {
262                                         print("Accepted master login from ", VoteNetname(e), "\n");
263                                         bprint("\{1}^2* ^3", VoteNetname(e), "^2 logged in as ^3master^2\n");
264                                         if(cvar("sv_eventlog"))
265                                                 GameLogEcho(strcat(":vote:vlogin:", ftos(e.playerid)));
266                                 }
267                                 else
268                                         print("REJECTED master login from ", VoteNetname(e), "\n");
269                         }
270                         else
271                                 print_to(e, "^1Login to become master is NOT allowed.");
272                 } else if(argv(1) == "yes") {
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                                 msg_entity = e;
280                                 VoteDialog_UpdateHighlight(1);
281                                 print_to(e, "^1You accepted the vote.");
282                                 e.vote_vote = 1;
283                                 centerprint_expire(e, CENTERPRIO_VOTE);
284                                 if(!cvar("sv_vote_singlecount")) {
285                                         VoteCount();
286                                 }
287                         } else {
288                                 print_to(e, "^1You have already voted.");
289                         }
290                 } else if(argv(1) == "no") {
291                         if(!votecalled) {
292                                 print_to(e, "^1No vote called.");
293                         } else if (!e) {
294                                 print_to(e, "^1You can't vote from the server console.");
295                         } else if(e.vote_vote == 0
296                                   || cvar("sv_vote_change")) {
297                                 msg_entity = e;
298                                 VoteDialog_UpdateHighlight(2);
299                                 print_to(e, "^1You rejected the vote.");
300                                 e.vote_vote = -1;
301                                 centerprint_expire(e, CENTERPRIO_VOTE);
302                                 if(!cvar("sv_vote_singlecount")) {
303                                         VoteCount();
304                                 }
305                         } else {
306                                 print_to(e, "^1You have already voted.");
307                         }
308                 } else if(argv(1) == "abstain" || argv(1) == "dontcare") {
309                         if(!votecalled) {
310                                 print_to(e, "^1No vote called.");
311                         } else if (!e) {
312                                 print_to(e, "^1You can't vote from the server console.");
313                         } else if(e.vote_vote == 0
314                                   || cvar("sv_vote_change")) {
315                                 msg_entity = e;
316                                 VoteDialog_UpdateHighlight(3);
317                                 print_to(e, "^1You abstained from your vote.");
318                                 e.vote_vote = -2;
319                                 centerprint_expire(e, CENTERPRIO_VOTE);
320                                 if(!cvar("sv_vote_singlecount")) {
321                                         VoteCount();
322                                 }
323                         } else {
324                                 print_to(e, "^1You have already voted.");
325                         }
326                 } else {
327                         // ignore this?
328                         print_to(e, "^1Unknown vote command.");
329                 }
330                 return TRUE;
331         }
332         return FALSE;
333 }
334
335 void VoteHelp(entity e) {
336         local string vmasterdis;
337         if(!cvar("sv_vote_master")) {
338                 vmasterdis = " ^1(disabled)";
339         }
340
341         local string vlogindis;
342         if("" == cvar_string("sv_vote_master_password")) {
343                 vlogindis = " ^1(disabled)";
344         }
345
346         local string vcalldis;
347         if(!cvar("sv_vote_call")) {
348                 vcalldis = " ^1(disabled)";
349         }
350
351         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\".");
352         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\".");
353         print_to(e, "^7\"^2help^7\" shows this info.");
354         print_to(e, "^7\"^2status^7\" shows if there is a vote called and who called it.");
355         print_to(e, strcat("^7\"^2call^7\" is used to call a vote. See the list of allowed commands.", vcalldis, "^7"));
356         print_to(e, "^7\"^2stop^7\" can be used by the vote caller or an admin to stop a vote and maybe correct it.");
357         print_to(e, strcat("^7\"^2master^7\" call a vote to become master who can execute commands without a vote", vmasterdis, "^7"));
358         print_to(e, strcat("^7\"^2login^7\" login to become master who can execute commands without a vote.", vlogindis, "^7"));
359         print_to(e, "^7\"^2do^7\" executes a command if you are a master. See the list of allowed commands.");
360         print_to(e, "^7\"^2yes^7\", \"^2no^7\", \"^2abstain^7\" and \"^2dontcare^7\" to make your vote.");
361         print_to(e, "^7If enough of the players vote yes the vote is accepted.");
362         print_to(e, "^7If enough of the players vote no the vote is rejected.");
363         print_to(e, strcat("^7If neither the vote will timeout after ", cvar_string("sv_vote_timeout"), "^7 seconds."));
364         print_to(e, "^7You can call a vote for or execute these commands:");
365         print_to(e, strcat("^3", cvar_string("sv_vote_commands"), "^7 and maybe further ^3arguments^7"));
366 }
367
368 string VoteNetname(entity e)
369 {
370         if(e) {
371                 return e.netname;
372         } else {
373                 if(cvar_string("sv_adminnick") != "") {
374                         return cvar_string("sv_adminnick");
375                 } else {
376                         return cvar_string("hostname");
377                 }
378         }
379 }
380
381 string ValidateMap(string m, entity e)
382 {
383         m = MapInfo_FixName(m);
384         if(!m)
385         {
386                 print_to(e, "This map is not available on this server.");
387                 return string_null;
388         }
389         if(!cvar("sv_vote_override_mostrecent"))
390                 if(Map_IsRecent(m))
391                 {
392                         print_to(e, "This server does not allow for recent maps to be played again. Please be patient for some rounds.");
393                         return string_null;
394                 }
395         if(!MapInfo_CheckMap(m))
396         {
397                 print_to(e, strcat("^1Invalid mapname, \"^3", m, "^1\" does not support the current game mode."));
398                 return string_null;
399         }
400
401         return m;
402 }
403
404
405 void VoteThink() {
406         if(votefinished > 0) // a vote was called
407         if(time > votefinished) // time is up
408         {
409                 VoteCount();
410         }
411 }
412
413 string VoteParse(string all, float argc) {
414         if(argc < 3)
415                 return "";
416         return substring(all, argv_start_index(2), argv_end_index(-1) - argv_start_index(2));
417 }
418
419 float VoteCommandInList(string votecommand, string list)
420 {
421         string l;
422         l = strcat(" ", list, " ");
423         
424         if(strstrofs(l, strcat(" ", votecommand, " "), 0) >= 0)
425                 return TRUE;
426         
427         // if gotomap is allowed, chmap is too, and vice versa
428         if(votecommand == "gotomap")
429                 if(strstrofs(l, " chmap ", 0) >= 0)
430                         return TRUE;
431         if(votecommand == "chmap")
432                 if(strstrofs(l, " gotomap ", 0) >= 0)
433                         return TRUE;
434         
435         return FALSE;
436 }
437
438 float VoteAllowed(string votecommand, string cmd) {
439         if(VoteCommandInList(votecommand, cvar_string("sv_vote_commands")))
440                 return TRUE;
441
442         if(cmd == "vdo")
443         {
444                 if(VoteCommandInList(votecommand, cvar_string("sv_vote_master_commands")))
445                         return TRUE;
446         }
447         else
448         {
449                 if(VoteCommandInList(votecommand, cvar_string("sv_vote_only_commands")))
450                         return TRUE;
451         }
452
453         return FALSE;
454 }
455
456 void VoteReset() {
457         local entity player;
458
459         FOR_EACH_CLIENT(player)
460         {
461                 player.vote_vote = 0;
462                 centerprint_expire(player, CENTERPRIO_VOTE);
463         }
464
465         if(votecalled)
466         {
467                 strunzone(votecalledvote);
468                 strunzone(votecalledvote_display);
469         }
470
471         votecalled = FALSE;
472         votecalledmaster = FALSE;
473         votefinished = 0;
474 }
475
476 void VoteAccept() {
477         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ^1", votecalledvote_display, "^2 was accepted\n");
478         if(votecalledmaster)
479         {
480                 if(votecaller) {
481                         votecaller.vote_master = 1;
482                 }
483         } else {
484                 localcmd(strcat(votecalledvote, "\n"));
485         }
486         if(votecaller) {
487                 votecaller.vote_next = 0; // people like your votes,
488                                           // no wait for next vote
489         }
490         VoteReset();
491 }
492
493 void VoteReject() {
494         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 was rejected\n");
495         VoteReset();
496 }
497
498 void VoteTimeout() {
499         bprint("\{1}^2* ^3", VoteNetname(votecaller), "^2's vote for ", votecalledvote_display, "^2 timed out\n");
500         VoteReset();
501 }
502
503 void VoteStop(entity stopper) {
504         bprint("\{1}^2* ^3", VoteNetname(stopper), "^2 stopped ^3", VoteNetname(votecaller), "^2's vote\n");
505         if(cvar("sv_eventlog"))
506                 GameLogEcho(strcat(":vote:vstop:", ftos(stopper.playerid)));
507         if(stopper == votecaller) {
508                 // no wait for next vote so you can correct your vote
509                 if(votecaller) {
510                         votecaller.vote_next = time + cvar("sv_vote_stop");
511                 }
512         }
513         VoteReset();
514 }
515
516 void VoteSpam(float yescount, float nocount, float abstaincount, float notvoters, float mincount, string result)
517 {
518         string s;
519         if(mincount >= 0)
520         {
521                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
522                 s = strcat(s, ftos(nocount), "^2 (^1");
523                 s = strcat(s, ftos(mincount), "^2 needed), ^1");
524                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
525                 s = strcat(s, ftos(notvoters), "^2 didn't vote\n");
526         }
527         else
528         {
529                 s = strcat("\{1}^2* vote results: ^1", ftos(yescount), "^2:^1");
530                 s = strcat(s, ftos(nocount), "^2, ^1");
531                 s = strcat(s, ftos(abstaincount), "^2 didn't care, ^1");
532                 s = strcat(s, ftos(notvoters), "^2 didn't have to vote\n");
533         }
534         bprint(s);
535         if(cvar("sv_eventlog"))
536         {
537                 s = strcat(":vote:v", result, ":", ftos(yescount));
538                 s = strcat(s, ":", ftos(nocount));
539                 s = strcat(s, ":", ftos(abstaincount));
540                 s = strcat(s, ":", ftos(notvoters));
541                 s = strcat(s, ":", ftos(mincount));
542                 GameLogEcho(s);
543         }
544 }
545
546 void VoteDialog_Update(float msg, float vyes, float vno, float needed) {
547         WriteByte(msg, SVC_TEMPENTITY);
548         WriteByte(msg, TE_CSQC_VOTE);
549         WriteByte(msg, 0);
550         WriteShort(msg, vyes);
551         WriteShort(msg, vno);
552         WriteShort(msg, needed);
553 }
554
555 void VoteCount() {
556         local float playercount;
557         playercount = 0;
558         local float yescount;
559         yescount = 0;
560         local float nocount;
561         nocount = 0;
562         local float abstaincount;
563         abstaincount = 0;
564         local entity player;
565         //same for real players
566         local float realplayercount;
567         local float realplayeryescount;
568         local float realplayernocount;
569         local float realplayerabstaincount;
570         realplayercount = realplayernocount = realplayerabstaincount = realplayeryescount = 0;
571
572         FOR_EACH_REALCLIENT(player)
573         {
574                 if(player.vote_vote == -1) {
575                         ++nocount;
576                 } else if(player.vote_vote == 1) {
577                         ++yescount;
578                 } else if(player.vote_vote == -2) {
579                         ++abstaincount;
580                 }
581                 ++playercount;
582                 //do the same for real players
583                 if(player.classname == "player") {
584                         if(player.vote_vote == -1) {
585                                 ++realplayernocount;
586                         } else if(player.vote_vote == 1) {
587                                 ++realplayeryescount;
588                         } else if(player.vote_vote == -2) {
589                                 ++realplayerabstaincount;
590                         }
591                         ++realplayercount;
592                 }
593         }
594
595         //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)
596         if(cvar("sv_vote_nospectators"))
597         if(realplayercount > 0) {
598                 yescount = realplayeryescount;
599                 nocount = realplayernocount;
600                 abstaincount = realplayerabstaincount;
601                 playercount = realplayercount;
602         }
603
604         float votefactor, simplevotefactor;
605         votefactor = bound(0.5, cvar("sv_vote_majority_factor"), 0.999);
606         simplevotefactor = cvar("sv_vote_simple_majority_factor");
607         float needed;
608         needed = floor((playercount - abstaincount) * max(votefactor, simplevotefactor)) + 1;
609         VoteDialog_Update(MSG_ALL, yescount, nocount, needed);
610
611         if(votecalledmaster
612            && playercount == 1) {
613                 // if only one player is on the server becoming vote
614                 // master is not allowed.  This could be used for
615                 // trolling or worse. 'self' is the user who has
616                 // called the vote because this function is called
617                 // by SV_ParseClientCommand. Maybe all voting should
618                 // be disabled for a single player?
619                 print_to(votecaller, "^1You are the only player on this server so you can not become vote master.");
620                 if(votecaller) {
621                         votecaller.vote_next = 0;
622                 }
623                 VoteReset();
624         } else {
625                 if(yescount > (playercount - abstaincount) * votefactor)
626                 {
627                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "yes");
628                         VoteAccept();
629                         VoteDialog_Reset();
630                 }
631                 else if(nocount >= (playercount - abstaincount) * (1 - votefactor)) // that means, yescount cannot reach minyes any more
632                 {
633                         VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, -1, "no");
634                         VoteReject();
635                         VoteDialog_Reset();
636                 }
637                 else if(time > votefinished)
638                 {
639                         if(simplevotefactor)
640                         {
641                                 string result;
642                                 simplevotefactor = bound(votefactor, simplevotefactor, 0.999);
643                                 if(yescount > (yescount + nocount) * simplevotefactor)
644                                         result = "yes";
645                                 else if(yescount + nocount > 0)
646                                         result = "no";
647                                 else
648                                         result = "timeout";
649                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor(min((playercount - abstaincount) * votefactor, (yescount + nocount) * simplevotefactor)) + 1, result);
650                                 if(result == "yes")
651                                         VoteAccept();
652                                 else if(result == "no")
653                                         VoteReject();
654                                 else
655                                         VoteTimeout();
656                         }
657                         else
658                         {
659                                 VoteSpam(yescount, nocount, abstaincount, playercount - yescount - nocount - abstaincount, floor((playercount - abstaincount) * votefactor) + 1, "timeout");
660                                 VoteTimeout();
661                         }
662                 VoteDialog_Reset();
663                 }
664         }
665 }