Freeciv21
Develop your civilization from humble roots to a global empire
voting.cpp File Reference
#include <cmath>
#include "fcintl.h"
#include "log.h"
#include "support.h"
#include "connection.h"
#include "packets.h"
#include "player.h"
#include "commands.h"
#include "console.h"
#include "hand_gen.h"
#include "notify.h"
#include "settings.h"
#include "stdinhand.h"
#include "voting.h"
+ Include dependency graph for voting.cpp:

Go to the source code of this file.

Functions

int count_voters (const struct vote *pvote)
 Helper function that returns the current number of eligible voters. More...
 
static void lsend_vote_new (struct conn_list *dest, struct vote *pvote)
 Tell clients that a new vote has been created. More...
 
static void lsend_vote_update (struct conn_list *dest, struct vote *pvote, int num_voters)
 Send updated status information about the given vote. More...
 
static void lsend_vote_remove (struct conn_list *dest, struct vote *pvote)
 Tell clients that the given vote no longer exists. More...
 
static void lsend_vote_resolve (struct conn_list *dest, struct vote *pvote, bool passed)
 Tell clients that the given vote resolved. More...
 
static void free_vote (struct vote *pvote)
 Free all memory used by the vote structure. More...
 
void remove_vote (struct vote *pvote)
 Remove the given vote and send a vote_remove packet to clients. More...
 
void clear_all_votes ()
 Remove all votes. More...
 
bool vote_is_team_only (const struct vote *pvote)
 Returns TRUE if this vote is a "teamvote". More...
 
bool conn_can_vote (const struct connection *pconn, const struct vote *pvote)
 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 nullptr, then the team condition is not checked. More...
 
bool conn_can_see_vote (const struct connection *pconn, const struct vote *pvote)
 Usually, all users can see, except in the team vote case. More...
 
struct voteget_vote_by_no (int vote_no)
 Returns the vote with vote number 'vote_no', or nullptr. More...
 
struct voteget_vote_by_caller (const struct connection *caller)
 Returns the vote called by 'caller', or nullptr if none exists. More...
 
struct votevote_new (struct connection *caller, const char *allargs, int command_id)
 Create and return a newly allocated vote for the command with id 'command_id' and all arguments in the string 'allargs'. More...
 
bool vote_would_pass_immediately (const struct connection *caller, int command_id)
 Return whether the vote would pass immediately when the caller will vote for. More...
 
static void check_vote (struct vote *pvote)
 Check if we satisfy the criteria for resolving a vote, and resolve it if these critera are indeed met. More...
 
static struct vote_castvote_cast_find (struct vote *pvote, int conn_id)
 Find the vote cast for the user id conn_id in a vote. More...
 
static struct vote_castvote_cast_new (struct vote *pvote)
 Return a new vote cast. More...
 
static void remove_vote_cast (struct vote *pvote, struct vote_cast *pvc)
 Remove a vote cast. More...
 
void connection_vote (struct connection *pconn, struct vote *pvote, enum vote_type type)
 Make the given connection vote 'type' on 'pvote', and check the vote. More...
 
void cancel_connection_votes (struct connection *pconn)
 Cancel the votes of a lost or a detached connection. More...
 
void voting_init ()
 Initialize data structures used by this module. More...
 
void voting_turn ()
 Check running votes. More...
 
void voting_free ()
 Free all memory used by this module. More...
 
int describe_vote (struct vote *pvote, char *buf, int buflen)
 Fills the supplied buffer with a string describing the given vote. More...
 
void handle_vote_submit (struct connection *pconn, int vote_no, int value)
 Handle a vote submit packet sent from a client. More...
 
void send_running_votes (struct connection *pconn, bool only_team_votes)
 Sends a packet_vote_new to pconn for every currently running votes. More...
 
void send_remove_team_votes (struct connection *pconn)
 Sends a packet_vote_remove to pconn for every currently running team vote 'pconn' can see. More...
 
void send_updated_vote_totals (struct conn_list *dest)
 Sends a packet_vote_update to every conn in dest. More...
 
const struct connectionvote_get_caller (const struct vote *pvote)
 Returns the connection that called this vote. More...
 

