Author: ilor
Date: Mon May 18 10:44:46 2009
New Revision: 35730
URL: http://svn.gna.org/viewcvs/wesnoth?rev=35730&view=rev
Log:
extract group data sending functions from wesnothd's game class to free
functions in a separete header/cpp pair
Added:
trunk/src/server/player_network.cpp
trunk/src/server/player_network.hpp
Modified:
trunk/src/server/game.cpp
trunk/src/server/game.hpp
Modified: trunk/src/server/game.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/game.cpp?rev=35730&r1=35729&r2=35730&view=diff
==============================================================================
--- trunk/src/server/game.cpp (original)
+++ trunk/src/server/game.cpp Mon May 18 10:44:46 2009
@@ -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 @@
// 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 @@
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 @@
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 @@
send_observerjoins(user->first);
}
-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);
+void game::send_data(simple_wml::document& data,
+ const network::connection
exclude,
+ std::string packet_type) const
+{
+ wesnothd::send_to_many(data, all_game_users(), exclude, packet_type);
}
void game::send_data_team(simple_wml::document& data,
@@ -1177,17 +1164,6 @@
simple_wml::string_span s = data.output_compressed();
for(user_vector::const_iterator i = players_.begin(); i !=
players_.end(); ++i) {
if(*i != exclude && is_on_team(team,*i)) {
- network::send_raw_data(s.begin(), s.size(), *i,
packet_type);
- }
- }
-}
-
-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);
}
}
@@ -1418,13 +1394,7 @@
}
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,
Modified: trunk/src/server/game.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/game.hpp?rev=35730&r1=35729&r2=35730&view=diff
==============================================================================
--- trunk/src/server/game.hpp (original)
+++ trunk/src/server/game.hpp Mon May 18 10:44:46 2009
@@ -153,7 +153,6 @@
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 @@
/** 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; }
Added: trunk/src/server/player_network.cpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/player_network.cpp?rev=35730&view=auto
==============================================================================
--- trunk/src/server/player_network.cpp (added)
+++ trunk/src/server/player_network.cpp Mon May 18 10:44:46 2009
@@ -1,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<bool (network::connection)>
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
Added: trunk/src/server/player_network.hpp
URL:
http://svn.gna.org/viewcvs/wesnoth/trunk/src/server/player_network.hpp?rev=35730&view=auto
==============================================================================
--- trunk/src/server/player_network.hpp (added)
+++ trunk/src/server/player_network.hpp Mon May 18 10:44:46 2009
@@ -1,0 +1,49 @@
+/* $Id$ */
+/*
+ Copyright (C) 2003 - 2009 by David White <[email protected]>
+ 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 <boost/function.hpp>
+
+namespace wesnothd {
+
+typedef std::map<network::connection,player> player_map;
+typedef std::vector<network::connection> 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<bool (network::connection)>
except_pred,
+ std::string packet_type);
+} //end namespace wesnothd
+
+#endif
_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits