diff --git a/src/server/game.cpp b/src/server/game.cpp index c38ef11af2f..ccdcf24e1aa 100644 --- a/src/server/game.cpp +++ b/src/server/game.cpp @@ -20,6 +20,7 @@ #include "../map.hpp" // gamemap::MAX_PLAYERS #include "game.hpp" +#include "player_network.hpp" #ifndef __func__ #ifdef __FUNCTION__ @@ -487,7 +488,7 @@ void game::change_controller(const size_t side_num, // side_drop already.) if (!player_left) { change.set_attr("controller", (side_controllers_[side_num] == "ai" ? "human_ai" : "human")); - send_to_one(response, sock); + wesnothd::send_to_one(response, sock); } // Update the level so observers who join get the new name. (The host handles level changes before game start.) @@ -909,7 +910,7 @@ bool game::process_turn(simple_wml::document& data, const player_map::const_iter send_data(*message, user->first, "game message"); record_data(message.release()); } else if (team_name == game_config::observer_team_name) { - send_data_observers(*message, user->first, "game message"); + wesnothd::send_to_many(*message, observers_, user->first, "game message"); record_data(message.release()); } else { send_data_team(*message, team_name, user->first, "game message"); @@ -1104,7 +1105,7 @@ bool game::remove_player(const network::connection player, const bool disconnect drop.root().set_attr("side_drop", side_drop.c_str()); drop.root().set_attr("controller", side_controllers_[side_num].c_str()); - send_to_one(drop, owner_); + wesnothd::send_to_one(drop, owner_); } if (ai_transfer) send_and_record_server_message("AI sides transferred to host."); @@ -1145,25 +1146,11 @@ void game::load_next_scenario(const player_map::const_iterator user) const { send_observerjoins(user->first); } -void game::send_data(simple_wml::document& data, const network::connection exclude, std::string packet_type) const +void game::send_data(simple_wml::document& data, + const network::connection exclude, + std::string packet_type) const { - if (packet_type.empty()) - packet_type = data.root().first_child().to_string(); - simple_wml::string_span s = data.output_compressed(); - const user_vector& users = all_game_users(); - for(user_vector::const_iterator i = users.begin(); i != users.end(); ++i) { - if (*i != exclude) { - network::send_raw_data(s.begin(), s.size(), *i, packet_type); - } - } -} - -void game::send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type) const -{ - if (packet_type.empty()) - packet_type = data.root().first_child().to_string(); - simple_wml::string_span s = data.output_compressed(); - network::send_raw_data(s.begin(), s.size(), sock, packet_type); + wesnothd::send_to_many(data, all_game_users(), exclude, packet_type); } void game::send_data_team(simple_wml::document& data, @@ -1182,17 +1169,6 @@ void game::send_data_team(simple_wml::document& data, } } -void game::send_data_observers(simple_wml::document& data, const network::connection exclude, std::string packet_type) const { - if (packet_type.empty()) - packet_type = data.root().first_child().to_string(); - simple_wml::string_span s = data.output_compressed(); - for(user_vector::const_iterator i = observers_.begin(); i != observers_.end(); ++i) { - if (*i != exclude) { - network::send_raw_data(s.begin(), s.size(), *i, packet_type); - } - } -} - bool game::is_on_team(const simple_wml::string_span& team, const network::connection player) const { const simple_wml::node::child_list& side_list = level_.root().children("side"); for (side_vector::const_iterator side = sides_.begin(); side != sides_.end(); ++side) { @@ -1418,13 +1394,7 @@ std::string game::debug_player_info() const { } player_map::const_iterator game::find_user(const simple_wml::string_span& name) const { - player_map::const_iterator pl; - for (pl = player_info_->begin(); pl != player_info_->end(); pl++) { - if (name == pl->second.name().c_str()) { - return pl; - } - } - return player_info_->end(); + return wesnothd::find_user(*player_info_, name); } void game::send_and_record_server_message(const char* message, diff --git a/src/server/game.hpp b/src/server/game.hpp index 802e20c268d..fb176da3e0e 100644 --- a/src/server/game.hpp +++ b/src/server/game.hpp @@ -153,7 +153,6 @@ public: void send_and_record_server_message(const char* message, const network::connection exclude=0); void send_data(simple_wml::document& data, const network::connection exclude=0, std::string packet_type = "") const; - void send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type = "") const; void clear_history(); void record_data(simple_wml::document* data); @@ -231,7 +230,7 @@ private: /** In case of a host transfer, notify the new host about its status. */ void notify_new_host(); - /** Convenience function for finding a user by name. */ + /** Shortcut to a convenience function for finding a user by name. */ player_map::const_iterator find_user(const simple_wml::string_span& name) const; bool observers_can_label() const { return false; } diff --git a/src/server/player_network.cpp b/src/server/player_network.cpp new file mode 100644 index 00000000000..c42ef8cc3fc --- /dev/null +++ b/src/server/player_network.cpp @@ -0,0 +1,52 @@ +#include "player_network.hpp" + +namespace wesnothd { + +player_map::const_iterator find_user(const player_map& all_players, + const simple_wml::string_span& name) +{ + player_map::const_iterator pl; + for (pl = all_players.begin(); pl != all_players.end(); pl++) { + if (name == pl->second.name().c_str()) { + return pl; + } + } + return all_players.end(); +} + +void send_to_one(simple_wml::document& data, const network::connection sock, std::string packet_type) +{ + if (packet_type.empty()) + packet_type = data.root().first_child().to_string(); + simple_wml::string_span s = data.output_compressed(); + network::send_raw_data(s.begin(), s.size(), sock, packet_type); +} + +void send_to_many(simple_wml::document& data, const connection_vector& vec, + const network::connection exclude, std::string packet_type) +{ + if (packet_type.empty()) + packet_type = data.root().first_child().to_string(); + simple_wml::string_span s = data.output_compressed(); + for(connection_vector::const_iterator i = vec.begin(); i != vec.end(); ++i) { + if (*i != exclude) { + network::send_raw_data(s.begin(), s.size(), *i, packet_type); + } + } +} + +void send_to_many(simple_wml::document& data, const connection_vector& vec, + boost::function except_pred, std::string packet_type) +{ + if (packet_type.empty()) + packet_type = data.root().first_child().to_string(); + simple_wml::string_span s = data.output_compressed(); + for(connection_vector::const_iterator i = vec.begin(); i != vec.end(); ++i) { + if (!except_pred(*i)) { + network::send_raw_data(s.begin(), s.size(), *i, packet_type); + } + } + +} + +} //end namespace wesnothd diff --git a/src/server/player_network.hpp b/src/server/player_network.hpp new file mode 100644 index 00000000000..dabdbff3c9a --- /dev/null +++ b/src/server/player_network.hpp @@ -0,0 +1,49 @@ +/* $Id$ */ +/* + Copyright (C) 2003 - 2009 by David White + Part of the Battle for Wesnoth Project http://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License 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. + + See the COPYING file for more details. +*/ + +#ifndef SERVER_PLAYER_NETWORK_HPP_INCLUDED +#define SERVER_PLAYER_NETWORK_HPP_INCLUDED + +#include "../network.hpp" +#include "player.hpp" +#include "simple_wml.hpp" + +#include + +namespace wesnothd { + +typedef std::map player_map; +typedef std::vector connection_vector; + +/** Convenience function for finding a user by name. */ +player_map::const_iterator find_user(const player_map& all_players, + const simple_wml::string_span& name); + + +void send_to_one(simple_wml::document& data, + const network::connection sock, + std::string packet_type = ""); + +void send_to_many(simple_wml::document& data, + const connection_vector& vec, + const network::connection exclude = 0, + std::string packet_type = ""); + +void send_to_many(simple_wml::document& data, + const connection_vector& vec, + boost::function except_pred, + std::string packet_type); +} //end namespace wesnothd + +#endif