Variables

struct vote_list * vote_list = nullptr
 
int vote_number_sequence = 0
 

Function Documentation

◆ cancel_connection_votes()

void cancel_connection_votes ( struct connection pconn)

Cancel the votes of a lost or a detached connection.

Definition at line 702 of file voting.cpp.

Referenced by connection_detach().

◆ check_vote()

static void check_vote ( struct vote pvote)
static

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.

Definition at line 427 of file voting.cpp.

Referenced by connection_vote(), remove_vote_cast(), and voting_turn().

◆ clear_all_votes()

void clear_all_votes ( )

Remove all votes.

Sends vote_remove packets to clients.

Definition at line 219 of file voting.cpp.

Referenced by cancelvote_command(), start_game(), and voting_free().

◆ conn_can_see_vote()

bool conn_can_see_vote ( const struct connection pconn,
const struct vote pvote 
)

Usually, all users can see, except in the team vote case.

Definition at line 274 of file voting.cpp.

Referenced by lsend_vote_new(), lsend_vote_resolve(), lsend_vote_update(), send_remove_team_votes(), send_running_votes(), and show_votes().

◆ conn_can_vote()

bool conn_can_vote ( const struct connection pconn,
const struct vote pvote 
)

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 nullptr, then the team condition is not checked.

Definition at line 250 of file voting.cpp.

Referenced by check_vote(), connection_vote(), count_voters(), handle_stdin_input_real(), vote_command(), vote_new(), and vote_would_pass_immediately().

◆ connection_vote()

void connection_vote ( struct connection pconn,
struct vote pvote,
enum vote_type  type 
)

Make the given connection vote 'type' on 'pvote', and check the vote.

Definition at line 675 of file voting.cpp.

Referenced by handle_stdin_input_real(), handle_vote_submit(), and vote_command().

◆ count_voters()

int count_voters ( const struct vote pvote)

Helper function that returns the current number of eligible voters.

Definition at line 42 of file voting.cpp.

Referenced by check_vote(), send_running_votes(), send_updated_vote_totals(), show_votes(), vote_new(), and vote_would_pass_immediately().

◆ describe_vote()

int describe_vote ( struct vote pvote,
char *  buf,
int  buflen 
)

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.

Definition at line 763 of file voting.cpp.

Referenced by handle_stdin_input_real(), and lsend_vote_new().

◆ free_vote()

static void free_vote ( struct vote pvote)
static

Free all memory used by the vote structure.

Definition at line 190 of file voting.cpp.

Referenced by clear_all_votes(), and remove_vote().

◆ get_vote_by_caller()

struct vote* get_vote_by_caller ( const struct connection caller)

Returns the vote called by 'caller', or nullptr if none exists.

Definition at line 323 of file voting.cpp.

Referenced by cancel_connection_votes(), cancelvote_command(), handle_stdin_input_real(), and vote_new().

◆ get_vote_by_no()

struct vote* get_vote_by_no ( int  vote_no)

Returns the vote with vote number 'vote_no', or nullptr.

Definition at line 303 of file voting.cpp.

Referenced by cancelvote_command(), handle_vote_submit(), and vote_command().

◆ handle_vote_submit()

void handle_vote_submit ( struct connection pconn,
int  vote_no,
int  value 
)

Handle a vote submit packet sent from a client.

This is basically just a Wrapper around connection_vote().

Definition at line 791 of file voting.cpp.

◆ lsend_vote_new()

static void lsend_vote_new ( struct conn_list *  dest,
struct vote pvote 
)
static

Tell clients that a new vote has been created.

Definition at line 60 of file voting.cpp.

Referenced by send_running_votes(), and vote_new().

◆ lsend_vote_remove()

static void lsend_vote_remove ( struct conn_list *  dest,
struct vote pvote 
)
static

Tell clients that the given vote no longer exists.

Definition at line 140 of file voting.cpp.

Referenced by clear_all_votes(), remove_vote(), and send_remove_team_votes().

◆ lsend_vote_resolve()

static void lsend_vote_resolve ( struct conn_list *  dest,
struct vote pvote,
bool  passed 
)
static

Tell clients that the given vote resolved.

