Wesnothd/Game: formatting cleanup

This commit is contained in:
Charles Dang 2017-12-15 14:16:39 +11:00
parent 8473e49288
commit 3e33da7409
2 changed files with 912 additions and 550 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,22 +14,21 @@
#pragma once #pragma once
#include "player_connection.hpp" #include "mt_rng.hpp"
#include "player.hpp" #include "player.hpp"
#include "player_connection.hpp"
#include "simple_wml.hpp" #include "simple_wml.hpp"
#include "utils/make_enum.hpp"
#include <boost/ptr_container/ptr_vector.hpp>
#include <map> #include <map>
#include <vector> #include <vector>
#include "mt_rng.hpp"
#include "utils/make_enum.hpp"
#include <boost/ptr_container/ptr_vector.hpp>
// class player; // class player;
namespace wesnothd { namespace wesnothd
{
typedef std::vector<socket_ptr> user_vector; typedef std::vector<socket_ptr> user_vector;
typedef std::vector<socket_ptr> side_vector; typedef std::vector<socket_ptr> side_vector;
@ -42,17 +41,34 @@ public:
(EMPTY, "null") (EMPTY, "null")
) )
game(player_connections& player_connections, socket_ptr host, game(player_connections& player_connections,
const std::string& name="", bool save_replays=false, socket_ptr host,
const std::string& name = "",
bool save_replays = false,
const std::string& replay_save_path = ""); const std::string& replay_save_path = "");
~game(); ~game();
int id() const { return id_; } int id() const
const std::string& name() const { return name_; } {
return id_;
}
const std::string& name() const
{
return name_;
}
bool is_owner(const socket_ptr player) const
{
return (player == owner_);
}
bool is_owner(const socket_ptr player) const { return (player == owner_); }
bool is_member(const socket_ptr player) const bool is_member(const socket_ptr player) const
{ return is_player(player) || is_observer(player); } {
return is_player(player) || is_observer(player);
}
bool allow_observers() const; bool allow_observers() const;
bool registered_users_only() const; bool registered_users_only() const;
bool is_observer(const socket_ptr player) const; bool is_observer(const socket_ptr player) const;
@ -60,34 +76,59 @@ public:
/** Checks whether the connection's ip address is banned. */ /** Checks whether the connection's ip address is banned. */
bool player_is_banned(const socket_ptr player) const; bool player_is_banned(const socket_ptr player) const;
bool level_init() const { return level_.child("snapshot") || level_.child("scenario"); }
bool level_init() const
{
return level_.child("snapshot") || level_.child("scenario");
}
static simple_wml::node* starting_pos(simple_wml::node& data) static simple_wml::node* starting_pos(simple_wml::node& data)
{ {
if(simple_wml::node* scenario = data.child("scenario")) if(simple_wml::node* scenario = data.child("scenario")) {
return scenario; return scenario;
else if(simple_wml::node* snapshot = data.child("snapshot")) } else if(simple_wml::node* snapshot = data.child("snapshot")) {
return snapshot; return snapshot;
else }
return &data; return &data;
} }
static const simple_wml::node* starting_pos(const simple_wml::node& data) static const simple_wml::node* starting_pos(const simple_wml::node& data)
{ {
if(const simple_wml::node* scenario = data.child("scenario")) if(const simple_wml::node* scenario = data.child("scenario")) {
return scenario; return scenario;
else if(const simple_wml::node* snapshot = data.child("snapshot")) } else if(const simple_wml::node* snapshot = data.child("snapshot")) {
return snapshot; return snapshot;
else }
return &data; return &data;
} }
const simple_wml::node::child_list& get_sides_list() const const simple_wml::node::child_list& get_sides_list() const
{ {
return starting_pos(level_.root())->children("side"); return starting_pos(level_.root())->children("side");
} }
bool started() const { return started_; }
size_t nplayers() const { return players_.size(); } bool started() const
size_t nobservers() const { return observers_.size(); } {
size_t current_turn() const { return (nsides_ ? end_turn_ / nsides_ + 1 : 0); } return started_;
}
size_t nplayers() const
{
return players_.size();
}
size_t nobservers() const
{
return observers_.size();
}
size_t current_turn() const
{
return (nsides_ ? end_turn_ / nsides_ + 1 : 0);
}
void set_current_turn(int turn) void set_current_turn(int turn)
{ {
int current_side = end_turn_ % nsides_; int current_side = end_turn_ % nsides_;
@ -144,6 +185,7 @@ public:
const user_vector all_game_users() const; const user_vector all_game_users() const;
void start_game(const socket_ptr starter); void start_game(const socket_ptr starter);
// this is performed just before starting and before [start_game] signal // this is performed just before starting and before [start_game] signal
// send scenario_diff's specific to each client so that they locally // send scenario_diff's specific to each client so that they locally
// control their human sides // control their human sides
@ -153,7 +195,9 @@ public:
/** A user (player only?) asks for the next scenario to advance to. */ /** A user (player only?) asks for the next scenario to advance to. */
void load_next_scenario(const socket_ptr user); // const void load_next_scenario(const socket_ptr user); // const
// iceiceice: I unmarked this const because I want to send and record server messages when I fail to tweak sides properly
// iceiceice: I unmarked this const because I want to send and record server messages when I fail to tweak sides
// properly
/** Resets the side configuration according to the scenario data. */ /** Resets the side configuration according to the scenario data. */
void update_side_data(); void update_side_data();
@ -192,8 +236,10 @@ public:
send_server_message_to_all(message.c_str(), exclude); send_server_message_to_all(message.c_str(), exclude);
} }
void send_server_message(const char* message, socket_ptr sock=socket_ptr(), simple_wml::document* doc=nullptr) const; void send_server_message(
void send_server_message(const std::string& message, socket_ptr sock=socket_ptr(), simple_wml::document* doc=nullptr) const const char* message, socket_ptr sock = socket_ptr(), simple_wml::document* doc = nullptr) const;
void send_server_message(
const std::string& message, socket_ptr sock = socket_ptr(), simple_wml::document* doc = nullptr) const
{ {
send_server_message(message.c_str(), sock, doc); send_server_message(message.c_str(), sock, doc);
} }
@ -205,30 +251,45 @@ public:
send_and_record_server_message(message.c_str(), exclude); send_and_record_server_message(message.c_str(), exclude);
} }
void send_data(simple_wml::document& data, const socket_ptr exclude=socket_ptr(), std::string packet_type = "") const; void send_data(
simple_wml::document& data, const socket_ptr exclude = socket_ptr(), std::string packet_type = "") const;
void clear_history(); void clear_history();
void record_data(simple_wml::document* data); void record_data(simple_wml::document* data);
void save_replay(); void save_replay();
/** The full scenario data. */ /** The full scenario data. */
simple_wml::document& level() { return level_; } simple_wml::document& level()
{
return level_;
}
/** /**
* Functions to set/get the address of the game's summary description as * Functions to set/get the address of the game's summary description as
* sent to players in the lobby. * sent to players in the lobby.
*/ */
void set_description(simple_wml::node* desc); void set_description(simple_wml::node* desc);
simple_wml::node* description() const { return description_; }
void set_password(const std::string& passwd) { password_ = passwd; } simple_wml::node* description() const
bool password_matches(const std::string& passwd) const { {
return description_;
}
void set_password(const std::string& passwd)
{
password_ = passwd;
}
bool password_matches(const std::string& passwd) const
{
return password_.empty() || passwd == password_; return password_.empty() || passwd == password_;
} }
const std::string& termination_reason() const { const std::string& termination_reason() const
{
static const std::string aborted = "aborted"; static const std::string aborted = "aborted";
static const std::string not_started = "not started"; static const std::string not_started = "not started";
return started_ ? (termination_.empty() ? aborted : termination_) : not_started; return started_ ? (termination_.empty() ? aborted : termination_) : not_started;
} }
@ -240,26 +301,46 @@ public:
void handle_controller_choice(const simple_wml::node& data); void handle_controller_choice(const simple_wml::node& data);
void reset_last_synced_context_id() { last_choice_request_id_ = -1; } void reset_last_synced_context_id()
{
last_choice_request_id_ = -1;
}
/** /**
* Function which returns true iff 'player' controls any of the sides spcified in 'sides'. * Function which returns true iff 'player' controls any of the sides spcified in 'sides'.
*/ */
bool controls_side(const std::vector<int>& sides, const socket_ptr player) const; bool controls_side(const std::vector<int>& sides, const socket_ptr player) const;
private: private:
// forbidden operations // forbidden operations
game(const game&); game(const game&);
void operator=(const game&); void operator=(const game&);
size_t current_side() const { return (nsides_ ? end_turn_ % nsides_ : 0); } size_t current_side() const
{
return (nsides_ ? end_turn_ % nsides_ : 0);
}
socket_ptr current_player() const socket_ptr current_player() const
{ return (nsides_ ? sides_[current_side()] : socket_ptr()); } {
return (nsides_ ? sides_[current_side()] : socket_ptr());
}
bool is_current_player(const socket_ptr player) const bool is_current_player(const socket_ptr player) const
{ return (current_player() == player); } {
return (current_player() == player);
}
bool is_muted_observer(const socket_ptr player) const; bool is_muted_observer(const socket_ptr player) const;
bool all_observers_muted() const { return all_observers_muted_; } bool all_observers_muted() const
{
return all_observers_muted_;
}
void send_muted_observers(const socket_ptr user) const; void send_muted_observers(const socket_ptr user) const;
bool send_taken_side(simple_wml::document& cfg, const simple_wml::node::child_list::const_iterator side) const; bool send_taken_side(simple_wml::document& cfg, const simple_wml::node::child_list::const_iterator side) const;
/** /**
* Figures out which side to take and tells that side to the game owner. * Figures out which side to take and tells that side to the game owner.
* *
@ -281,12 +362,17 @@ private:
const bool player_left = true); const bool player_left = true);
void transfer_ai_sides(const socket_ptr player); void transfer_ai_sides(const socket_ptr player);
void send_leave_game(socket_ptr user) const; void send_leave_game(socket_ptr user) const;
/** /**
@param sides a comma sperated list of side numbers to which the package should be sent, @param sides a comma sperated list of side numbers to which the package should be sent,
*/ */
void send_data_sides(simple_wml::document& data, const simple_wml::string_span& sides, void send_data_sides(simple_wml::document& data,
const socket_ptr exclude=socket_ptr(), std::string packet_type = "") const; const simple_wml::string_span& sides,
void send_data_observers(simple_wml::document& data, const socket_ptr exclude=socket_ptr(), std::string packet_type = "") const; const socket_ptr exclude = socket_ptr(),
std::string packet_type = "") const;
void send_data_observers(
simple_wml::document& data, const socket_ptr exclude = socket_ptr(), std::string packet_type = "") const;
/** /**
* Send [observer] tags of all the observers in the game to the user or * Send [observer] tags of all the observers in the game to the user or
@ -302,8 +388,16 @@ private:
/** Shortcut to a convenience function for finding a user by name. */ /** Shortcut to a convenience function for finding a user by name. */
socket_ptr find_user(const simple_wml::string_span& name); socket_ptr find_user(const simple_wml::string_span& name);
bool observers_can_label() const { return false; } bool observers_can_label() const
bool observers_can_chat() const { return true; } {
return false;
}
bool observers_can_chat() const
{
return true;
}
bool is_legal_command(const simple_wml::node& command, const socket_ptr user); bool is_legal_command(const simple_wml::node& command, const socket_ptr user);
/** /**
@ -339,8 +433,10 @@ private:
/** calculates the initial value for sides_, side_controllerds_, nsides_*/ /** calculates the initial value for sides_, side_controllerds_, nsides_*/
void reset_sides(); void reset_sides();
/** Helps debugging player and observer lists. */ /** Helps debugging player and observer lists. */
std::string debug_player_info() const; std::string debug_player_info() const;
/** Helps debugging controller tweaks. */ /** Helps debugging controller tweaks. */
std::string debug_sides_info() const; std::string debug_sides_info() const;
@ -413,19 +509,36 @@ private:
int last_choice_request_id_; int last_choice_request_id_;
}; };
struct game_is_member { struct game_is_member
game_is_member(socket_ptr sock) : sock_(sock) {} {
bool operator()(const game& g) const { return g.is_owner(sock_) || g.is_member(sock_); } game_is_member(socket_ptr sock)
: sock_(sock)
{
}
bool operator()(const game& g) const
{
return g.is_owner(sock_) || g.is_member(sock_);
}
private: private:
socket_ptr sock_; socket_ptr sock_;
}; };
struct game_id_matches { struct game_id_matches
game_id_matches(int id) : id_(id) {} {
bool operator()(const game& g) const { return g.id() == id_; } game_id_matches(int id)
: id_(id)
{
}
bool operator()(const game& g) const
{
return g.id() == id_;
}
private: private:
int id_; int id_;
}; };
}
} // namespace wesnothd