1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
|
/***********************************************************************
Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
***********************************************************************/
#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif
#include <math.h>
/* utility */
#include "fcintl.h"
#include "log.h"
#include "support.h"
/* common */
#include "capability.h"
#include "connection.h"
#include "packets.h"
#include "player.h"
/* server */
#include "commands.h"
#include "console.h"
#include <hand_gen.h> /* <> so looked from the build directory first. */
#include "notify.h"
#include "settings.h"
#include "stdinhand.h"
#include "voting.h"
struct vote_list *vote_list = NULL;
int vote_number_sequence = 0;
/**********************************************************************//**
Helper function that returns the current number of eligible voters.
**************************************************************************/
int count_voters(const struct vote *pvote)
{
int num_voters = 0;
conn_list_iterate(game.est_connections, pconn) {
if (conn_can_vote(pconn, pvote)) {
num_voters++;
}
} conn_list_iterate_end;
return num_voters;
}
/**********************************************************************//**
Tell clients that a new vote has been created.
**************************************************************************/
static void lsend_vote_new(struct conn_list *dest, struct vote *pvote)
{
struct packet_vote_new packet;
struct connection *pconn;
if (pvote == NULL) {
return;
}
pconn = conn_by_number(pvote->caller_id);
if (pconn == NULL) {
return;
}
log_debug("lsend_vote_new %p (%d) --> %p", pvote, pvote->vote_no, dest);
packet.vote_no = pvote->vote_no;
sz_strlcpy(packet.user, pconn->username);
describe_vote(pvote, packet.desc, sizeof(packet.desc));
packet.percent_required = 100 * pvote->need_pc;
packet.flags = pvote->flags;
if (dest == NULL) {
dest = game.est_connections;
}
conn_list_iterate(dest, conn) {
if (!conn_can_see_vote(conn, pvote)) {
continue;
}
send_packet_vote_new(conn, &packet);
} conn_list_iterate_end;
}
/**********************************************************************//**
Send updated status information about the given vote.
**************************************************************************/
static void lsend_vote_update(struct conn_list *dest, struct vote *pvote,
int num_voters)
{
struct packet_vote_update packet;
struct connection *pconn;
if (pvote == NULL) {
return;
}
pconn = conn_by_number(pvote->caller_id);
if (pconn == NULL) {
return;
}
log_debug("lsend_vote_update %p (%d) --> %p", pvote, pvote->vote_no, dest);
packet.vote_no = pvote->vote_no;
packet.yes = pvote->yes;
packet.no = pvote->no;
packet.abstain = pvote->abstain;
packet.num_voters = num_voters;
if (dest == NULL) {
dest = game.est_connections;
}
conn_list_iterate(dest, aconn) {
if (!conn_can_see_vote(aconn, pvote)) {
continue;
}
send_packet_vote_update(aconn, &packet);
} conn_list_iterate_end;
}
/**********************************************************************//**
Tell clients that the given vote no longer exists.
**************************************************************************/
static void lsend_vote_remove(struct conn_list *dest, struct vote *pvote)
{
struct packet_vote_remove packet;
if (!pvote) {
return;
}
packet.vote_no = pvote->vote_no;
if (dest == NULL) {
dest = game.est_connections;
}
conn_list_iterate(dest, pconn) {
send_packet_vote_remove(pconn, &packet);
} conn_list_iterate_end;
}
/**********************************************************************//**
Tell clients that the given vote resolved.
**************************************************************************/
static void lsend_vote_resolve(struct conn_list *dest,
struct vote *pvote, bool passed)
{
struct packet_vote_resolve packet;
if (!pvote) {
return;
}
packet.vote_no = pvote->vote_no;
packet.passed = passed;
if (dest == NULL) {
dest = game.est_connections;
}
conn_list_iterate(dest, pconn) {
if (!conn_can_see_vote(pconn, pvote)) {
continue;
}
send_packet_vote_resolve(pconn, &packet);
} conn_list_iterate_end;
}
/**********************************************************************//**
Free all memory used by the vote structure.
**************************************************************************/
static void free_vote(struct vote *pvote)
{
if (!pvote) {
return;
}
vote_cast_list_iterate(pvote->votes_cast, pvc) {
free(pvc);
} vote_cast_list_iterate_end;
vote_cast_list_destroy(pvote->votes_cast);
free(pvote);
}
/**********************************************************************//**
Remove the given vote and send a vote_remove packet to clients.
**************************************************************************/
void remove_vote(struct vote *pvote)
{
if (!vote_list || !pvote) {
return;
}
vote_list_remove(vote_list, pvote);
lsend_vote_remove(NULL, pvote);
free_vote(pvote);
}
/**********************************************************************//**
Remove all votes. Sends vote_remove packets to clients.
**************************************************************************/
void clear_all_votes(void)
{
if (!vote_list) {
return;
}
vote_list_iterate(vote_list, pvote) {
lsend_vote_remove(NULL, pvote);
free_vote(pvote);
} vote_list_iterate_end;
vote_list_clear(vote_list);
}
/**********************************************************************//**
Returns TRUE if this vote is a "teamvote".
**************************************************************************/
bool vote_is_team_only(const struct vote *pvote)
{
return pvote && (pvote->flags & VCF_TEAMONLY);
}
/**********************************************************************//**
A user cannot vote if:
* is not connected
* access level < basic
* isn't a player
* the vote is a team vote and not on the caller's team
NB: If 'pvote' is NULL, then the team condition is not checked.
**************************************************************************/
bool conn_can_vote(const struct connection *pconn, const struct vote *pvote)
{
if (!pconn || !conn_controls_player(pconn)
|| conn_get_access(pconn) < ALLOW_BASIC) {
return FALSE;
}
if (vote_is_team_only(pvote)) {
const struct player *pplayer, *caller_plr;
pplayer = conn_get_player(pconn);
caller_plr = conn_get_player(vote_get_caller(pvote));
if (!pplayer || !caller_plr
|| !players_on_same_team(pplayer, caller_plr)) {
return FALSE;
}
}
return TRUE;
}
/**********************************************************************//**
Usually, all users can see, except in the team vote case.
**************************************************************************/
bool conn_can_see_vote(const struct connection *pconn,
const struct vote *pvote)
{
if (!pconn) {
return FALSE;
}
if (conn_is_global_observer(pconn)) {
/* All is visible for global observer. */
return TRUE;
}
if (vote_is_team_only(pvote)) {
const struct player *pplayer, *caller_plr;
pplayer = conn_get_player(pconn);
caller_plr = conn_get_player(vote_get_caller(pvote));
if (!pplayer || !caller_plr
|| !players_on_same_team(pplayer, caller_plr)) {
return FALSE;
}
}
return TRUE;
}
/**********************************************************************//**
Returns the vote with vote number 'vote_no', or NULL.
**************************************************************************/
struct vote *get_vote_by_no(int vote_no)
{
if (!vote_list) {
return NULL;
}
vote_list_iterate(vote_list, pvote) {
if (pvote->vote_no == vote_no) {
return pvote;
}
} vote_list_iterate_end;
return NULL;
}
/**********************************************************************//**
Returns the vote called by 'caller', or NULL if none exists.
**************************************************************************/
struct vote *get_vote_by_caller(const struct connection *caller)
{
if (caller == NULL || !vote_list) {
return NULL;
}
vote_list_iterate(vote_list, pvote) {
if (pvote->caller_id == caller->id) {
return pvote;
}
} vote_list_iterate_end;
return NULL;
}
/**********************************************************************//**
Create and return a newly allocated vote for the command with id
'command_id' and all arguments in the string 'allargs'.
**************************************************************************/
struct vote *vote_new(struct connection *caller,
const char *allargs,
int command_id)
{
struct vote *pvote;
const struct command *pcmd;
if (!conn_can_vote(caller, NULL)) {
return NULL;
}
/* Cancel previous vote */
remove_vote(get_vote_by_caller(caller));
/* Make a new vote */
pvote = fc_malloc(sizeof(struct vote));
pvote->caller_id = caller->id;
pvote->command_id = command_id;
pcmd = command_by_number(command_id);
sz_strlcpy(pvote->cmdline, command_name(pcmd));
if (allargs != NULL && allargs[0] != '\0') {
sz_strlcat(pvote->cmdline, " ");
sz_strlcat(pvote->cmdline, allargs);
}
pvote->turn_count = 0;
pvote->votes_cast = vote_cast_list_new();
pvote->vote_no = ++vote_number_sequence;
vote_list_append(vote_list, pvote);
pvote->flags = command_vote_flags(pcmd);
pvote->need_pc = (double) command_vote_percent(pcmd) / 100.0;
if (pvote->flags & VCF_NOPASSALONE) {
int num_voters = count_voters(pvote);
double min_pc = 1.0 / (double) num_voters;
if (num_voters > 1 && min_pc > pvote->need_pc) {
pvote->need_pc = MIN(0.5, 2.0 * min_pc);
}
}
lsend_vote_new(NULL, pvote);
return pvote;
}
/**********************************************************************//**
Return whether the vote would pass immediately when the caller will vote
for.
**************************************************************************/
bool vote_would_pass_immediately(const struct connection *caller,
int command_id)
{
struct vote virtual_vote;
const struct command *pcmd;
if (!conn_can_vote(caller, NULL)) {
return FALSE;
}
pcmd = command_by_number(command_id);
fc_assert(pcmd != NULL);
memset(&virtual_vote, 0, sizeof(virtual_vote));
virtual_vote.flags = command_vote_flags(pcmd);
if (virtual_vote.flags & VCF_NOPASSALONE) {
return FALSE;
}
virtual_vote.caller_id = caller->id;
return (((double) (command_vote_percent(pcmd)
* count_voters(&virtual_vote)) / 100.0) < 1.0);
}
/**********************************************************************//**
Check if we satisfy the criteria for resolving a vote, and resolve it
if these critera are indeed met. Updates yes and no variables in voting
struct as well.
**************************************************************************/
static void check_vote(struct vote *pvote)
{
int num_cast = 0, num_voters = 0;
bool resolve = FALSE, passed = FALSE;
struct connection *pconn = NULL;
double yes_pc = 0.0, no_pc = 0.0, rem_pc = 0.0, base = 0.0;
int flags;
double need_pc;
char cmdline[MAX_LEN_CONSOLE_LINE];
const double MY_EPSILON = 0.000001;
const char *title;
const struct player *callplr;
if (!pvote) {
return;
}
pvote->yes = 0;
pvote->no = 0;
pvote->abstain = 0;
num_voters = count_voters(pvote);
vote_cast_list_iterate(pvote->votes_cast, pvc) {
if (!(pconn = conn_by_number(pvc->conn_id))
|| !conn_can_vote(pconn, pvote)) {
continue;
}
num_cast++;
switch (pvc->vote_cast) {
case VOTE_YES:
pvote->yes++;
continue;
case VOTE_NO:
pvote->no++;
continue;
case VOTE_ABSTAIN:
pvote->abstain++;
continue;
case VOTE_NUM:
break;
}
log_error("Unknown vote cast variant: %d.", pvc->vote_cast);
pvote->abstain++;
} vote_cast_list_iterate_end;
flags = pvote->flags;
need_pc = pvote->need_pc;
/* Check if we should resolve the vote. */
if (num_voters > 0) {
/* Players that abstain essentially remove themselves from
* the voting pool. */
base = num_voters - pvote->abstain;
if (base > MY_EPSILON) {
yes_pc = (double) pvote->yes / base;
no_pc = (double) pvote->no / base;
/* The fraction of people who have not voted at all. */
rem_pc = (double) (num_voters - num_cast) / base;
}
if (flags & VCF_NODISSENT && no_pc > MY_EPSILON) {
resolve = TRUE;
}
if (!resolve) {
resolve = (/* We have enough yes votes. */
(yes_pc - need_pc > MY_EPSILON)
/* We have too many no votes. */
|| (no_pc - 1.0 + need_pc > MY_EPSILON
|| fabs(no_pc - 1.0 + need_pc) < MY_EPSILON)
/* We can't get enough no votes. */
|| (no_pc + rem_pc - 1.0 + need_pc < -MY_EPSILON)
/* We can't get enough yes votes. */
|| (yes_pc + rem_pc - need_pc < -MY_EPSILON
|| fabs(yes_pc + rem_pc - need_pc) < MY_EPSILON));
}
/* Resolve if everyone voted already. */
if (!resolve && fabs(rem_pc) < MY_EPSILON) {
resolve = TRUE;
}
/* Resolve this vote if it has been around long enough. */
if (!resolve && pvote->turn_count > 1) {
resolve = TRUE;
}
/* Resolve this vote if everyone tries to abstain. */
if (!resolve && fabs(base) < MY_EPSILON) {
resolve = TRUE;
}
}
log_debug("check_vote flags=%d need_pc=%0.2f yes_pc=%0.2f "
"no_pc=%0.2f rem_pc=%0.2f base=%0.2f resolve=%d",
flags, need_pc, yes_pc, no_pc, rem_pc, base, resolve);
lsend_vote_update(NULL, pvote, num_voters);
if (!resolve) {
return;
}
passed = yes_pc - need_pc > MY_EPSILON;
if (passed && flags & VCF_NODISSENT) {
passed = fabs(no_pc) < MY_EPSILON;
}
if (vote_is_team_only(pvote)) {
const struct connection *caller;
/* TRANS: "Vote" as a process. Used as part of a sentence. */
title = _("Teamvote");
caller = vote_get_caller(pvote);
callplr = conn_get_player(caller);
} else {
/* TRANS: "Vote" as a process. Used as part of a sentence. */
title = _("Vote");
callplr = NULL;
}
if (passed) {
notify_team(callplr, NULL, E_VOTE_RESOLVED, ftc_vote_passed,
/* TRANS: "[Vote|Teamvote] 3 \"proposed change\" is ..." */
_("%s %d \"%s\" is passed %d to %d with "
"%d abstentions and %d who did not vote."),
title, pvote->vote_no, pvote->cmdline, pvote->yes,
pvote->no, pvote->abstain, num_voters - num_cast);
} else {
notify_team(callplr, NULL, E_VOTE_RESOLVED, ftc_vote_failed,
/* TRANS: "[Vote|Teamvote] 3 \"proposed change\" failed ..." */
_("%s %d \"%s\" failed with %d against, %d for, "
"%d abstentions and %d who did not vote."),
title, pvote->vote_no, pvote->cmdline, pvote->no,
pvote->yes, pvote->abstain, num_voters - num_cast);
}
lsend_vote_resolve(NULL, pvote, passed);
vote_cast_list_iterate(pvote->votes_cast, pvc) {
if (!(pconn = conn_by_number(pvc->conn_id))) {
log_error("Got a vote from a lost connection");
continue;
} else if (!conn_can_vote(pconn, pvote)) {
log_error("Got a vote from a non-voting connection");
continue;
}
switch (pvc->vote_cast) {
case VOTE_YES:
notify_team(callplr, NULL, E_VOTE_RESOLVED, ftc_vote_yes,
_("%s %d: %s voted yes."),
title, pvote->vote_no, pconn->username);
break;
case VOTE_NO:
notify_team(callplr, NULL, E_VOTE_RESOLVED, ftc_vote_no,
_("%s %d: %s voted no."),
title, pvote->vote_no, pconn->username);
break;
case VOTE_ABSTAIN:
notify_team(callplr, NULL, E_VOTE_RESOLVED, ftc_vote_abstain,
_("%s %d: %s chose to abstain."),
title, pvote->vote_no, pconn->username);
break;
default:
break;
}
} vote_cast_list_iterate_end;
/* Remove the vote before executing the command because it's the
* cause of many crashes due to the /cut command:
* - If the caller is the target.
* - If the target votes on this vote. */
sz_strlcpy(cmdline, pvote->cmdline);
remove_vote(pvote);
if (passed) {
handle_stdin_input(NULL, cmdline);
}
}
/**********************************************************************//**
Find the vote cast for the user id conn_id in a vote.
**************************************************************************/
static struct vote_cast *vote_cast_find(struct vote *pvote, int conn_id)
{
if (!pvote) {
return NULL;
}
vote_cast_list_iterate(pvote->votes_cast, pvc) {
if (pvc->conn_id == conn_id) {
return pvc;
}
} vote_cast_list_iterate_end;
return NULL;
}
/**********************************************************************//**
Return a new vote cast.
**************************************************************************/
static struct vote_cast *vote_cast_new(struct vote *pvote)
{
struct vote_cast *pvc;
if (!pvote) {
return NULL;
}
pvc = fc_malloc(sizeof(struct vote_cast));
pvc->conn_id = -1;
pvc->vote_cast = VOTE_ABSTAIN;
vote_cast_list_append(pvote->votes_cast, pvc);
return pvc;
}
/**********************************************************************//**
Remove a vote cast. This unlinks it and frees its memory.
**************************************************************************/
static void remove_vote_cast(struct vote *pvote, struct vote_cast *pvc)
{
if (!pvote || !pvc) {
return;
}
vote_cast_list_remove(pvote->votes_cast, pvc);
free(pvc);
check_vote(pvote); /* Maybe can pass */
}
/**********************************************************************//**
Make the given connection vote 'type' on 'pvote', and check the vote.
**************************************************************************/
void connection_vote(struct connection *pconn,
struct vote *pvote,
enum vote_type type)
{
struct vote_cast *pvc;
if (!conn_can_vote(pconn, pvote)) {
return;
}
/* Try to find a previous vote */
if ((pvc = vote_cast_find(pvote, pconn->id))) {
pvc->vote_cast = type;
} else if ((pvc = vote_cast_new(pvote))) {
pvc->vote_cast = type;
pvc->conn_id = pconn->id;
} else {
/* Must never happen */
log_error("Failed to create a vote cast for connection %s.",
pconn->username);
return;
}
check_vote(pvote);
}
/**********************************************************************//**
Cancel the votes of a lost or a detached connection.
**************************************************************************/
void cancel_connection_votes(struct connection *pconn)
{
if (!pconn || !vote_list) {
return;
}
remove_vote(get_vote_by_caller(pconn));
vote_list_iterate(vote_list, pvote) {
remove_vote_cast(pvote, vote_cast_find(pvote, pconn->id));
} vote_list_iterate_end;
}
/**********************************************************************//**
Initialize data structures used by this module.
**************************************************************************/
void voting_init(void)
{
if (!vote_list) {
vote_list = vote_list_new();
vote_number_sequence = 0;
}
}
/**********************************************************************//**
Check running votes. This should be called every turn.
**************************************************************************/
void voting_turn(void)
{
if (!vote_list) {
log_error("voting_turn() called before voting_init()");
return;
}
vote_list_iterate(vote_list, pvote) {
pvote->turn_count++;
check_vote(pvote);
} vote_list_iterate_end;
}
/**********************************************************************//**
Free all memory used by this module.
**************************************************************************/
void voting_free(void)
{
clear_all_votes();
if (vote_list) {
vote_list_destroy(vote_list);
vote_list = NULL;
}
}
/**********************************************************************//**
Fills the supplied buffer with a string describing the given vote. This
includes the vote command line, the percent required to pass, and any
special conditions.
**************************************************************************/
int describe_vote(struct vote *pvote, char *buf, int buflen)
{
int ret = 0;
/* NB We don't handle votes with multiple flags here. */
if (pvote->flags & VCF_NODISSENT) {
ret = fc_snprintf(buf, buflen,
/* TRANS: Describing a new vote that can only pass
* if there are no dissenting votes. */
_("%s (needs %0.0f%% and no dissent)."),
pvote->cmdline, MIN(100.0, pvote->need_pc * 100.0 + 1));
} else {
ret = fc_snprintf(buf, buflen,
/* TRANS: Describing a new vote that can pass only if the
* given percentage of players votes 'yes'. */
_("%s (needs %0.0f%% in favor)."),
pvote->cmdline, MIN(100.0, pvote->need_pc * 100.0 + 1));
}
return ret;
}
/**********************************************************************//**
Handle a vote submit packet sent from a client. This is basically just
a Wrapper around connection_vote().
**************************************************************************/
void handle_vote_submit(struct connection *pconn, int vote_no, int value)
{
struct vote *pvote;
enum vote_type type;
log_debug("Got vote submit (%d %d) from %s.",
vote_no, value, conn_description(pconn));
pvote = get_vote_by_no(vote_no);
if (pvote == NULL) {
/* The client is out of synchronization: this vote is probably just
* resolved or cancelled. Not an error, let's just ignore the packet. */
log_verbose("Submit request for unknown vote_no %d from %s ignored.",
vote_no, conn_description(pconn));
return;
}
if (value == 1) {
type = VOTE_YES;
} else if (value == -1) {
type = VOTE_NO;
} else if (value == 0) {
type = VOTE_ABSTAIN;
} else {
log_error("Invalid packet data for submit of vote %d "
"from %s ignored.", vote_no, conn_description(pconn));
return;
}
connection_vote(pconn, pvote, type);
}
/**********************************************************************//**
Sends a packet_vote_new to pconn for every currently running votes.
**************************************************************************/
void send_running_votes(struct connection *pconn, bool only_team_votes)
{
if (NULL == vote_list
|| vote_list_size(vote_list) < 1
|| NULL == pconn
|| (only_team_votes && NULL == conn_get_player(pconn))) {
return;
}
log_debug("Sending %s running votes to %s.",
only_team_votes ? "team" : "all", conn_description(pconn));
connection_do_buffer(pconn);
vote_list_iterate(vote_list, pvote) {
if (vote_is_team_only(pvote)) {
if (conn_can_see_vote(pconn, pvote)) {
lsend_vote_new(pconn->self, pvote);
lsend_vote_update(pconn->self, pvote, count_voters(pvote));
}
} else if (!only_team_votes) {
lsend_vote_new(pconn->self, pvote);
lsend_vote_update(pconn->self, pvote, count_voters(pvote));
}
} vote_list_iterate_end;
connection_do_unbuffer(pconn);
}
/**********************************************************************//**
Sends a packet_vote_remove to pconn for every currently running team vote
'pconn' can see.
**************************************************************************/
void send_remove_team_votes(struct connection *pconn)
{
if (NULL == vote_list
|| vote_list_size(vote_list) < 1
|| NULL == pconn
|| NULL == conn_get_player(pconn)) {
return;
}
log_debug("Sending remove info of the team votes to %s.",
conn_description(pconn));
connection_do_buffer(pconn);
vote_list_iterate(vote_list, pvote) {
if (vote_is_team_only(pvote) && conn_can_see_vote(pconn, pvote)) {
lsend_vote_remove(pconn->self, pvote);
}
} vote_list_iterate_end;
connection_do_unbuffer(pconn);
}
/**********************************************************************//**
Sends a packet_vote_update to every conn in dest. If dest is NULL, then
sends to all established connections.
**************************************************************************/
void send_updated_vote_totals(struct conn_list *dest)
{
int num_voters;
if (vote_list == NULL || vote_list_size(vote_list) <= 0) {
return;
}
log_debug("Sending updated vote totals to conn_list %p", dest);
if (dest == NULL) {
dest = game.est_connections;
}
conn_list_do_buffer(dest);
vote_list_iterate(vote_list, pvote) {
num_voters = count_voters(pvote);
lsend_vote_update(dest, pvote, num_voters);
} vote_list_iterate_end;
conn_list_do_unbuffer(dest);
}
/**********************************************************************//**
Returns the connection that called this vote.
**************************************************************************/
const struct connection *vote_get_caller(const struct vote *pvote)
{
return conn_by_number(pvote->caller_id);
}
|