Definition at line 161 of file voting.cpp.

Referenced by check_vote().

◆ lsend_vote_update()

static void lsend_vote_update ( struct conn_list *  dest,
struct vote pvote,
int  num_voters 
)
static

Send updated status information about the given vote.

Definition at line 100 of file voting.cpp.

Referenced by check_vote(), send_running_votes(), and send_updated_vote_totals().

◆ remove_vote()

void remove_vote ( struct vote pvote)

Remove the given vote and send a vote_remove packet to clients.

Definition at line 205 of file voting.cpp.

Referenced by cancel_connection_votes(), cancelvote_command(), check_vote(), and vote_new().

◆ remove_vote_cast()

static void remove_vote_cast ( struct vote pvote,
struct vote_cast pvc 
)
static

Remove a vote cast.

This unlinks it and frees its memory.

Definition at line 661 of file voting.cpp.

Referenced by cancel_connection_votes().

◆ send_remove_team_votes()

void send_remove_team_votes ( struct connection pconn)

Sends a packet_vote_remove to pconn for every currently running team vote 'pconn' can see.

Definition at line 859 of file voting.cpp.

Referenced by connection_detach().

◆ send_running_votes()

void send_running_votes ( struct connection pconn,
bool  only_team_votes 
)

Sends a packet_vote_new to pconn for every currently running votes.

Definition at line 827 of file voting.cpp.

Referenced by connection_attach_real(), and establish_new_connection().

◆ send_updated_vote_totals()

void send_updated_vote_totals ( struct conn_list *  dest)

Sends a packet_vote_update to every conn in dest.

If dest is nullptr, then sends to all established connections.

Definition at line 884 of file voting.cpp.

Referenced by close_connection(), connection_attach_real(), connection_detach(), kill_dying_players(), and player_limit_to_max_rates().

◆ vote_cast_find()

static struct vote_cast* vote_cast_find ( struct vote pvote,
int  conn_id 
)
static

Find the vote cast for the user id conn_id in a vote.

Definition at line 621 of file voting.cpp.

Referenced by cancel_connection_votes(), and connection_vote().

◆ vote_cast_new()

static struct vote_cast* vote_cast_new ( struct vote pvote)
static

Return a new vote cast.

Definition at line 641 of file voting.cpp.

Referenced by connection_vote().

◆ vote_get_caller()

const struct connection* vote_get_caller ( const struct vote pvote)

Returns the connection that called this vote.

Definition at line 911 of file voting.cpp.

Referenced by cancelvote_command(), check_vote(), conn_can_see_vote(), and conn_can_vote().

◆ vote_is_team_only()

bool vote_is_team_only ( const struct vote pvote)

Returns TRUE if this vote is a "teamvote".

Definition at line 237 of file voting.cpp.

Referenced by check_vote(), conn_can_see_vote(), conn_can_vote(), handle_stdin_input_real(), send_remove_team_votes(), send_running_votes(), and show_votes().

◆ vote_new()

struct vote* vote_new ( struct connection caller,
const char *  allargs,
int  command_id 
)

Create and return a newly allocated vote for the command with id 'command_id' and all arguments in the string 'allargs'.

Definition at line 344 of file voting.cpp.

Referenced by handle_stdin_input_real().

◆ vote_would_pass_immediately()

bool vote_would_pass_immediately ( const struct connection caller,
int  command_id 
)

Return whether the vote would pass immediately when the caller will vote for.

Definition at line 396 of file voting.cpp.

Referenced by handle_stdin_input_real().

◆ voting_free()

void voting_free ( )

Free all memory used by this module.

Definition at line 749 of file voting.cpp.

Referenced by server_quit().

◆ voting_init()

void voting_init ( )

Initialize data structures used by this module.

Definition at line 720 of file voting.cpp.

◆ voting_turn()

void voting_turn ( )

Check running votes.

This should be called every turn.

Definition at line 731 of file voting.cpp.

Referenced by end_turn().

Variable Documentation

◆ vote_list

◆ vote_number_sequence

int vote_number_sequence = 0

Definition at line 37 of file voting.cpp.

Referenced by vote_command(), vote_new(), and voting_init().